diff --git a/ft_atof.c b/ft_atof.c index 98b5ef1..1855b8a 100644 --- a/ft_atof.c +++ b/ft_atof.c @@ -6,13 +6,13 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 21:19:17 by yantoine #+# #+# */ -/* Updated: 2025/02/18 17:14:00 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 17:22:12 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" -inline static char *check_sign(char *str, int *sign) +inline static char *check_sign(char *str, int *sign) { if (*str == '-') { diff --git a/main.c b/main.c index da52a16..ca7f091 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 19:54:03 by yantoine #+# #+# */ -/* Updated: 2025/02/17 23:35:02 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 17:23:15 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,30 +28,18 @@ static int init_mlx_and_image(t_app *app) { app->mlx = mlx_init(); if (!app->mlx) - { - ft_putstr_fd("Erreur mlx_init\n", 2); return (1); - } app->win = mlx_new_window(app->mlx, app->win_width, app->win_height, "Raytracer interactif"); if (!app->win) - { - ft_putstr_fd("Erreur mlx_new_window\n", 2); return (1); - } app->img = mlx_new_image(app->mlx, app->win_width, app->win_height); if (!app->img) - { - ft_putstr_fd("Erreur mlx_new_image\n", 2); return (1); - } app->pixels = (int *)mlx_get_data_addr(app->img, &app->bpp, &app->size_line, &app->endian); if (!app->pixels) - { - ft_putstr_fd("Erreur mlx_get_data_addr\n", 2); return (1); - } return (0); } diff --git a/miniRT b/miniRT index 4a728e7..315364e 100755 Binary files a/miniRT and b/miniRT differ diff --git a/miniRT.h b/miniRT.h index 1798085..bdd499e 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/17 23:42:16 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 17:39:45 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -220,7 +220,7 @@ void check_if_max(t_scene scene, const int to_test, const int max); 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); -bool isInShadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene); +bool is_in_shadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene); bool intersectObjects(t_ray ray, float *tMin, t_vec3 *hitNormal, t_vec3 *objColor, t_scene scene); t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor, diff --git a/parsing_cylinder.c b/parsing_cylinder.c index 2d28754..757d697 100644 --- a/parsing_cylinder.c +++ b/parsing_cylinder.c @@ -6,95 +6,12 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:54:13 by yantoine #+# #+# */ -/* Updated: 2025/02/18 17:14:45 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 17:24:08 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" -/* -float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal) -{ - t_calc calc; - char **tokens; - - calc.d = ray.dir; - calc.oc = vec3_sub(ray.origin, cy.center); - calc.v = cy.axis; - calc.d_dot_v = vec3_dot(calc.d, calc.v); - calc.oc_dot_v = vec3_dot(calc.oc, calc.v); - calc.d_perp = vec3_sub(calc.calc.d, vec3_scale(calc.v, calc.d_dot_v)); - oc_perp = vec3_sub(calc.oc, vec3_scale(calc.v, calc.oc_dot_v)); - calc.a = vec3_dot(calc.d_perp, calc.d_perp); - calc.b = 2 * vec3_dot(calc.d_perp, oc_perp); - calc.c = vec3_dot(oc_perp, oc_perp) - cy.radius * cy.radius; - calc.disc = calc.b * calc.b - 4 * calc.a * calc.c; - if (calc.disc < 0) - return (-1); - 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_side = -1; - if (calc.t0 > 1e-3f) - { - calc.y = calc.oc_dot_v + calc.t0 * calc.d_dot_v; - if (fabs(calc.y) <= cy.height / 2.0f) - calc.t_side = calc.t0; - } - if (calc.t_side < 0 && calc.t1 > 1e-3f) - { - calc.y = calc.oc_dot_v + calc.t1 * calc.d_dot_v; - if (fabs(calc.y) <= cy.height / 2.0f) - calc.t_side = calc.t1; - } - calc.t_cap = -1; - if (fabs(calc.d_dot_v) > 1e-6f) - { - calc.t_bot = ((-cy.height / 2.0f) - calc.oc_dot_v) / calc.d_dot_v; - if (calc.t_bot > 1e-3f) - { - p = vec3_add(ray.origin, vec3_scale(calc.d, calc.t_bot)); - cp = vec3_sub(p, cy.center); - calc.dist = vec3_length(vec3_sub(cp, vec3_scale(calc.v, vec3_dot(cp, - calc.v)))); - if (calc.dist <= cy.radius) - calc.t_cap = calc.t_bot; - } - t_top = ((cy.height / 2.0f) - calc.oc_dot_v) / calc.d_dot_v; - if (t_top > 1e-3f) - { - p = vec3_add(ray.origin, vec3_scale(calc.d, t_top)); - cp = vec3_sub(p, cy.center); - calc.dist = vec3_length(vec3_sub(cp, vec3_scale(calc.v, vec3_dot(cp, - calc.v)))); - if (calc.dist <= cy.radius && (calc.t_cap < 0 - || t_top < calc.t_cap)) - calc.t_cap = t_top; - } - } - calc.t_final = -1; - if (calc.t_side > 1e-3f && calc.t_cap > 1e-3f) - calc.t_final = (calc.t_side < calc.t_cap) ? calc.t_side : calc.t_cap; - else if (calc.t_side > 1e-3f) - calc.t_final = calc.t_side; - else - calc.t_final = calc.t_cap; - if (calc.t_final < 1e-3f) - return (-1); - calc.hitPoint = vec3_add(ray.origin, vec3_scale(calc.d, calc.t_final)); - cp = vec3_sub(calc.hitPoint, cy.center); - calc.proj = vec3_dot(cp, calc.v); - if (fabs(calc.proj) < cy.height / 2.0f - 1e-3f) - { - calc.n = vec3_sub(cp, vec3_scale(calc.v, calc.proj)); - *hitNormal = vec3_normalize(calc.n); - } - else - { - *hitNormal = (calc.proj > 0) ? calc.v : vec3_scale(calc.v, -1); - } - return (calc.t_final); -}*/ t_scene parsing_cylindre(t_scene scene) { char **tokens; diff --git a/parsing_cylinder_utils.c b/parsing_cylinder_utils.c index 5a6246d..a8f3a45 100644 --- a/parsing_cylinder_utils.c +++ b/parsing_cylinder_utils.c @@ -6,13 +6,14 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 18:54:45 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:28:56 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 17:38:56 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" -// Initialise les variables de calcul et les coefficients du polynôme d'intersection +// 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) { calc->d = ray.dir; @@ -34,7 +35,8 @@ static int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc) return (0); } -// Calcule l'intersection sur la surface latérale du cylindre +// Calcule l'intersection sur la surface +//latérale du cylindre static void compute_side_intersection(t_cylinder cy, t_calc *calc) { calc->t_side = -1; @@ -52,7 +54,8 @@ static void compute_side_intersection(t_cylinder cy, t_calc *calc) } } -// Calcule l'intersection sur les capuchons supérieur et inférieur +// 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) { calc->t_cap = -1; @@ -82,16 +85,24 @@ 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 +// Sélectionne l'intersection la plus +//proche entre la surface latérale et les capuchons static float select_final_intersection(t_calc *calc) { if (calc->t_side > 1e-3f && calc->t_cap > 1e-3f) - calc->t_final = (calc->t_side < calc->t_cap) ? calc->t_side : calc->t_cap; + { + if (calc->t_side < calc->t_cap) + calc->t_final = calc->t_side; + else + calc->t_final = calc->t_cap; + } else if (calc->t_side > 1e-3f) calc->t_final = calc->t_side; else calc->t_final = calc->t_cap; - return ((calc->t_final > 1e-3f) ? calc->t_final : -1); + if (calc->t_final > 1e-3f) + return (calc->t_final); + return (-1); } // Calcule la normale au point d'intersection @@ -108,7 +119,10 @@ static void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc, } else { - *hitNormal = (calc->proj > 0) ? calc->v : vec3_scale(calc->v, -1); + if (calc->proj > 0) + *hitNormal = calc->v; + else + *hitNormal = vec3_scale(calc->v, -1); } } diff --git a/shadows.c b/shadows.c index 32c1bff..51751f6 100644 --- a/shadows.c +++ b/shadows.c @@ -6,16 +6,17 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 18:58:42 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:44:07 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 17:48:22 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" // Vérifie les intersections avec les sphères -static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon, - t_scene scene) +static bool check_shadow_sphere(const t_ray shadow_ray, \ + float max_t, t_scene scene) { + const float epsilon = 1e-3f; int i; t_vec3 dummy; float t; @@ -23,8 +24,8 @@ static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon, i = 0; while (i < scene.numSpheres) { - t = intersectSphere(shadowRay, scene.spheres[i], &dummy); - if (t > epsilon && t < maxT) + t = intersectSphere(shadow_ray, scene.spheres[i], &dummy); + if (t > epsilon && t < max_t) return (true); i++; } @@ -32,9 +33,10 @@ static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon, } // Vérifie les intersections avec les plans -static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon, - t_scene scene) +static bool check_shadow_plane(const t_ray shadow_ray, \ + float max_t, t_scene scene) { + const float epsilon = 1e-3f; int i; t_vec3 dummy; float t; @@ -42,8 +44,8 @@ static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon, i = 0; while (i < scene.numPlanes) { - t = intersectPlane(shadowRay, scene.planes[i], &dummy); - if (t > epsilon && t < maxT) + t = intersectPlane(shadow_ray, scene.planes[i], &dummy); + if (t > epsilon && t < max_t) return (true); i++; } @@ -51,9 +53,10 @@ static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon, } // Vérifie les intersections avec les cylindres -static bool checkShadowCylinder(const t_ray shadowRay, float maxT, - float epsilon, t_scene scene) +static bool check_shadow_cylinder(const t_ray shadow_ray, \ + float max_t, t_scene scene) { + const float epsilon = 1e-3f; int i; t_vec3 dummy; float t; @@ -61,8 +64,8 @@ static bool checkShadowCylinder(const t_ray shadowRay, float maxT, i = 0; while (i < scene.numCylinders) { - t = intersectCylinder(shadowRay, scene.cylinders[i], &dummy); - if (t > epsilon && t < maxT) + t = intersectCylinder(shadow_ray, scene.cylinders[i], &dummy); + if (t > epsilon && t < max_t) return (true); i++; } @@ -70,24 +73,24 @@ static bool checkShadowCylinder(const t_ray shadowRay, float maxT, } // Fonction principale qui détermine si le point est dans l'ombre -bool isInShadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene) +bool is_in_shadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene) { const float epsilon = 1e-3f; - t_vec3 toLight; - float maxT; - t_vec3 L; - t_ray shadowRay; + t_vec3 to_light; + float max_t; + t_vec3 l; + t_ray shadow_ray; - toLight = vec3_sub(lightPos, hitPoint); - maxT = vec3_length(toLight); - L = vec3_normalize(toLight); - shadowRay.origin = vec3_add(hitPoint, vec3_scale(L, epsilon)); - shadowRay.dir = L; - if (checkShadowSphere(shadowRay, maxT, epsilon, scene)) + to_light = vec3_sub(lightPos, hitPoint); + max_t = vec3_length(to_light); + l = vec3_normalize(to_light); + shadow_ray.origin = vec3_add(hitPoint, vec3_scale(l, epsilon)); + shadow_ray.dir = l; + if (check_shadow_sphere(shadow_ray, max_t, scene)) return (true); - if (checkShadowPlane(shadowRay, maxT, epsilon, scene)) + if (check_shadow_plane(shadow_ray, max_t, scene)) return (true); - if (checkShadowCylinder(shadowRay, maxT, epsilon, scene)) + if (check_shadow_cylinder(shadow_ray, max_t, scene)) return (true); return (false); } diff --git a/tags b/tags index 3c0bb97..89f06b8 100644 --- a/tags +++ b/tags @@ -291,11 +291,11 @@ camera miniRT.h /^ t_camera camera;$/;" m struct:s_scene typeref:typename:t_came 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 minilibx-linux/Makefile.mk /^check: all$/;" t -checkShadowCylinder shadows.c /^static bool checkShadowCylinder(const t_ray shadowRay, float maxT,$/;" f typeref:typename:bool file: -checkShadowPlane shadows.c /^static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon,$/;" f typeref:typename:bool file: -checkShadowSphere shadows.c /^static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon,$/;" f typeref:typename:bool file: check_if_max check.c /^void check_if_max(t_scene scene, const int to_test, const int max)$/;" f typeref:typename:void -check_sign ft_atof.c /^inline static char *check_sign(char *str, int *sign)$/;" f typeref:typename:char * file: +check_shadow_cylinder shadows.c /^static bool check_shadow_cylinder(const t_ray shadow_ray, \\$/;" f typeref:typename:bool file: +check_shadow_plane shadows.c /^static bool check_shadow_plane(const t_ray shadow_ray, \\$/;" f typeref:typename:bool file: +check_shadow_sphere shadows.c /^static bool check_shadow_sphere(const t_ray shadow_ray, \\$/;" f typeref:typename:bool file: +check_sign ft_atof.c /^inline static char *check_sign(char *str, int *sign)$/;" f typeref:typename:char * file: check_tokens check.c /^int check_tokens(char **tokens, int expected)$/;" f typeref:typename:int clean libft/Makefile /^clean:$/;" t clean makefile /^clean:$/;" t @@ -438,7 +438,7 @@ intersectCylinder parsing_cylinder_utils.c /^float intersectCylinder(t_ray ray, intersectObjects trace.c /^bool intersectObjects(t_ray ray, float *tMin, t_vec3 *hitNormal,$/;" f typeref:typename:bool 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 -isInShadow shadows.c /^bool isInShadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene)$/;" f typeref:typename:bool +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 key_down miniRT.h /^ int key_down;$/;" m struct:s_app typeref:typename:int diff --git a/trace.c b/trace.c index b62e03e..02e9753 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 17:15:32 by yantoine ### ########.fr */ +/* Updated: 2025/02/18 17:40:17 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -84,7 +84,7 @@ t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor, { light = scene.lights[i]; L = vec3_normalize(vec3_sub(light.pos, hitPoint)); - if (!isInShadow(hitPoint, light.pos, scene)) + if (!is_in_shadow(hitPoint, light.pos, scene)) { diff = fmaxf(0.0f, vec3_dot(hitNormal, L)); viewDir = vec3_normalize(vec3_sub(scene.camera.camPos, hitPoint));