This commit is contained in:
2024-01-14 23:36:45 +01:00
parent 2fd2c96579
commit b378e60269
44 changed files with 3041 additions and 7203 deletions

89
draw/ft_2d_view.c Normal file
View File

@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_2d_view.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 17:49:42 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 23:34:45 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void drawRays2D(t_structure_main *w)
{
int r;
int color;
int tilesize;
int numrays;
float ra;
float disH;
float disV;
float disT;
float hx;
float hy;
float vx;
float vy;
float FOV;
float DR;
WallDirection hwalldir;
WallDirection vwalldir;
t_ray_params rayparams;
t_ray_calc_params hrayparams;
t_ray_calc_params vrayparams;
tilesize = w->s_map.mapS;
numrays = NUMRAY;
FOV = FOVIEW * (PI / 180);
DR = FOV / numrays;
draw_background(w);
ra = w->s_player.pa - (FOV / 2);
r = 0;
while (r++ < numrays)
{
ra = fmod(ra + 2 * PI, 2 * PI);
hrayparams.w = w;
hrayparams.ra = ra;
hrayparams.disRay = &disH;
hrayparams.rx = &hx;
hrayparams.ry = &hy;
hrayparams.wallDir = &hwalldir;
calculatehorizontalray(&hrayparams);
vrayparams.w = w;
vrayparams.ra = ra;
vrayparams.disRay = &disV;
vrayparams.rx = &vx;
vrayparams.ry = &vy;
vrayparams.wallDir = &vwalldir;
calculateverticalray(&vrayparams);
if (disH < disV)
{
disT = disH;
color = 0xFF0000;
rayparams.wallDir = hwalldir;
rayparams.rx = hx;
rayparams.ry = hy;
}
else
{
disT = disV;
color = 0x00FF00;
rayparams.wallDir = vwalldir;
rayparams.rx = vx;
rayparams.ry = vy;
}
disT = correctFisheye(disT, ra, w->s_player.pa);
rayparams.w = w;
rayparams.tileSize = tilesize;
rayparams.r = r;
rayparams.disT = disT;
rayparams.numRays = numrays;
rayparams.color = color;
drawray(&rayparams);
ra += DR;
}
}

61
draw/ft_3d_view.c Normal file
View File

@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_3d_view.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 17:35:53 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:19:42 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void init_line_params(t_line_params *lineparams, t_ray_params *rayParams)
{
lineparams->w = rayParams->w;
lineparams->x0 = (int)rayParams->w->s_player.px;
lineparams->y0 = (int)rayParams->w->s_player.py;
lineparams->x1 = (int)rayParams->rx;
lineparams->y1 = (int)rayParams->ry;
lineparams->color = rayParams->color;
}
void init_texture_params(t_texture_params *textureparams,
t_ray_params *rayParams)
{
textureparams->w = rayParams->w;
textureparams->lineOff = rayParams->lineOff;
textureparams->lineH = rayParams->lineH;
textureparams->wallDir = rayParams->wallDir;
textureparams->rx = rayParams->rx + rayParams->backgroundOffsetX;
textureparams->ry = rayParams->ry;
textureparams->disT = rayParams->disT;
}
void drawray(t_ray_params *rayparams)
{
t_line_params lineparams;
t_texture_params textureparams;
rayparams->start3DHeight = 0;
rayparams->max3DHeight = rayparams->w->s_win.height
+ rayparams->start3DHeight;
rayparams->lineH = (rayparams->tileSize * rayparams->max3DHeight)
/ rayparams->disT;
if (rayparams->lineH > rayparams->max3DHeight)
{
rayparams->lineH = rayparams->max3DHeight;
}
rayparams->lineOff = ((rayparams->max3DHeight - rayparams->lineH) / 2);
rayparams->backgroundOffsetX = BOV;
rayparams->raywidth = rayparams->w->s_win.width / rayparams->numRays;
textureparams.startX = rayparams->r * rayparams->raywidth
+ rayparams->backgroundOffsetX;
textureparams.endX = textureparams.startX + rayparams->raywidth;
init_line_params(&lineparams, rayparams);
draw_line(&lineparams);
init_texture_params(&textureparams, rayparams);
draw_texture(&textureparams);
}

