From 0bd320f2042beda9368a638b2ea42899197874aa Mon Sep 17 00:00:00 2001 From: H3XploR Date: Sat, 15 Feb 2025 20:02:43 +0100 Subject: [PATCH] s'occuper des intersections --- config.c | 3 ++- miniRT.h | 12 +++++++--- parsing_camera.c | 12 +++++++++- parsing_color.c | 15 ++++++------ parsing_cylinder.c | 27 ++++++++++++++++++++++ parsing_light.c | 25 ++++++++++++++++++++ parsing_plane.c | 25 ++++++++++++++++++++ parsing_sphere.c | 25 ++++++++++++++++++++ parsing_vector.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++ tags | 11 ++++++++- 10 files changed, 199 insertions(+), 13 deletions(-) create mode 100644 parsing_cylinder.c create mode 100644 parsing_light.c create mode 100644 parsing_plane.c create mode 100644 parsing_sphere.c create mode 100644 parsing_vector.c diff --git a/config.c b/config.c index aaf36bd..06fe65b 100644 --- a/config.c +++ b/config.c @@ -6,7 +6,7 @@ /* 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); parsing_line(line, scene); } + return (scene); } diff --git a/miniRT.h b/miniRT.h index 07e6f6e..02d7aad 100644 --- a/miniRT.h +++ b/miniRT.h @@ -6,7 +6,7 @@ /* 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 # include # include "libft.h" +# include "float.h" // ----- Taille ecran ---- # define WIDTH 320 @@ -31,8 +32,9 @@ # define MAX_CYLINDERS 128 # define MAX_LIGHTS 16 # define MAX_AMBIENT 1 +# define MAX_CAMERA 1 -// ----- Structures +// ----- Espace 3d typedef struct s_vec3 { float x; @@ -100,6 +102,7 @@ typedef struct s_scene t_cylinder cylinders[MAX_CYLINDERS]; t_light lights[MAX_LIGHTS]; t_ambient ambient; + t_camera camera; int numSpheres; int numPlanes; 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_plane(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 inline char **get_tokens_secure(t_scene scene, const int numObject, const int numObjectMax, const int supposed_nb_token); diff --git a/parsing_camera.c b/parsing_camera.c index 54b3967..f8d4c0c 100644 --- a/parsing_camera.c +++ b/parsing_camera.c @@ -6,7 +6,7 @@ /* 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) { + 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); } diff --git a/parsing_color.c b/parsing_color.c index db10107..850218e 100644 --- a/parsing_color.c +++ b/parsing_color.c @@ -6,7 +6,7 @@ /* 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; 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); i++; } 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, ','); - t_color color; + t_vec3 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); exit(1); } - color.r = ft_atoi(token_color[0]) / 255; - color.g = ft_atoi(token_color[1]) / 255; - color.b = ft_atoi(token_color[2]) / 255; + color.x = ft_atof(token_color[0]) / 255; + color.y = ft_atof(token_color[1]) / 255; + color.z = ft_atof(token_color[2]) / 255; + ft_free_array(tokens_color); return (color); } diff --git a/parsing_cylinder.c b/parsing_cylinder.c new file mode 100644 index 0000000..adfed24 --- /dev/null +++ b/parsing_cylinder.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_cylinder.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/parsing_light.c b/parsing_light.c new file mode 100644 index 0000000..f175fff --- /dev/null +++ b/parsing_light.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_light.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/parsing_plane.c b/parsing_plane.c new file mode 100644 index 0000000..9c8451a --- /dev/null +++ b/parsing_plane.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_plane.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/parsing_sphere.c b/parsing_sphere.c new file mode 100644 index 0000000..c5bcfa0 --- /dev/null +++ b/parsing_sphere.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_sphere.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/parsing_vector.c b/parsing_vector.c new file mode 100644 index 0000000..9ed280f --- /dev/null +++ b/parsing_vector.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_vector.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/tags b/tags index 798f879..3cbaa3a 100644 --- a/tags +++ b/tags @@ -71,6 +71,7 @@ !_TAG_ROLE_DESCRIPTION!C++!partition imported /imported with "imported ..."/ HEIGHT miniRT.h /^# define HEIGHT /;" 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_LIGHTS miniRT.h /^# define MAX_LIGHTS /;" 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 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 +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_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 @@ -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 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 -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_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_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 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[] @@ -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; \/\/ 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_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_camera miniRT.h /^typedef struct s_camera$/;" s s_cylinder miniRT.h /^typedef struct s_cylinder$/;" s