【问题标题】:How do I fix the warped perspective in my raycaster?如何修复我的光线投射器中的扭曲透视?
【发布时间】:2021-06-09 22:55:39
【问题描述】:

我正在使用 SDL 的 C API 编写光线投射器。我花了数周时间试图修复臭名昭著的鱼眼效应,但无济于事。根据this 来源,我可以将计算出的距离乘以 FOV 一半的余弦来修复它。这对我不起作用。尽管如此,我的代码中仍然有余弦校正。

这是展示失真的两张图片:

我认为我的代码的一个核心问题可能是我的角度增量是恒定的,而增量应该更小,因为我更接近屏幕边框。不幸的是,我不知道如何开始实施。

如果可能的话,有人可以看看我的代码并给我一个关于如何去除鱼眼的提示吗?要向任何方向移动,请使用箭头键。使用“a”和“s”键分别向左和向右转。

这就是我的编译方式:clang `pkg-config --cflags --libs sdl2` raycaster.c

#include <SDL2/SDL.h>
#include <math.h>

typedef struct {
    float x, y, prev_x, prev_y, angle, fov;
} Player;

enum {
    map_width = 12, map_height = 15,
    screen_width = 800, screen_height = 500
};

const float
    move_speed_decr = 0.08,
    angle_turn = 2.0,
    ray_theta_step = 0.4,
    ray_dist_step = 0.8,
    darkening = 1.8,
    width_ratio = (float) screen_width / map_width,
    height_ratio = (float) screen_height / map_height;

const unsigned char map[map_height][map_width] = {
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1},
    {1, 0, 4, 3, 2, 0, 0, 0, 2, 0, 0, 1},
    {1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 1},
    {1, 0, 0, 0, 4, 3, 2, 1, 4, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

SDL_Window* window;
SDL_Renderer* renderer;

float to_radians(float degrees) {
    return degrees * M_PI / 180;
}

float distance(float x0, float y0, float x1, float y1) {
    return sqrt(((x1 - x0) * (x1 - x0)) + ((y1 - y0) * (y1 - y0)));
}

void shade(int* color, int darkener) {
    int darkened = *color - darkener;
    *color = darkened < 0 ? 0 : darkened;
}

void draw_rectangle(SDL_Rect rectangle, int r, int g, int b) {
    SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
    SDL_RenderFillRect(renderer, &rectangle);
    SDL_RenderDrawRect(renderer, &rectangle);
}

void raycast(Player player) {
    float relative_x = player.x * width_ratio;
    float relative_y = player.y * height_ratio;
    float half_fov = player.fov / 2;
    float width_fov_ratio = (screen_width / player.fov) / 2;
    float distort_adjust = cos(to_radians(half_fov));

    // the core of my problem may be in the constant increment of my angle
    for (float theta = player.angle - half_fov, screen_x = 0;
        theta < player.angle + half_fov;
        theta += ray_theta_step, screen_x += width_fov_ratio) {

        float radian_theta = to_radians(theta);
        float cos_theta = cos(radian_theta), sin_theta = sin(radian_theta);

        float d = 0, new_x, new_y;
        while (d += ray_dist_step) {
            new_x = cos_theta * d + relative_x;
            new_y = sin_theta * d + relative_y;

            int map_x = new_x / width_ratio, map_y = new_y / height_ratio;
            int map_point = map[map_y][map_x];

            if (map_point) {
                int dist_wall = distance(relative_x, relative_y, new_x, new_y) * distort_adjust;
                int twice_dist_wall = 2 * dist_wall;
                if (twice_dist_wall >= screen_height) break;
                else if (map_point) { // succeeds when a wall is present
                    int r, g, b;
                    switch (map_point) {
                        case 1: r = 255, g = 255, b = 0; break;
                        case 2: r = 0, g = 128, b = 128; break;
                        case 3: r = 255, g = 165, b = 0; break;
                        case 4: r = 255, g = 0, b = 0; break;
                    }

                    int color_decr = dist_wall / darkening;
                    shade(&r, color_decr);
                    shade(&g, color_decr);
                    shade(&b, color_decr);

                    SDL_Rect vertical_line = {
                        screen_x, dist_wall,
                        width_fov_ratio + 1,
                        screen_height - twice_dist_wall
                    };

                    draw_rectangle(vertical_line, r, g, b);
                    break;
                }
            }
        }
    }
}

void handle_input(const Uint8* keys, Player* player) {
    SDL_Event event;

    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            SDL_DestroyWindow(window);
            SDL_DestroyRenderer(renderer);
            exit(0);
        }

        else if (event.type == SDL_KEYDOWN) {
            float radian_theta = to_radians(player -> angle);
            float move_x = cos(radian_theta) * move_speed_decr,
                move_y = sin(radian_theta) * move_speed_decr;

            // handle arrow keys
            if (keys[SDL_SCANCODE_UP]) player -> x += move_x, player -> y += move_y;
            if (keys[SDL_SCANCODE_DOWN]) player -> x -= move_x, player -> y -= move_y;
            if (keys[SDL_SCANCODE_LEFT]) player -> x += move_y, player -> y -= move_x;
            if (keys[SDL_SCANCODE_RIGHT]) player -> x -= move_y, player -> y += move_x;

            // handle 'a' and 's' for angle changes
            if (keys[SDL_SCANCODE_A]) player -> angle -= angle_turn;
            if (keys[SDL_SCANCODE_S]) player -> angle += angle_turn;

            // safeguards for invalid positions and angles
            if (player -> x < 0) player -> x = 0;
            else if (player -> x > screen_width) player -> x = screen_width;

            if (player -> y < 0) player -> y = 0;
            else if (player -> y > screen_height) player -> y = screen_height;

            // move the player to their previous coordinate if they're in a wall
            if (map[(int) player -> y][(int) player -> x])
                player -> y = player -> prev_y, player -> x = player -> prev_x;

            if (player -> angle > 360) player -> angle = 0;
            else if (player -> angle < 0) player -> angle = 360;

            player -> prev_y = player -> y, player -> prev_x = player -> x;
        }
    }
}

