diff --git a/README.md b/README.md index 59c7835..7506f0b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # todo -- resolve wall height - resolve view buble - add textures diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index 85cb711..d4e9b89 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -17,8 +17,8 @@ # define SCREEN_FOCAL 90 /* size of a cell on the map */ # define CELL 20 -/* height of a wall without perpectiv */ -# define WALL_HEIGHT 200 +/* horizon fr perspectiv */ +# define HORIZON 700 /* * keys macro diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index e2df48d..37a24c5 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -26,27 +26,19 @@ typedef struct s_vec t_coord end; } t_vec; -/* - * struct for walls 3d drawing - */ - -typedef struct s_wall -{ - t_vec pos; - int height; -} t_wall; - /* * struct with all elements for raycasting */ typedef struct s_rcast { - t_vec screen_dist; t_vec screen_size; t_vec ray; + t_vec wall; + int hor; int screen_width; int screen_height; + int screen_dist; int cell; int cell_x; int cell_y; @@ -132,7 +124,6 @@ typedef struct s_game t_plr plr; // rays t_rcast rcast; - t_wall wall; // map t_map map; // map window diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 08a0245..bdcbfb8 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -102,6 +102,7 @@ void draw_line(t_img *img, t_vec *vec, int color) draw_square(game, pos, 0x00999900, 0x00330033, cell, 1); } +/* static void draw_screen(t_game *game, t_rcast *rcast) { t_vec screen; @@ -122,6 +123,7 @@ void draw_line(t_img *img, t_vec *vec, int color) rotate(&(game->plr), &(screen.end)); draw_line(&game->map_img, &screen, 0x00FFFFFF); } +*/ // tmp end void draw(t_game *game) @@ -133,7 +135,7 @@ void draw(t_game *game) raycast(game, &(game->rcast)); // tmp, to draw map - draw_screen(game, &(game->rcast)); +// draw_screen(game, &(game->rcast)); mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT); // tmp end diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 4bef579..c4c8e02 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -30,67 +30,70 @@ } // tmp end -static void wall_length(t_rcast *rcast, t_wall *wall) +static void wall_length(t_rcast *rcast) { - int length; + int len; - wall->pos.start.x = rcast->ray_nb; - wall->pos.end.x = rcast->ray_nb; + rcast->wall.start.x = rcast->ray_nb; + rcast->wall.end.x = rcast->ray_nb; +// len = sqrt(pow(ray->end.x - ray->start.x, 2) + pow(ray->end.y - ray->start.y, 2)); if (rcast->is_x == 1) { - length = rcast->next_x - rcast->ray_step_x; + len = rcast->next_x - rcast->ray_step_x; if (rcast->slope_y) - length /= ft_abs(rcast->slope_y); - length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; + len /= ft_abs(rcast->slope_y); + // len = (double)len * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; + // len = rcast->screen_dist * len / (rcast->slope_x); } else if (rcast->is_x == 0) { - length = rcast->next_y - rcast->ray_step_y; + len = rcast->next_y - rcast->ray_step_y; if (rcast->slope_x) - length /= ft_abs(rcast->slope_x); - length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; + len /= ft_abs(rcast->slope_x); + // len = (double)len * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; + // len = rcast->screen_dist * len / (rcast->slope_y); } - rcast->ray_len = ft_abs(length); + rcast->ray_len = ft_abs(len); } -static void wall_height(t_rcast *rcast, t_wall *wall) +static void wall_height(t_rcast *rcast) { int height; - height = wall->height - rcast->ray_len; + 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; - wall->pos.start.y = rcast->screen_height / 2 + height / 2; - wall->pos.end.y = rcast->screen_height / 2 - height / 2; + 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_wall *wall) +void draw_column(t_game *game, t_rcast *rcast) { - t_vec plane; + t_vec plan; int color; - plane.start.x = rcast->ray_nb; - plane.end.x = rcast->ray_nb; - if (wall->pos.start.y > 0) + plan.start.x = rcast->ray_nb; + plan.end.x = rcast->ray_nb; + if (rcast->wall.start.y > 0) { - plane.start.y = rcast->screen_height; - plane.end.y = wall->pos.start.y; - draw_line(&game->img, &plane, 0x00FF0000); + plan.start.y = rcast->screen_height; + plan.end.y = rcast->wall.start.y; + draw_line(&game->img, &plan, 0x00FF0000); } - if (wall->pos.start.y < rcast->screen_height) + if (rcast->wall.start.y < rcast->screen_height) { - plane.start.y = wall->pos.end.y; - plane.end.y = 0; - draw_line(&game->img, &plane, 0x000000FF); + 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, &wall->pos, color); + draw_line(&game->img, &rcast->wall, color); } void raycast(t_game *game, t_rcast *rcast) @@ -108,9 +111,9 @@ void raycast(t_game *game, t_rcast *rcast) // tmp end - wall_length(rcast, &game->wall); - wall_height(rcast, &game->wall); - draw_column(game, rcast, &game->wall); + wall_length(rcast); + wall_height(rcast); + draw_column(game, rcast); (rcast->ray_nb)++; } } diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 542dcc5..3f2f426 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -68,13 +68,10 @@ static void init_raycast(t_rcast *rcast) dist = (SCREEN_FOCAL / 2) * M_PI / 180; dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); + rcast->screen_dist = dist; rcast->screen_width = SCREEN_WIDTH; rcast->screen_height = SCREEN_HEIGHT; - // screen dist - rcast->screen_dist.start.x = 0; - rcast->screen_dist.start.y = 0; - rcast->screen_dist.end.x = 0; - rcast->screen_dist.end.y = -dist; + rcast->hor = HORIZON; // screen size rcast->screen_size.start.x = -SCREEN_WIDTH / 2; rcast->screen_size.start.y = -dist; @@ -132,6 +129,5 @@ t_game *init_game(void) // raycasting init_raycast(&(game->rcast)); game->rcast.cell = game->map.cell; - game->wall.height = WALL_HEIGHT; return (game); }