tmp push merge resolution not finished
This commit is contained in:
parent
2568aa69a6
commit
00dc0e9e67
16 changed files with 454 additions and 59 deletions
|
|
@ -6,17 +6,126 @@
|
|||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/11/18 12:12:29 by bgoulard ### ########.fr */
|
||||
/* Updated: 2024/11/28 15:18:47 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d.h"
|
||||
#include "cub3d_struct.h"
|
||||
|
||||
#include "mlx_functions.h"
|
||||
#include "ft_string.h"
|
||||
#include "ft_math.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data)
|
||||
{
|
||||
const int lineHeight = (int)(data->screen_size.y / perpWallDist) * cos(deg2rad(FOV/ 2));
|
||||
int drawStart;
|
||||
int drawEnd;
|
||||
int color;
|
||||
|
||||
drawStart = -lineHeight / 2 + data->screen_size.y / 2;
|
||||
drawEnd = lineHeight / 2 + data->screen_size.y / 2;
|
||||
// normalize drawStart and drawEnd
|
||||
drawStart = ft_clamp(drawStart, 0, data->screen_size.y - 1);
|
||||
drawEnd = ft_clamp(drawEnd, 0, data->screen_size.y - 1);
|
||||
color = get_cl(side, step);
|
||||
while (drawStart < drawEnd)
|
||||
my_mlx_pixel_put(data, x, drawStart++, color);
|
||||
}
|
||||
|
||||
void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *map, t_ipoint step, void *_data[2])
|
||||
{
|
||||
int *side = (int *)_data[1];
|
||||
t_info *data = (t_info *)_data[0];
|
||||
|
||||
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;
|
||||
}
|
||||
printf("map.x: %d, map.y: %d\n", map->x, map->y);
|
||||
if ((*c3_get_cell(data->map.map, data->map.size, *map))
|
||||
.tile_type != EMPTY)
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
void static set_step(t_ipoint *step, t_dpoint 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_dpoint *sideDist, t_dpoint *tb, t_ipoint map)
|
||||
{
|
||||
t_dpoint rayDir = tb[0];
|
||||
t_dpoint pos = tb[1];
|
||||
t_dpoint 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_ipoint map, t_dpoint rayDir, t_info *data, int x)
|
||||
{
|
||||
t_dpoint sideDist;
|
||||
t_dpoint deltaDist;
|
||||
double perpWallDist;
|
||||
t_ipoint step;
|
||||
int side;
|
||||
|
||||
deltaDist = (t_dpoint){fabs(1 / rayDir.x), fabs(1 / rayDir.y)};
|
||||
set_step(&step, rayDir);
|
||||
set_side_dist(&sideDist, (t_dpoint[]){rayDir, data->player.pos, deltaDist}, map);
|
||||
search_hit(&sideDist, deltaDist, &map, step, (void *[]){data, &side});
|
||||
if (side == 0)
|
||||
perpWallDist = (map.x - data->player.pos.x + (double)(1 - step.x) / 2)
|
||||
/ rayDir.x;
|
||||
else
|
||||
perpWallDist = (map.y - data->player.pos.y + (double)(1 - step.y) / 2)
|
||||
/ rayDir.y;
|
||||
draw_(side, perpWallDist, step, x, data);
|
||||
}
|
||||
|
||||
int render_frame(t_info *data)
|
||||
{
|
||||
double camera_x;
|
||||
double coef;
|
||||
|
||||
coef = 2 * tan(deg2rad(FOV) / 2) / (double)data->screen_size.x;
|
||||
ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y * data->camera.bpp / 8);
|
||||
for(int x = 0; x < data->screen_size.x; x++)
|
||||
{
|
||||
camera_x = x * coef - 1;
|
||||
column_handler((t_ipoint){(int)data->player.pos.x, (int)data->player.pos.y},
|
||||
(t_dpoint){data->player.dir.x + data->player.plane.x * camera_x, data->player.dir.y + data->player.plane.y * camera_x},
|
||||
data, x);
|
||||
}
|
||||
mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, data->camera.screen_buff, 0, 0);
|
||||
return (0);
|
||||
}
|
||||
// Return 42 is irrelevant check mlx_loop to see that mlx doesn't care for the
|
||||
// return of the function...
|
||||
// (Why 'int (*)(void*)' when you dont use the int)
|
||||
|
|
|
|||
29
raycast/get_cl.c
Normal file
29
raycast/get_cl.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_cl.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/28 14:15:04 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/11/28 14:57:25 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d_struct.h"
|
||||
|
||||
int get_cl(int side, t_ipoint step)
|
||||
{
|
||||
int color;
|
||||
|
||||
if (side == 0 && step.x > 0)
|
||||
color = 0x00FF0000;
|
||||
else if (side == 0 && step.x < 0)
|
||||
color = 0x0000FF00;
|
||||
else if (side == 1 && step.y > 0)
|
||||
color = 0x000000FF;
|
||||
else
|
||||
color = 0x00FFFF00;
|
||||
return (color);
|
||||
}
|
||||
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/12 12:24:35 by rparodi #+# #+# */
|
||||
/* Updated: 2024/11/26 13:39:56 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/11/28 15:15:15 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,17 +14,7 @@
|
|||
#include "cub3d_struct.h"
|
||||
#include "mlx_functions.h"
|
||||
#include "raycast.h"
|
||||
|
||||
/**
|
||||
* @brief convert a angle in degrees to radian
|
||||
*
|
||||
* @param degrees the angle in degrees
|
||||
* @return the angle in radian
|
||||
*/
|
||||
double cub_convert_deg_to_rad(double degrees)
|
||||
{
|
||||
return (degrees * (M_PI / 180.0));
|
||||
}
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @brief Launches a ray for raycasting to determine the distance to the first wall.
|
||||
|
|
@ -78,7 +68,7 @@ void draw_line(t_info *info, int x, int start, int end, unsigned int color)
|
|||
}
|
||||
}
|
||||
|
||||
void shelves_launch(t_info *info)
|
||||
/*
|
||||
{
|
||||
info->player.pos = (t_dpoint){ 4.5, 4.5 }; // Starting in the middle of the map
|
||||
info->player.view = 0; int x;
|
||||
|
|
@ -99,4 +89,10 @@ void shelves_launch(t_info *info)
|
|||
|
||||
draw_line(info, x, start, end, 0xFFFFFF);
|
||||
}
|
||||
}*/
|
||||
|
||||
int shelves_launch(t_info *info)
|
||||
{
|
||||
render_frame(info);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
39
raycast/utils_math.c
Normal file
39
raycast/utils_math.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_math.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/28 14:09:12 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/11/28 14:54:32 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cub3d_struct.h"
|
||||
#include <math.h>
|
||||
|
||||
void my_mlx_pixel_put(t_info *data, int x, int y, int color)
|
||||
{
|
||||
uint *dst;
|
||||
|
||||
x *= data->camera.bpp / 8;
|
||||
dst = (uint *)(data->camera.img_addr + (y * data->camera.line_len + x));
|
||||
*dst = color;
|
||||
}
|
||||
|
||||
double deg2rad(int deg)
|
||||
{
|
||||
return (deg * M_PI / 180);
|
||||
}
|
||||
|
||||
void rotate_plane(t_dpoint *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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue