update norme

This commit is contained in:
H3XploR
2025-02-18 17:48:52 +01:00
parent 6e963545c9
commit 8c683956ee
9 changed files with 64 additions and 142 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 21:19:17 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
+1 -13
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 19:54:03 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(); app->mlx = mlx_init();
if (!app->mlx) if (!app->mlx)
{
ft_putstr_fd("Erreur mlx_init\n", 2);
return (1); return (1);
}
app->win = mlx_new_window(app->mlx, app->win_width, app->win_height, app->win = mlx_new_window(app->mlx, app->win_width, app->win_height,
"Raytracer interactif"); "Raytracer interactif");
if (!app->win) if (!app->win)
{
ft_putstr_fd("Erreur mlx_new_window\n", 2);
return (1); return (1);
}
app->img = mlx_new_image(app->mlx, app->win_width, app->win_height); app->img = mlx_new_image(app->mlx, app->win_width, app->win_height);
if (!app->img) if (!app->img)
{
ft_putstr_fd("Erreur mlx_new_image\n", 2);
return (1); return (1);
}
app->pixels = (int *)mlx_get_data_addr(app->img, &app->bpp, &app->size_line, app->pixels = (int *)mlx_get_data_addr(app->img, &app->bpp, &app->size_line,
&app->endian); &app->endian);
if (!app->pixels) if (!app->pixels)
{
ft_putstr_fd("Erreur mlx_get_data_addr\n", 2);
return (1); return (1);
}
return (0); return (0);
} }
BIN
View File
Binary file not shown.
+2 -2
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 20:02:36 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 intersectCylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal);
float intersectPlane(t_ray ray, t_plane p, 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 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, bool intersectObjects(t_ray ray, float *tMin, t_vec3 *hitNormal,
t_vec3 *objColor, t_scene scene); t_vec3 *objColor, t_scene scene);
t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor, t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor,
+1 -84
View File
@@ -6,95 +6,12 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 19:54:13 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" #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) t_scene parsing_cylindre(t_scene scene)
{ {
char **tokens; char **tokens;
+22 -8
View File
@@ -6,13 +6,14 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 18:54:45 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" #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) static int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc)
{ {
calc->d = ray.dir; calc->d = ray.dir;
@@ -34,7 +35,8 @@ static int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc)
return (0); 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) static void compute_side_intersection(t_cylinder cy, t_calc *calc)
{ {
calc->t_side = -1; 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) static void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc)
{ {
calc->t_cap = -1; 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) static float select_final_intersection(t_calc *calc)
{ {
if (calc->t_side > 1e-3f && calc->t_cap > 1e-3f) 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) else if (calc->t_side > 1e-3f)
calc->t_final = calc->t_side; calc->t_final = calc->t_side;
else else
calc->t_final = calc->t_cap; 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 // 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 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);
} }
} }
+29 -26
View File
@@ -6,16 +6,17 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 18:58:42 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" #include "miniRT.h"
// Vérifie les intersections avec les sphères // Vérifie les intersections avec les sphères
static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon, static bool check_shadow_sphere(const t_ray shadow_ray, \
t_scene scene) float max_t, t_scene scene)
{ {
const float epsilon = 1e-3f;
int i; int i;
t_vec3 dummy; t_vec3 dummy;
float t; float t;
@@ -23,8 +24,8 @@ static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon,
i = 0; i = 0;
while (i < scene.numSpheres) while (i < scene.numSpheres)
{ {
t = intersectSphere(shadowRay, scene.spheres[i], &dummy); t = intersectSphere(shadow_ray, scene.spheres[i], &dummy);
if (t > epsilon && t < maxT) if (t > epsilon && t < max_t)
return (true); return (true);
i++; i++;
} }
@@ -32,9 +33,10 @@ static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon,
} }
// Vérifie les intersections avec les plans // Vérifie les intersections avec les plans
static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon, static bool check_shadow_plane(const t_ray shadow_ray, \
t_scene scene) float max_t, t_scene scene)
{ {
const float epsilon = 1e-3f;
int i; int i;
t_vec3 dummy; t_vec3 dummy;
float t; float t;
@@ -42,8 +44,8 @@ static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon,
i = 0; i = 0;
while (i < scene.numPlanes) while (i < scene.numPlanes)
{ {
t = intersectPlane(shadowRay, scene.planes[i], &dummy); t = intersectPlane(shadow_ray, scene.planes[i], &dummy);
if (t > epsilon && t < maxT) if (t > epsilon && t < max_t)
return (true); return (true);
i++; i++;
} }
@@ -51,9 +53,10 @@ static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon,
} }
// Vérifie les intersections avec les cylindres // Vérifie les intersections avec les cylindres
static bool checkShadowCylinder(const t_ray shadowRay, float maxT, static bool check_shadow_cylinder(const t_ray shadow_ray, \
float epsilon, t_scene scene) float max_t, t_scene scene)
{ {
const float epsilon = 1e-3f;
int i; int i;
t_vec3 dummy; t_vec3 dummy;
float t; float t;
@@ -61,8 +64,8 @@ static bool checkShadowCylinder(const t_ray shadowRay, float maxT,
i = 0; i = 0;
while (i < scene.numCylinders) while (i < scene.numCylinders)
{ {
t = intersectCylinder(shadowRay, scene.cylinders[i], &dummy); t = intersectCylinder(shadow_ray, scene.cylinders[i], &dummy);
if (t > epsilon && t < maxT) if (t > epsilon && t < max_t)
return (true); return (true);
i++; 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 // 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; const float epsilon = 1e-3f;
t_vec3 toLight; t_vec3 to_light;
float maxT; float max_t;
t_vec3 L; t_vec3 l;
t_ray shadowRay; t_ray shadow_ray;
toLight = vec3_sub(lightPos, hitPoint); to_light = vec3_sub(lightPos, hitPoint);
maxT = vec3_length(toLight); max_t = vec3_length(to_light);
L = vec3_normalize(toLight); l = vec3_normalize(to_light);
shadowRay.origin = vec3_add(hitPoint, vec3_scale(L, epsilon)); shadow_ray.origin = vec3_add(hitPoint, vec3_scale(l, epsilon));
shadowRay.dir = L; shadow_ray.dir = l;
if (checkShadowSphere(shadowRay, maxT, epsilon, scene)) if (check_shadow_sphere(shadow_ray, max_t, scene))
return (true); return (true);
if (checkShadowPlane(shadowRay, maxT, epsilon, scene)) if (check_shadow_plane(shadow_ray, max_t, scene))
return (true); return (true);
if (checkShadowCylinder(shadowRay, maxT, epsilon, scene)) if (check_shadow_cylinder(shadow_ray, max_t, scene))
return (true); return (true);
return (false); return (false);
} }
+4 -4
View File
@@ -291,10 +291,10 @@ 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_cylinder typeref:typename:t_vec3
center miniRT.h /^ t_vec3 center;$/;" m struct:s_sphere 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 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_if_max check.c /^void check_if_max(t_scene scene, const int to_test, const int max)$/;" f typeref:typename:void
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_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 check_tokens check.c /^int check_tokens(char **tokens, int expected)$/;" f typeref:typename:int
clean libft/Makefile /^clean:$/;" t clean libft/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 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 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 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_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_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 key_down miniRT.h /^ int key_down;$/;" m struct:s_app typeref:typename:int
+2 -2
View File
@@ -6,7 +6,7 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */ /* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 19:07:07 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]; light = scene.lights[i];
L = vec3_normalize(vec3_sub(light.pos, hitPoint)); 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)); diff = fmaxf(0.0f, vec3_dot(hitNormal, L));
viewDir = vec3_normalize(vec3_sub(scene.camera.camPos, hitPoint)); viewDir = vec3_normalize(vec3_sub(scene.camera.camPos, hitPoint));