47
draw/ft_background.c Normal file
View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_background.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 23:19:39 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:37:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void draw_sky_ground(t_sky_ground_params *params)
{
t_square_params square_params;
square_params.w = params->w;
square_params.x = params->backgroundOffsetX;
square_params.y = params->startHeight;
square_params.xo = params->w->s_win.width + params->backgroundOffsetX;
square_params.yo = params->endHeight;
square_params.color = params->color;
draw_square_raw(&square_params);
}
void draw_background(t_structure_main *w)
{
int backgroundoffsetx;
t_sky_ground_params sky_params;
t_sky_ground_params ground_params;
backgroundoffsetx = BOV;
sky_params.w = w;
sky_params.startHeight = 0;
sky_params.endHeight = w->s_win.height / 2;
sky_params.color = 0xB2FFFF;
sky_params.backgroundOffsetX = backgroundoffsetx;
draw_sky_ground(&sky_params);
ground_params.w = w;
ground_params.startHeight = w->s_win.height / 2;
ground_params.endHeight = w->s_win.height;
ground_params.color = 0x280000;
ground_params.backgroundOffsetX = backgroundoffsetx;
draw_sky_ground(&ground_params);
}

89
draw/ft_draw.c Normal file
View File

@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:38:28 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:54:44 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void draw_square(t_structure_main *w, int x, int y, int color)
{
int xo;
int yo;
int size;
int i;
int j;
xo = x * w->s_map.mapS;
yo = y * w->s_map.mapS;
size = w->s_map.mapS;
i = 0;
while (i < size)
{
j = 0;
while (j < size)
{
put_pixel_img(w, xo + j, yo + i, color);
j++;
}
i++;
}
}
static void init_line_deltas(t_line_deltas *deltas)
{
(deltas->dx) = abs(deltas->params->x1 - deltas->params->x0);
(deltas->dy) = abs(deltas->params->y1 - deltas->params->y0);
(deltas->err) = (deltas->dx) - (deltas->dy);
if (deltas->params->x0 < deltas->params->x1)
(deltas->sx) = 1;
else
(deltas->sx) = -1;
if (deltas->params->y0 < deltas->params->y1)
(deltas->sy) = 1;
else
(deltas->sy) = -1;
}
static void trace_line(t_line_deltas *deltas)
{
int e2;
while (deltas->params->x0 != deltas->params->x1
|| deltas->params->y0 != deltas->params->y1)
{
put_pixel_img(deltas->params->w, deltas->params->x0,
deltas->params->y0, deltas->params->color);
e2 = 2 * (deltas->err);
if (e2 > -((deltas->dy)))
{
(deltas->err) -= (deltas->dy);
deltas->params->x0 += (deltas->sx);
}
if (e2 < (deltas->dx))
{
(deltas->err) += (deltas->dx);
deltas->params->y0 += (deltas->sy);
}
}
}
void draw_line(t_line_params *params)
{
t_line_deltas deltas;
deltas.params = params;
init_line_deltas(&deltas);
trace_line(&deltas);
}
float dist(float ax, float ay, float bx, float by)
{
return (sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay)));
}

82
draw/ft_horizontal_ray.c Normal file
View File

