diff --git a/miniRT b/miniRT index 88699d4..a4eb0e8 100755 Binary files a/miniRT and b/miniRT differ diff --git a/miniRT.h b/miniRT.h index 81392e7..d74ef76 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/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_hit; +typedef struct s_hit_info { + t_vec3 point; + t_vec3 normal; + t_vec3 color; +} t_hit_info; + + typedef struct s_camera { t_vec3 camPos; diff --git a/tags b/tags index 7da3771..e729cf1 100644 --- a/tags +++ b/tags @@ -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 brightness miniRT.h /^ float brightness;$/;" m struct:s_light 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 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 @@ -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_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 +clamp_color trace.c /^static t_vec3 clamp_color(t_vec3 color)$/;" f typeref:typename:t_vec3 file: clean libft/Makefile /^clean:$/;" t clean 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_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_info typeref:typename:t_vec3 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_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 * 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_info typeref:typename:t_vec3 numAmbient miniRT.h /^ int numAmbient;$/;" 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 @@ -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 * 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_hit_info 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_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_event_list minilibx-linux/mlx_int.h /^typedef struct s_event_list$/;" 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_info libft/libft.h /^struct s_info$/;" 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_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_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_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 diff --git a/trace.c b/trace.c index 649e837..f65fb01 100644 --- a/trace.c +++ b/trace.c @@ -6,63 +6,71 @@ /* 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" -// Calcule l'éclairage (ambiant, diffus et spéculaire) sur un point d'impact -t_vec3 calc_lighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor, - t_scene scene) +static t_vec3 clamp_color(t_vec3 color) { - int i; - t_calc calc; + 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; +} - calc.color = vec3_scale(objColor, scene.ambient.ratio); - i = 0; - while (i < scene.numLights) - { - calc.light = scene.lights[i]; - calc.l = vec3_normalize(vec3_sub(calc.light.pos, hitPoint)); - if (!is_in_shadow(hitPoint, calc.light.pos, scene)) - { - 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 (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); +static t_vec3 calc_light_contribution(t_hit_info hit, t_light light, t_camera camera, t_scene scene) +{ + t_vec3 l = vec3_normalize(vec3_sub(light.pos, hit.point)); // Variable 1 + if (is_in_shadow(hit.point, light.pos, scene)) + return (t_vec3){0, 0, 0}; + float diff = fmaxf(0.0f, vec3_dot(hit.normal, l)); // Variable 2 + 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 + float spec = powf(fmaxf(0.0f, vec3_dot(hit.normal, half_dir)), 32.0f); // Variable 5 + return vec3_add( + vec3_scale(vec3_mul(hit.color, light.color), diff * light.brightness), + vec3_scale((t_vec3){1, 1, 1}, spec * light.brightness)); +} + +t_vec3 calc_lighting(t_vec3 hit_point, t_vec3 hit_normal, t_vec3 obj_color, t_scene scene) +{ + t_vec3 color = vec3_scale(obj_color, scene.ambient.ratio); // Variable 1 + t_hit_info hit; // Variable 2 + hit.point = hit_point; + hit.normal = hit_normal; + hit.color = obj_color; + int i = 0; // Variable 3 + while (i < scene.numLights) + { + color = vec3_add(color, + calc_light_contribution(hit, scene.lights[i], scene.camera, scene)); + i++; + } + return clamp_color(color); } // Fonction principale de lancer de rayon (trace) t_vec3 trace(t_ray ray, t_scene scene) { - float tMin; - t_vec3 hitNormal; - t_vec3 objColor; - t_vec3 hitPoint; + float t_min; + t_vec3 hit_normal; + t_vec3 obj_color; + t_vec3 hit_point; - tMin = 1e9; - hitNormal = (t_vec3){0, 0, 0}; - objColor = (t_vec3){0, 0, 0}; + t_min = 1e9; + hit_normal = (t_vec3){0, 0, 0}; + obj_color = (t_vec3){0, 0, 0}; 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)); - return (calc_lighting(hitPoint, hitNormal, objColor, scene)); + hit_point = vec3_add(scene.ray.origin, \ + 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}); }