Files
42_INT_10_cube3d/srcs/draw/raycast.c

116 lines
2.5 KiB
C

#include "cube3d.h"
// tmp, to draw rays
static void calcul_ray_end(t_rcast *rcast, t_vec *ray)
{
if (rcast->is_x)
{
ray->end.x = rcast->cell_x * rcast->cell;
if (rcast->ray_sign_x == 1)
ray->end.x += rcast->cell;
if (rcast->slope_x)
{
rcast->ratio = (double)(ray->end.x - ray->start.x);
rcast->ratio /= (double)rcast->slope_x;
ray->end.y = ray->start.y + (double)rcast->slope_y * rcast->ratio;
}
}
else
{
ray->end.y = rcast->cell_y * rcast->cell;
if (rcast->ray_sign_y == 1)
ray->end.y += rcast->cell;
if (rcast->slope_y)
{
rcast->ratio = (double)(ray->end.y - ray->start.y);
rcast->ratio /= (double)rcast->slope_y;
ray->end.x = ray->start.x + (double)rcast->slope_x * rcast->ratio;
}
}
}
// tmp end
static void wall_length(t_rcast *rcast)
{
int len;
rcast->wall.start.x = rcast->ray_nb;
rcast->wall.end.x = rcast->ray_nb;
if (rcast->is_x == 1)
{
len = rcast->next_x - rcast->ray_step_x;
if (rcast->slope_y)
len /= ft_abs(rcast->slope_y);
len = rcast->screen_dist * len / (rcast->slope_x);
}
else if (rcast->is_x == 0)
{
len = rcast->next_y - rcast->ray_step_y;
if (rcast->slope_x)
len /= ft_abs(rcast->slope_x);
len = rcast->screen_dist * len / (rcast->slope_y);
}
rcast->ray_len = ft_abs(len);
}
static void wall_height(t_rcast *rcast)
{
int height;
height = rcast->screen_height * (rcast->hor - rcast->ray_len) / rcast->hor;
if (height < 0)
height = 0;
if (height > rcast->screen_height)
height = rcast->screen_height;
rcast->wall.start.y = rcast->screen_height / 2 + height / 2;
rcast->wall.end.y = rcast->screen_height / 2 - height / 2;
}
void draw_column(t_game *game, t_rcast *rcast)
{
t_vec plan;
int color;
plan.start.x = rcast->ray_nb;
plan.end.x = rcast->ray_nb;
if (rcast->wall.start.y > 0)
{
plan.start.y = rcast->screen_height;
plan.end.y = rcast->wall.start.y;
draw_line(&game->img, &plan, 0x00FF0000);
}
if (rcast->wall.start.y < rcast->screen_height)
{
plan.start.y = rcast->wall.end.y;
plan.end.y = 0;
draw_line(&game->img, &plan, 0x000000FF);
}
color = 0x00FF00FF;
if (rcast->is_x)
color = 0x00EE00EE;
draw_line(&game->img, &rcast->wall, color);
}
void raycast(t_game *game, t_rcast *rcast)
{
t_vec ray;
rcast->ray_nb = 0;
while (rcast->ray_nb <= rcast->screen_width)
{
ray_intersect(game, rcast, &ray);
// tmp, to draw the map
calcul_ray_end(rcast, &ray);
draw_line(&game->map_img, &ray, 0x00FF00FF);
// tmp end
wall_length(rcast);
wall_height(rcast);
draw_column(game, rcast);
(rcast->ray_nb)++;
}
}