test ray v.final :D (if you're here again you f'ed up)

This commit is contained in:
Baptiste GOULARD 2024-11-28 12:11:31 +01:00
parent f100ccef11
commit abfb831803
3 changed files with 253 additions and 201 deletions

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/11/27 19:51:13 by rparodi ### ########.fr #
# Updated: 2024/11/28 09:20:29 by bgoulard ### ########.fr #
# #
# **************************************************************************** #
@ -27,13 +27,14 @@ CFLAGS += -g3 -MMD
# CFLAGS += -fsanitize=thread
# INCLUDES += -I /opt/X11/include
INCLUDES = -I /usr/include -I ./includes -I ./includes/include -I ./minilibx-linux
INCLUDES = -I /usr/include -I ../includes -I ../includes/include -I ../minilibx-linux
INCLUDES += -I ../
# Paths
MLX_DIR = ./minilibx-linux
MLX_DIR = ../minilibx-linux
# Library flags
LDFLAGS = -L./build -lmlx -lm
LDFLAGS = -L../build -lmlx -lm -lft
# MiniLibX flags for macOS with XQuartz
MLXFLAGS = -L$(MLX_DIR) -lX11 -lXext
@ -83,8 +84,9 @@ fclean: clean
re: header fclean all
# Compile external libraries
#
build/libmlx.a:
@make --no-print-directory -C $(MLX_DIR)
# @make --no-print-directory -C $(MLX_DIR)
# Dependences for all
$(NAME): $(OBJ) build/libmlx.a

View file