int main() {
    SDL_CreateWindowAndRenderer(screen_width, screen_height, 0, &window, &renderer);
    SDL_SetWindowTitle(window, "Raycaster");    

    Player player = {5, 5, 0, 0, 0, 60};
    SDL_Rect the_ceiling = {0, 0, screen_width, screen_height / 2};
    SDL_Rect the_floor = {0, screen_height / 2, screen_width, screen_height};
    const Uint8* keys = SDL_GetKeyboardState(NULL);

    while (1) {
        handle_input(keys, &player);

        draw_rectangle(the_ceiling, 96, 96, 96);
        draw_rectangle(the_floor, 210, 180, 140);

        raycast(player);

        SDL_RenderPresent(renderer);
        SDL_UpdateWindowSurface(window);
    }
}

在 RandomDavis 的帮助下,失真减少了。这是新的结果。尽管如此,仍然存在一些扭曲:

注意: 对于仍在为这个问题苦苦挣扎的人,我在这里解决了它: How do I fix warped walls in my raycaster?

【问题讨论】:

  • 我以前见过这个确切的问题,为了避免鱼缸效应,你必须在显示之前对每列的高度进行某种三角运算。我会看看我能不能确切地知道它是什么。
  • @RandomDavis 谢谢,这将非常有帮助。
  • 不确定是否会有所帮助,但也许将degrees * M_PI / 180; 写为degrees * M_PI_DEGREES 并添加#define M_PI_DEGREES (M_PI / 180.0f)
  • @AntoninGAVREL 我试过了 - 它似乎没有什么不同:20 * 3.14 / 180.0 == 20 * (3.14 / 180.0) 在 Python 提示符下产生了 True。不过还是谢谢你!

标签: c sdl raycasting fisheye


【解决方案1】:

好的,我找到了一个guide,它谈到了这个确切的问题。

在画墙之前,有一个问题必须解决。这个问题被称为“鱼缸效应”。鱼缸效应的发生是因为光线投射实现将极坐标和笛卡尔坐标混合在一起。因此,对不在观察者正前方的墙切片使用上述公式会得到更长的距离。这不是我们想要的,因为它会导致如下图所示的观看失真。

因此,为了消除观看失真,从图 17 中的等式获得的结果距离必须乘以 cos(BETA);其中 BETA 是投射的光线相对于视角的角度。在上图中,视角 (ALPHA) 为 90 度,因为玩家正面朝上。因为我们有 60 度的视场,所以 BETA 对最左边的光线是 30 度,对最右边的光线是 -30 度。

【讨论】:

  • 很抱歉,这种方法对我不起作用。我在帖子顶部链接了该指南,并引用了它如何不影响失真。 float distort_adjust = cos(to_radians(half_fov)); 可能有误?
  • @CaspianAhlberg half_fov 不是正确的值。 BETA 值,因此distort_adjust 随每一列而变化;它是相机和柱子之间的角度。因此,如果玩家角度为 90 度,而您的 FOV 为 30,则 BETA 开始时为 -30 度,最终为 30 度。所以我相信你的代码是theta - player.angle,但你必须确保这是正确的。也就是说,我认为distort_adjust 将设置为cos(to_radians(theta - player_angle))。但是您当前的尝试肯定是错误的,尤其是因为它没有针对 theta 的每个值进行更新。
  • 您对theta - player.angle 的想法给了我从-15 到15 度的范围。这与 FOV 一致,但仍有一点失真。是 -15 到 15,还是别的什么?
  • 当您说 -30 到 30 时,您可能一直在考虑教程中的示例。
  • @CaspianAhlberg 嗯,即使你重新开始,它可能会容易得多。有时从头开始真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多