update norme

This commit is contained in:
H3XploR
2025-02-19 16:30:41 +01:00
parent 1ecd5c5658
commit a8f9e1724a
5 changed files with 169 additions and 88 deletions
+25 -84
View File
@@ -6,104 +6,45 @@
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 19:07:07 by yantoine #+# #+# */
/* Updated: 2025/02/18 20:34:28 by yantoine ### ########.fr */
/* Updated: 2025/02/19 16:30:27 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "miniRT.h"
// Renvoie true si le rayon intersecte un objet, et met à jour tMin,
//hitNormal et objColor
bool interesct_objects(float *tMin, t_vec3 *hitNormal,
t_vec3 *objColor, t_scene scene)
{
bool hit;
float t;
t_vec3 n;
int i;
hit = false;
i = 0;
while (i < scene.numSpheres)
{
t = intersect_sphere(scene.ray, scene.spheres[i], &n);
if (t > 1e-3f && t < *tMin)
{
*tMin = t;
*hitNormal = n;
*objColor = scene.spheres[i].color;
hit = true;
}
i++;
}
i = 0;
while (i < scene.numPlanes)
{
t = intersect_plane(scene.ray, scene.planes[i], &n);
if (t > 1e-3f && t < *tMin)
{
*tMin = t;
*hitNormal = n;
*objColor = scene.planes[i].color;
hit = true;
}
i++;
}
i = 0;
while (i < scene.numCylinders)
{
t = intersect_cylinder(scene.ray, scene.cylinders[i], &n);
if (t > 1e-3f && t < *tMin)
{
*tMin = t;
*hitNormal = n;
*objColor = scene.cylinders[i].color;
hit = true;
}
i++;
}
return (hit);
}
// Calcule l'éclairage (ambiant, diffus et spéculaire) sur un point d'impact
t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor,
t_vec3 calc_lighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor,
t_scene scene)
{
t_vec3 color;
int i;
t_light light;
t_vec3 L;
float diff;
t_vec3 viewDir;
t_vec3 halfDir;
float spec;
t_calc calc;
color = vec3_scale(objColor, scene.ambient.ratio);
calc.color = vec3_scale(objColor, scene.ambient.ratio);
i = 0;
while (i < scene.numLights)
{
light = scene.lights[i];
L = vec3_normalize(vec3_sub(light.pos, hitPoint));
if (!is_in_shadow(hitPoint, light.pos, scene))
calc.light = scene.lights[i];
calc.l = vec3_normalize(vec3_sub(calc.light.pos, hitPoint));
if (!is_in_shadow(hitPoint, calc.light.pos, scene))
{
diff = fmaxf(0.0f, vec3_dot(hitNormal, L));
viewDir = vec3_normalize(vec3_sub(scene.camera.camPos, hitPoint));
halfDir = vec3_normalize(vec3_add(L, viewDir));
spec = powf(fmaxf(0.0f, vec3_dot(hitNormal, halfDir)), 32.0f);
color = vec3_add(color, vec3_scale(vec3_mul(objColor, light.color),
diff * light.brightness));
color = vec3_add(color, vec3_scale((t_vec3){1, 1, 1}, spec
* light.brightness));
calc.diff = fmaxf(0.0f, vec3_dot(hitNormal, calc.l));
calc.view_dir = vec3_normalize(vec3_sub(scene.camera.camPos, hitPoint));
calc.half_dir = vec3_normalize(vec3_add(calc.l, calc.view_dir));
calc.spec = powf(fmaxf(0.0f, vec3_dot(hitNormal, calc.half_dir)), 32.0f);
calc.color = vec3_add(calc.color, vec3_scale(vec3_mul(objColor, calc.light.color),
calc.diff * calc.light.brightness));
calc.color = vec3_add(calc.color, vec3_scale((t_vec3){1, 1, 1}, calc.spec
* calc.light.brightness));
}
i++;
}
if (color.x > 1.0f)
color.x = 1.0f;
if (color.y > 1.0f)
color.y = 1.0f;
if (color.z > 1.0f)
color.z = 1.0f;
return (color);
if (calc.color.x > 1.0f)
calc.color.x = 1.0f;
if (calc.color.y > 1.0f)
calc.color.y = 1.0f;
if (calc.color.z > 1.0f)
calc.color.z = 1.0f;
return (calc.color);
}
// Fonction principale de lancer de rayon (trace)
@@ -118,10 +59,10 @@ t_vec3 trace(t_ray ray, t_scene scene)
hitNormal = (t_vec3){0, 0, 0};
objColor = (t_vec3){0, 0, 0};
scene.ray = ray;
if (interesct_objects(&tMin, &hitNormal, &objColor, scene))
if (intersect_objects(&tMin, &hitNormal, &objColor, scene))
{
hitPoint = vec3_add(scene.ray.origin, vec3_scale(scene.ray.dir, tMin));
return (calcLighting(hitPoint, hitNormal, objColor, scene));
return (calc_lighting(hitPoint, hitNormal, objColor, scene));
}
return ((t_vec3){0.2f, 0.7f, 1.0f}); // Couleur de fond (ciel)
return ((t_vec3){0, 0, 0}); // Couleur de fond (ciel)
}