feat: texture rendering

This commit is contained in:
Khaïs COLIN 2025-08-05 12:55:57 +02:00
parent c09ba88d90
commit c242df5b72
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */
/* Updated: 2025/08/05 12:49:26 by kcolin ### ########.fr */
/* Updated: 2025/08/05 13:25:01 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,43 +33,50 @@ t_cardinal_dir get_cardinal(t_ray *ray)
}
}
static int get_color(t_cardinal_dir dir)
static int my_mlx_pixel_get(t_img_data *img, int x, int y)
{
int color;
char *dst;
if (dir == NORTH)
color = 0x0000ff;
else if (dir == SOUTH)
color = 0x00ff00;
else if (dir == WEST)
color = 0xff0000;
else if (dir == EAST)
color = 0xffeb3b;
else
color = 0xff53ff;
return (color);
dst = img->addr + (y * img->line_length + x * (img->bits_per_pixel / 8));
return (*(int *)dst);
}
void render_walls(t_cub3d_data *data, t_ray *ray, int x)
static int get_color(t_cub3d_data *data, t_ray *ray, int tex_y)
{
t_cardinal_dir dir;
int tex_x;
unsigned int color;
double step;
double pos;
t_img_data *texture;
dir = get_cardinal(ray);
tex_x = (int)(ray->wall_x * TEXTURE_SIZE);
if ((ray->side == NORTH && ray->dir_x < 0)
|| (ray->side == SOUTH && ray->dir_y > 0))
tex_x = TEXTURE_SIZE - tex_x - 1;
dir = get_cardinal(ray);
if (dir == NORTH)
texture = data->no_texture;
else if (dir == SOUTH)
texture = data->so_texture;
else if (dir == WEST)
texture = data->we_texture;
else if (dir == EAST)
texture = data->ea_texture;
else
return (0xff53ff);
return (my_mlx_pixel_get(texture, tex_x, tex_y));
}
void render_walls(t_cub3d_data *data, t_ray *ray, int x)
{
unsigned int color;
double step;
double tex_y;
step = 1.0 * TEXTURE_SIZE / ray->wall_height;
pos = (ray->draw_start - HEIGHT / 2 + ray->wall_height / 2) * step;
tex_y = (ray->draw_start - HEIGHT / 2 + ray->wall_height / 2) * step;
while (ray->draw_start < ray->draw_end)
{
pos += step;
color = get_color(dir);
(void)pos;
tex_y += step;
color = get_color(data, ray, (int)tex_y);
matrix_set(data, x, ray->draw_start, color);
ray->draw_start++;
}