@ -6,13 +6,19 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 12:09:00 by rparodi #+# #+# */
/* Updated: 2024/11/27 15:59:53 by rparodi ### ########.fr */
/* Updated: 2024/11/28 12:09:36 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "test.h"
#include "ft_string.h"
#include "ft_math.h"
#include "minilibx-linux/mlx.h"
#include <math.h>
#include <X11/X.h>
int worldMap[MAP_WIDTH][MAP_HEIGHT] = {\
#include "./test.h"
const int g_worldMap[MAP_WIDTH][MAP_HEIGHT] = { \
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, \
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, \
{1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, \
@ -41,198 +47,219 @@ int worldMap[MAP_WIDTH][MAP_HEIGHT] = {\
void my_mlx_pixel_put(t_data *data, int x, int y, int color)
{
char *dst;
uint *dst;
dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
*(unsigned int *)dst = color;
x *= data->bits_per_pixel / 8;
dst = (uint *)(data->addr + (y * data->line_length + x));
*dst = color;
}
double deg2rad(int deg)
{
return (deg * M_PI / 180);
}
void rotate_plane(t_2dpoint *plane, double angle)
{
double old_plane_x;
double old_plane_y;
old_plane_x = (*plane).x;
old_plane_y = (*plane).y;
plane->x = plane->x * cos(angle) - plane->y * sin(angle);
plane->y = old_plane_x * sin(angle) + plane->y * cos(angle);
}
void move_straight(t_data *data)
{
if (!worldMap[\
(int)(data->pos_x + data->dir_x * MOVE_SPEED)][(int)data->pos_y])
data->pos_x += data->dir_x * MOVE_SPEED;
if (!worldMap[\
(int)data->pos_x][(int)(data->pos_y + data->dir_y * MOVE_SPEED)])
data->pos_y += data->dir_y * MOVE_SPEED;
if (!g_worldMap[\
(int)(data->pos.x + data->dir.x * MOVE_SPEED)][(int)data->pos.y])
data->pos.x += data->dir.x * MOVE_SPEED;
if (!g_worldMap[\
(int)data->pos.x][(int)(data->pos.y + data->dir.y * MOVE_SPEED)])
data->pos.y += data->dir.y * MOVE_SPEED;
}
void move_backward(t_data *data)
{
if (!worldMap[\
(int)(data->pos_x - data->dir_x * MOVE_SPEED)][(int)data->pos_y])
data->pos_x -= data->dir_x * MOVE_SPEED;
if (!worldMap[\
(int)data->pos_x][(int)(data->pos_y - data->dir_y * MOVE_SPEED)])
data->pos_y -= data->dir_y * MOVE_SPEED;
if (!g_worldMap[\
(int)(data->pos.x - data->dir.x * MOVE_SPEED)][(int)data->pos.y])
data->pos.x -= data->dir.x * MOVE_SPEED;
if (!g_worldMap[\
(int)data->pos.x][(int)(data->pos.y - data->dir.y * MOVE_SPEED)])
data->pos.y -= data->dir.y * MOVE_SPEED;
}
void move_left(t_data *data)
{
double perp_dir_x;
double perp_dir_y;
double pdir_x;
double pdir_y;
perp_dir_x = -data->dir_y;
perp_dir_y = data->dir_x;
if (!worldMap[\
(int)(data->pos_x + perp_dir_x * MOVE_SPEED)][(int)data->pos_y])
data->pos_x -= perp_dir_x * MOVE_SPEED;
if (!worldMap[\
(int)data->pos_x][(int)(data->pos_y - perp_dir_y * MOVE_SPEED)])
data->pos_y -= perp_dir_y * MOVE_SPEED;
pdir_x = -data->dir.y;
pdir_y = data->dir.x;
if (!g_worldMap[(int)(data->pos.x + pdir_x * MOVE_SPEED)][(int)data->pos.y])
data->pos.x -= pdir_x * MOVE_SPEED;
if (!g_worldMap[(int)data->pos.x][(int)(data->pos.y - pdir_y * MOVE_SPEED)])
data->pos.y -= pdir_y * MOVE_SPEED;
}
void move_right(t_data *data)
{
double perp_dir_x;
double perp_dir_y;
double pdir_x;
double pdir_y;
perp_dir_x = -data->dir_y;
perp_dir_y = data->dir_x;
if (!worldMap[\
(int)(data->pos_x - perp_dir_x * MOVE_SPEED)][(int)data->pos_y])
data->pos_x += perp_dir_x * MOVE_SPEED;
if (!worldMap[\
(int)data->pos_x][(int)(data->pos_y + perp_dir_y * MOVE_SPEED)])
data->pos_y += perp_dir_y * MOVE_SPEED;
pdir_x = -data->dir.y;
pdir_y = data->dir.x;
if (!g_worldMap[(int)(data->pos.x - pdir_x * MOVE_SPEED)][(int)data->pos.y])
data->pos.x += pdir_x * MOVE_SPEED;
if (!g_worldMap[(int)data->pos.x][(int)(data->pos.y + pdir_y * MOVE_SPEED)])
data->pos.y += pdir_y * MOVE_SPEED;
}
void look_left(t_data *data)
{
double old_dir_x;
double old_plane_x;
old_dir_x = data->dir_x;
old_plane_x = data->plane_x;
data->dir_x = data->dir_x * cos(ROT_SPEED) - data->dir_y * sin(ROT_SPEED);
data->dir_y = old_dir_x * sin(ROT_SPEED) + data->dir_y * cos(ROT_SPEED);
data->plane_x = data->plane_x * cos(ROT_SPEED) \
- data->plane_y * sin(ROT_SPEED);
data->plane_y = old_plane_x * sin(ROT_SPEED) \
+ data->plane_y * cos(ROT_SPEED);
rotate_plane(&data->dir, ROT_SPEED);
rotate_plane(&data->plane, ROT_SPEED);
}
void look_right(t_data *data)
{
double old_dir_x;
double old_plane_x;
old_dir_x = data->dir_x;
old_plane_x = data->plane_x;
data->dir_x = data->dir_x * cos(-ROT_SPEED) - data->dir_y * sin(-ROT_SPEED);
data->dir_y = old_dir_x * sin(-ROT_SPEED) + data->dir_y * cos(-ROT_SPEED);
data->plane_x = data->plane_x * cos(-ROT_SPEED) \
- data->plane_y * sin(-ROT_SPEED);
data->plane_y = old_plane_x * sin(-ROT_SPEED) \
+ data->plane_y * cos(-ROT_SPEED);
rotate_plane(&data->dir, -ROT_SPEED);
rotate_plane(&data->plane, -ROT_SPEED);
}
int key_hook(int keycode, t_data *data)
{
if (keycode == 65307)
if (keycode == XK_Escape)
exit(0);
if (keycode == 119)
if (keycode == XK_w)
move_straight(data);
if (keycode == 115)
if (keycode == XK_s)
move_backward(data);
if (keycode == 97)
if (keycode == XK_a)
move_right(data);
if (keycode == 100)
if (keycode == XK_d)
move_left(data);
if (keycode == 65361)
if (keycode == XK_Left)
look_left(data);
if (keycode == 65363)
if (keycode == XK_Right)
look_right(data);
return (0);
}
int render_frame(t_data *data)
int get_cl(int side, t_2ipoint step)
{
int y;
int x;
y = 0;
while (y < WINDOW_HEIGHT)
{
x = 0;
while (x < WINDOW_WIDTH)
{
my_mlx_pixel_put(data, x, y, 0x00000000);
x++;
}
y++;
}
for(int x = 0; x < WINDOW_WIDTH; x++)
{
double cameraX = 2 * x / (double)WINDOW_WIDTH - 1;
double rayDirX = data->dir_x + data->plane_x * cameraX;
double rayDirY = data->dir_y + data->plane_y * cameraX;
int mapX = (int)data->pos_x;
int mapY = (int)data->pos_y;
double sideDistX, sideDistY;
double deltaDistX = fabs(1 / rayDirX);
double deltaDistY = fabs(1 / rayDirY);
double perpWallDist;
int stepX, stepY;
int hit = 0;
int side;
if (rayDirX < 0) {
stepX = -1;
sideDistX = (data->pos_x - mapX) * deltaDistX;
} else {
stepX = 1;
sideDistX = (mapX + 1.0 - data->pos_x) * deltaDistX;
}
if (rayDirY < 0) {
stepY = -1;
sideDistY = (data->pos_y - mapY) * deltaDistY;
} else {
stepY = 1;
sideDistY = (mapY + 1.0 - data->pos_y) * deltaDistY;
}
while (hit == 0) {
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
} else {
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
if (worldMap[mapX][mapY] > 0)
hit = 1;
}
if (side == 0)
perpWallDist = (mapX - data->pos_x + (1 - stepX) / 2) / rayDirX;
else
perpWallDist = (mapY - data->pos_y + (1 - stepY) / 2) / rayDirY;
int lineHeight = (int)(WINDOW_HEIGHT / perpWallDist);
int drawStart = -lineHeight / 2 + WINDOW_HEIGHT / 2;
if(drawStart < 0) drawStart = 0;
int drawEnd = lineHeight / 2 + WINDOW_HEIGHT / 2;
if(drawEnd >= WINDOW_HEIGHT) drawEnd = WINDOW_HEIGHT - 1;
int color;
if (side == 0 && stepX > 0)
if (side == 0 && step.x > 0)
color = 0x00FF0000;
else if (side == 0 && stepX < 0)
else if (side == 0 && step.x < 0)
color = 0x0000FF00;
else if (side == 1 && stepY > 0)
else if (side == 1 && step.y > 0)
color = 0x000000FF;
else
color = 0x00FFFF00;
return (color);
}
for(int y = drawStart; y < drawEnd; y++)
my_mlx_pixel_put(data, x, y, color);
void draw_(int side, double perpWallDist, t_2ipoint step, int x, t_data *data)
{
const int lineHeight = (int)(WINDOW_HEIGHT / perpWallDist) * cos(deg2rad(FOV/ 2));
int drawStart;
int drawEnd;
int color;
drawStart = -lineHeight / 2 + WINDOW_HEIGHT / 2;
drawEnd = lineHeight / 2 + WINDOW_HEIGHT / 2;
// normalize drawStart and drawEnd
drawStart = ft_clamp(drawStart, 0, WINDOW_HEIGHT - 1);
drawEnd = ft_clamp(drawEnd, 0, WINDOW_HEIGHT - 1);
color = get_cl(side, step);
while (drawStart < drawEnd)
my_mlx_pixel_put(data, x, drawStart++, color);
}
void search_hit(t_2dpoint *sideDist, t_2dpoint deltaDist, t_2ipoint *map, t_2ipoint step, int *side)
{
while (true) {
if (sideDist->x < sideDist->y) {
sideDist->x += deltaDist.x;
map->x += step.x;
*side = 0;
} else {
sideDist->y += deltaDist.y;
map->y += step.y;
*side = 1;
}
if (g_worldMap[map->x][map->y] > 0)
return ;
}
}
void static set_step(t_2ipoint *step, t_2dpoint raydir)
{
if (raydir.x < 0)
step->x = -1;
else
step->x = 1;
if (raydir.y < 0)
step->y = -1;
else
step->y = 1;
}
void static set_side_dist(t_2dpoint *sideDist, t_2dpoint *tb, t_2ipoint map)
{
t_2dpoint rayDir = tb[0];
t_2dpoint pos = tb[1];
t_2dpoint deltaDist = tb[2];
if (rayDir.x < 0)
sideDist->x = (pos.x - map.x) * deltaDist.x;
else
sideDist->x = (map.x + 1.0 - pos.x) * deltaDist.x;
if (rayDir.y < 0)
sideDist->y = (pos.y - map.y) * deltaDist.y;
else
sideDist->y = (map.y + 1.0 - pos.y) * deltaDist.y;
}
void column_handler(t_2ipoint map, t_2dpoint rayDir, t_data *data, int x)
{
t_2dpoint sideDist;
t_2dpoint deltaDist;
double perpWallDist;
t_2ipoint step;
int side;
deltaDist = (t_2dpoint){fabs(1 / rayDir.x), fabs(1 / rayDir.y)};
set_step(&step, rayDir);
set_side_dist(&sideDist, (t_2dpoint[]){rayDir, data->pos, deltaDist}, map);
search_hit(&sideDist, deltaDist, &map, step, &side);
if (side == 0)
perpWallDist = (map.x - data->pos.x + (double)(1 - step.x) / 2)
/ rayDir.x;
else
perpWallDist = (map.y - data->pos.y + (double)(1 - step.y) / 2)
/ rayDir.y;
draw_(side, perpWallDist, step, x, data);
}
int render_frame(t_data *data)
{
double cameraX;
double coef;
coef = 2 * tan(deg2rad(FOV) / 2) / (double)WINDOW_WIDTH;
ft_bzero(data->addr, WINDOW_WIDTH * WINDOW_HEIGHT * data->bits_per_pixel / 8);
for(int x = 0; x < WINDOW_WIDTH; x++)
{
cameraX = x * coef - 1;
column_handler((t_2ipoint){(int)data->pos.x, (int)data->pos.y},
(t_2dpoint){data->dir.x + data->plane.x * cameraX, data->dir.y + data->plane.y * cameraX},
data, x);
}
mlx_put_image_to_window(data->mlx, data->win, data->img, 0, 0);
return (0);
@ -247,14 +274,26 @@ int main(void)
data.img = mlx_new_image(data.mlx, WINDOW_WIDTH, WINDOW_HEIGHT);
data.addr = mlx_get_data_addr(data.img, &data.bits_per_pixel, &data.line_length, &data.endian);
data.pos_x = 22;
data.pos_y = 12;
data.dir_x = -1;
data.dir_y = 0;
data.plane_x = 0;
data.plane_y = 0.66;
mlx_hook(data.win, 2, 1L<<0, key_hook, &data);
// player position
//
// used to get player position in the map
data.pos.x = 22;
data.pos.y = 12;
// player look direction
//
// used to get player look direction vector
data.dir.x = -1;
data.dir.y = 0;
// plane perpendicular to look direction
//
// used to get player camera plane (aka projected screen in front of the
// player for ray intersection)
// the length of the camera plane is 2 * tan(fov / 2) * 1
// where fov is the field of view of the camera
data.plane.x = 0;
data.plane.y = \
2 * tan(deg2rad(FOV) / 2);
mlx_hook(data.win, KeyPress, KeyPressMask, key_hook, &data);
mlx_loop_hook(data.mlx, render_frame, &data);
mlx_loop(data.mlx);
return (0);

View file

@ -6,24 +6,37 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 12:07:20 by rparodi #+# #+# */
/* Updated: 2024/11/27 19:29:06 by rparodi ### ########.fr */
/* Updated: 2024/11/28 11:56:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TEST_H
# define TEST_H
# include "minilibx-linux/mlx.h"
# include <math.h>
# include <stdio.h>
# include <stdlib.h>
// define keys
# define XK_LATIN_1
# include <X11/keysym.h>
# define WINDOW_WIDTH 1920
# define WINDOW_HEIGHT 1080
# define MAP_WIDTH 24
# define MAP_HEIGHT 24
# define MOVE_SPEED 0.5
# define ROT_SPEED 0.5
# define ROT_SPEED 0.1
# define FOV 70
typedef struct s_2dpoint
{
double x;
double y;
} t_2dpoint;
typedef struct s_2ipoint
{
int x;
int y;
} t_2ipoint;
typedef struct s_data
{
@ -34,14 +47,12 @@ typedef struct s_data
int bits_per_pixel;
int line_length;
int endian;
double pos_x;
double pos_y;
double dir_x;
double dir_y;
double plane_x;
double plane_y;
int screen_x;
int screen_y;
t_2ipoint screen_size;
t_2dpoint pos;
t_2ipoint pos_i;
t_2dpoint dir;
t_2dpoint plane;
} t_data;
#endif