s'occuper des intersections

This commit is contained in:
H3XploR
2025-02-15 20:02:43 +01:00
parent d6a8c5cf31
commit 0bd320f204
10 changed files with 199 additions and 13 deletions
+2 -1
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 01:41:17 by yantoine #+# #+# */ /* Created: 2025/02/14 01:41:17 by yantoine #+# #+# */
/* Updated: 2025/02/14 18:53:17 by yantoine ### ########.fr */ /* Updated: 2025/02/15 19:56:05 by yantoine ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -57,4 +57,5 @@ t_scene load_config(const char *filename)
free(line); free(line);
parsing_line(line, scene); parsing_line(line, scene);
} }
return (scene);
} }
+9 -3
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 20:02:36 by yantoine #+# #+# */ /* Created: 2025/02/13 20:02:36 by yantoine #+# #+# */
/* Updated: 2025/02/14 20:45:21 by yantoine ### ########.fr */ /* Updated: 2025/02/15 19:36:06 by yantoine ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -20,6 +20,7 @@
# include <stdlib.h> # include <stdlib.h>
# include <string.h> # include <string.h>
# include "libft.h" # include "libft.h"
# include "float.h"
// ----- Taille ecran ---- // ----- Taille ecran ----
# define WIDTH 320 # define WIDTH 320
@@ -31,8 +32,9 @@
# define MAX_CYLINDERS 128 # define MAX_CYLINDERS 128
# define MAX_LIGHTS 16 # define MAX_LIGHTS 16
# define MAX_AMBIENT 1 # define MAX_AMBIENT 1
# define MAX_CAMERA 1
// ----- Structures // ----- Espace 3d
typedef struct s_vec3 typedef struct s_vec3
{ {
float x; float x;
@@ -100,6 +102,7 @@ typedef struct s_scene
t_cylinder cylinders[MAX_CYLINDERS]; t_cylinder cylinders[MAX_CYLINDERS];
t_light lights[MAX_LIGHTS]; t_light lights[MAX_LIGHTS];
t_ambient ambient; t_ambient ambient;
t_camera camera;
int numSpheres; int numSpheres;
int numPlanes; int numPlanes;
int numCylinders; int numCylinders;
@@ -129,7 +132,10 @@ t_scene parsing_light(const char *line, t_scene scene);
t_scene parsing_sphere(const char *line, t_scene scene); t_scene parsing_sphere(const char *line, t_scene scene);
t_scene parsing_plane(const char *line, t_scene scene); t_scene parsing_plane(const char *line, t_scene scene);
t_scene parsing_cylindre(const char *line, t_scene scene); t_scene parsing_cylindre(const char *line, t_scene scene);
t_color parse_color(const char *token, t_scene scene); t_vec3 parse_color(const char *token, t_scene scene);
t_vec3 parse_vector(const char *token, t_scene scene);
t_vec3 parse_vector_normalize(const char *token, t_scene scene);
// Parsing utils // Parsing utils
inline char **get_tokens_secure(t_scene scene, const int numObject, const int numObjectMax, const int supposed_nb_token); inline char **get_tokens_secure(t_scene scene, const int numObject, const int numObjectMax, const int supposed_nb_token);
+11 -1
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 20:02:29 by yantoine #+# #+# */ /* Created: 2025/02/14 20:02:29 by yantoine #+# #+# */
/* Updated: 2025/02/14 20:03:25 by yantoine ### ########.fr */ /* Updated: 2025/02/15 19:43:52 by yantoine ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -14,4 +14,14 @@
t_scene parsing_camera(const char *line, t_scene scene) t_scene parsing_camera(const char *line, t_scene scene)
{ {
const char **tokens = get_tokens_secure(scene, scene.numCamera, MAX_CAMERA, 4);
scene.token_if_exit = tokens;
scene.camera.camPos = parse_vector(tokens[1], scene);
scene.camera.camDir = parse_vector_normalize(tokens[2], scene);
scene.camera.fov = ft_atof(tokens[3]);
scene.camera.yaw = atan2f(camDir.x, -camDir.z);
scene.camera.pitch = asinf(camDir.y);
ft_free_array(tokens);
scene.numCamera++;
return (scene);
} }
+8 -7
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 20:05:49 by yantoine #+# #+# */ /* Created: 2025/02/14 20:05:49 by yantoine #+# #+# */
/* Updated: 2025/02/15 18:34:45 by yantoine ### ########.fr */ /* Updated: 2025/02/15 19:23:20 by yantoine ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -19,17 +19,17 @@ static inline int range_is_ok(char **token_color)
i = 0; i = 0;
while (i < 3) while (i < 3)
{ {
if (ft_atoi(token_color[i]) < 0 || ft_atoi(token_color[i]) > 255) if (ft_atof(token_color[i]) < 0 || ft_atof(token_color[i]) > 255)
return (0); return (0);
i++; i++;
} }
return (1); return (1);
} }
t_color parse_color(const char *token, t_scene scene) t_vec3 parse_color(const char *token, t_scene scene)
{ {
const char **token_color = ft_split(token, ','); const char **token_color = ft_split(token, ',');
t_color color; t_vec3 color;
if (!check_tokens(token_color, 3) || !range_is_ok(token_color)) if (!check_tokens(token_color, 3) || !range_is_ok(token_color))
{ {
@@ -40,8 +40,9 @@ t_color parse_color(const char *token, t_scene scene)
close(scene.fd); close(scene.fd);
exit(1); exit(1);
} }
color.r = ft_atoi(token_color[0]) / 255; color.x = ft_atof(token_color[0]) / 255;
color.g = ft_atoi(token_color[1]) / 255; color.y = ft_atof(token_color[1]) / 255;
color.b = ft_atoi(token_color[2]) / 255; color.z = ft_atof(token_color[2]) / 255;
ft_free_array(tokens_color);
return (color); return (color);
} }
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_cylinder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 19:54:13 by yantoine #+# #+# */
/* Updated: 2025/02/15 19:54:28 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "miniRT.h"
t_scene parsing_cylinder(const char *line, t_scene scene)
{
const char **tokens = get_tokens_secure(scene, scene.numCylinders, MAX_CYLINDERS, 6);
scene.token_if_exit = tokens;
scene.cylinders[scene.numCylinders].center = parse_vector(tokens[1], scene);
scene.cylinders[scene.numCylinders].axis = parse_vector_normalize(tokens[2], scene);
scene.cylinders[scene.numCylinders].radius = parse_float(tokens[3], scene);
scene.cylinders[scene.numCylinders].height = parse_float(tokens[4], scene);
scene.cylinders[scene.numCylinders].color = parse_color(tokens[5], scene);
ft_free_array(tokens);
scene.numCylinders++;
return (scene);
}
+25
View File
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_light.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 19:39:08 by yantoine #+# #+# */
/* Updated: 2025/02/15 19:44:19 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "minRT.h"
t_scene parsing_light(const char *line, t_scene scene)
{
const char **tokens = get_tokens_secure(scene, scene.numLights, MAX_LIGHTS, 4);
scene.token_if_exit = tokens;
scene.lights[scene.numLights].pos = parse_vector(tokens[1], scene);
scene.lights[scene.numLights].brightness = ft_atof(tokens[2], scene);
scene.lights[scene.numLights].color = parse_color(tokens[3], scene);
ft_free_array(tokens);
scene.numLights++;
return (scene);
}
+25
View File
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_plane.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 19:49:41 by yantoine #+# #+# */
/* Updated: 2025/02/15 19:53:30 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "miniRT.h"
t_scene parsing_plane(const char *line, t_scene scene)
{
const char **tokens = get_tokens_secure(scene, scene.numPlanes, MAX_PLANES, 4);
scene.token_if_exit = tokens;
scene.planes[scene.numPlanes].point = parse_vector(tokens[1], scene);
scene.planes[scene.numPlanes].normal = parse_vector_normalize(tokens[2], scene);
scene.planes[scene.numPlanes].color = parse_color(tokens[3], scene);
ft_free_array(tokens);
scene.numPlanes++;
return (scene);
}
+25
View File
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_sphere.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 19:46:16 by yantoine #+# #+# */
/* Updated: 2025/02/15 19:48:43 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "miniRT.h"
t_scene parsing_sphere(const char *line, t_scene scene)
{
const char **tokens = get_tokens_secure(scene, scene.numSpheres, MAX_SPHERES, 4);
scene.token_if_exit = tokens;
scene.spheres[scene.numSpheres].center = parse_vector(tokens[1], scene);
scene.spheres[scene.numSpheres].radius = ft_atof(tokens[2], scene) / 2.0f;
scene.spheres[scene.numSpheres].color = parse_color(tokens[3], scene);
ft_free_array(tokens);
scene.numSpheres++;
return (scene);
}
+57
View File
@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_vector.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 19:16:01 by yantoine #+# #+# */
/* Updated: 2025/02/15 19:36:24 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "miniRT.h"
static inline int range_is_ok(char **token_vector)
{
int i;
i = 0;
while (i < 3)
{
if (ft_atof(token_vector[i]) < -FLT_MAX || ft_atof(token_vector[i]) > FLT_MAX)
return (0);
i++;
}
return (1);
}
t_vec3 parse_vector(const char *token, t_scene scene)
{
const char **token_vector = ft_split(token, ',');
t_vec3 vector;
if (!check_tokens(token_vector, 3) || !range_is_ok(token_vector))
{
ft_free_array(token_vector);
ft_free_array(scene.token_if_exit);
ft_putendl_fd("error", 2);
free(scene.line_if_exit);
close(scene.fd_if_exit);
exit(1);
}
vector.x = ft_atof(token_vector[0]);
vector.y = ft_atof(token_vector[1]);
vector.z = ft_atof(token_vector[2]);
ft_free_array(tokens_vector);
return (vector);
}
t_vec3 parse_vector_normalize(const char *token, t_scene scene)
{
t_vec3 vector;
vector = parse_vector(token, scene);
vector = vec3_normalize(vector);
return (vector);
}
+10 -1
View File
@@ -71,6 +71,7 @@
!_TAG_ROLE_DESCRIPTION!C++!partition imported /imported with "imported ..."/ !_TAG_ROLE_DESCRIPTION!C++!partition imported /imported with "imported ..."/
HEIGHT miniRT.h /^# define HEIGHT /;" d HEIGHT miniRT.h /^# define HEIGHT /;" d
MAX_AMBIENT miniRT.h /^# define MAX_AMBIENT /;" d MAX_AMBIENT miniRT.h /^# define MAX_AMBIENT /;" d
MAX_CAMERA miniRT.h /^# define MAX_CAMERA /;" d
MAX_CYLINDERS miniRT.h /^# define MAX_CYLINDERS /;" d MAX_CYLINDERS miniRT.h /^# define MAX_CYLINDERS /;" d
MAX_LIGHTS miniRT.h /^# define MAX_LIGHTS /;" d MAX_LIGHTS miniRT.h /^# define MAX_LIGHTS /;" d
MAX_PLANES miniRT.h /^# define MAX_PLANES /;" d MAX_PLANES miniRT.h /^# define MAX_PLANES /;" d
@@ -89,6 +90,7 @@ camDir miniRT.h /^ t_vec3 camDir;$/;" m struct:s_camera typeref:typename:t_vec3
camDir raytracer_formatted.c /^t_vec3 camDir = {0.0f, 0.0f, -1.0f};$/;" v typeref:typename:t_vec3 camDir raytracer_formatted.c /^t_vec3 camDir = {0.0f, 0.0f, -1.0f};$/;" v typeref:typename:t_vec3
camPos miniRT.h /^ t_vec3 camPos;$/;" m struct:s_camera typeref:typename:t_vec3 camPos miniRT.h /^ t_vec3 camPos;$/;" m struct:s_camera typeref:typename:t_vec3
camPos raytracer_formatted.c /^t_vec3 camPos = {0.0f, 0.0f, 0.0f};$/;" v typeref:typename:t_vec3 camPos raytracer_formatted.c /^t_vec3 camPos = {0.0f, 0.0f, 0.0f};$/;" v typeref:typename:t_vec3
camera miniRT.h /^ t_camera camera;$/;" m struct:s_scene typeref:typename:t_camera
center miniRT.h /^ t_vec3 center;$/;" m struct:s_cylinder typeref:typename:t_vec3 center miniRT.h /^ t_vec3 center;$/;" m struct:s_cylinder typeref:typename:t_vec3
center miniRT.h /^ t_vec3 center;$/;" m struct:s_sphere typeref:typename:t_vec3 center miniRT.h /^ t_vec3 center;$/;" m struct:s_sphere typeref:typename:t_vec3
check_if_max check.c /^inline void check_if_max(t_scene scene, const int to_test, const int max)$/;" f typeref:typename:void check_if_max check.c /^inline void check_if_max(t_scene scene, const int to_test, const int max)$/;" f typeref:typename:void
@@ -121,10 +123,16 @@ numLights miniRT.h /^ int numLights;$/;" m struct:s_scene typeref:typename:int
numPlanes miniRT.h /^ int numPlanes;$/;" m struct:s_scene typeref:typename:int numPlanes miniRT.h /^ int numPlanes;$/;" m struct:s_scene typeref:typename:int
numSpheres miniRT.h /^ int numSpheres;$/;" m struct:s_scene typeref:typename:int numSpheres miniRT.h /^ int numSpheres;$/;" m struct:s_scene typeref:typename:int
origin miniRT.h /^ t_vec3 origin;$/;" m struct:s_ray typeref:typename:t_vec3 origin miniRT.h /^ t_vec3 origin;$/;" m struct:s_ray typeref:typename:t_vec3
parse_color parsing_color.c /^t_color parse_color(const char *token, t_scene scene)$/;" f typeref:typename:t_color parse_color parsing_color.c /^t_vec3 parse_color(const char *token, t_scene scene)$/;" f typeref:typename:t_vec3
parse_vector parsing_vector.c /^t_vec3 parse_vector(const char *token, t_scene scene)$/;" f typeref:typename:t_vec3
parse_vector_normalize parsing_vector.c /^t_vec3 parse_vector_normalize(const char *token, t_scene scene)$/;" f typeref:typename:t_vec3
parsing_ambiant parsing_ambiant.c /^t_scene parsing_ambiant(const char *line, t_scene scene)$/;" f typeref:typename:t_scene parsing_ambiant parsing_ambiant.c /^t_scene parsing_ambiant(const char *line, t_scene scene)$/;" f typeref:typename:t_scene
parsing_camera parsing_camera.c /^t_scene parsing_camera(const char *line, t_scene scene)$/;" f typeref:typename:t_scene parsing_camera parsing_camera.c /^t_scene parsing_camera(const char *line, t_scene scene)$/;" f typeref:typename:t_scene
parsing_cylinder parsing_cylinder.c /^t_scene parsing_cylinder(const char *line, t_scene scene)$/;" f typeref:typename:t_scene
parsing_light parsing_light.c /^t_scene parsing_light(const char *line, t_scene scene)$/;" f typeref:typename:t_scene
parsing_line config.c /^static inline t_scene parsing_line(const char *line, t_scene scene)$/;" f typeref:typename:t_scene file: parsing_line config.c /^static inline t_scene parsing_line(const char *line, t_scene scene)$/;" f typeref:typename:t_scene file:
parsing_plane parsing_plane.c /^t_scene parsing_plane(const char *line, t_scene scene)$/;" f typeref:typename:t_scene
parsing_sphere parsing_sphere.c /^t_scene parsing_sphere(const char *line, t_scene scene)$/;" f typeref:typename:t_scene
pitch miniRT.h /^ float pitch; \/\/ vue de haut en bas en radians$/;" m struct:s_camera typeref:typename:float pitch miniRT.h /^ float pitch; \/\/ vue de haut en bas en radians$/;" m struct:s_camera typeref:typename:float
pitch raytracer_formatted.c /^float pitch = 0.0f; \/\/ vue de haut en bas en radians$/;" v typeref:typename:float pitch raytracer_formatted.c /^float pitch = 0.0f; \/\/ vue de haut en bas en radians$/;" v typeref:typename:float
planes miniRT.h /^ t_plane planes[MAX_PLANES];$/;" m struct:s_scene typeref:typename:t_plane[] planes miniRT.h /^ t_plane planes[MAX_PLANES];$/;" m struct:s_scene typeref:typename:t_plane[]
@@ -133,6 +141,7 @@ pos miniRT.h /^ t_vec3 pos;$/;" m struct:s_light typeref:typename:t_vec3
radius miniRT.h /^ float radius;$/;" m struct:s_sphere typeref:typename:float radius miniRT.h /^ float radius;$/;" m struct:s_sphere typeref:typename:float
radius miniRT.h /^ float radius; \/\/ Demi-diamètre$/;" m struct:s_cylinder typeref:typename:float radius miniRT.h /^ float radius; \/\/ Demi-diamètre$/;" m struct:s_cylinder typeref:typename:float
range_is_ok parsing_color.c /^static inline int range_is_ok(char **token_color)$/;" f typeref:typename:int file: range_is_ok parsing_color.c /^static inline int range_is_ok(char **token_color)$/;" f typeref:typename:int file:
range_is_ok parsing_vector.c /^static inline int range_is_ok(char **token_vector)$/;" f typeref:typename:int file:
s_ambient miniRT.h /^typedef struct s_ambient$/;" s s_ambient miniRT.h /^typedef struct s_ambient$/;" s
s_camera miniRT.h /^typedef struct s_camera$/;" s s_camera miniRT.h /^typedef struct s_camera$/;" s
s_cylinder miniRT.h /^typedef struct s_cylinder$/;" s s_cylinder miniRT.h /^typedef struct s_cylinder$/;" s