From 224c55611a165535ee1b8f883c1c8507f2ab2bef Mon Sep 17 00:00:00 2001 From: H3XploR Date: Mon, 17 Feb 2025 18:08:58 +0100 Subject: [PATCH] coupe interesction cylinder --- miniRT.h | 36 ++++++++++++++++++- parsing_cylinder.c | 82 ++++++++++++++++++++++++++++++++++++++++++- parsing_plane.c | 18 +++++++++- parsing_sphere.c | 25 ++++++++++++- raytracer_formatted.c | 14 ++------ tags | 32 +++++++++++++++++ 6 files changed, 191 insertions(+), 16 deletions(-) diff --git a/miniRT.h b/miniRT.h index 02d7aad..939b0ff 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/15 19:36:06 by yantoine ### ########.fr */ +/* Updated: 2025/02/17 18:08:03 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,40 @@ # define MAX_AMBIENT 1 # define MAX_CAMERA 1 +// ----- Calcul d'interesction + +typdef struct s_calc +{ + t_vec3 oc; + float a; + float b; + float c; + float disc; + float sqrtDisc; + float t0; + float t1; + float t; + t_vec3 hitPoint; + t_vec3 d; + t_vec3 v; + float d_dot_v; + float oc_dot_v; + t_vec3 d_perp; + t_vec3 oc_perp; + float t_side; + float y; + float t_cap; + float t_bot; + float dist; + float t_top; + t_vec3 p; + t_vec3 cp; + float dist; + float t_final; + float proj; + t_vec3 n; +} t_calc; + // ----- Espace 3d typedef struct s_vec3 { diff --git a/parsing_cylinder.c b/parsing_cylinder.c index adfed24..27681e3 100644 --- a/parsing_cylinder.c +++ b/parsing_cylinder.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:54:13 by yantoine #+# #+# */ -/* Updated: 2025/02/15 19:54:28 by yantoine ### ########.fr */ +/* Updated: 2025/02/17 18:08:15 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,3 +25,83 @@ t_scene parsing_cylinder(const char *line, t_scene scene) scene.numCylinders++; return (scene); } + +float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal) +{ + t_calc calc; + + 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; +} + diff --git a/parsing_plane.c b/parsing_plane.c index 9c8451a..efe6cce 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/15 19:53:30 by yantoine ### ########.fr */ +/* Updated: 2025/02/17 17:49:28 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,3 +23,19 @@ t_scene parsing_plane(const char *line, t_scene scene) scene.numPlanes++; return (scene); } + +float intersectPlane(Ray ray, Plane p, t_vec3 *hitNormal) +{ + float denom; + float t; + + denom = vec3_dot(p.normal, ray.dir); + if (fabs(denom) < 1e-6f) + return (-1); + t = vec3_dot(vec3_sub(p.point, ray.origin), p.normal) / denom; + if (t < 1e-3f) + return (-1); + *hitNormal = p.normal; + return (t); +} + diff --git a/parsing_sphere.c b/parsing_sphere.c index c5bcfa0..159ede0 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/15 19:48:43 by yantoine ### ########.fr */ +/* Updated: 2025/02/17 17:31:42 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,3 +23,26 @@ t_scene parsing_sphere(const char *line, t_scene scene) scene.numSpheres++; return (scene); } + +float intersectSphere(Ray ray, Sphere s, t_vec3 *hitNormal) +{ + t_calc calc; + + calc.oc = vec3_sub(ray.origin, s.center); + calc.a = vec3_dot(ray.dir, ray.dir); + calc.b = 2.0f * vec3_dot(calc.oc, ray.dir); + c = vec3_dot(calc.oc, calc.oc) - s.radius * s.radius; + calc.disc = calc.b * calc.b - 4 * calc.a * 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 = (calc.t0 > 1e-3f) ? calc.t0 : calc.t1; + if (calc.t < 1e-3f) + return (-1); + calc.hitPoint = vec3_add(ray.origin, vec3_scale(ray.dir, calc.t)); + *hitNormal = vec3_normalize(vec3_sub(calc.hitPoint, s.center)); + return (calc.t); +} + diff --git a/raytracer_formatted.c b/raytracer_formatted.c index 2849346..89212f8 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 16:50:44 by yantoine ### ########.fr */ +/* Updated: 2025/02/17 17:18:17 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -165,17 +165,7 @@ void load_config(const char *filename) // ----- Fonctions d'intersection ----- float intersectSphere(Ray ray, Sphere s, t_vec3 *hitNormal) { - t_vec3 oc; - float a; - float b; - float c; - float disc; - float sqrtDisc; - float t0; - float t1; - float t; - t_vec3 hitPoint; - + oc = vec3_sub(ray.origin, s.center); a = vec3_dot(ray.dir, ray.dir); b = 2.0f * vec3_dot(oc, ray.dir); diff --git a/tags b/tags index 3cbaa3a..af44277 100644 --- a/tags +++ b/tags @@ -78,14 +78,17 @@ 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 +a miniRT.h /^ float a;$/;" m struct:s_calc typeref:typename:float 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 ambient_ratio raytracer_formatted.c /^float ambient_ratio = 0.1f;$/;" v typeref:typename:float axis miniRT.h /^ t_vec3 axis; \/\/ Axe normalisé$/;" m struct:s_cylinder typeref:typename:t_vec3 +b miniRT.h /^ float b;$/;" m struct:s_calc typeref:typename:float better_ray_tracer README.md /^# better_ray_tracer/;" c brightness miniRT.h /^ float brightness;$/;" m struct:s_light typeref:typename:float +c miniRT.h /^ float c;$/;" m struct:s_calc typeref:typename:float 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 @@ -99,16 +102,26 @@ 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 color miniRT.h /^ t_vec3 color;$/;" m struct:s_sphere typeref:typename:t_vec3 +cp miniRT.h /^ t_vec3 cp;$/;" m struct:s_calc typeref:typename:t_vec3 create_scene scene.c /^t_scene create_scene(void)$/;" f typeref:typename:t_scene cylinders miniRT.h /^ t_cylinder cylinders[MAX_CYLINDERS];$/;" m struct:s_scene typeref:typename:t_cylinder[] +d miniRT.h /^ t_vec3 d;$/;" m struct:s_calc typeref:typename:t_vec3 +d_dot_v miniRT.h /^ float d_dot_v;$/;" m struct:s_calc typeref:typename:float +d_perp miniRT.h /^ t_vec3 d_perp;$/;" m struct:s_calc typeref:typename:t_vec3 dir miniRT.h /^ t_vec3 dir;$/;" m struct:s_ray typeref:typename:t_vec3 +disc miniRT.h /^ float disc;$/;" m struct:s_calc typeref:typename:float +dist miniRT.h /^ float dist;$/;" m struct:s_calc typeref:typename:float fd_if_exit miniRT.h /^ const int fd_if_exit;$/;" m struct:s_scene typeref:typename:const int fov miniRT.h /^ float fov;$/;" m struct:s_camera typeref:typename:float fov raytracer_formatted.c /^float fov = 90.0f;$/;" v typeref:typename:float get_tokens_secure parsing_utils.c /^inline char **get_tokens_secure(t_scene scene, const int numObject, const int numObjectMax, cons/;" f typeref:typename:char ** height miniRT.h /^ float height;$/;" m struct:s_cylinder typeref:typename:float +hitPoint miniRT.h /^ t_vec3 hitPoint;$/;" m struct:s_calc typeref:typename:t_vec3 +intersectCylinder parsing_cylinder.c /^float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal)$/;" f typeref:typename:float intersectCylinder raytracer_formatted.c /^float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal)$/;" f typeref:typename:float +intersectPlane parsing_plane.c /^float intersectPlane(Ray ray, Plane p, t_vec3 *hitNormal)$/;" f typeref:typename:float intersectPlane raytracer_formatted.c /^float intersectPlane(Ray ray, Plane p, t_vec3 *hitNormal)$/;" f typeref:typename:float +intersectSphere parsing_sphere.c /^float intersectSphere(Ray ray, Sphere s, t_vec3 *hitNormal)$/;" f typeref:typename:float 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[] @@ -116,13 +129,18 @@ line_if_exit miniRT.h /^ const char *line_if_exit;$/;" m struct:s_scene typere 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 +n miniRT.h /^ t_vec3 n;$/;" m struct:s_calc typeref:typename:t_vec3 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 +oc miniRT.h /^ t_vec3 oc;$/;" m struct:s_calc typeref:typename:t_vec3 +oc_dot_v miniRT.h /^ float oc_dot_v;$/;" m struct:s_calc typeref:typename:float +oc_perp miniRT.h /^ t_vec3 oc_perp;$/;" m struct:s_calc typeref:typename:t_vec3 origin miniRT.h /^ t_vec3 origin;$/;" m struct:s_ray typeref:typename:t_vec3 +p miniRT.h /^ t_vec3 p;$/;" m struct:s_calc typeref:typename:t_vec3 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 @@ -138,11 +156,13 @@ pitch raytracer_formatted.c /^float pitch = 0.0f; \/\/ vue de haut en bas en 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 +proj miniRT.h /^ float proj;$/;" m struct:s_calc typeref:typename:float 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_calc miniRT.h /^typdef struct s_calc$/;" s s_camera miniRT.h /^typedef struct s_camera$/;" s s_cylinder miniRT.h /^typedef struct s_cylinder$/;" s s_light miniRT.h /^typedef struct s_light$/;" s @@ -152,17 +172,28 @@ s_scene miniRT.h /^typedef struct s_scene$/;" s s_sphere miniRT.h /^typedef struct s_sphere$/;" s s_vec3 miniRT.h /^typedef struct s_vec3$/;" s spheres miniRT.h /^ t_sphere spheres[MAX_SPHERES];$/;" m struct:s_scene typeref:typename:t_sphere[] +sqrtDisc miniRT.h /^ float sqrtDisc;$/;" m struct:s_calc typeref:typename:float +t miniRT.h /^ float t;$/;" m struct:s_calc typeref:typename:float +t0 miniRT.h /^ float t0;$/;" m struct:s_calc typeref:typename:float +t1 miniRT.h /^ float t1;$/;" m struct:s_calc typeref:typename:float t_ambient miniRT.h /^} t_ambient;$/;" t typeref:struct:s_ambient +t_bot miniRT.h /^ float t_bot;$/;" m struct:s_calc typeref:typename:float +t_calc miniRT.h /^} t_calc;$/;" v typeref:struct:s_calc t_camera miniRT.h /^} t_camera;$/;" t typeref:struct:s_camera +t_cap miniRT.h /^ float t_cap;$/;" m struct:s_calc typeref:typename:float t_cylinder miniRT.h /^} t_cylinder;$/;" t typeref:struct:s_cylinder +t_final miniRT.h /^ float t_final;$/;" m struct:s_calc typeref:typename:float t_light miniRT.h /^} t_light;$/;" t typeref:struct:s_light t_plane miniRT.h /^} t_plane;$/;" t typeref:struct:s_plane t_ray miniRT.h /^} t_ray;$/;" t typeref:struct:s_ray t_scene miniRT.h /^} t_scene;$/;" t typeref:struct:s_scene +t_side miniRT.h /^ float t_side;$/;" m struct:s_calc typeref:typename:float t_sphere miniRT.h /^} t_sphere;$/;" t typeref:struct:s_sphere +t_top miniRT.h /^ float t_top;$/;" m struct:s_calc typeref:typename:float t_vec3 miniRT.h /^} t_vec3;$/;" t typeref:struct:s_vec3 token_if_exit miniRT.h /^ const char **token_if_exit;$/;" m struct:s_scene typeref:typename:const char ** trace raytracer_formatted.c /^t_vec3 trace(Ray ray)$/;" f typeref:typename:t_vec3 +v miniRT.h /^ t_vec3 v;$/;" m struct:s_calc typeref:typename:t_vec3 vec3_add calcul_de_vecteur.c /^t_vec3 vec3_add(t_vec3 a, t_vec3 b)$/;" f typeref:typename:t_vec3 vec3_cross calcul_de_vecteur.c /^t_vec3 vec3_cross(t_vec3 a, t_vec3 b)$/;" f typeref:typename:t_vec3 vec3_dot calcul_de_vecteur.c /^float vec3_dot(t_vec3 a, t_vec3 b)$/;" f typeref:typename:float @@ -173,6 +204,7 @@ vec3_scale calcul_de_vecteur.c /^t_vec3 vec3_scale(t_vec3 a, float s)$/;" f type vec3_sub calcul_de_vecteur.c /^t_vec3 vec3_sub(t_vec3 a, t_vec3 b)$/;" f typeref:typename:t_vec3 x miniRT.h /^ float x;$/;" m struct:s_vec3 typeref:typename:float y miniRT.h /^ float y;$/;" m struct:s_vec3 typeref:typename:float +y miniRT.h /^ float y;$/;" m struct:s_calc typeref:typename:float yaw miniRT.h /^ float yaw;\/\/ vue de gauche a droite$/;" m struct:s_camera typeref:typename:float yaw raytracer_formatted.c /^float yaw = 0.0f;\/\/ vue de gauche a droite$/;" v typeref:typename:float z miniRT.h /^ float z;$/;" m struct:s_vec3 typeref:typename:float