update norm
This commit is contained in:
@@ -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/19 16:27:24 by yantoine ### ########.fr */
|
/* Updated: 2025/02/19 16:37:07 by yantoine ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -95,6 +95,13 @@ typedef struct s_hit
|
|||||||
t_vec3 color;
|
t_vec3 color;
|
||||||
} t_hit;
|
} t_hit;
|
||||||
|
|
||||||
|
typedef struct s_hit_info {
|
||||||
|
t_vec3 point;
|
||||||
|
t_vec3 normal;
|
||||||
|
t_vec3 color;
|
||||||
|
} t_hit_info;
|
||||||
|
|
||||||
|
|
||||||
typedef struct s_camera
|
typedef struct s_camera
|
||||||
{
|
{
|
||||||
t_vec3 camPos;
|
t_vec3 camPos;
|
||||||
|
|||||||
@@ -284,7 +284,8 @@ bpp3 minilibx-linux/test/main.c /^int bpp3;$/;" v typeref:typename:int
|
|||||||
bpp4 minilibx-linux/test/main.c /^int bpp4;$/;" v typeref:typename:int
|
bpp4 minilibx-linux/test/main.c /^int bpp4;$/;" v typeref:typename:int
|
||||||
brightness miniRT.h /^ float brightness;$/;" m struct:s_light typeref:typename:float
|
brightness miniRT.h /^ float brightness;$/;" m struct:s_light typeref:typename:float
|
||||||
c miniRT.h /^ float c;$/;" m struct:s_calc typeref:typename:float
|
c miniRT.h /^ float c;$/;" m struct:s_calc typeref:typename:float
|
||||||
calc_lighting trace.c /^t_vec3 calc_lighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor,$/;" f typeref:typename:t_vec3
|
calc_light_contribution trace.c /^static t_vec3 calc_light_contribution(t_hit_info hit, t_light light, t_camera camera, t_scene sc/;" f typeref:typename:t_vec3 file:
|
||||||
|
calc_lighting trace.c /^t_vec3 calc_lighting(t_vec3 hit_point, t_vec3 hit_normal, t_vec3 obj_color, t_scene scene)$/;" f typeref:typename:t_vec3
|
||||||
camDir miniRT.h /^ t_vec3 camDir;$/;" m struct:s_camera typeref:typename:t_vec3
|
camDir miniRT.h /^ t_vec3 camDir;$/;" m struct:s_camera typeref:typename:t_vec3
|
||||||
camPos miniRT.h /^ t_vec3 camPos;$/;" m struct:s_camera typeref:typename:t_vec3
|
camPos miniRT.h /^ t_vec3 camPos;$/;" m struct:s_camera typeref:typename:t_vec3
|
||||||
camera miniRT.h /^ t_camera camera;$/;" m struct:s_scene typeref:typename:t_camera
|
camera miniRT.h /^ t_camera camera;$/;" m struct:s_scene typeref:typename:t_camera
|
||||||
@@ -297,6 +298,7 @@ check_shadow_plane shadows.c /^static bool check_shadow_plane(const t_ray shadow
|
|||||||
check_shadow_sphere shadows.c /^static bool check_shadow_sphere(const t_ray shadow_ray, float max_t,$/;" f typeref:typename:bool file:
|
check_shadow_sphere shadows.c /^static bool check_shadow_sphere(const t_ray shadow_ray, float max_t,$/;" 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
|
||||||
|
clamp_color trace.c /^static t_vec3 clamp_color(t_vec3 color)$/;" f typeref:typename:t_vec3 file:
|
||||||
clean libft/Makefile /^clean:$/;" t
|
clean libft/Makefile /^clean:$/;" t
|
||||||
clean makefile /^clean:$/;" t
|
clean makefile /^clean:$/;" t
|
||||||
clean minilibx-linux/Makefile /^clean :$/;" t
|
clean minilibx-linux/Makefile /^clean :$/;" t
|
||||||
@@ -312,6 +314,7 @@ 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_plane typeref:typename:t_vec3
|
||||||
color miniRT.h /^ t_vec3 color;$/;" m struct:s_sphere typeref:typename:t_vec3
|
color miniRT.h /^ t_vec3 color;$/;" m struct:s_sphere typeref:typename:t_vec3
|
||||||
color miniRT.h /^ t_vec3 color;$/;" m struct:s_hit typeref:typename:t_vec3
|
color miniRT.h /^ t_vec3 color;$/;" m struct:s_hit typeref:typename:t_vec3
|
||||||
|
color miniRT.h /^ t_vec3 color;$/;" m struct:s_hit_info typeref:typename:t_vec3
|
||||||
color minilibx-linux/mlx_int.h /^ int color;$/;" m struct:s_col_name typeref:typename:int
|
color minilibx-linux/mlx_int.h /^ int color;$/;" m struct:s_col_name typeref:typename:int
|
||||||
color_map_1 minilibx-linux/test/main.c /^int color_map_1(void *win,int w,int h)$/;" f typeref:typename:int
|
color_map_1 minilibx-linux/test/main.c /^int color_map_1(void *win,int w,int h)$/;" f typeref:typename:int
|
||||||
color_map_2 minilibx-linux/test/main.c /^int color_map_2(unsigned char *data,int bpp,int sl,int w,int h,int endian, int type)$/;" f typeref:typename:int
|
color_map_2 minilibx-linux/test/main.c /^int color_map_2(unsigned char *data,int bpp,int sl,int w,int h,int endian, int type)$/;" f typeref:typename:int
|
||||||
@@ -568,6 +571,7 @@ ndc_y miniRT.h /^ float ndc_y;$/;" m struct:s_calc typeref:typename:float
|
|||||||
next minilibx-linux/mlx_int.h /^ struct s_win_list *next;$/;" m struct:s_win_list typeref:struct:s_win_list *
|
next minilibx-linux/mlx_int.h /^ struct s_win_list *next;$/;" m struct:s_win_list typeref:struct:s_win_list *
|
||||||
normal miniRT.h /^ t_vec3 normal;$/;" m struct:s_plane typeref:typename:t_vec3
|
normal miniRT.h /^ t_vec3 normal;$/;" m struct:s_plane typeref:typename:t_vec3
|
||||||
normal miniRT.h /^ t_vec3 normal;$/;" m struct:s_hit typeref:typename:t_vec3
|
normal miniRT.h /^ t_vec3 normal;$/;" m struct:s_hit typeref:typename:t_vec3
|
||||||
|
normal miniRT.h /^ t_vec3 normal;$/;" m struct:s_hit_info typeref:typename:t_vec3
|
||||||
numAmbient miniRT.h /^ int numAmbient;$/;" m struct:s_scene typeref:typename:int
|
numAmbient miniRT.h /^ int numAmbient;$/;" m struct:s_scene typeref:typename:int
|
||||||
numCamera miniRT.h /^ int numCamera;$/;" m struct:s_scene typeref:typename:int
|
numCamera miniRT.h /^ int numCamera;$/;" m struct:s_scene typeref:typename:int
|
||||||
numCylinders miniRT.h /^ int numCylinders;$/;" m struct:s_scene typeref:typename:int
|
numCylinders miniRT.h /^ int numCylinders;$/;" m struct:s_scene typeref:typename:int
|
||||||
@@ -597,6 +601,7 @@ pix minilibx-linux/mlx_int.h /^ Pixmap pix;$/;" m struct:s_img typeref:typenam
|
|||||||
pixels miniRT.h /^ int *pixels;$/;" m struct:s_app typeref:typename:int *
|
pixels miniRT.h /^ int *pixels;$/;" m struct:s_app typeref:typename:int *
|
||||||
planes miniRT.h /^ t_plane planes[MAX_PLANES];$/;" m struct:s_scene typeref:typename:t_plane[]
|
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
|
point miniRT.h /^ t_vec3 point;$/;" m struct:s_plane typeref:typename:t_vec3
|
||||||
|
point miniRT.h /^ t_vec3 point;$/;" m struct:s_hit_info typeref:typename:t_vec3
|
||||||
pos miniRT.h /^ t_vec3 pos;$/;" m struct:s_light typeref:typename:t_vec3
|
pos miniRT.h /^ t_vec3 pos;$/;" m struct:s_light typeref:typename:t_vec3
|
||||||
print_cylinder print_next.c /^void print_cylinder(t_cylinder cyl)$/;" f typeref:typename:void
|
print_cylinder print_next.c /^void print_cylinder(t_cylinder cyl)$/;" f typeref:typename:void
|
||||||
print_plane print_next.c /^void print_plane(t_plane plane)$/;" f typeref:typename:void
|
print_plane print_next.c /^void print_plane(t_plane plane)$/;" f typeref:typename:void
|
||||||
@@ -634,6 +639,7 @@ s_col_name minilibx-linux/mlx_int.h /^struct s_col_name$/;" s
|
|||||||
s_cylinder miniRT.h /^typedef struct s_cylinder$/;" s
|
s_cylinder miniRT.h /^typedef struct s_cylinder$/;" s
|
||||||
s_event_list minilibx-linux/mlx_int.h /^typedef struct s_event_list$/;" s
|
s_event_list minilibx-linux/mlx_int.h /^typedef struct s_event_list$/;" s
|
||||||
s_hit miniRT.h /^typedef struct s_hit$/;" s
|
s_hit miniRT.h /^typedef struct s_hit$/;" s
|
||||||
|
s_hit_info miniRT.h /^typedef struct s_hit_info {$/;" s
|
||||||
s_img minilibx-linux/mlx_int.h /^typedef struct s_img$/;" s
|
s_img minilibx-linux/mlx_int.h /^typedef struct s_img$/;" s
|
||||||
s_info libft/libft.h /^struct s_info$/;" s
|
s_info libft/libft.h /^struct s_info$/;" s
|
||||||
s_light miniRT.h /^typedef struct s_light$/;" s
|
s_light miniRT.h /^typedef struct s_light$/;" s
|
||||||
@@ -683,6 +689,7 @@ t_cylinder miniRT.h /^} t_cylinder;$/;" t typeref:struct:s_cylinder
|
|||||||
t_event_list minilibx-linux/mlx_int.h /^} t_event_list;$/;" t typeref:struct:s_event_list
|
t_event_list minilibx-linux/mlx_int.h /^} t_event_list;$/;" t typeref:struct:s_event_list
|
||||||
t_final miniRT.h /^ float t_final;$/;" m struct:s_calc typeref:typename:float
|
t_final miniRT.h /^ float t_final;$/;" m struct:s_calc typeref:typename:float
|
||||||
t_hit miniRT.h /^} t_hit;$/;" t typeref:struct:s_hit
|
t_hit miniRT.h /^} t_hit;$/;" t typeref:struct:s_hit
|
||||||
|
t_hit_info miniRT.h /^} t_hit_info;$/;" t typeref:struct:s_hit_info
|
||||||
t_img minilibx-linux/mlx_int.h /^} t_img;$/;" t typeref:struct:s_img
|
t_img minilibx-linux/mlx_int.h /^} t_img;$/;" t typeref:struct:s_img
|
||||||
t_info libft/libft.h /^typedef struct s_info t_info;$/;" t typeref:struct:s_info
|
t_info libft/libft.h /^typedef struct s_info t_info;$/;" t typeref:struct:s_info
|
||||||
t_light miniRT.h /^} t_light;$/;" t typeref:struct:s_light
|
t_light miniRT.h /^} t_light;$/;" t typeref:struct:s_light
|
||||||
|
|||||||
@@ -6,63 +6,71 @@
|
|||||||
/* 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/19 16:30:27 by yantoine ### ########.fr */
|
/* Updated: 2025/02/19 16:37:40 by yantoine ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "miniRT.h"
|
#include "miniRT.h"
|
||||||
|
|
||||||
// Calcule l'éclairage (ambiant, diffus et spéculaire) sur un point d'impact
|
static t_vec3 clamp_color(t_vec3 color)
|
||||||
t_vec3 calc_lighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor,
|
|
||||||
t_scene scene)
|
|
||||||
{
|
{
|
||||||
int i;
|
if (color.x > 1.0f)
|
||||||
t_calc calc;
|
color.x = 1.0f;
|
||||||
|
if (color.y > 1.0f)
|
||||||
|
color.y = 1.0f;
|
||||||
|
if (color.z > 1.0f)
|
||||||
|
color.z = 1.0f;
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
calc.color = vec3_scale(objColor, scene.ambient.ratio);
|
static t_vec3 calc_light_contribution(t_hit_info hit, t_light light, t_camera camera, t_scene scene)
|
||||||
i = 0;
|
{
|
||||||
while (i < scene.numLights)
|
t_vec3 l = vec3_normalize(vec3_sub(light.pos, hit.point)); // Variable 1
|
||||||
{
|
if (is_in_shadow(hit.point, light.pos, scene))
|
||||||
calc.light = scene.lights[i];
|
return (t_vec3){0, 0, 0};
|
||||||
calc.l = vec3_normalize(vec3_sub(calc.light.pos, hitPoint));
|
float diff = fmaxf(0.0f, vec3_dot(hit.normal, l)); // Variable 2
|
||||||
if (!is_in_shadow(hitPoint, calc.light.pos, scene))
|
t_vec3 view_dir = vec3_normalize(vec3_sub(camera.camPos, hit.point)); // Variable 3
|
||||||
{
|
t_vec3 half_dir = vec3_normalize(vec3_add(l, view_dir)); // Variable 4
|
||||||
calc.diff = fmaxf(0.0f, vec3_dot(hitNormal, calc.l));
|
float spec = powf(fmaxf(0.0f, vec3_dot(hit.normal, half_dir)), 32.0f); // Variable 5
|
||||||
calc.view_dir = vec3_normalize(vec3_sub(scene.camera.camPos, hitPoint));
|
return vec3_add(
|
||||||
calc.half_dir = vec3_normalize(vec3_add(calc.l, calc.view_dir));
|
vec3_scale(vec3_mul(hit.color, light.color), diff * light.brightness),
|
||||||
calc.spec = powf(fmaxf(0.0f, vec3_dot(hitNormal, calc.half_dir)), 32.0f);
|
vec3_scale((t_vec3){1, 1, 1}, spec * light.brightness));
|
||||||
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
|
t_vec3 calc_lighting(t_vec3 hit_point, t_vec3 hit_normal, t_vec3 obj_color, t_scene scene)
|
||||||
* calc.light.brightness));
|
{
|
||||||
}
|
t_vec3 color = vec3_scale(obj_color, scene.ambient.ratio); // Variable 1
|
||||||
i++;
|
t_hit_info hit; // Variable 2
|
||||||
}
|
hit.point = hit_point;
|
||||||
if (calc.color.x > 1.0f)
|
hit.normal = hit_normal;
|
||||||
calc.color.x = 1.0f;
|
hit.color = obj_color;
|
||||||
if (calc.color.y > 1.0f)
|
int i = 0; // Variable 3
|
||||||
calc.color.y = 1.0f;
|
while (i < scene.numLights)
|
||||||
if (calc.color.z > 1.0f)
|
{
|
||||||
calc.color.z = 1.0f;
|
color = vec3_add(color,
|
||||||
return (calc.color);
|
calc_light_contribution(hit, scene.lights[i], scene.camera, scene));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return clamp_color(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;
|
float t_min;
|
||||||
t_vec3 hitNormal;
|
t_vec3 hit_normal;
|
||||||
t_vec3 objColor;
|
t_vec3 obj_color;
|
||||||
t_vec3 hitPoint;
|
t_vec3 hit_point;
|
||||||
|
|
||||||
tMin = 1e9;
|
t_min = 1e9;
|
||||||
hitNormal = (t_vec3){0, 0, 0};
|
hit_normal = (t_vec3){0, 0, 0};
|
||||||
objColor = (t_vec3){0, 0, 0};
|
obj_color = (t_vec3){0, 0, 0};
|
||||||
scene.ray = ray;
|
scene.ray = ray;
|
||||||
if (intersect_objects(&tMin, &hitNormal, &objColor, scene))
|
if (intersect_objects(&t_min, &hit_normal, &obj_color, scene))
|
||||||
{
|
{
|
||||||
hitPoint = vec3_add(scene.ray.origin, vec3_scale(scene.ray.dir, tMin));
|
hit_point = vec3_add(scene.ray.origin, \
|
||||||
return (calc_lighting(hitPoint, hitNormal, objColor, scene));
|
vec3_scale(scene.ray.dir, t_min));
|
||||||
|
return (calc_lighting(hit_point, hit_normal, obj_color, scene));
|
||||||
}
|
}
|
||||||
return ((t_vec3){0, 0, 0}); // Couleur de fond (ciel)
|
return ((t_vec3){0, 0, 0});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user