Skip to content

Commit

Permalink
Textures less trippy (still rotating with the players cam)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesim Zaim committed Jan 24, 2024
1 parent 67f736b commit d361c67
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
7 changes: 4 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"files.associations": {
"cstdlib": "c",
"stdio.h": "c"
}
"cstdlib": "c",
"stdio.h": "c",
"meta.h": "c"
}
}
2 changes: 2 additions & 0 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ typedef enum e_cell_type {
MAP_EMPTY,
MAP_WALL,
MAP_SPACE,
MAP_DOOR
} t_cell_type;

typedef enum e_font_family {
Expand Down Expand Up @@ -156,6 +157,7 @@ typedef struct s_player {
t_meta *meta;
t_ray rays[WINDOW_WIDTH];
t_vray vrays[WINDOW_HEIGHT];
bool should_render;
t_vec2d cam_plane;
t_vec2d position;
t_vec2d direction;
Expand Down
9 changes: 7 additions & 2 deletions src/game/floorcaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/24 13:13:37 by yzaim #+# #+# */
/* Updated: 2024/01/24 14:17:50 by yzaim ######## odam.nl */
/* Updated: 2024/01/24 16:06:35 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"

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

t_vray ray;
t_vec2d raydir_left;
t_vec2d raydir_right;

raydir_left = (t_vec2d){dir.x - cam_plane.x, dir.y - cam_plane.y};
raydir_right = (t_vec2d){dir.x + cam_plane.x, dir.y + cam_plane.y};


int p = y - WINDOW_HEIGHT / 2;
double posZ = 0.5 * WINDOW_HEIGHT;
Expand All @@ -29,5 +31,8 @@ t_vray floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y)

ray.floor.x = pp.x + row_distance + raydir_left.x;
ray.floor.y = pp.y + row_distance + raydir_left.y;



return (ray);
}
3 changes: 2 additions & 1 deletion 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 14:06:15 by yzaim ######## odam.nl */
/* Updated: 2024/01/24 16:09:02 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -74,6 +74,7 @@ void player_raycast(t_player *p)
p->vrays[row] = floorcaster(p->position, p->direction, p->cam_plane, row);
row++;
}
p->should_render = true;

// TODO Just create the player.plane here instead of saving it.
col = 0;
Expand Down
34 changes: 21 additions & 13 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 14:25:11 by yzaim ######## odam.nl */
/* Updated: 2024/01/24 16:06:25 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,17 +59,20 @@ static void draw_column(t_meta *meta, t_ray *ray, uint32_t col, uint32_t h)
}
}

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)
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)vray.floor.x, (int)vray.floor.y};
cell = (t_vec2i){(int)vray->floor.x, (int)vray->floor.y};
// cell = vec2d_to_vec2i(floor);

// 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 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);
Expand All @@ -82,17 +85,22 @@ void render_viewport(mlx_image_t *image, t_player *p)
uint32_t col = 0;
uint32_t row = 0;
//floor and ceiling
while (row < image->height)

if (p->should_render)
{
col = 0;
while (col < image->width)
row = 0;
while (row < image->height)
{
draw_fc(p->meta->image, p->vrays[row], p->meta->attributes.f.tex, p->meta->attributes.c.tex, col, row);
col++;
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++;
}
row++;
p->should_render = false;
}

col = 0;
while(col < image->width)
{
Expand Down
5 changes: 1 addition & 4 deletions src/renderer/pixel_picker.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:26:51 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:50:31 by yzaim ######## odam.nl */
/* Updated: 2024/01/24 15:49:05 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -17,9 +17,6 @@
// calculate texture position
void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h)
{
static double x_old = 0;
static double y_old = 0;

ray->texture_point.x = (int)(ray->wall_x * (double)texture->width);

if ((ray->hit_side == SIDE_N || ray->hit_side == SIDE_S) && ray->direction.x > 0)
Expand Down
12 changes: 6 additions & 6 deletions test_maps/valid.cub
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
NO path_to_north
WE path_to_west
EA path_to_east
SO path_to_south
F 255,89,89
C 12, 34,3
NO texture_examples/redbrick.png
WE texture_examples/purplestone.png
EA texture_examples/purplestone.png
SO texture_examples/redbrick.png
F texture_examples/redbrick.png
C texture_examples/redbrick.png

11111111111111
1000010001
Expand Down

0 comments on commit d361c67

Please sign in to comment.