parsing ambient

This commit is contained in:
H3XploR
2025-02-14 18:56:25 +01:00
parent 16f49eb686
commit 1ecb24da74
6 changed files with 129 additions and 12 deletions
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
}
+38 -7
View File
@@ -6,24 +6,55 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
}
+18 -2
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
+33
View File
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_ambiant.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
+2 -2
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 #
+9 -1
View File
@@ -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[]