@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_horizontal_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 19:53:47 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 18:34:26 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void handle_ra_equal_pi(t_ray_calc_params *params)
{
*params->rx = params->w->s_player.px;
*params->ry = params->w->s_player.py;
params->dof = DOF;
}
void inithorizontalray(t_ray_calc_params *params)
{
float atan;
int tilesize;
atan = -1 / tan(params->ra);
tilesize = params->w->s_map.mapS;
if (params->ra != PI)
handle_ra_not_equal_pi(params, atan, tilesize);
else
handle_ra_equal_pi(params);
}
void update_ray_params(t_ray_calc_params *params, int mx, int my)
{
int mp;
mp = my * params->w->s_map.mapX + mx;
if (mp >= 0 && mp < params->w->s_map.mapX * params->w->s_map.mapY
&& params->w->s_map.map[mp] == '1')
{
params->dof = DOF;
*params->disRay = dist(params->w->s_player.px, params->w->s_player.py,
*params->rx, *params->ry);
if (params->ra > PI)
*params->wallDir = NORTH;
else
*params->wallDir = SOUTH;
}
}
void processhorizontalray(t_ray_calc_params *params)
{
int mx;
int my;
int tilesize;
tilesize = params->w->s_map.mapS;
*params->disRay = DISRAY;
while (params->dof < DOF)
{
mx = (int)(*params->rx) / tilesize;
my = (int)(*params->ry) / tilesize;
update_ray_params(params, mx, my);
if (params->dof < DOF)
{
*params->rx += params->xo;
*params->ry += params->yo;
params->dof++;
}
}
}
void calculatehorizontalray(t_ray_calc_params *params)
{
params->xo = 0;
params->yo = 0;
params->dof = 0;
inithorizontalray(params);
processhorizontalray(params);
}

44
draw/ft_load_textures.c Normal file
View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_load_textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 20:30:59 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 20:56:42 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void load_texture(t_structure_main *w, char *file_path, void **texture_ptr)
{
int width;
int height;
width = 0;
height = 0;
*texture_ptr = mlx_xpm_file_to_image(w->s_win.mlx,
file_path, &width, &height);
if (!(*texture_ptr))
{
fprintf(stderr, "Failed to load texture: %s\n", file_path);
exit_error(w);
}
w->s_img.texture_width = width;
w->s_img.texture_height = height;
}
void load_wall_textures(t_structure_main *w)
{
load_texture(w, "textures/NO.xpm", (void **)&w->s_img.north_texture);
load_texture(w, "textures/SO.xpm", (void **)&w->s_img.south_texture);
load_texture(w, "textures/WE.xpm", (void **)&w->s_img.west_texture);
load_texture(w, "textures/EA.xpm", (void **)&w->s_img.east_texture);
}
void exit_error(t_structure_main *w)
{
exit(1);
}

50
draw/ft_pixel.c Normal file
View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pixel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 17:54:04 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:54:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void put_pixel_img(t_structure_main *w, int x, int y, int color)
{
char *dst;
if (x >= 0 && y >= 0 && x < w->s_win.width && y < w->s_win.height)
{
dst = w->s_img.addr + (y * w->s_img.line_len + x * (w->s_img.bpp / 8));
*(unsigned int *)dst = color;
}
else
{
}
}
void draw_square_raw(t_square_params *params)
{
int i;
int j;
int size_x;
int size_y;
i = 0;
size_x = abs(params->xo - params->x);
size_y = abs(params->yo - params->y);
while (i < size_y)
{
j = 0;
while (j < size_x)
{
put_pixel_img(params->w, params->x + j,
params->y + i, params->color);
j++;
}
i++;
}
}

59
draw/ft_ray_utils.c Normal file
View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ray_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 18:25:39 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 18:41:22 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void handle_ra_vertical(t_ray_calc_params *params, float nTan, int tileSize)
{
if (params->ra > P2 && params->ra < P3)
{
*params->rx = (((int)params->w->s_player.px / tileSize)
* tileSize) - 0.0001;
params->xo = -tileSize;
}
else if (params->ra < P2 || params->ra > P3)
{
*params->rx = (((int)params->w->s_player.px / tileSize)
* tileSize) + tileSize;
params->xo = tileSize;
}
else
{
*params->rx = params->w->s_player.px;
*params->ry = params->w->s_player.py;
params->dof = DOF;
return ;
}
*params->ry = (params->w->s_player.px - *params->rx)
* nTan + params->w->s_player.py;
params->yo = -params->xo * nTan;
}
void handle_ra_not_equal_pi(t_ray_calc_params *params,
float atan, int tilesize)
{
if (params->ra > PI)
{
*params->ry = (((int)params->w->s_player.py / tilesize)
* tilesize) - 0.0001;
params->yo = -tilesize;
}
else
{
*params->ry = (((int)params->w->s_player.py / tilesize)
* tilesize) + tilesize;
params->yo = tilesize;
}
*params->rx = (params->w->s_player.py - *params->ry)
* atan + params->w->s_player.px;
params->xo = -params->yo * atan;
}

