Skip to content

Commit

Permalink
adding floorcaster
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesim Zaim committed Jan 24, 2024
1 parent 9968c7e commit 6ed2982
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 38 deletions.
7 changes: 7 additions & 0 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ typedef struct s_ray {
double step;
} t_ray;



typedef struct s_player {
t_meta *meta;
t_ray rays[WINDOW_WIDTH];
t_vec2d vertical_rays[WINDOW_HEIGHT];
t_vec2d cam_plane;
t_vec2d position;
t_vec2d direction;
Expand Down Expand Up @@ -260,4 +263,8 @@ int set_textures(t_attr *attributes);
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);

#endif
34 changes: 34 additions & 0 deletions src/game/floorcaster.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* floorcaster.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/24 13:13:37 by yzaim #+# #+# */
/* Updated: 2024/01/24 13:29:23 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

#include "meta.h"

t_vec2d floorcaster(t_vec2d pp, t_vec2d dir, t_vec2d cam_plane, uint32_t y)
{
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;
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;

t_vec2d floor;
floor.x = pp.x + row_distance + raydir_left.x;
floor.y = pp.y + row_distance + raydir_left.y;
return (floor);
}
31 changes: 20 additions & 11 deletions src/game/player.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* player.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */
/* Updated: 2024/01/17 15:55:18 by jboeve ######## odam.nl */
/* :::::::: */
/* player.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */
/* Updated: 2024/01/24 13:31:54 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -61,11 +61,20 @@ void player_turn(t_player *p, float radiant)

void player_raycast(t_player *p)
{
uint32_t w = p->meta->image->width;
uint32_t col;
t_vec2d ray_start;
double camera_x;
uint32_t w = p->meta->image->width;
uint32_t h = p->meta->image->height;
uint32_t col;
uint32_t row;
t_vec2d ray_start;
double camera_x;

row = 0;
while (row < h)
{
p->vertical_rays[row] = floorcaster(p->position, p->direction, p->cam_plane, row);
row++;
}

// TODO Just create the player.plane here instead of saving it.
col = 0;
while(col < w)
Expand Down
14 changes: 7 additions & 7 deletions src/game/raycaster.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycaster.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */
/* Updated: 2024/01/18 12:05:53 by jboeve ######## odam.nl */
/* :::::::: */
/* raycaster.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */
/* Updated: 2024/01/24 13:11:30 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
22 changes: 20 additions & 2 deletions src/game/render_viewport.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* render_viewport.c :+: :+: */
/* render_viewport.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */
/* Updated: 2024/01/18 12:27:01 by jboeve ######## odam.nl */
/* Updated: 2024/01/24 13:37:01 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

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

void draw_fc(t_vec2d floor, )
{
t_vec2i cell;

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

t_vec2i t;
t.x = (int)
}

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

}

col = 0;
while(col < image->width)
{
draw_column(p->meta, &p->rays[col], col, image->height);
Expand Down
34 changes: 16 additions & 18 deletions src/renderer/pixel_picker.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pixel_picker.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */
/* Updated: 2024/01/17 15:42:56 by jboeve ######## odam.nl */
/* :::::::: */
/* pixel_picker.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */
/* Updated: 2024/01/24 11:50:31 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,18 +43,16 @@ void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_poin

uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y)
{
int32_t r;
int32_t g;
int32_t b;
int32_t a;
int32_t index;
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
uint32_t index;

// printf("x: %u | y: %u\n", x, y);
index = y * texture->width * 4 + x * 4;
// printf("index: %d\n", index);
r = (uint32_t)texture->pixels[index];
g = (uint32_t)texture->pixels[index + 1];
b = (uint32_t)texture->pixels[index + 2];
a = (uint32_t)texture->pixels[index + 3];
r = texture->pixels[index];
g = texture->pixels[index + 1];
b = texture->pixels[index + 2];
a = texture->pixels[index + 3];
return (r << 24 | g << 16 | b << 8 | a);
}

0 comments on commit 6ed2982

Please sign in to comment.