diff --git a/config.c b/config.c index de02ecc..ff3132b 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/17 23:28:25 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:44:41 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,7 +57,5 @@ t_scene load_config(const char *filename) scene = parsing_line(line, scene); free(line); } - printf("AFFICHE CONFIG\n"); - print_scene(scene); return (scene); } diff --git a/miniRT b/miniRT deleted file mode 100755 index efc7ba5..0000000 Binary files a/miniRT and /dev/null differ diff --git a/miniRT.h b/miniRT.h index ba002c7..ee70f85 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/18 17:50:11 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:43:16 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -109,6 +109,7 @@ typedef struct s_scene t_light lights[MAX_LIGHTS]; t_ambient ambient; t_camera camera; + t_ray ray; int numSpheres; int numPlanes; int numCylinders; @@ -217,16 +218,24 @@ int check_tokens(char **tokens, int expected); void check_if_max(t_scene scene, const int to_test, const int max); // Intersection -float intersectCylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal); -float intersectPlane(t_ray ray, t_plane p, t_vec3 *hitNormal); -float intersectSphere(t_ray ray, t_sphere s, t_vec3 *hitNormal); +float intersect_cylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal); +float intersect_plane(t_ray ray, t_plane p, t_vec3 *hitNormal); +float intersect_sphere(t_ray ray, t_sphere s, t_vec3 *hitNormal); bool is_in_shadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene); -bool intersect_objects(t_ray ray, float *tMin, t_vec3 *hitNormal, +bool intersect_objects(float *tMin, t_vec3 *hitNormal, t_vec3 *objColor, t_scene scene); t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor, t_scene scene); t_vec3 trace(t_ray ray, t_scene scene); +// Interesction utils cylindre +int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc); +void compute_side_intersection(t_cylinder cy, t_calc *calc); +void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc); +float select_final_intersection(t_calc *calc); +void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc, + t_vec3 *hitNormal); + // Peripherique int key_press(int keycode, t_app *app); int key_release(int keycode, t_app *app); diff --git a/parsing_cylinder_utils.c b/parsing_cylinder_utils.c index a8f3a45..aec4311 100644 --- a/parsing_cylinder_utils.c +++ b/parsing_cylinder_utils.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 18:54:45 by yantoine #+# #+# */ -/* Updated: 2025/02/18 17:38:56 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:43:49 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ // Initialise les variables de calcul et les // coefficients du polynôme d'intersection -static int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc) +int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc) { calc->d = ray.dir; calc->oc = vec3_sub(ray.origin, cy.center); @@ -37,7 +37,7 @@ static int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc) // Calcule l'intersection sur la surface //latérale du cylindre -static void compute_side_intersection(t_cylinder cy, t_calc *calc) +void compute_side_intersection(t_cylinder cy, t_calc *calc) { calc->t_side = -1; if (calc->t0 > 1e-3f) @@ -56,7 +56,7 @@ static void compute_side_intersection(t_cylinder cy, t_calc *calc) // Calcule l'intersection sur les // capuchons supérieur et inférieur -static void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc) +void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc) { calc->t_cap = -1; if (fabs(calc->d_dot_v) > 1e-6f) @@ -87,7 +87,7 @@ static void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc) // Sélectionne l'intersection la plus //proche entre la surface latérale et les capuchons -static float select_final_intersection(t_calc *calc) +float select_final_intersection(t_calc *calc) { if (calc->t_side > 1e-3f && calc->t_cap > 1e-3f) { @@ -106,7 +106,7 @@ static float select_final_intersection(t_calc *calc) } // Calcule la normale au point d'intersection -static void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc, +void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc, t_vec3 *hitNormal) { calc->hitPoint = vec3_add(ray.origin, vec3_scale(calc->d, calc->t_final)); @@ -125,18 +125,3 @@ static void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc, *hitNormal = vec3_scale(calc->v, -1); } } - -// Fonction principale d'intersection du cylindre -float intersectCylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal) -{ - t_calc calc; - - if (init_intersection(ray, cy, &calc) < 0) - return (-1); - compute_side_intersection(cy, &calc); - compute_cap_intersection(ray, cy, &calc); - if (select_final_intersection(&calc) < 0) - return (-1); - compute_hit_normal(ray, cy, &calc, hitNormal); - return (calc.t_final); -} diff --git a/parsing_cylinder_utils2.c b/parsing_cylinder_utils2.c new file mode 100644 index 0000000..31935e9 --- /dev/null +++ b/parsing_cylinder_utils2.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_cylinder_utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/18 20:41:15 by yantoine #+# #+# */ +/* Updated: 2025/02/18 20:41:16 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +// Fonction principale d'intersection du cylindre +float intersect_cylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal) +{ + t_calc calc; + + if (init_intersection(ray, cy, &calc) < 0) + return (-1); + compute_side_intersection(cy, &calc); + compute_cap_intersection(ray, cy, &calc); + if (select_final_intersection(&calc) < 0) + return (-1); + compute_hit_normal(ray, cy, &calc, hitNormal); + return (calc.t_final); +} diff --git a/parsing_plane.c b/parsing_plane.c index 7ded4ef..9be62ad 100644 --- a/parsing_plane.c +++ b/parsing_plane.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:49:41 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:30:47 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:34:02 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ t_scene parsing_plane(t_scene scene) return (scene); } -float intersectPlane(t_ray ray, t_plane p, t_vec3 *hitNormal) +float intersect_plane(t_ray ray, t_plane p, t_vec3 *hitNormal) { float denom; float t; diff --git a/parsing_sphere.c b/parsing_sphere.c index 393495f..19bacf0 100644 --- a/parsing_sphere.c +++ b/parsing_sphere.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:46:16 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:31:36 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:39:35 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ t_scene parsing_sphere(t_scene scene) return (scene); } -float intersectSphere(t_ray ray, t_sphere s, t_vec3 *hitNormal) +float intersect_sphere(t_ray ray, t_sphere s, t_vec3 *hitNormal) { t_calc calc; @@ -40,7 +40,10 @@ float intersectSphere(t_ray ray, t_sphere s, t_vec3 *hitNormal) calc.sqrtDisc = sqrtf(calc.disc); calc.t0 = (-calc.b - calc.sqrtDisc) / (2 * calc.a); calc.t1 = (-calc.b + calc.sqrtDisc) / (2 * calc.a); - calc.t = (calc.t0 > 1e-3f) ? calc.t0 : calc.t1; + if (calc.t0 > 1e-3f) + calc.t = calc.t0; + else + calc.t = calc.t1; if (calc.t < 1e-3f) return (-1); calc.hitPoint = vec3_add(ray.origin, vec3_scale(ray.dir, calc.t)); diff --git a/print.c b/print.c index c92eedd..de4bd49 100644 --- a/print.c +++ b/print.c @@ -6,12 +6,12 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 23:16:21 by yantoine #+# #+# */ -/* Updated: 2025/02/18 17:03:05 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:44:33 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" - +/* void print_light(t_light light) { printf("Position : "); @@ -92,3 +92,4 @@ void print_scene(t_scene scene) i++; } } +*/ diff --git a/print_next.c b/print_next.c index 60a5b33..b6d83b4 100644 --- a/print_next.c +++ b/print_next.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/18 17:02:32 by yantoine #+# #+# */ -/* Updated: 2025/02/18 17:02:42 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:31:27 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,4 +59,3 @@ void print_cylinder(t_cylinder cyl) print_vec3(cyl.color); printf("\n"); } - diff --git a/shadows.c b/shadows.c index ee61115..86922d1 100644 --- a/shadows.c +++ b/shadows.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 18:58:42 by yantoine #+# #+# */ -/* Updated: 2025/02/18 17:48:22 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:32:30 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ static bool check_shadow_sphere(const t_ray shadow_ray, float max_t, i = 0; while (i < scene.numSpheres) { - t = intersectSphere(shadow_ray, scene.spheres[i], &dummy); + t = intersect_sphere(shadow_ray, scene.spheres[i], &dummy); if (t > epsilon && t < max_t) return (true); i++; @@ -44,7 +44,7 @@ static bool check_shadow_plane(const t_ray shadow_ray, float max_t, i = 0; while (i < scene.numPlanes) { - t = intersectPlane(shadow_ray, scene.planes[i], &dummy); + t = intersect_plane(shadow_ray, scene.planes[i], &dummy); if (t > epsilon && t < max_t) return (true); i++; @@ -64,7 +64,7 @@ static bool check_shadow_cylinder(const t_ray shadow_ray, float max_t, i = 0; while (i < scene.numCylinders) { - t = intersectCylinder(shadow_ray, scene.cylinders[i], &dummy); + t = intersect_cylinder(shadow_ray, scene.cylinders[i], &dummy); if (t > epsilon && t < max_t) return (true); i++; diff --git a/tags b/tags index 99e747b..ae2698d 100644 --- a/tags +++ b/tags @@ -314,9 +314,9 @@ color miniRT.h /^ t_vec3 color;$/;" m struct:s_sphere typeref:typename:t_vec3 color minilibx-linux/mlx_int.h /^ int color;$/;" m struct:s_col_name typeref:typename:int color_map_1 minilibx-linux/test/main.c /^int color_map_1(void *win,int w,int h)$/;" f typeref:typename:int color_map_2 minilibx-linux/test/main.c /^int color_map_2(unsigned char *data,int bpp,int sl,int w,int h,int endian, int type)$/;" f typeref:typename:int -compute_cap_intersection parsing_cylinder_utils.c /^static void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc)$/;" f typeref:typename:void file: -compute_hit_normal parsing_cylinder_utils.c /^static void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc,$/;" f typeref:typename:void file: -compute_side_intersection parsing_cylinder_utils.c /^static void compute_side_intersection(t_cylinder cy, t_calc *calc)$/;" f typeref:typename:void file: +compute_cap_intersection parsing_cylinder_utils.c /^void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc)$/;" f typeref:typename:void +compute_hit_normal parsing_cylinder_utils.c /^void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc,$/;" f typeref:typename:void +compute_side_intersection parsing_cylinder_utils.c /^void compute_side_intersection(t_cylinder cy, t_calc *calc)$/;" f typeref:typename:void count_word libft/ft_split.c /^static size_t count_word(char *str, char c)$/;" f typeref:typename:size_t file: counter libft/libft.h /^ size_t counter;$/;" m struct:s_info typeref:typename:size_t cp miniRT.h /^ t_vec3 cp;$/;" m struct:s_calc typeref:typename:t_vec3 @@ -432,12 +432,12 @@ im4 minilibx-linux/test/main.c /^void *im4;$/;" v typeref:typename:void * image minilibx-linux/mlx_int.h /^ XImage *image;$/;" m struct:s_img typeref:typename:XImage * img miniRT.h /^ void *img;$/;" m struct:s_app typeref:typename:void * init_app_config main.c /^static int init_app_config(t_app *app, int argc, char **argv)$/;" f typeref:typename:int file: -init_intersection parsing_cylinder_utils.c /^static int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc)$/;" f typeref:typename:int file: +init_intersection parsing_cylinder_utils.c /^int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc)$/;" f typeref:typename:int init_mlx_and_image main.c /^static int init_mlx_and_image(t_app *app)$/;" f typeref:typename:int file: -interesct_objects trace.c /^bool interesct_objects(t_ray ray, float *tMin, t_vec3 *hitNormal,$/;" f typeref:typename:bool -intersectCylinder parsing_cylinder_utils.c /^float intersectCylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal)$/;" f typeref:typename:float -intersectPlane parsing_plane.c /^float intersectPlane(t_ray ray, t_plane p, t_vec3 *hitNormal)$/;" f typeref:typename:float -intersectSphere parsing_sphere.c /^float intersectSphere(t_ray ray, t_sphere s, t_vec3 *hitNormal)$/;" f typeref:typename:float +interesct_objects trace.c /^bool interesct_objects(float *tMin, t_vec3 *hitNormal,$/;" f typeref:typename:bool +intersect_cylinder parsing_cylinder_utils2.c /^float intersect_cylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal)$/;" f typeref:typename:float +intersect_plane parsing_plane.c /^float intersect_plane(t_ray ray, t_plane p, t_vec3 *hitNormal)$/;" f typeref:typename:float +intersect_sphere parsing_sphere.c /^float intersect_sphere(t_ray ray, t_sphere s, t_vec3 *hitNormal)$/;" f typeref:typename:float is_in_shadow shadows.c /^bool is_in_shadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene)$/;" f typeref:typename:bool key_a miniRT.h /^ int key_a;$/;" m struct:s_app typeref:typename:int key_d miniRT.h /^ int key_d;$/;" m struct:s_app typeref:typename:int @@ -589,13 +589,9 @@ pixels miniRT.h /^ int *pixels;$/;" m struct:s_app typeref:typename:int * planes miniRT.h /^ t_plane planes[MAX_PLANES];$/;" m struct:s_scene typeref:typename:t_plane[] point miniRT.h /^ t_vec3 point;$/;" m struct:s_plane typeref:typename:t_vec3 pos miniRT.h /^ t_vec3 pos;$/;" m struct:s_light typeref:typename:t_vec3 -print_ambient print.c /^void print_ambient(t_ambient amb)$/;" f typeref:typename:void -print_camera print.c /^void print_camera(t_camera cam)$/;" f typeref:typename:void print_cylinder print_next.c /^void print_cylinder(t_cylinder cyl)$/;" f typeref:typename:void -print_light print.c /^void print_light(t_light light)$/;" f typeref:typename:void print_plane print_next.c /^void print_plane(t_plane plane)$/;" f typeref:typename:void print_ray print_next.c /^void print_ray(t_ray ray)$/;" f typeref:typename:void -print_scene print.c /^void print_scene(t_scene scene)$/;" f typeref:typename:void print_sphere print_next.c /^void print_sphere(t_sphere sphere)$/;" f typeref:typename:void print_vec3 print_next.c /^void print_vec3(t_vec3 vec)$/;" f typeref:typename:void printf_fd libft/printf_fd.c /^int printf_fd(int fd, const char *str, ...)$/;" f typeref:typename:int @@ -610,6 +606,7 @@ range_is_ok parsing_color.c /^static inline int range_is_ok(char **token_color)$ range_is_ok parsing_vector.c /^static inline int range_is_ok(char **token_vector)$/;" f typeref:typename:int file: ratio miniRT.h /^ float ratio;$/;" m struct:s_ambient typeref:typename:float ray miniRT.h /^ t_ray ray;$/;" m struct:s_calc typeref:typename:t_ray +ray miniRT.h /^ t_ray ray;$/;" m struct:s_scene typeref:typename:t_ray ray_dir miniRT.h /^ t_vec3 ray_dir;$/;" m struct:s_calc typeref:typename:t_vec3 re libft/Makefile /^re: fclean all$/;" t re makefile /^re: fclean all$/;" t @@ -644,7 +641,7 @@ scene miniRT.h /^ t_scene scene;$/;" m struct:s_app typeref:typename:t_scene screen minilibx-linux/mlx_int.h /^ int screen;$/;" m struct:s_xvar typeref:typename:int screen_x miniRT.h /^ float screen_x;$/;" m struct:s_calc typeref:typename:float screen_y miniRT.h /^ float screen_y;$/;" m struct:s_calc typeref:typename:float -select_final_intersection parsing_cylinder_utils.c /^static float select_final_intersection(t_calc *calc)$/;" f typeref:typename:float file: +select_final_intersection parsing_cylinder_utils.c /^float select_final_intersection(t_calc *calc)$/;" f typeref:typename:float setup_hooks main.c /^static void setup_hooks(t_app *app)$/;" f typeref:typename:void file: shm minilibx-linux/mlx_int.h /^ XShmSegmentInfo shm;$/;" m struct:s_img typeref:typename:XShmSegmentInfo shm_att_pb minilibx-linux/mlx_new_image.c /^int shm_att_pb(Display *d,XErrorEvent *ev)$/;" f typeref:typename:int diff --git a/trace.c b/trace.c index 8cea09c..bfbd2d9 100644 --- a/trace.c +++ b/trace.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 19:07:07 by yantoine #+# #+# */ -/* Updated: 2025/02/18 18:04:11 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 20:34:28 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ // Renvoie true si le rayon intersecte un objet, et met à jour tMin, //hitNormal et objColor -bool interesct_objects(t_ray ray, float *tMin, t_vec3 *hitNormal, +bool interesct_objects(float *tMin, t_vec3 *hitNormal, t_vec3 *objColor, t_scene scene) { bool hit; @@ -26,7 +26,7 @@ bool interesct_objects(t_ray ray, float *tMin, t_vec3 *hitNormal, i = 0; while (i < scene.numSpheres) { - t = intersectSphere(ray, scene.spheres[i], &n); + t = intersect_sphere(scene.ray, scene.spheres[i], &n); if (t > 1e-3f && t < *tMin) { *tMin = t; @@ -39,7 +39,7 @@ bool interesct_objects(t_ray ray, float *tMin, t_vec3 *hitNormal, i = 0; while (i < scene.numPlanes) { - t = intersectPlane(ray, scene.planes[i], &n); + t = intersect_plane(scene.ray, scene.planes[i], &n); if (t > 1e-3f && t < *tMin) { *tMin = t; @@ -52,7 +52,7 @@ bool interesct_objects(t_ray ray, float *tMin, t_vec3 *hitNormal, i = 0; while (i < scene.numCylinders) { - t = intersectCylinder(ray, scene.cylinders[i], &n); + t = intersect_cylinder(scene.ray, scene.cylinders[i], &n); if (t > 1e-3f && t < *tMin) { *tMin = t; @@ -117,9 +117,10 @@ t_vec3 trace(t_ray ray, t_scene scene) tMin = 1e9; hitNormal = (t_vec3){0, 0, 0}; objColor = (t_vec3){0, 0, 0}; - if (interesct_objects(ray, &tMin, &hitNormal, &objColor, scene)) + scene.ray = ray; + if (interesct_objects(&tMin, &hitNormal, &objColor, scene)) { - hitPoint = vec3_add(ray.origin, vec3_scale(ray.dir, tMin)); + hitPoint = vec3_add(scene.ray.origin, vec3_scale(scene.ray.dir, tMin)); return (calcLighting(hitPoint, hitNormal, objColor, scene)); } return ((t_vec3){0.2f, 0.7f, 1.0f}); // Couleur de fond (ciel)