/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* render.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 14:55:05 by greg #+# #+# */ /* Updated: 2024/10/22 15:50:27 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ #include "algo.h" #include static void calculate_perpwalldist3(int x, t_point_double *deltadist, t_ray *ray) { const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1; t_point_double plane; t_point_double dir; vector_from_rotation(&dir, g_map.player.rot, 1); vector_from_rotation(&plane, g_map.player.rot + PI / 2, 1); ray->dir.x = dir.x + plane.x * ray_direction; ray->dir.y = dir.y + plane.y * ray_direction; deltadist->x = 1e30; if (ray->dir.x != 0) deltadist->x = fabs(1 / ray->dir.x); deltadist->y = 1e30; if (ray->dir.y != 0) deltadist->y = fabs(1 / ray->dir.y); } static void calculate_perpwalldist2(int x, t_point_double *sidedist, t_point_double *deltadist, t_point_int *step, t_ray *ray) { const int map_x = (int)g_map.player.pos.x; const int map_y = (int)g_map.player.pos.y; calculate_perpwalldist3(x, deltadist, ray); if (ray->dir.x < 0) { step->x = -1; sidedist->x = (g_map.player.pos.x - map_x) * deltadist->x; } else { step->x = 1; sidedist->x = (map_x + 1.0 - g_map.player.pos.x) * deltadist->x; } if (ray->dir.y < 0) { step->y = -1; sidedist->y = (g_map.player.pos.y - map_y) * deltadist->y; } else { step->y = 1; sidedist->y = (map_y + 1.0 - g_map.player.pos.y) * deltadist->y; } } /// @brief Calculate the distance of the wall perpendicularly to the camera /// plane. /// /// @param x Ray direction x between 0 and 1. /// @return Result. static double calculate_perpwalldist(int x, t_ray *ray) { t_point_int map_pos; t_point_double sidedist; t_point_double deltadist; t_point_int step; map_pos.x = (int)g_map.player.pos.x; map_pos.y = (int)g_map.player.pos.y; calculate_perpwalldist2(x, &sidedist, &deltadist, &step, ray); while (map_get_case(&g_map, map_pos.x, map_pos.y)->wall == EMPTY) { if (sidedist.x < sidedist.y) { sidedist.x += deltadist.x; map_pos.x += step.x; ray->side = false; continue ; } sidedist.y += deltadist.y; map_pos.y += step.y; ray->side = true; } if (!ray->side) return (sidedist.x - deltadist.x); return (sidedist.y - deltadist.y); } static void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex) { if (ray->side == 0 && ray->dir.x < 0) tex->tex_dir = 0; if (ray->side == 0 && ray->dir.x >= 0) tex->tex_dir = 1; if (ray->side == 1 && ray->dir.y < 0) tex->tex_dir = 2; if (ray->side == 1 && ray->dir.y >= 0) tex->tex_dir = 3; if (ray->side == 0) ray->wallx = g_map.player.pos.y + perpwalldist \ * ray->dir.y; else ray->wallx = g_map.player.pos.x + perpwalldist \ * ray->dir.x; ray->wallx -= floor((ray->wallx)); } /// @brief Draw a vertical line according to the ray direction x. /// /// @param x Ray direction x between 0 and 1. static void draw_vertical_line(int x, u_int32_t *img_data, t_ray *ray, t_tex *tex) { const double perpwalldist = calculate_perpwalldist(x, ray); const int line_height = (int)(SCREEN_HEIGHT / perpwalldist); int draw_start; int draw_end; int y; int tex_y; int tex_x; int bpp; int size_line; int endian; u_int32_t color; ft_init_texture(perpwalldist, ray, tex); draw_start = -line_height / 2 + SCREEN_HEIGHT / 2; if (draw_start < 0) draw_start = 0; draw_end = line_height / 2 + SCREEN_HEIGHT / 2; if (draw_end >= SCREEN_HEIGHT) draw_end = SCREEN_HEIGHT - 1; y = 0; while (y < SCREEN_HEIGHT) { if (y < draw_start) img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING; else if (y < draw_end) { tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) * tex->tex_height) / line_height; tex_x = (int)(ray->wallx * (double)tex->tex_width); if (ray->side == 0 && ray->dir.x > 0) tex_x = tex->tex_width - tex_x - 1; if (ray->side == 1 && ray->dir.y < 0) tex_x = tex->tex_height - tex_x - 1; u_int32_t *texture_data = (u_int32_t *)mlx_get_data_addr(tex->textures[tex->tex_dir], &bpp, &size_line, &endian); color = texture_data[tex_x * tex->tex_width + tex_y]; img_data[y * SCREEN_WIDTH + x] = color; } else img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR; y++; } } int render(u_int32_t *img_data, t_tex *tex) { int x; t_ray ray; x = 0; while (x < SCREEN_WIDTH) { draw_vertical_line(x, img_data, &ray, tex); x++; } return (0); }