mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
feat: texture rendering
This commit is contained in:
parent
c09ba88d90
commit
c242df5b72
1 changed files with 30 additions and 23 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */
|
/* 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)
|
dst = img->addr + (y * img->line_length + x * (img->bits_per_pixel / 8));
|
||||||
color = 0x0000ff;
|
return (*(int *)dst);
|
||||||
else if (dir == SOUTH)
|
|
||||||
color = 0x00ff00;
|
|
||||||
else if (dir == WEST)
|
|
||||||
color = 0xff0000;
|
|
||||||
else if (dir == EAST)
|
|
||||||
color = 0xffeb3b;
|
|
||||||
else
|
|
||||||
color = 0xff53ff;
|
|
||||||
return (color);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
t_cardinal_dir dir;
|
||||||
int tex_x;
|
int tex_x;
|
||||||
unsigned int color;
|
t_img_data *texture;
|
||||||
double step;
|
|
||||||
double pos;
|
|
||||||
|
|
||||||
dir = get_cardinal(ray);
|
|
||||||
tex_x = (int)(ray->wall_x * TEXTURE_SIZE);
|
tex_x = (int)(ray->wall_x * TEXTURE_SIZE);
|
||||||
if ((ray->side == NORTH && ray->dir_x < 0)
|
if ((ray->side == NORTH && ray->dir_x < 0)
|
||||||
|| (ray->side == SOUTH && ray->dir_y > 0))
|
|| (ray->side == SOUTH && ray->dir_y > 0))
|
||||||
tex_x = TEXTURE_SIZE - tex_x - 1;
|
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;
|
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)
|
while (ray->draw_start < ray->draw_end)
|
||||||
{
|
{
|
||||||
pos += step;
|
tex_y += step;
|
||||||
color = get_color(dir);
|
color = get_color(data, ray, (int)tex_y);
|
||||||
(void)pos;
|
|
||||||
matrix_set(data, x, ray->draw_start, color);
|
matrix_set(data, x, ray->draw_start, color);
|
||||||
ray->draw_start++;
|
ray->draw_start++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue