From a714e8110d30e74039ab45ab643451957b525913 Mon Sep 17 00:00:00 2001 From: ReverseSky Date: Thu, 17 Oct 2024 12:28:34 +0200 Subject: [PATCH 1/2] temp --- utils2.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 utils2.c diff --git a/utils2.c b/utils2.c new file mode 100644 index 0000000..6e3d3b3 --- /dev/null +++ b/utils2.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: greg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 01:57:03 by greg #+# #+# */ +/* Updated: 2024/10/17 12:28:01 by greg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// #include "algo.h" +void ft_init_texture() +{ + if (ray.side == 0 && ray.raydirx < 0) + direction = 0; + if (ray.side == 0 && ray.raydirx >= 0) + direction = 1; + if (ray.side == 1 && ray.raydiry < 0) + direction = 2; + if (ray.side == 1 && ray.raydiry >= 0) + direction = 3; + if (ray.side == 0) + t.wallx = ray.posy + ray.perpwalldist \ + * ray.raydiry; + else + t.wallx = ray.posx + ray.perpwalldist \ + * ray.raydirx; + t.wallx -= floor((t.wallx)); +} + +texture[0].addr = (int *)mlx_get_data_addr(texture[0].img, &bpp, + texture[0].line_length, &endian); + + +void load_textures() +{ + g_tex.tex_width = 64; + g_tex.tex_height = 64; + + g_tex.textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", + &g_tex.tex_width, &g_tex.tex_height); +} \ No newline at end of file From bc2d42d9417d70fbb7778b68ab23232bc67e8ee8 Mon Sep 17 00:00:00 2001 From: Gregory Robledo Date: Thu, 17 Oct 2024 16:32:01 +0200 Subject: [PATCH 2/2] add texture --- Makefile | 2 +- algo.c | 29 +++++++++++---------- algo.h | 33 +++++++++++++++--------- render.c | 76 ++++++++++++++++++++++++++++++++++++++++++-------------- utils.c | 4 +-- utils2.c | 76 ++++++++++++++++++++++++++++++-------------------------- 6 files changed, 138 insertions(+), 82 deletions(-) diff --git a/Makefile b/Makefile index 4ef9264..ad84bcb 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ NAME = cub3d CPP = gcc $(FLAGS) LIBRARIES_DIR = Libft Minilibx LIBRARIES_A = Libft/libft.a Minilibx/libmlx.a -CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1) +CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1) -g CPP_2 = $(CPP) $(FLAGS_2) SHARED = -lX11 -lXext -lm C_FILES = $(wildcard **.c) diff --git a/algo.c b/algo.c index 08bdf5c..f042a1b 100644 --- a/algo.c +++ b/algo.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* algo.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ -/* Updated: 2024/10/15 16:38:31 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 16:20:19 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ void *g_mlx = NULL; void *g_win = NULL; t_map g_map; t_player g_player; +t_tex g_tex; +t_ray g_ray; // TODO manage image format error better @@ -39,24 +41,25 @@ void draw_screen(void) mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0); } -void load_textures(t_tex *tex) +void load_textures() { - int width; - int height; + g_tex.tex_width = 64; + g_tex.tex_height = 64; - tex->tex_north = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", - &width, &height); - tex->tex_south = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", - &width, &height); - tex->tex_east = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", - &width, &height); - tex->tex_west = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", - &width, &height); + g_tex.textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", + &g_tex.tex_width, &g_tex.tex_height); + g_tex.textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", + &g_tex.tex_width, &g_tex.tex_height); } int main(int argc, char *argv[]) { g_mlx = mlx_init(); + load_textures(); if (argc != 2) { printf("Syntax: %s \n", argv[0]); diff --git a/algo.h b/algo.h index 55be665..03dafc2 100644 --- a/algo.h +++ b/algo.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* algo.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ -/* Updated: 2024/10/15 16:57:48 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 15:17:36 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,20 +40,29 @@ # define SCREEN_HEIGHT 480 // px # define FOV 0.66 // ? TODO unit +typedef struct s_tex +{ + void *textures[4]; + void *current_tex; + int tex_height; + int tex_width; + int tex_dir; +} t_tex; + +typedef struct s_ray +{ + // pos player on map (cases) + t_point_double dir; + bool side; + double wallx; +} t_ray; + extern void *g_mlx; extern void *g_win; extern t_player g_player; extern t_map g_map; - -typedef struct s_tex -{ - void *tex_north; - void *tex_south; - void *tex_east; - void *tex_west; - void *current_tex; - -} t_tex; +extern t_tex g_tex; +extern t_ray g_ray; /// @brief Write an error message on stderr. /// diff --git a/render.c b/render.c index 6279fd5..26633f1 100644 --- a/render.c +++ b/render.c @@ -3,17 +3,18 @@ /* ::: :::::::: */ /* render.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ +/* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 14:55:05 by greg #+# #+# */ -/* Updated: 2024/10/15 16:55:02 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 16:28:20 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ #include "algo.h" +#include static void calculate_perpwalldist3(int x, - t_point_double *raydir, t_point_double *deltadist) + t_point_double *deltadist) { const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1; t_point_double plane; @@ -21,14 +22,14 @@ static void calculate_perpwalldist3(int x, vector_from_rotation(&dir, g_map.player.rot, 1); vector_from_rotation(&plane, g_map.player.rot + PI / 2, 1); - raydir->x = dir.x + plane.x * ray_direction; - raydir->y = dir.y + plane.y * ray_direction; + g_ray.dir.x = dir.x + plane.x * ray_direction; + g_ray.dir.y = dir.y + plane.y * ray_direction; deltadist->x = 1e30; - if (raydir->x != 0) - deltadist->x = fabs(1 / raydir->x); + if (g_ray.dir.x != 0) + deltadist->x = fabs(1 / g_ray.dir.x); deltadist->y = 1e30; - if (raydir->y != 0) - deltadist->y = fabs(1 / raydir->y); + if (g_ray.dir.y != 0) + deltadist->y = fabs(1 / g_ray.dir.y); } static void calculate_perpwalldist2(int x, @@ -38,10 +39,10 @@ static void calculate_perpwalldist2(int x, { const int map_x = (int)g_map.player.pos.x; const int map_y = (int)g_map.player.pos.y; - t_point_double raydir; + // t_point_double raydir; - calculate_perpwalldist3(x, &raydir, deltadist); - if (raydir.x < 0) + calculate_perpwalldist3(x, deltadist); + if (g_ray.dir.x < 0) { step->x = -1; sidedist->x = (g_map.player.pos.x - map_x) * deltadist->x; @@ -51,7 +52,7 @@ static void calculate_perpwalldist2(int x, step->x = 1; sidedist->x = (map_x + 1.0 - g_map.player.pos.x) * deltadist->x; } - if (raydir.y < 0) + if (g_ray.dir.y < 0) { step->y = -1; sidedist->y = (g_map.player.pos.y - map_y) * deltadist->y; @@ -71,7 +72,7 @@ static void calculate_perpwalldist2(int x, static double calculate_perpwalldist(int x) { t_point_int map_pos; - bool side; + // bool side; t_point_double sidedist; t_point_double deltadist; t_point_int step; @@ -85,18 +86,37 @@ static double calculate_perpwalldist(int x) { sidedist.x += deltadist.x; map_pos.x += step.x; - side = false; + g_ray.side = false; continue ; } sidedist.y += deltadist.y; map_pos.y += step.y; - side = true; + g_ray.side = true; } - if (!side) + if (!g_ray.side) return (sidedist.x - deltadist.x); return (sidedist.y - deltadist.y); } +static void ft_init_texture(const double perpwalldist) +{ + if (g_ray.side == 0 && g_ray.dir.x < 0) + g_tex.tex_dir = 0; + if (g_ray.side == 0 && g_ray.dir.x >= 0) + g_tex.tex_dir = 1; + if (g_ray.side == 1 && g_ray.dir.y < 0) + g_tex.tex_dir = 2; + if (g_ray.side == 1 && g_ray.dir.y >= 0) + g_tex.tex_dir = 3; + if (g_ray.side == 0) + g_ray.wallx = g_player.pos.y + perpwalldist \ + * g_ray.dir.y; + else + g_ray.wallx = g_player.pos.x + perpwalldist \ + * g_ray.dir.x; + g_ray.wallx -= floor((g_ray.wallx)); +} + /// @brief Draw a vertical line according to the ray direction x. /// /// @param x Ray direction x between 0 and 1. @@ -107,7 +127,14 @@ static void draw_vertical_line(int x, u_int32_t *img_data) 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); draw_start = -line_height / 2 + SCREEN_HEIGHT / 2; if (draw_start < 0) draw_start = 0; @@ -120,7 +147,18 @@ static void draw_vertical_line(int x, u_int32_t *img_data) if (y < draw_start) img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING; else if (y < draw_end) - img_data[y * SCREEN_WIDTH + x] = COLOR_WALL; + { + tex_y = ((y - draw_start) * g_tex.tex_height) / line_height; + tex_x = (int)(g_ray.wallx * g_tex.tex_width); + if (g_ray.side == 0 && g_ray.dir.x > 0) + tex_x = g_tex.tex_width - tex_x - 1; + if (g_ray.side == 1 && g_ray.dir.y < 0) + tex_x = g_tex.tex_height - tex_x - 1; + u_int32_t *texture_data = (u_int32_t *)mlx_get_data_addr(g_tex.textures[g_tex.tex_dir], &bpp, &size_line, &endian); + color = texture_data[tex_y * g_tex.tex_width + tex_x]; + img_data[y * SCREEN_WIDTH + x] = color; + // img_data[y * SCREEN_WIDTH + x] = COLOR_WALL; + } else img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR; y++; @@ -138,4 +176,4 @@ int render(u_int32_t *img_data) x++; } return (0); -} +} \ No newline at end of file diff --git a/utils.c b/utils.c index 48e0c16..283a990 100644 --- a/utils.c +++ b/utils.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */ -/* Updated: 2024/10/16 18:01:25 by mcolonna ### ########.fr */ +/* Updated: 2024/10/17 13:43:50 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/utils2.c b/utils2.c index 6e3d3b3..dc81ffa 100644 --- a/utils2.c +++ b/utils2.c @@ -3,48 +3,54 @@ /* ::: :::::::: */ /* utils2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: greg +#+ +:+ +#+ */ +/* By: grobledo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 01:57:03 by greg #+# #+# */ -/* Updated: 2024/10/17 12:28:01 by greg ### ########.fr */ +/* Updated: 2024/10/17 14:58:33 by grobledo ### ########.fr */ /* */ /* ************************************************************************** */ // #include "algo.h" -void ft_init_texture() -{ - if (ray.side == 0 && ray.raydirx < 0) - direction = 0; - if (ray.side == 0 && ray.raydirx >= 0) - direction = 1; - if (ray.side == 1 && ray.raydiry < 0) - direction = 2; - if (ray.side == 1 && ray.raydiry >= 0) - direction = 3; - if (ray.side == 0) - t.wallx = ray.posy + ray.perpwalldist \ - * ray.raydiry; - else - t.wallx = ray.posx + ray.perpwalldist \ - * ray.raydirx; - t.wallx -= floor((t.wallx)); -} -texture[0].addr = (int *)mlx_get_data_addr(texture[0].img, &bpp, - texture[0].line_length, &endian); +// void ft_init_texture(const double perpwalldist) +// { +// if (g_ray.side == 0 && g_ray.dir.x < 0) +// g_tex.tex_dir = 0; +// if (g_ray.side == 0 && g_ray.dir.x >= 0) +// g_tex.tex_dir = 1; +// if (g_ray.side == 1 && g_ray.dir.y < 0) +// g_tex.tex_dir = 2; +// if (g_ray.side == 1 && g_ray.dir.y >= 0) +// g_tex.tex_dir = 3; +// if (g_ray.side == 0) +// g_ray.wallx = g_player.pos.y + perpwalldist +// * g_ray.dir.y; +// else +// g_ray.wallx = g_player.pos.x + perpwalldist +// * g_ray.dir.x; +// g_ray.wallx -= floor((g_ray.wallx)); +// } + +// void *tex_ptr = (int *)mlx_get_data_addr(texture[0].img, &bpp, +// g_tex, &endian); -void load_textures() -{ - g_tex.tex_width = 64; - g_tex.tex_height = 64; +// tex_y = ((y - draw_start) * texture_height) / line_height; - g_tex.textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", - &g_tex.tex_width, &g_tex.tex_height); - g_tex.textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", - &g_tex.tex_width, &g_tex.tex_height); - g_tex.textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", - &g_tex.tex_width, &g_tex.tex_height); - g_tex.textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", - &g_tex.tex_width, &g_tex.tex_height); -} \ No newline at end of file +// tex_x = (int)(wall_x * (double)texture_width); +// if ((!side && raydir.x > 0) || (side && raydir.y < 0)) +// { +// tex_x = texture_width - tex_x - 1; +// } + +// u_int32_t color = textures[direction][tex_y * texture_width + tex_x]; + + + // else if (y < draw_end) + // { + // tex_y = ((y - draw_start) * g_tex.tex_height) / line_height; + // tex_x = (int)(g_ray.wallx * g_tex.tex_width); + // u_int32_t *texture_data = (u_int32_t *)g_tex.textures[g_tex.tex_dir]; + // u_int32_t color = texture_data[tex_y * g_tex.tex_width + tex_x]; + // img_data[y * SCREEN_WIDTH + x] = color; + // } \ No newline at end of file