From 1ecb24da74f01549f6957666a204792405349e47 Mon Sep 17 00:00:00 2001 From: H3XploR Date: Fri, 14 Feb 2025 18:56:25 +0100 Subject: [PATCH] parsing ambient --- check.c | 29 ++++++++++++++++++++++++++++ config.c | 45 ++++++++++++++++++++++++++++++++++++------- miniRT.h | 20 +++++++++++++++++-- parsing_ambiant.c | 33 +++++++++++++++++++++++++++++++ raytracer_formatted.c | 4 ++-- tags | 10 +++++++++- 6 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 check.c create mode 100644 parsing_ambiant.c diff --git a/check.c b/check.c new file mode 100644 index 0000000..8eaf01d --- /dev/null +++ b/check.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/14 18:28:42 by yantoine #+# #+# */ +/* Updated: 2025/02/14 18:51:17 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +inline int check_tokens(char **tokens, int expected) +{ + return (tokens && ft_arraylen(tokens) == expected); +} + +inline void check_if_max(t_scene scene, const int lower_than) +{ + if (scene.numAmbient >= lower_than) + { + printf("error:\n"); + close(scene.fd_if_exit); + free(scene->line_if_exit); + exit(1); + } +} diff --git a/config.c b/config.c index 434d820..aaf36bd 100644 --- a/config.c +++ b/config.c @@ -6,24 +6,55 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/14 01:41:17 by yantoine #+# #+# */ -/* Updated: 2025/02/14 01:50:00 by yantoine ### ########.fr */ +/* Updated: 2025/02/14 18:53:17 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" -// ----- Parsing du fichier de configuration ----- -int load_config(const char *filename) +static inline t_scene parsing_line(const char *line, t_scene scene) { - int fd; - char line[256]; - float ratio; + scene->line_if_exit = line; + if (line[0] == '#') + return ; + else if (line[0] == 'A') + parsing_ambiant(line, scene); + else if (line[0] == 'C') + parsing_camera(line, scene); + else if (line[0] == 'L') + parsing_light(line, scene); + else if (ft_strncmp(line, "sp", 2) == 0) + parsing_sphere(line, scene); + else if (ft_strncmp(line, "pl", 2) == 0) + parsing_plane(line, scene); + else if (ft_strncmp(line, "cy", 2) == 0) + parsing_cylindre(line, scene); + else + printf("Erreur : ligne non reconnue\n"); +} +// ----- Parsing du fichier de configuration ----- +t_scene load_config(const char *filename) +{ + int fd; + char line[256]; + const char *line; + t_scene scene; + + scene = create_scene(void); fd = open(filename, "r"); if (!fd) { printf("Erreur : impossible d'ouvrir %s\n", filename); return ; } - return (fd); + scene.fd_if_exit = fd; + while (1) + { + line = (const char *)get_next_line(fd); + if (!line) + break ; + free(line); + parsing_line(line, scene); + } } diff --git a/miniRT.h b/miniRT.h index 5eb746f..8d0940f 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 01:50:11 by yantoine ### ########.fr */ +/* Updated: 2025/02/14 18:49:30 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ # define MAX_PLANES 128 # define MAX_CYLINDERS 128 # define MAX_LIGHTS 16 +# define MAX_AMBIENT 1 // ----- Structures typedef struct s_vec3 @@ -97,10 +98,13 @@ typedef struct s_scene t_plane planes[MAX_PLANES]; t_cylinder cylinders[MAX_CYLINDERS]; t_light lights[MAX_LIGHTS]; + t_ambient ambient; int numSpheres; int numPlanes; int numCylinders; int numLights; + int numAmbient; + const char *line; } t_scene; // Calcul de vecteur @@ -112,6 +116,18 @@ t_vec3 vec3_cross(t_vec3 a, t_vec3 b); float vec3_length(t_vec3 a); t_vec3 vec3_normalize(t_vec3 a); t_vec3 vec3_mul(t_vec3 a, t_vec3 b); + +// Config de scene t_scene create_scene(void); -int load_config(const char *filename); +t_scene load_config(const char *filename); +t_scene parsing_ambiant(const char *line, t_scene scene); +t_scene parsing_camera(const char *line, t_scene scene); +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); + +// Check +inline int check_tokens(char **tokens, int expected) +inline void check_if_max(t_scene scene, const int lower_than); #endif diff --git a/parsing_ambiant.c b/parsing_ambiant.c new file mode 100644 index 0000000..2d3d5f4 --- /dev/null +++ b/parsing_ambiant.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_ambiant.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/14 18:04:39 by yantoine #+# #+# */ +/* Updated: 2025/02/14 18:56:00 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + + +t_scene parsing_ambiant(const char *line, t_scene scene) +{ + const int result_if_max = check_if_max(scene, MAX_AMBIENT); + const char **tokens = ft_split(line, ' '); + const int result_check_tokens = check_tokens(tokens, 3); + + if (!result_check_tokens) + { + ft_free_array(tokens); + ft_putendl_fd("error", 2); + close(scene.fd); + exit(1); + } + scene.ambient.ambient_ratio = ft_atof(tokens[1]); + scene.ambient.ambient_color = parse_color(tokens[2], scene); + ft_free_array(tokens); + return (scene); +} diff --git a/raytracer_formatted.c b/raytracer_formatted.c index be2e096..2849346 100644 --- a/raytracer_formatted.c +++ b/raytracer_formatted.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 19:56:17 by yantoine #+# #+# */ -/* Updated: 2025/02/14 01:50:12 by yantoine ### ########.fr */ +/* Updated: 2025/02/14 16:50:44 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ float pitch = 0.0f; // vue de haut en bas en radians // ----- Parsing du fichier de configuration ----- void load_config(const char *filename) { - +//---- Jlaisse pour contextualiser ----- while (fgets(line, sizeof(line), fp)) { // Ignore les lignes vides ou commençant par # diff --git a/tags b/tags index 7ebd885..8647b94 100644 --- a/tags +++ b/tags @@ -70,12 +70,14 @@ !_TAG_ROLE_DESCRIPTION!C++!module partOwner /used for specifying a partition/ !_TAG_ROLE_DESCRIPTION!C++!partition imported /imported with "imported ..."/ HEIGHT miniRT.h /^# define HEIGHT /;" d +MAX_AMBIENT miniRT.h /^# define MAX_AMBIENT /;" 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 MAX_SPHERES miniRT.h /^# define MAX_SPHERES /;" d MINIRT_H miniRT.h /^# define MINIRT_H$/;" d WIDTH miniRT.h /^# define WIDTH /;" d +ambient miniRT.h /^ t_ambient ambient;$/;" m struct:s_scene typeref:typename:t_ambient ambient_color miniRT.h /^ t_vec3 ambient_color;$/;" m struct:s_ambient typeref:typename:t_vec3 ambient_color raytracer_formatted.c /^t_vec3 ambient_color = {0.1f, 0.1f, 0.1f};$/;" v typeref:typename:t_vec3 ambient_ratio miniRT.h /^ float ambient_ratio;$/;" m struct:s_ambient typeref:typename:float @@ -89,6 +91,8 @@ 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 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 lower_than)$/;" f typeref:typename:void +check_tokens check.c /^inline int check_tokens(char **tokens, int expected)$/;" f typeref:typename:int color miniRT.h /^ t_vec3 color;$/;" m struct:s_cylinder typeref:typename:t_vec3 color miniRT.h /^ t_vec3 color;$/;" m struct:s_light typeref:typename:t_vec3 color miniRT.h /^ t_vec3 color;$/;" m struct:s_plane typeref:typename:t_vec3 @@ -104,15 +108,19 @@ intersectPlane raytracer_formatted.c /^float intersectPlane(Ray ray, Plane p, t_ intersectSphere raytracer_formatted.c /^float intersectSphere(Ray ray, Sphere s, t_vec3 *hitNormal)$/;" f typeref:typename:float isInShadow raytracer_formatted.c /^bool isInShadow(t_vec3 hitPoint, t_vec3 lightPos)$/;" f typeref:typename:bool lights miniRT.h /^ t_light lights[MAX_LIGHTS];$/;" m struct:s_scene typeref:typename:t_light[] -load_config config.c /^int load_config(const char *filename)$/;" f typeref:typename:int +line miniRT.h /^ const char *line;$/;" m struct:s_scene typeref:typename:const char * +load_config config.c /^t_scene load_config(const char *filename)$/;" f typeref:typename:t_scene load_config raytracer_formatted.c /^void load_config(const char *filename)$/;" f typeref:typename:void main raytracer_formatted.c /^int main(int argc, char *argv[])$/;" f typeref:typename:int normal miniRT.h /^ t_vec3 normal;$/;" m struct:s_plane typeref:typename:t_vec3 +numAmbient miniRT.h /^ int numAmbient;$/;" m struct:s_scene typeref:typename:int numCylinders miniRT.h /^ int numCylinders;$/;" m struct:s_scene typeref:typename:int 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 +parsing_ambiant parsing_ambiant.c /^t_scene parsing_ambiant(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: 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[]