104
draw/ft_textures.c Normal file
View File

@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 17:39:11 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 21:26:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void draw_texture_line(t_texture_params *params, float y, int textureY)
{
int x;
int texturewidth;
int texturex;
int color;
texturewidth = params->w->s_img.texture_width;
x = params->startX;
while (x < params->endX)
{
if (params->wallDir == NORTH || params->wallDir == SOUTH)
texturex = (int)(params->rx * texturewidth
/ params->w->s_map.mapS) % texturewidth;
else
texturex = (int)(params->ry * texturewidth
/ params->w->s_map.mapS) % texturewidth;
if (texturex >= texturewidth)
texturex = texturewidth - 1;
color = get_texture_color(params->w,
params->wallDir, texturex, textureY);
put_pixel_img(params->w, x, y, color);
x++;
}
}
void draw_texture(t_texture_params *params)
{
int textureheight;
float y;
float perspectivefactor;
int texturey;
textureheight = params->w->s_img.texture_height;
y = params->lineOff;
while (y < params->lineOff + params->lineH)
{
perspectivefactor = (y - params->lineOff) / params->lineH;
texturey = perspectivefactor * textureheight;
if (texturey >= textureheight)
texturey = textureheight - 1;
draw_texture_line(params, y, texturey);
y++;
}
}
void *get_selected_texture(t_structure_main *w, WallDirection wallDir)
{
if (wallDir == NORTH)
return (w->s_img.north_texture);
else if (wallDir == SOUTH)
return (w->s_img.south_texture);
else if (wallDir == WEST)
return (w->s_img.west_texture);
else if (wallDir == EAST)
return (w->s_img.east_texture);
else
{
fprintf(stderr, "Invalid wall direction.\n");
exit_error(w);
return (NULL);
}
}
t_texture_data get_texture_data(void *texture)
{
t_texture_data texture_data;
texture_data.data = mlx_get_data_addr(texture, &texture_data.bpp,
&texture_data.size_line, &texture_data.endian);
return (texture_data);
}
int get_texture_color(t_structure_main *w, WallDirection wallDir,
int textureX, int textureY)
{
void *selected_texture;
t_texture_data texture_data;
int pixel_pos;
selected_texture = get_selected_texture(w, wallDir);
if (selected_texture == NULL)
{
return (0);
}
texture_data = get_texture_data(selected_texture);
pixel_pos = (textureX + textureY * w->s_img.texture_width)
* (texture_data.bpp / 8);
return (*(int *)(texture_data.data + pixel_pos));
}

72
draw/ft_vertical_ray.c Normal file
View File

@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vertical_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 20:03:21 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 18:39:19 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void initverticalray(t_ray_calc_params *params)
{
float ntan;
int tilesize;
ntan = -tan(params->ra);
tilesize = params->w->s_map.mapS;
handle_ra_vertical(params, ntan, tilesize);
}
void update_vertical_ray_params(t_ray_calc_params *params, int mx, int my)
{
int mp;
mp = my * params->w->s_map.mapX + mx;
if (mp >= 0 && mp < params->w->s_map.mapX * params->w->s_map.mapY
&& params->w->s_map.map[mp] == '1')
{
params->dof = DOF;
*params->disRay = dist(params->w->s_player.px, params->w->s_player.py,
*params->rx, *params->ry);
if (params->ra > P2 && params->ra < P3)
*params->wallDir = WEST;
else
*params->wallDir = EAST;
}
}
void processverticalray(t_ray_calc_params *params)
{
int mx;
int my;
int tilesize;
tilesize = params->w->s_map.mapS;
*params->disRay = DISRAY;
while (params->dof < DOF)
{
mx = (int)(*params->rx) / tilesize;
my = (int)(*params->ry) / tilesize;
update_vertical_ray_params(params, mx, my);
if (params->dof < DOF)
{
*params->rx += params->xo;
*params->ry += params->yo;
params->dof++;
}
}
}
void calculateverticalray(t_ray_calc_params *params)
{
params->xo = 0;
params->yo = 0;
params->dof = 0;
initverticalray(params);
processverticalray(params);
}