norm render wall render floor dda
This commit is contained in:
parent
10518bf5e9
commit
08edfa99a6
12
inc/render.h
12
inc/render.h
@ -3,10 +3,10 @@
|
||||
/* :::::::: */
|
||||
/* render.h :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 13:08:37 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/14 19:23:07 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,16 +18,18 @@
|
||||
int render_check_bounds(t_screen *screen, t_vec2 *point);
|
||||
void render_line(t_screen *screen,
|
||||
t_vec2 start, t_vec2 end, unsigned int color);
|
||||
void render_circle(
|
||||
t_screen *screen, t_vec2 point, int radius, unsigned int color);
|
||||
void render_circle(t_screen *screen,
|
||||
t_vec2 point, int radius, unsigned int color);
|
||||
void render_clear(t_screen *screen);
|
||||
void render_entities(t_game *game);
|
||||
void render_map(t_game *game);
|
||||
t_render cast_ray(t_game *game, int x);
|
||||
void cast_rays(t_game *game);
|
||||
void render_sprites(t_render *render, t_game *game);
|
||||
double dda(t_vec2 ray_dir, t_vec2 pos, t_map *map);
|
||||
void draw_floor(t_game *game);
|
||||
void draw_line(t_game *game, t_render render, int x);
|
||||
unsigned int get_texture_color(mlx_texture_t *texture,
|
||||
t_render render, int tex_x, int tex_y);
|
||||
t_render render, t_vec2_int tex);
|
||||
|
||||
#endif
|
||||
|
||||
10
inc/types.h
10
inc/types.h
@ -3,10 +3,10 @@
|
||||
/* :::::::: */
|
||||
/* types.h :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/14 12:48:10 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/14 18:42:39 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -36,6 +36,12 @@ typedef struct s_vec2_int
|
||||
int y;
|
||||
} t_vec2_int;
|
||||
|
||||
typedef struct s_range
|
||||
{
|
||||
int start;
|
||||
int end;
|
||||
} t_range;
|
||||
|
||||
typedef struct s_vec2_line
|
||||
{
|
||||
t_vec2 support;
|
||||
|
||||
12
src/player.c
12
src/player.c
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* player.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* :::::::: */
|
||||
/* player.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/08 17:07:17 by qmennen ### ########.fr */
|
||||
/* Updated: 2025/05/14 18:31:54 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
143
src/render/dda.c
143
src/render/dda.c
@ -3,10 +3,10 @@
|
||||
/* :::::::: */
|
||||
/* dda.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/14 13:49:32 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/14 19:26:07 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -94,142 +94,3 @@ double dda(t_vec2 ray_dir, t_vec2 pos, t_map *map)
|
||||
return ((1 - side) * (side_dist.x - delta_dist.x)
|
||||
- side * (side_dist.y - delta_dist.y));
|
||||
}
|
||||
|
||||
// t_side get_side(t_vec2 ray_dir, double perp_dist)
|
||||
// {
|
||||
// if (perp_dist > 0)
|
||||
// {
|
||||
// if (ray_dir.x > 0)
|
||||
// return (SIDE_WEST); //d=1 r.x=1 : 10
|
||||
// else
|
||||
// return (SIDE_EAST); // d=1 r.x=0 : 11
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (ray_dir.y > 0)
|
||||
// return (SIDE_NORTH); // d=0 r.y=1 : 00
|
||||
// else
|
||||
// return (SIDE_SOUTH);
|
||||
// }
|
||||
// }
|
||||
|
||||
// t_render cast_ray(t_game *game, int x)
|
||||
// {
|
||||
// t_vec2 ray_dir;
|
||||
// t_vec2 pos;
|
||||
// t_render render;
|
||||
// double perp_dist;
|
||||
|
||||
// ray_dir = add(game->player->dir,
|
||||
// mul(game->player->camera,
|
||||
// (2.0f * x / (double)game->screen->width - 1)));
|
||||
// pos = game->player->pos;
|
||||
// perp_dist = dda(ray_dir, pos, game->map);
|
||||
// render.perp_dist = fabs(perp_dist);
|
||||
// render.side = get_side(ray_dir, perp_dist);
|
||||
// if (perp_dist < 0)
|
||||
// render.wall_x = pos.x + ray_dir.x * render.perp_dist;
|
||||
// else
|
||||
// render.wall_x = pos.y + ray_dir.y * render.perp_dist;
|
||||
// render.wall_x -= floor(render.wall_x);
|
||||
// return (render);
|
||||
// }
|
||||
|
||||
// unsigned int get_texture_color(mlx_texture_t *texture,
|
||||
// t_render render, int tex_x, int tex_y)
|
||||
// {
|
||||
// int index;
|
||||
// double dist;
|
||||
|
||||
// index = (tex_x + tex_y * texture->height) * texture->bytes_per_pixel;
|
||||
// dist = (render.perp_dist == 0) * 1e30
|
||||
// + (render.perp_dist > 1) * render.perp_dist
|
||||
// + (render.perp_dist <= 1) * 1;
|
||||
// return (texture->pixels[index] << 24
|
||||
// | texture->pixels[index + 1] << 16
|
||||
// | texture->pixels[index + 2] << 8
|
||||
// | (int)(1.0 / dist * 255));
|
||||
// }
|
||||
|
||||
// void draw_line(t_game *game, t_render render, int x)
|
||||
// {
|
||||
// int y;
|
||||
// int color;
|
||||
// int height;
|
||||
// int start;
|
||||
// int tex_start;
|
||||
// int end;
|
||||
// int tex_x;
|
||||
// int tex_y;
|
||||
|
||||
// height = (int)(game->screen->height / render.perp_dist);
|
||||
// start = -height / 2 + game->screen->height / 2;
|
||||
// tex_start = start;
|
||||
// if (start < 0)
|
||||
// start = 0;
|
||||
// end = height / 2 + game->screen->height / 2;
|
||||
// if (end >= game->screen->height)
|
||||
// end = game->screen->height - 1;
|
||||
// tex_x = (int)(render.wall_x * (double)game->map->textures[render.side]->width);
|
||||
// if (render.side == SIDE_NORTH || render.side == SIDE_EAST)
|
||||
// tex_x = game->map->textures[render.side]->width - tex_x - 1;
|
||||
// y = start;
|
||||
// while (y < end)
|
||||
// {
|
||||
// tex_y = (int)(((double) game->map->textures[render.side]->height / (double) height) * (y - tex_start));
|
||||
// color = get_texture_color(game->map->textures[render.side], render, tex_x, tex_y);
|
||||
// if (x < 0 || x >= game->screen->width || y < 0 || y >= game->screen->height)
|
||||
// break ;
|
||||
// mlx_put_pixel(game->screen->img, x, y, color);
|
||||
// y++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// void draw_floor(t_game *game)
|
||||
// {
|
||||
// int y;
|
||||
// int x;
|
||||
// int color;
|
||||
// double row_dist;
|
||||
// t_vec2 floor_step;
|
||||
// t_vec2 floor_pos;
|
||||
// t_vec2_int tex_coords;
|
||||
// const t_vec2 left_ray = sub(game->player->dir, game->player->camera);
|
||||
// const t_vec2 right_ray = add(game->player->dir, game->player->camera);
|
||||
|
||||
// y = game->screen->height / 2;
|
||||
// while (y < game->screen->height)
|
||||
// {
|
||||
// row_dist = 0.5 * game->screen->height / (y - game->screen->height / 2.0);
|
||||
// floor_step = mul(mul(sub(right_ray, left_ray), row_dist), 1.0 / game->screen->width);
|
||||
// floor_pos = add(game->player->pos, mul(left_ray, row_dist));
|
||||
// x = 0;
|
||||
// while (x < game->screen->width)
|
||||
// {
|
||||
// tex_coords = vec2_to_int(mul(get_fraction(floor_pos), 64)); // & 63 is gone heap buffer overflow with asan i dont get it
|
||||
// color = get_texture_color(game->map->texture_floor, (t_render){row_dist, 0, 0}, tex_coords.x, tex_coords.y);
|
||||
// mlx_put_pixel(game->screen->img, x, y, color);
|
||||
// color = get_texture_color(game->map->texture_ceiling, (t_render){row_dist, 0, 0}, tex_coords.x, tex_coords.y);
|
||||
// mlx_put_pixel(game->screen->img, x, game->screen->height - y - 1, color);
|
||||
// floor_pos = add(floor_pos, floor_step);
|
||||
// x++;
|
||||
// }
|
||||
// y++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// void cast_rays(t_game *game)
|
||||
// {
|
||||
// int x;
|
||||
// t_render render[WIDTH];
|
||||
|
||||
// x = 0;
|
||||
// draw_floor(game);
|
||||
// while (x < game->screen->width)
|
||||
// {
|
||||
// render[x] = cast_ray(game, x);
|
||||
// draw_line(game, render[x], x);
|
||||
// x++;
|
||||
// }
|
||||
// render_sprites(render, game);
|
||||
// }
|
||||
|
||||
@ -3,15 +3,14 @@
|
||||
/* :::::::: */
|
||||
/* render.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 16:28:10 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/09 15:20:24 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/14 19:28:47 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "render.h"
|
||||
#include "MLX42.h"
|
||||
#include "cub3d.h"
|
||||
|
||||
int render_check_bounds(t_screen *screen, t_vec2 *point)
|
||||
{
|
||||
@ -65,3 +64,19 @@ void render_entities(t_game *game)
|
||||
{
|
||||
player_render(game->screen, game->player);
|
||||
}
|
||||
|
||||
void cast_rays(t_game *game)
|
||||
{
|
||||
int x;
|
||||
t_render render[WIDTH];
|
||||
|
||||
x = 0;
|
||||
draw_floor(game);
|
||||
while (x < game->screen->width)
|
||||
{
|
||||
render[x] = cast_ray(game, x);
|
||||
draw_line(game, render[x], x);
|
||||
x++;
|
||||
}
|
||||
render_sprites(render, game);
|
||||
}
|
||||
|
||||
@ -6,41 +6,69 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/14 13:06:39 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/14 13:49:09 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/14 19:51:02 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
|
||||
void draw_floor(t_game *game)
|
||||
static t_vec2 calc_floor_step(t_game *game,
|
||||
double row_dist, t_vec2 left_ray, t_vec2 right_ray)
|
||||
{
|
||||
int y;
|
||||
int x;
|
||||
t_vec2 floor_step;
|
||||
|
||||
floor_step = sub(right_ray, left_ray);
|
||||
floor_step = mul(floor_step, row_dist);
|
||||
floor_step = mul(floor_step, 1.0 / game->screen->width);
|
||||
return (floor_step);
|
||||
}
|
||||
|
||||
static void draw_floor_ceiling_pixel(t_game *game,
|
||||
t_vec2_int coord, double row_dist, t_vec2 floor_pos)
|
||||
{
|
||||
const t_vec2_int tex = vec2_to_int(mul(get_fraction(floor_pos), 64));
|
||||
int color;
|
||||
|
||||
color = get_texture_color(game->map->texture_floor,
|
||||
(t_render){row_dist, 0, 0}, tex);
|
||||
mlx_put_pixel(game->screen->img,
|
||||
coord.x, coord.y, color);
|
||||
color = get_texture_color(game->map->texture_ceiling,
|
||||
(t_render){row_dist, 0, 0}, tex);
|
||||
mlx_put_pixel(game->screen->img,
|
||||
coord.x, game->screen->height - coord.y - 1, color);
|
||||
}
|
||||
|
||||
static void draw_floor_row(t_game *game,
|
||||
t_vec2_int coord, t_vec2 left_ray, t_vec2 right_ray)
|
||||
{
|
||||
double row_dist;
|
||||
t_vec2 floor_step;
|
||||
t_vec2 floor_pos;
|
||||
t_vec2_int tex_coords;
|
||||
|
||||
row_dist = 0.5 * game->screen->height;
|
||||
row_dist /= (coord.y - 0.5 * game->screen->height);
|
||||
floor_step = calc_floor_step(game, row_dist, left_ray, right_ray);
|
||||
floor_pos = add(game->player->pos, mul(left_ray, row_dist));
|
||||
while (coord.x < game->screen->width)
|
||||
{
|
||||
draw_floor_ceiling_pixel(game, coord, row_dist, floor_pos);
|
||||
floor_pos = add(floor_pos, floor_step);
|
||||
coord.x++;
|
||||
}
|
||||
}
|
||||
|
||||
void draw_floor(t_game *game)
|
||||
{
|
||||
const t_vec2 left_ray = sub(game->player->dir, game->player->camera);
|
||||
const t_vec2 right_ray = add(game->player->dir, game->player->camera);
|
||||
t_vec2_int coord;
|
||||
|
||||
y = game->screen->height / 2;
|
||||
while (y < game->screen->height)
|
||||
coord.y = game->screen->height / 2;
|
||||
coord.x = 0;
|
||||
while (coord.y < game->screen->height)
|
||||
{
|
||||
row_dist = 0.5 * game->screen->height / (y - game->screen->height / 2.0);
|
||||
floor_step = mul(mul(sub(right_ray, left_ray), row_dist), 1.0 / game->screen->width);
|
||||
floor_pos = add(game->player->pos, mul(left_ray, row_dist));
|
||||
x = 0;
|
||||
while (x < game->screen->width)
|
||||
{
|
||||
tex_coords = vec2_to_int(mul(get_fraction(floor_pos), 64)); // & 63 is gone heap buffer overflow with asan i dont get it
|
||||
color = get_texture_color(game->map->texture_floor, (t_render){row_dist, 0, 0}, tex_coords.x, tex_coords.y);
|
||||
mlx_put_pixel(game->screen->img, x, y, color);
|
||||
color = get_texture_color(game->map->texture_ceiling, (t_render){row_dist, 0, 0}, tex_coords.x, tex_coords.y);
|
||||
mlx_put_pixel(game->screen->img, x, game->screen->height - y - 1, color);
|
||||
floor_pos = add(floor_pos, floor_step);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
draw_floor_row(game, coord, left_ray, right_ray);
|
||||
coord.y++;
|
||||
}
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/05/14 13:05:28 by whaffman #+# #+# */
|
||||
/* Updated: 2025/05/14 13:49:13 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/14 19:26:50 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,14 +17,14 @@ t_side get_side(t_vec2 ray_dir, double perp_dist)
|
||||
if (perp_dist > 0)
|
||||
{
|
||||
if (ray_dir.x > 0)
|
||||
return (SIDE_WEST); //d=1 r.x=1 : 10
|
||||
return (SIDE_WEST);
|
||||
else
|
||||
return (SIDE_EAST); // d=1 r.x=0 : 11
|
||||
return (SIDE_EAST);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ray_dir.y > 0)
|
||||
return (SIDE_NORTH); // d=0 r.y=1 : 00
|
||||
return (SIDE_NORTH);
|
||||
else
|
||||
return (SIDE_SOUTH);
|
||||
}
|
||||
@ -53,12 +53,12 @@ t_render cast_ray(t_game *game, int x)
|
||||
}
|
||||
|
||||
unsigned int get_texture_color(mlx_texture_t *texture,
|
||||
t_render render, int tex_x, int tex_y)
|
||||
t_render render, t_vec2_int tex)
|
||||
{
|
||||
int index;
|
||||
double dist;
|
||||
|
||||
index = (tex_x + tex_y * texture->height) * texture->bytes_per_pixel;
|
||||
index = (tex.x + tex.y * texture->height) * texture->bytes_per_pixel;
|
||||
dist = (render.perp_dist == 0) * 1e30
|
||||
+ (render.perp_dist > 1) * render.perp_dist
|
||||
+ (render.perp_dist <= 1) * 1;
|
||||
@ -68,52 +68,45 @@ unsigned int get_texture_color(mlx_texture_t *texture,
|
||||
| (int)(1.0 / dist * 255));
|
||||
}
|
||||
|
||||
static int set_range(t_game *game, t_render render, t_range *range, int height)
|
||||
{
|
||||
int tex_start;
|
||||
|
||||
height = game->screen->height / render.perp_dist;
|
||||
range->start = game->screen->height / 2 - height / 2;
|
||||
tex_start = range->start;
|
||||
range->start = (range->start > 0) * range->start;
|
||||
range->end = height / 2 + game->screen->height / 2;
|
||||
if (range->end >= game->screen->height)
|
||||
range->end = game->screen->height - 1;
|
||||
return (tex_start);
|
||||
}
|
||||
|
||||
void draw_line(t_game *game, t_render render, int x)
|
||||
{
|
||||
int y;
|
||||
int color;
|
||||
int height;
|
||||
int start;
|
||||
int tex_start;
|
||||
int end;
|
||||
int tex_x;
|
||||
int tex_y;
|
||||
t_vec2_int tex;
|
||||
t_range range;
|
||||
|
||||
height = (int)(game->screen->height / render.perp_dist);
|
||||
start = -height / 2 + game->screen->height / 2;
|
||||
tex_start = start;
|
||||
if (start < 0)
|
||||
start = 0;
|
||||
end = height / 2 + game->screen->height / 2;
|
||||
if (end >= game->screen->height)
|
||||
end = game->screen->height - 1;
|
||||
tex_x = (int)(render.wall_x * (double)game->map->textures[render.side]->width);
|
||||
height = game->screen->height / render.perp_dist;
|
||||
tex_start = set_range(game, render, &range, height);
|
||||
tex.x = render.wall_x * game->map->textures[render.side]->width;
|
||||
if (render.side == SIDE_NORTH || render.side == SIDE_EAST)
|
||||
tex_x = game->map->textures[render.side]->width - tex_x - 1;
|
||||
y = start;
|
||||
while (y < end)
|
||||
tex.x = game->map->textures[render.side]->width - tex.x - 1;
|
||||
while (range.start < range.end)
|
||||
{
|
||||
tex_y = (int)(((double) game->map->textures[render.side]->height / (double) height) * (y - tex_start));
|
||||
color = get_texture_color(game->map->textures[render.side], render, tex_x, tex_y);
|
||||
if (x < 0 || x >= game->screen->width || y < 0 || y >= game->screen->height)
|
||||
tex.y = (range.start - tex_start);
|
||||
tex.y *= ((double)game->map->textures[render.side]->height / height);
|
||||
color = get_texture_color(game->map->textures[render.side],
|
||||
render, tex);
|
||||
if (x < 0
|
||||
|| x >= game->screen->width
|
||||
|| range.start < 0
|
||||
|| range.start >= game->screen->height)
|
||||
break ;
|
||||
mlx_put_pixel(game->screen->img, x, y, color);
|
||||
y++;
|
||||
mlx_put_pixel(game->screen->img, x, range.start, color);
|
||||
range.start++;
|
||||
}
|
||||
}
|
||||
|
||||
void cast_rays(t_game *game)
|
||||
{
|
||||
int x;
|
||||
t_render render[WIDTH];
|
||||
|
||||
x = 0;
|
||||
draw_floor(game);
|
||||
while (x < game->screen->width)
|
||||
{
|
||||
render[x] = cast_ray(game, x);
|
||||
draw_line(game, render[x], x);
|
||||
x++;
|
||||
}
|
||||
render_sprites(render, game);
|
||||
}
|
||||
@ -3,10 +3,10 @@
|
||||
/* :::::::: */
|
||||
/* screen.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
|
||||
/* Updated: 2025/05/11 14:04:28 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/05/14 18:31:32 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -63,8 +63,6 @@ int screen_display(t_screen *screen)
|
||||
int m_width;
|
||||
int m_height;
|
||||
|
||||
|
||||
|
||||
m_width = 0;
|
||||
m_height = 0;
|
||||
fill_background(screen, 0x000000FF);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user