Skip to content

Commit

Permalink
almost there with floor and ceiling textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesim Zaim committed Jan 24, 2024
1 parent 6ed2982 commit 67f736b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub

# CFLAGS += -Wall -Wextra -Werror
CFLAGS += -Wall -Wextra
CFLAGS += -g -fsanitize=address
# CFLAGS += -g -fsanitize=address
# CFLAGS += -g
# CFLAGS += -Ofast -flto -march=native

Expand Down Expand Up @@ -55,7 +55,8 @@ SRCS = parser/check_elements.c \
cub3d.c \
test_utils.c \
timer.c \
set_textures.c
set_textures.c \
game/floorcaster.c

HEADER_DIR := include
HEADERS := meta.h \
Expand Down
12 changes: 7 additions & 5 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,15 @@ typedef struct s_ray {
double step;
} t_ray;


typedef struct s_vray {
t_vec2d floor;
t_vec2d step;
} t_vray;

typedef struct s_player {
t_meta *meta;
t_ray rays[WINDOW_WIDTH];
t_vec2d vertical_rays[WINDOW_HEIGHT];
t_vray vrays[WINDOW_HEIGHT];
t_vec2d cam_plane;
t_vec2d position;
t_vec2d direction;
Expand All @@ -174,7 +177,7 @@ typedef struct s_tex {
} t_tex;

typedef struct s_attr {
t_tex n;
t_tex n; //add bit flag, if tex_path is missing then it means it is a color value
t_tex s;
t_tex e;
t_tex w;
Expand Down Expand Up @@ -264,7 +267,6 @@ uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y);
void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h);

// floorcaster.c

t_vec2d floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y);
t_vray floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y);

#endif
17 changes: 8 additions & 9 deletions src/game/floorcaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/24 13:13:37 by yzaim #+# #+# */
/* Updated: 2024/01/24 13:29:23 by yzaim ######## odam.nl */
/* Updated: 2024/01/24 14:17:50 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"

t_vec2d floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y)
t_vray floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y)
{
t_vray ray;
t_vec2d raydir_left;
t_vec2d raydir_right;

Expand All @@ -23,12 +24,10 @@ t_vec2d floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y)
int p = y - WINDOW_HEIGHT / 2;
double posZ = 0.5 * WINDOW_HEIGHT;
double row_distance = posZ / p;
t_vec2d floor_step;
floor_step.x = row_distance * (raydir_right.x - raydir_left.x) / WINDOW_WIDTH;
floor_step.y = row_distance * (raydir_right.y - raydir_left.y) / WINDOW_WIDTH;
ray.step.x = row_distance * (raydir_right.x - raydir_left.x) / WINDOW_WIDTH;
ray.step.y = row_distance * (raydir_right.y - raydir_left.y) / WINDOW_WIDTH;

t_vec2d floor;
floor.x = pp.x + row_distance + raydir_left.x;
floor.y = pp.y + row_distance + raydir_left.y;
return (floor);
ray.floor.x = pp.x + row_distance + raydir_left.x;
ray.floor.y = pp.y + row_distance + raydir_left.y;
return (ray);
}
4 changes: 2 additions & 2 deletions src/game/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */
/* Updated: 2024/01/24 13:31:54 by yzaim ######## odam.nl */
/* Updated: 2024/01/24 14:06:15 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -71,7 +71,7 @@ void player_raycast(t_player *p)
row = 0;
while (row < h)
{
p->vertical_rays[row] = floorcaster(p->position, p->direction, p->cam_plane, row);
p->vrays[row] = floorcaster(p->position, p->direction, p->cam_plane, row);
row++;
}

Expand Down
34 changes: 24 additions & 10 deletions src/game/render_viewport.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */
/* Updated: 2024/01/24 13:37:01 by yzaim ######## odam.nl */
/* Updated: 2024/01/24 14:25:11 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -53,30 +53,44 @@ static void draw_column(t_meta *meta, t_ray *ray, uint32_t col, uint32_t h)
ray->texture_point.y = ((int) ray->texture_position) & (texture->height - 1);
ray->texture_position += ray->step;
color = pixel_picker(texture, (int)round(ray->texture_point.x), (int)round(ray->texture_point.y));
mlx_put_pixel(meta->image, col, y, color);
}
mlx_put_pixel(meta->image, col, y, color);
y++;
}
}

void draw_fc(t_vec2d floor, )
void draw_fc(mlx_image_t *image, t_vray vray, mlx_texture_t *f_tex, mlx_texture_t *c_tex, uint32_t col, uint32_t row)
{
t_vec2i cell;

cell = (t_vec2i){(int)floor.x, (int)floor.y};
cell = (t_vec2i){(int)vray.floor.x, (int)vray.floor.y};
// cell = vec2d_to_vec2i(floor);

t_vec2i t;
t.x = (int)

// coordinate of pixel in texture
const t_vec2i c_t = (t_vec2i){(int)(c_tex->width * (vray.floor.x - cell.x)) & (c_tex->width - 1), (int)(c_tex->height * (vray.floor.y - cell.y)) & (c_tex->height - 1)};
const t_vec2i f_t = (t_vec2i){(int)(f_tex->width * (vray.floor.x - cell.x)) & (f_tex->width - 1), (int)(f_tex->height * (vray.floor.y - cell.y)) & (f_tex->height - 1)};
vray.floor = vec2d_add(vray.floor, vray.step);

const uint32_t c_color = pixel_picker(c_tex, c_t.x, c_t.y);
const uint32_t f_color = pixel_picker(f_tex, f_t.x, f_t.y);
mlx_put_pixel(image, col, WINDOW_HEIGHT - row - 1, c_color);
mlx_put_pixel(image, col, row, f_color);
}

void render_viewport(mlx_image_t *image, t_player *p)
{
uint32_t col = 0;
uint32_t col = 0;
uint32_t row = 0;
//floor and ceiling
while (col < image->width)
while (row < image->height)
{

col = 0;
while (col < image->width)
{
draw_fc(p->meta->image, p->vrays[row], p->meta->attributes.f.tex, p->meta->attributes.c.tex, col, row);
col++;
}
row++;
}

col = 0;
Expand Down

0 comments on commit 67f736b

Please sign in to comment.