faire la norme

This commit is contained in:
H3XploR
2025-02-17 23:52:47 +01:00
parent 960a2ad8ae
commit c6c13573cb
20 changed files with 539 additions and 440 deletions
+2 -2
View File
@@ -36,6 +36,6 @@ float vec3_dot(t_vec3 a, t_vec3 b)
t_vec3 vec3_cross(t_vec3 a, t_vec3 b) t_vec3 vec3_cross(t_vec3 a, t_vec3 b)
{ {
return ((t_vec3){a.y * b.z - a.z * b.y, \ return ((t_vec3){a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y
a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}); - a.y * b.x});
} }
+4 -4
View File
@@ -32,8 +32,8 @@ static int init_mlx_and_image(t_app *app)
ft_putstr_fd("Erreur mlx_init\n", 2); ft_putstr_fd("Erreur mlx_init\n", 2);
return (1); return (1);
} }
app->win = mlx_new_window(app->mlx, app->win_width, app->win = mlx_new_window(app->mlx, app->win_width, app->win_height,
app->win_height, "Raytracer interactif"); "Raytracer interactif");
if (!app->win) if (!app->win)
{ {
ft_putstr_fd("Erreur mlx_new_window\n", 2); ft_putstr_fd("Erreur mlx_new_window\n", 2);
@@ -45,8 +45,8 @@ static int init_mlx_and_image(t_app *app)
ft_putstr_fd("Erreur mlx_new_image\n", 2); 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->pixels = (int *)mlx_get_data_addr(app->img, &app->bpp, &app->size_line,
&app->size_line, &app->endian); &app->endian);
if (!app->pixels) if (!app->pixels)
{ {
ft_putstr_fd("Erreur mlx_get_data_addr\n", 2); ft_putstr_fd("Erreur mlx_get_data_addr\n", 2);
+12 -11
View File
@@ -13,16 +13,16 @@
#ifndef MINIRT_H #ifndef MINIRT_H
# define MINIRT_H # define MINIRT_H
# include "float.h"
# include "get_next_line.h"
# include "libft.h"
# include "mlx.h"
# include <fcntl.h>
# include <math.h> # include <math.h>
# include <stdbool.h> # include <stdbool.h>
# include <stdio.h> # include <stdio.h>
# include <stdlib.h> # include <stdlib.h>
# include <string.h> # include <string.h>
# include <fcntl.h>
# include "get_next_line.h"
# include "libft.h"
# include "float.h"
# include "mlx.h"
// ----- Taille ecran ---- // ----- Taille ecran ----
# define WIDTH 640 # define WIDTH 640
@@ -97,7 +97,7 @@ typedef struct s_camera
float rot_speed; float rot_speed;
float move_speed; float move_speed;
float fov; float fov;
float yaw;// vue de gauche a droite float yaw; // vue de gauche a droite
float pitch; // vue de haut en bas en radians float pitch; // vue de haut en bas en radians
} t_camera; } t_camera;
@@ -144,7 +144,6 @@ typedef struct s_app
t_scene scene; t_scene scene;
} t_app; } t_app;
// ----- Calcul // ----- Calcul
typedef struct s_calc typedef struct s_calc
{ {
@@ -209,9 +208,9 @@ t_vec3 parse_color(const char *token, t_scene scene);
t_vec3 parse_vector(const char *token, t_scene scene); t_vec3 parse_vector(const char *token, t_scene scene);
t_vec3 parse_vector_normalize(const char *token, t_scene scene); t_vec3 parse_vector_normalize(const char *token, t_scene scene);
// Parsing utils // Parsing utils
char **get_tokens_secure(t_scene scene, const int numObject, const int numObjectMax, const int supposed_nb_token); char **get_tokens_secure(t_scene scene, const int numObject,
const int numObjectMax, const int supposed_nb_token);
// Check // Check
int check_tokens(char **tokens, int expected); int check_tokens(char **tokens, int expected);
@@ -222,8 +221,10 @@ 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 isInShadow(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); bool intersectObjects(t_ray ray, float *tMin, t_vec3 *hitNormal,
t_vec3 calcLighting(t_vec3 hitPoint, 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_scene scene);
t_vec3 trace(t_ray ray, t_scene scene); t_vec3 trace(t_ray ray, t_scene scene);
// Peripherique // Peripherique
-1
View File
@@ -12,7 +12,6 @@
#include "miniRT.h" #include "miniRT.h"
t_scene parsing_ambiant(t_scene scene) t_scene parsing_ambiant(t_scene scene)
{ {
char **tokens; char **tokens;
+10 -10
View File
@@ -16,6 +16,7 @@
float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal) float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal)
{ {
t_calc calc; t_calc calc;
char **tokens;
calc.d = ray.dir; calc.d = ray.dir;
calc.oc = vec3_sub(ray.origin, cy.center); calc.oc = vec3_sub(ray.origin, cy.center);
@@ -54,7 +55,8 @@ float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal)
{ {
p = vec3_add(ray.origin, vec3_scale(calc.d, calc.t_bot)); p = vec3_add(ray.origin, vec3_scale(calc.d, calc.t_bot));
cp = vec3_sub(p, cy.center); cp = vec3_sub(p, cy.center);
calc.dist = vec3_length(vec3_sub(cp, vec3_scale(calc.v, vec3_dot(cp, calc.v)))); calc.dist = vec3_length(vec3_sub(cp, vec3_scale(calc.v, vec3_dot(cp,
calc.v))));
if (calc.dist <= cy.radius) if (calc.dist <= cy.radius)
calc.t_cap = calc.t_bot; calc.t_cap = calc.t_bot;
} }
@@ -63,8 +65,10 @@ float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal)
{ {
p = vec3_add(ray.origin, vec3_scale(calc.d, t_top)); p = vec3_add(ray.origin, vec3_scale(calc.d, t_top));
cp = vec3_sub(p, cy.center); cp = vec3_sub(p, cy.center);
calc.dist = vec3_length(vec3_sub(cp, vec3_scale(calc.v, vec3_dot(cp, calc.v)))); calc.dist = vec3_length(vec3_sub(cp, vec3_scale(calc.v, vec3_dot(cp,
if (calc.dist <= cy.radius && (calc.t_cap < 0 || t_top < calc.t_cap)) calc.v))));
if (calc.dist <= cy.radius && (calc.t_cap < 0
|| t_top < calc.t_cap))
calc.t_cap = t_top; calc.t_cap = t_top;
} }
} }
@@ -89,17 +93,15 @@ float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal)
{ {
*hitNormal = (calc.proj > 0) ? calc.v : vec3_scale(calc.v, -1); *hitNormal = (calc.proj > 0) ? calc.v : vec3_scale(calc.v, -1);
} }
return calc.t_final; return (calc.t_final);
}*/ }*/
t_scene parsing_cylindre(t_scene scene) t_scene parsing_cylindre(t_scene scene)
{ {
char **tokens;
tokens = get_tokens_secure(scene, scene.numCylinders, MAX_CYLINDERS, 6); tokens = get_tokens_secure(scene, scene.numCylinders, MAX_CYLINDERS, 6);
scene.token_if_exit = tokens; scene.token_if_exit = tokens;
scene.cylinders[scene.numCylinders].center = parse_vector(tokens[1], scene); scene.cylinders[scene.numCylinders].center = parse_vector(tokens[1], scene);
scene.cylinders[scene.numCylinders].axis = parse_vector_normalize(tokens[2], scene); scene.cylinders[scene.numCylinders].axis = parse_vector_normalize(tokens[2],
scene);
scene.cylinders[scene.numCylinders].radius = ft_atof(tokens[3]); scene.cylinders[scene.numCylinders].radius = ft_atof(tokens[3]);
scene.cylinders[scene.numCylinders].height = ft_atof(tokens[4]); scene.cylinders[scene.numCylinders].height = ft_atof(tokens[4]);
scene.cylinders[scene.numCylinders].color = parse_color(tokens[5], scene); scene.cylinders[scene.numCylinders].color = parse_color(tokens[5], scene);
@@ -107,5 +109,3 @@ t_scene parsing_cylindre(t_scene scene)
scene.numCylinders++; scene.numCylinders++;
return (scene); return (scene);
} }
+41 -24
View File
@@ -13,7 +13,8 @@
#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;
calc->oc = vec3_sub(ray.origin, cy.center); calc->oc = vec3_sub(ray.origin, cy.center);
calc->v = cy.axis; calc->v = cy.axis;
@@ -26,22 +27,25 @@ static int init_intersection(t_ray ray, t_cylinder cy, t_calc *calc) {
calc->c = vec3_dot(calc->oc_perp, calc->oc_perp) - cy.radius * cy.radius; calc->c = vec3_dot(calc->oc_perp, calc->oc_perp) - cy.radius * cy.radius;
calc->disc = calc->b * calc->b - 4 * calc->a * calc->c; calc->disc = calc->b * calc->b - 4 * calc->a * calc->c;
if (calc->disc < 0) if (calc->disc < 0)
return -1; return (-1);
calc->sqrtDisc = sqrtf(calc->disc); calc->sqrtDisc = sqrtf(calc->disc);
calc->t0 = (-calc->b - calc->sqrtDisc) / (2 * calc->a); calc->t0 = (-calc->b - calc->sqrtDisc) / (2 * calc->a);
calc->t1 = (-calc->b + calc->sqrtDisc) / (2 * calc->a); calc->t1 = (-calc->b + calc->sqrtDisc) / (2 * calc->a);
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;
if (calc->t0 > 1e-3f) { if (calc->t0 > 1e-3f)
{
calc->y = calc->oc_dot_v + calc->t0 * calc->d_dot_v; calc->y = calc->oc_dot_v + calc->t0 * calc->d_dot_v;
if (fabs(calc->y) <= cy.height / 2.0f) if (fabs(calc->y) <= cy.height / 2.0f)
calc->t_side = calc->t0; calc->t_side = calc->t0;
} }
if (calc->t_side < 0 && calc->t1 > 1e-3f) { if (calc->t_side < 0 && calc->t1 > 1e-3f)
{
calc->y = calc->oc_dot_v + calc->t1 * calc->d_dot_v; calc->y = calc->oc_dot_v + calc->t1 * calc->d_dot_v;
if (fabs(calc->y) <= cy.height / 2.0f) if (fabs(calc->y) <= cy.height / 2.0f)
calc->t_side = calc->t1; calc->t_side = calc->t1;
@@ -49,63 +53,76 @@ 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;
if (fabs(calc->d_dot_v) > 1e-6f) { if (fabs(calc->d_dot_v) > 1e-6f)
{
calc->t_bot = ((-cy.height / 2.0f) - calc->oc_dot_v) / calc->d_dot_v; calc->t_bot = ((-cy.height / 2.0f) - calc->oc_dot_v) / calc->d_dot_v;
if (calc->t_bot > 1e-3f) { if (calc->t_bot > 1e-3f)
{
calc->p = vec3_add(ray.origin, vec3_scale(calc->d, calc->t_bot)); calc->p = vec3_add(ray.origin, vec3_scale(calc->d, calc->t_bot));
calc->cp = vec3_sub(calc->p, cy.center); calc->cp = vec3_sub(calc->p, cy.center);
calc->dist = vec3_length(vec3_sub(calc->cp, vec3_scale(calc->v, vec3_dot(calc->cp, calc->v)))); calc->dist = vec3_length(vec3_sub(calc->cp, vec3_scale(calc->v,
vec3_dot(calc->cp, calc->v))));
if (calc->dist <= cy.radius) if (calc->dist <= cy.radius)
calc->t_cap = calc->t_bot; calc->t_cap = calc->t_bot;
} }
calc->t_top = ((cy.height / 2.0f) - calc->oc_dot_v) / calc->d_dot_v; calc->t_top = ((cy.height / 2.0f) - calc->oc_dot_v) / calc->d_dot_v;
if (calc->t_top > 1e-3f) { if (calc->t_top > 1e-3f)
{
calc->p = vec3_add(ray.origin, vec3_scale(calc->d, calc->t_top)); calc->p = vec3_add(ray.origin, vec3_scale(calc->d, calc->t_top));
calc->cp = vec3_sub(calc->p, cy.center); calc->cp = vec3_sub(calc->p, cy.center);
calc->dist = vec3_length(vec3_sub(calc->cp, vec3_scale(calc->v, vec3_dot(calc->cp, calc->v)))); calc->dist = vec3_length(vec3_sub(calc->cp, vec3_scale(calc->v,
if (calc->dist <= cy.radius && (calc->t_cap < 0 || calc->t_top < calc->t_cap)) vec3_dot(calc->cp, calc->v))));
if (calc->dist <= cy.radius && (calc->t_cap < 0
|| calc->t_top < calc->t_cap))
calc->t_cap = calc->t_top; calc->t_cap = calc->t_top;
} }
} }
} }
// 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; calc->t_final = (calc->t_side < calc->t_cap) ? calc->t_side : 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; return ((calc->t_final > 1e-3f) ? calc->t_final : -1);
} }
// Calcule la normale au point d'intersection // Calcule la normale au point d'intersection
static void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc, t_vec3 *hitNormal) { static 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)); calc->hitPoint = vec3_add(ray.origin, vec3_scale(calc->d, calc->t_final));
calc->cp = vec3_sub(calc->hitPoint, cy.center); calc->cp = vec3_sub(calc->hitPoint, cy.center);
calc->proj = vec3_dot(calc->cp, calc->v); calc->proj = vec3_dot(calc->cp, calc->v);
if (fabs(calc->proj) < cy.height / 2.0f - 1e-3f) { if (fabs(calc->proj) < cy.height / 2.0f - 1e-3f)
{
calc->n = vec3_sub(calc->cp, vec3_scale(calc->v, calc->proj)); calc->n = vec3_sub(calc->cp, vec3_scale(calc->v, calc->proj));
*hitNormal = vec3_normalize(calc->n); *hitNormal = vec3_normalize(calc->n);
} else { }
else
{
*hitNormal = (calc->proj > 0) ? calc->v : vec3_scale(calc->v, -1); *hitNormal = (calc->proj > 0) ? calc->v : vec3_scale(calc->v, -1);
} }
} }
// Fonction principale d'intersection du cylindre // Fonction principale d'intersection du cylindre
float intersectCylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal) { float intersectCylinder(t_ray ray, t_cylinder cy, t_vec3 *hitNormal)
{
t_calc calc; t_calc calc;
if (init_intersection(ray, cy, &calc) < 0) if (init_intersection(ray, cy, &calc) < 0)
return -1; return (-1);
compute_side_intersection(cy, &calc); compute_side_intersection(cy, &calc);
compute_cap_intersection(ray, cy, &calc); compute_cap_intersection(ray, cy, &calc);
if (select_final_intersection(&calc) < 0) if (select_final_intersection(&calc) < 0)
return -1; return (-1);
compute_hit_normal(ray, cy, &calc, hitNormal); compute_hit_normal(ray, cy, &calc, hitNormal);
return calc.t_final; return (calc.t_final);
} }
+2 -2
View File
@@ -19,7 +19,8 @@ t_scene parsing_plane(t_scene scene)
tokens = get_tokens_secure(scene, scene.numPlanes, MAX_PLANES, 4); tokens = get_tokens_secure(scene, scene.numPlanes, MAX_PLANES, 4);
scene.token_if_exit = tokens; scene.token_if_exit = tokens;
scene.planes[scene.numPlanes].point = parse_vector(tokens[1], scene); scene.planes[scene.numPlanes].point = parse_vector(tokens[1], scene);
scene.planes[scene.numPlanes].normal = parse_vector_normalize(tokens[2], scene); scene.planes[scene.numPlanes].normal = parse_vector_normalize(tokens[2],
scene);
scene.planes[scene.numPlanes].color = parse_color(tokens[3], scene); scene.planes[scene.numPlanes].color = parse_color(tokens[3], scene);
ft_free_array(tokens); ft_free_array(tokens);
scene.numPlanes++; scene.numPlanes++;
@@ -40,4 +41,3 @@ float intersectPlane(t_ray ray, t_plane p, t_vec3 *hitNormal)
*hitNormal = p.normal; *hitNormal = p.normal;
return (t); return (t);
} }
-1
View File
@@ -47,4 +47,3 @@ float intersectSphere(t_ray ray, t_sphere s, t_vec3 *hitNormal)
*hitNormal = vec3_normalize(vec3_sub(calc.hitPoint, s.center)); *hitNormal = vec3_normalize(vec3_sub(calc.hitPoint, s.center));
return (calc.t); return (calc.t);
} }
+2 -1
View File
@@ -12,7 +12,8 @@
#include "miniRT.h" #include "miniRT.h"
char **get_tokens_secure(t_scene scene, const int numObject, const int numObjectMax, const int supposed_nb_token) char **get_tokens_secure(t_scene scene, const int numObject,
const int numObjectMax, const int supposed_nb_token)
{ {
char **tokens; char **tokens;
+2 -1
View File
@@ -19,7 +19,8 @@ static inline int range_is_ok(char **token_vector)
i = 0; i = 0;
while (i < 3) while (i < 3)
{ {
if (ft_atof(token_vector[i]) < -FLT_MAX || ft_atof(token_vector[i]) > FLT_MAX) if (ft_atof(token_vector[i]) < -FLT_MAX
|| ft_atof(token_vector[i]) > FLT_MAX)
return (0); return (0);
i++; i++;
} }
-1
View File
@@ -77,4 +77,3 @@ int mouse_move(int x, int y, t_app *app)
last_y = y; last_y = y;
return (0); return (0);
} }
+30 -21
View File
@@ -12,11 +12,13 @@
#include "miniRT.h" #include "miniRT.h"
void print_vec3(t_vec3 vec) { void print_vec3(t_vec3 vec)
{
printf("(x: %.2f, y: %.2f, z: %.2f)", vec.x, vec.y, vec.z); printf("(x: %.2f, y: %.2f, z: %.2f)", vec.x, vec.y, vec.z);
} }
void print_ray(t_ray ray) { void print_ray(t_ray ray)
{
printf("Rayon d'origine : "); printf("Rayon d'origine : ");
print_vec3(ray.origin); print_vec3(ray.origin);
printf("\nDirection : "); printf("\nDirection : ");
@@ -24,7 +26,8 @@ void print_ray(t_ray ray) {
printf("\n"); printf("\n");
} }
void print_sphere(t_sphere sphere) { void print_sphere(t_sphere sphere)
{
printf("Centre : "); printf("Centre : ");
print_vec3(sphere.center); print_vec3(sphere.center);
printf("\nRayon : %.2f", sphere.radius); printf("\nRayon : %.2f", sphere.radius);
@@ -33,7 +36,8 @@ void print_sphere(t_sphere sphere) {
printf("\n"); printf("\n");
} }
void print_plane(t_plane plane) { void print_plane(t_plane plane)
{
printf("Point : "); printf("Point : ");
print_vec3(plane.point); print_vec3(plane.point);
printf("\nNormale : "); printf("\nNormale : ");
@@ -43,7 +47,8 @@ void print_plane(t_plane plane) {
printf("\n"); printf("\n");
} }
void print_cylinder(t_cylinder cyl) { void print_cylinder(t_cylinder cyl)
{
printf("Centre : "); printf("Centre : ");
print_vec3(cyl.center); print_vec3(cyl.center);
printf("\nAxe : "); printf("\nAxe : ");
@@ -55,7 +60,8 @@ void print_cylinder(t_cylinder cyl) {
printf("\n"); printf("\n");
} }
void print_light(t_light light) { void print_light(t_light light)
{
printf("Position : "); printf("Position : ");
print_vec3(light.pos); print_vec3(light.pos);
printf("\nIntensité : %.2f", light.brightness); printf("\nIntensité : %.2f", light.brightness);
@@ -64,14 +70,16 @@ void print_light(t_light light) {
printf("\n"); printf("\n");
} }
void print_ambient(t_ambient amb) { void print_ambient(t_ambient amb)
{
printf("Ratio d'ambiance : %.2f\n", amb.ratio); printf("Ratio d'ambiance : %.2f\n", amb.ratio);
printf("Couleur ambiante : "); printf("Couleur ambiante : ");
print_vec3(amb.color); print_vec3(amb.color);
printf("\n"); printf("\n");
} }
void print_camera(t_camera cam) { void print_camera(t_camera cam)
{
printf("Position de la caméra : "); printf("Position de la caméra : ");
print_vec3(cam.camPos); print_vec3(cam.camPos);
printf("\nDirection : "); printf("\nDirection : ");
@@ -86,52 +94,53 @@ void print_camera(t_camera cam) {
printf("\n"); printf("\n");
} }
void print_scene(t_scene scene) { void print_scene(t_scene scene)
int i = 0; {
int i;
i = 0;
printf("=== SCENE ===\n"); printf("=== SCENE ===\n");
// Ambiance // Ambiance
printf("\n--- Ambiance ---\n"); printf("\n--- Ambiance ---\n");
print_ambient(scene.ambient); print_ambient(scene.ambient);
// Caméra // Caméra
if (scene.numCamera > 0) { if (scene.numCamera > 0)
{
printf("\n--- Caméra ---\n"); printf("\n--- Caméra ---\n");
print_camera(scene.camera); print_camera(scene.camera);
} }
// Sphères // Sphères
printf("\n--- Sphères (%d) ---\n", scene.numSpheres); printf("\n--- Sphères (%d) ---\n", scene.numSpheres);
i = 0; i = 0;
while (i < scene.numSpheres) { while (i < scene.numSpheres)
{
printf("\nSphère %d :\n", i); printf("\nSphère %d :\n", i);
print_sphere(scene.spheres[i]); print_sphere(scene.spheres[i]);
i++; i++;
} }
// Plans // Plans
printf("\n--- Plans (%d) ---\n", scene.numPlanes); printf("\n--- Plans (%d) ---\n", scene.numPlanes);
i = 0; i = 0;
while (i < scene.numPlanes) { while (i < scene.numPlanes)
{
printf("\nPlan %d :\n", i); printf("\nPlan %d :\n", i);
print_plane(scene.planes[i]); print_plane(scene.planes[i]);
i++; i++;
} }
// Cylindres // Cylindres
printf("\n--- Cylindres (%d) ---\n", scene.numCylinders); printf("\n--- Cylindres (%d) ---\n", scene.numCylinders);
i = 0; i = 0;
while (i < scene.numCylinders) { while (i < scene.numCylinders)
{
printf("\nCylindre %d :\n", i); printf("\nCylindre %d :\n", i);
print_cylinder(scene.cylinders[i]); print_cylinder(scene.cylinders[i]);
i++; i++;
} }
// Lumières // Lumières
printf("\n--- Lumières (%d) ---\n", scene.numLights); printf("\n--- Lumières (%d) ---\n", scene.numLights);
i = 0; i = 0;
while (i < scene.numLights) { while (i < scene.numLights)
{
printf("\nLumière %d :\n", i); printf("\nLumière %d :\n", i);
print_light(scene.lights[i]); print_light(scene.lights[i]);
i++; i++;
+2 -1
View File
@@ -35,7 +35,8 @@ static void render_pixel(t_app *app, int x, int y)
r = (unsigned char)(fminf(calc.color.x, 1.0f) * 255); r = (unsigned char)(fminf(calc.color.x, 1.0f) * 255);
g = (unsigned char)(fminf(calc.color.y, 1.0f) * 255); g = (unsigned char)(fminf(calc.color.y, 1.0f) * 255);
b = (unsigned char)(fminf(calc.color.z, 1.0f) * 255); b = (unsigned char)(fminf(calc.color.z, 1.0f) * 255);
app->pixels[y * app->win_width + x] = (255 << 24) | (r << 16) | (g << 8) | b; app->pixels[y * app->win_width
+ x] = (255 << 24) | (r << 16) | (g << 8) | b;
} }
void render_scene(t_app *app) void render_scene(t_app *app)
+43 -26
View File
@@ -13,64 +13,81 @@
#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, t_scene scene) { static bool checkShadowSphere(const t_ray shadowRay, float maxT, float epsilon,
int i = 0; t_scene scene)
{
int i;
t_vec3 dummy; t_vec3 dummy;
float t; float t;
while (i < scene.numSpheres) {
i = 0;
while (i < scene.numSpheres)
{
t = intersectSphere(shadowRay, scene.spheres[i], &dummy); t = intersectSphere(shadowRay, scene.spheres[i], &dummy);
if (t > epsilon && t < maxT) if (t > epsilon && t < maxT)
return true; return (true);
i++; i++;
} }
return false; return (false);
} }
// 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, t_scene scene) { static bool checkShadowPlane(const t_ray shadowRay, float maxT, float epsilon,
int i = 0; t_scene scene)
{
int i;
t_vec3 dummy; t_vec3 dummy;
float t; float t;
while (i < scene.numPlanes) {
i = 0;
while (i < scene.numPlanes)
{
t = intersectPlane(shadowRay, scene.planes[i], &dummy); t = intersectPlane(shadowRay, scene.planes[i], &dummy);
if (t > epsilon && t < maxT) if (t > epsilon && t < maxT)
return true; return (true);
i++; i++;
} }
return false; return (false);
} }
// Vérifie les intersections avec les cylindres // Vérifie les intersections avec les cylindres
static bool checkShadowCylinder(const t_ray shadowRay, float maxT, float epsilon, t_scene scene) { static bool checkShadowCylinder(const t_ray shadowRay, float maxT,
int i = 0; float epsilon, t_scene scene)
{
int i;
t_vec3 dummy; t_vec3 dummy;
float t; float t;
while (i < scene.numCylinders) {
i = 0;
while (i < scene.numCylinders)
{
t = intersectCylinder(shadowRay, scene.cylinders[i], &dummy); t = intersectCylinder(shadowRay, scene.cylinders[i], &dummy);
if (t > epsilon && t < maxT) if (t > epsilon && t < maxT)
return true; return (true);
i++; i++;
} }
return false; return (false);
} }
// 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 isInShadow(t_vec3 hitPoint, t_vec3 lightPos, t_scene scene)
{
const float epsilon = 1e-3f; const float epsilon = 1e-3f;
t_vec3 toLight = vec3_sub(lightPos, hitPoint); t_vec3 toLight;
float maxT = vec3_length(toLight); float maxT;
t_vec3 L = vec3_normalize(toLight); t_vec3 L;
t_ray shadowRay; t_ray shadowRay;
toLight = vec3_sub(lightPos, hitPoint);
maxT = vec3_length(toLight);
L = vec3_normalize(toLight);
shadowRay.origin = vec3_add(hitPoint, vec3_scale(L, epsilon)); shadowRay.origin = vec3_add(hitPoint, vec3_scale(L, epsilon));
shadowRay.dir = L; shadowRay.dir = L;
if (checkShadowSphere(shadowRay, maxT, epsilon, scene)) if (checkShadowSphere(shadowRay, maxT, epsilon, scene))
return true; return (true);
if (checkShadowPlane(shadowRay, maxT, epsilon, scene)) if (checkShadowPlane(shadowRay, maxT, epsilon, scene))
return true; return (true);
if (checkShadowCylinder(shadowRay, maxT, epsilon, scene)) if (checkShadowCylinder(shadowRay, maxT, epsilon, scene))
return true; return (true);
return (false);
return false;
} }
+87 -40
View File
@@ -12,68 +12,115 @@
#include "miniRT.h" #include "miniRT.h"
#include "miniRT.h" // Renvoie true si le rayon intersecte un objet, et met à jour tMin,
hitNormal et objColor
// Renvoie true si le rayon intersecte un objet, et met à jour tMin, hitNormal et objColor 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)
bool hit = false; {
bool hit;
float t; float t;
t_vec3 n; t_vec3 n;
int i = 0; int i;
while (i < scene.numSpheres) {
hit = false;
i = 0;
while (i < scene.numSpheres)
{
t = intersectSphere(ray, scene.spheres[i], &n); t = intersectSphere(ray, scene.spheres[i], &n);
if (t > 1e-3f && t < *tMin) { *tMin = t; *hitNormal = n; *objColor = scene.spheres[i].color; hit = true; } if (t > 1e-3f && t < *tMin)
{
*tMin = t;
*hitNormal = n;
*objColor = scene.spheres[i].color;
hit = true;
}
i++; i++;
} }
i = 0; i = 0;
while (i < scene.numPlanes) { while (i < scene.numPlanes)
{
t = intersectPlane(ray, scene.planes[i], &n); t = intersectPlane(ray, scene.planes[i], &n);
if (t > 1e-3f && t < *tMin) { *tMin = t; *hitNormal = n; *objColor = scene.planes[i].color; hit = true; } if (t > 1e-3f && t < *tMin)
{
*tMin = t;
*hitNormal = n;
*objColor = scene.planes[i].color;
hit = true;
}
i++; i++;
} }
i = 0; i = 0;
while (i < scene.numCylinders) { while (i < scene.numCylinders)
{
t = intersectCylinder(ray, scene.cylinders[i], &n); t = intersectCylinder(ray, scene.cylinders[i], &n);
if (t > 1e-3f && t < *tMin) { *tMin = t; *hitNormal = n; *objColor = scene.cylinders[i].color; hit = true; } if (t > 1e-3f && t < *tMin)
{
*tMin = t;
*hitNormal = n;
*objColor = scene.cylinders[i].color;
hit = true;
}
i++; i++;
} }
return hit; return (hit);
} }
// Calcule l'éclairage (ambiant, diffus et spéculaire) sur un point d'impact // 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_scene scene) { t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor,
t_vec3 color = vec3_scale(objColor, scene.ambient.ratio); t_scene scene)
int i = 0; {
while (i < scene.numLights) { t_vec3 color;
t_light light = scene.lights[i]; int i;
t_vec3 L = vec3_normalize(vec3_sub(light.pos, hitPoint)); t_light light;
if (!isInShadow(hitPoint, light.pos, scene)) { t_vec3 L;
float diff = fmaxf(0.0f, vec3_dot(hitNormal, L)); float diff;
t_vec3 viewDir = vec3_normalize(vec3_sub(scene.camera.camPos, hitPoint)); t_vec3 viewDir;
t_vec3 halfDir = vec3_normalize(vec3_add(L, viewDir)); t_vec3 halfDir;
float spec = powf(fmaxf(0.0f, vec3_dot(hitNormal, halfDir)), 32.0f); float spec;
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 (!isInShadow(hitPoint, 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), color = vec3_add(color, vec3_scale(vec3_mul(objColor, light.color),
diff * light.brightness)); diff * light.brightness));
color = vec3_add(color, vec3_scale((t_vec3){1, 1, 1}, color = vec3_add(color, vec3_scale((t_vec3){1, 1, 1}, spec
spec * light.brightness)); * light.brightness));
} }
i++; i++;
} }
if (color.x > 1.0f) color.x = 1.0f; if (color.x > 1.0f)
if (color.y > 1.0f) color.y = 1.0f; color.x = 1.0f;
if (color.z > 1.0f) color.z = 1.0f; if (color.y > 1.0f)
return color; color.y = 1.0f;
if (color.z > 1.0f)
color.z = 1.0f;
return (color);
} }
// Fonction principale de lancer de rayon (trace) // Fonction principale de lancer de rayon (trace)
t_vec3 trace(t_ray ray, t_scene scene) { t_vec3 trace(t_ray ray, t_scene scene)
float tMin = 1e9; {
t_vec3 hitNormal = {0, 0, 0}; float tMin;
t_vec3 objColor = {0, 0, 0}; t_vec3 hitNormal;
if (intersectObjects(ray, &tMin, &hitNormal, &objColor, scene)) { t_vec3 objColor;
t_vec3 hitPoint = vec3_add(ray.origin, vec3_scale(ray.dir, tMin)); t_vec3 hitPoint;
return calcLighting(hitPoint, hitNormal, objColor, scene);
}
return (t_vec3){0.2f, 0.7f, 1.0f}; // Couleur de fond (ciel)
}
tMin = 1e9;
hitNormal = {0, 0, 0};
objColor = {0, 0, 0};
if (intersectObjects(ray, &tMin, &hitNormal, &objColor, scene))
{
hitPoint = vec3_add(ray.origin, vec3_scale(ray.dir, tMin));
return (calcLighting(hitPoint, hitNormal, objColor, scene));
}
return ((t_vec3){0.2f, 0.7f, 1.0f}); // Couleur de fond (ciel)
}
+16 -8
View File
@@ -26,29 +26,37 @@ static void update_camera_rotation(t_app *app)
app->scene.camera.pitch = 1.57f - 0.01f; app->scene.camera.pitch = 1.57f - 0.01f;
if (app->scene.camera.pitch < -1.57f + 0.01f) if (app->scene.camera.pitch < -1.57f + 0.01f)
app->scene.camera.pitch = -1.57f + 0.01f; app->scene.camera.pitch = -1.57f + 0.01f;
app->scene.camera.camDir.x = cosf(app->scene.camera.pitch) * sinf(app->scene.camera.yaw); app->scene.camera.camDir.x = cosf(app->scene.camera.pitch)
* sinf(app->scene.camera.yaw);
app->scene.camera.camDir.y = sinf(app->scene.camera.pitch); app->scene.camera.camDir.y = sinf(app->scene.camera.pitch);
app->scene.camera.camDir.z = -cosf(app->scene.camera.pitch) * cosf(app->scene.camera.yaw); app->scene.camera.camDir.z = -cosf(app->scene.camera.pitch)
* cosf(app->scene.camera.yaw);
app->scene.camera.camDir = vec3_normalize(app->scene.camera.camDir); app->scene.camera.camDir = vec3_normalize(app->scene.camera.camDir);
app->scene.camera.right = (t_vec3){cosf(app->scene.camera.yaw), 0, sinf(app->scene.camera.yaw)}; app->scene.camera.right = (t_vec3){cosf(app->scene.camera.yaw), 0,
sinf(app->scene.camera.yaw)};
app->scene.camera.right = vec3_normalize(app->scene.camera.right); app->scene.camera.right = vec3_normalize(app->scene.camera.right);
app->scene.camera.up = vec3_normalize(vec3_cross(app->scene.camera.right, app->scene.camera.camDir)); app->scene.camera.up = vec3_normalize(vec3_cross(app->scene.camera.right,
app->scene.camera.camDir));
} }
static void update_camera_movement(t_app *app) static void update_camera_movement(t_app *app)
{ {
if (app->key_w) if (app->key_w)
app->scene.camera.camPos = vec3_add(app->scene.camera.camPos, app->scene.camera.camPos = vec3_add(app->scene.camera.camPos,
vec3_scale(app->scene.camera.camDir, app->scene.camera.move_speed)); vec3_scale(app->scene.camera.camDir,
app->scene.camera.move_speed));
if (app->key_s) if (app->key_s)
app->scene.camera.camPos = vec3_sub(app->scene.camera.camPos, app->scene.camera.camPos = vec3_sub(app->scene.camera.camPos,
vec3_scale(app->scene.camera.camDir, app->scene.camera.move_speed)); vec3_scale(app->scene.camera.camDir,
app->scene.camera.move_speed));
if (app->key_a) if (app->key_a)
app->scene.camera.camPos = vec3_sub(app->scene.camera.camPos, app->scene.camera.camPos = vec3_sub(app->scene.camera.camPos,
vec3_scale(app->scene.camera.right, app->scene.camera.move_speed)); vec3_scale(app->scene.camera.right,
app->scene.camera.move_speed));
if (app->key_d) if (app->key_d)
app->scene.camera.camPos = vec3_add(app->scene.camera.camPos, app->scene.camera.camPos = vec3_add(app->scene.camera.camPos,
vec3_scale(app->scene.camera.right, app->scene.camera.move_speed)); vec3_scale(app->scene.camera.right,
app->scene.camera.move_speed));
} }
void update_camera(t_app *app) void update_camera(t_app *app)