Fisheye Raycasting

178 Views Asked by At

I'm trying to build simple raycasting game. I'm rendering wall based on light length. Wall size is calculated based on natural log formula. Problem is fisheye! I tries cos formula but it's not applicable to my architecture. Here's the code:

void    draw_3d(t_cub3d *this, double len, int num_of_ray, double start)
{
double height;
double width;
double x;
double y;

height = 720 / log10(len + 1);
if (height > 720)
    height = 720;
width = 1280.0 / 640;
y = (720 - height) / 2;
while (y < height)
{
    x = 2 * num_of_ray;
    while(x < 2 + width * num_of_ray)
    {
        my_mlx_pixel_put(this, (int)x, (int)y, 0xfffff);
        x++;
    }
    y++;
  }
}

int raycast(t_cub3d *this)
{
double start = this->raycast->pa - this->raycast->fov;
double end = this->raycast->pa + this->raycast->fov;
double ray_p_x;
double ray_p_y;
double len;
int num_of_ray;

num_of_ray = 0;
while (start <= end)
{
    ray_p_x = this->raycast->px;
    ray_p_y = this->raycast->py;
    while (this->gameinfo->map[(int) ray_p_y / 30][(int) ray_p_x / 30] != '1' && this->gameinfo->map[(int) ray_p_y / 30][(int) ray_p_x / 30] != ' ')
    {
        ray_p_x += cos(start);
        ray_p_y += sin(start);
    }
    len = fabs(this->raycast->px - ray_p_x) / fabs(cos(start));
    len *= cos(this->raycast->pa - start);
    start += this->raycast->fov / 320;
    draw_3d(this, len, num_of_ray, start);
    num_of_ray++;
}
mlx_put_image_to_window(this->mlx_info->mlx, this->mlx_info->mlx_win, this->img->img, 0, 0);
return (0);
}

How to fix fisheye distortion if cos formula doesn't apply to my case.

How it looks like

How it looks like

0

There are 0 best solutions below