diff --git a/intersect_objects.c b/intersect_objects.c index 882e283..4e98b29 100644 --- a/intersect_objects.c +++ b/intersect_objects.c @@ -6,23 +6,23 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/19 16:07:55 by yantoine #+# #+# */ -/* Updated: 2025/02/19 16:16:52 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:47:28 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" static bool intersect_spheres(const t_ray ray, t_hit *hit, t_sphere *spheres, - int numSpheres) + int num_spheres) { int i; - bool hitAny; - t_vec3 n; + bool hit_any; + t_vec3 n; float t; i = 0; - hitAny = false; - while (i < numSpheres) + hit_any = false; + while (i < num_spheres) { t = intersect_sphere(ray, spheres[i], &n); if (t > 1e-3f && t < hit->t) @@ -30,24 +30,24 @@ static bool intersect_spheres(const t_ray ray, t_hit *hit, t_sphere *spheres, hit->t = t; hit->normal = n; hit->color = spheres[i].color; - hitAny = true; + hit_any = true; } i++; } - return (hitAny); + return (hit_any); } static bool intersect_planes(const t_ray ray, t_hit *hit, t_plane *planes, - int numPlanes) + int num_planes) { int i; - bool hitAny; - t_vec3 n; + bool hit_any; + t_vec3 n; float t; i = 0; - hitAny = false; - while (i < numPlanes) + hit_any = false; + while (i < num_planes) { t = intersect_plane(ray, planes[i], &n); if (t > 1e-3f && t < hit->t) @@ -55,24 +55,24 @@ static bool intersect_planes(const t_ray ray, t_hit *hit, t_plane *planes, hit->t = t; hit->normal = n; hit->color = planes[i].color; - hitAny = true; + hit_any = true; } i++; } - return (hitAny); + return (hit_any); } static bool intersect_cylinders(const t_ray ray, t_hit *hit, - t_cylinder *cylinders, int numCylinders) + t_cylinder *cylinders, int num_cylinders) { int i; - bool hitAny; - t_vec3 n; + bool hit_any; + t_vec3 n; float t; i = 0; - hitAny = false; - while (i < numCylinders) + hit_any = false; + while (i < num_cylinders) { t = intersect_cylinder(ray, cylinders[i], &n); if (t > 1e-3f && t < hit->t) @@ -80,33 +80,33 @@ static bool intersect_cylinders(const t_ray ray, t_hit *hit, hit->t = t; hit->normal = n; hit->color = cylinders[i].color; - hitAny = true; + hit_any = true; } i++; } - return (hitAny); + return (hit_any); } bool intersect_objects(float *tMin, t_vec3 *hitNormal, t_vec3 *objColor, t_scene scene) { t_hit hit; - bool hitFound; + bool hit_found; - hitFound = false; + hit_found = false; hit.t = *tMin; - if (intersect_spheres(scene.ray, &hit, scene.spheres, scene.numSpheres)) - hitFound = true; - if (intersect_planes(scene.ray, &hit, scene.planes, scene.numPlanes)) - hitFound = true; + if (intersect_spheres(scene.ray, &hit, scene.spheres, scene.num_spheres)) + hit_found = true; + if (intersect_planes(scene.ray, &hit, scene.planes, scene.num_planes)) + hit_found = true; if (intersect_cylinders(scene.ray, &hit, scene.cylinders, - scene.numCylinders)) - hitFound = true; - if (hitFound) + scene.num_cylinders)) + hit_found = true; + if (hit_found) { *tMin = hit.t; *hitNormal = hit.normal; *objColor = hit.color; } - return (hitFound); + return (hit_found); } diff --git a/miniRT b/miniRT index a4eb0e8..deee9a0 100755 Binary files a/miniRT and b/miniRT differ diff --git a/miniRT.h b/miniRT.h index d74ef76..85931dc 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:37:07 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:48:27 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -124,12 +124,12 @@ typedef struct s_scene t_ambient ambient; t_camera camera; t_ray ray; - int numSpheres; - int numPlanes; - int numCylinders; - int numCamera; - int numLights; - int numAmbient; + int num_spheres; + int num_planes; + int num_cylinders; + int num_camera; + int num_lights; + int num_ambient; char *line_if_exit; char **token_if_exit; int fd_if_exit; diff --git a/parsing_ambiant.c b/parsing_ambiant.c index 159ac8e..70c4a1d 100644 --- a/parsing_ambiant.c +++ b/parsing_ambiant.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/14 18:04:39 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:53:34 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:48:50 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,11 +16,11 @@ t_scene parsing_ambiant(t_scene scene) { char **tokens; - tokens = get_tokens_secure(scene, scene.numAmbient, MAX_AMBIENT, 3); + tokens = get_tokens_secure(scene, scene.num_ambient, MAX_AMBIENT, 3); scene.token_if_exit = tokens; scene.ambient.ratio = ft_atof(tokens[1]); scene.ambient.color = parse_color(tokens[2], scene); ft_free_array(tokens); - scene.numAmbient++; + scene.num_ambient++; return (scene); } diff --git a/parsing_camera.c b/parsing_camera.c index 5dd63a3..5a396a3 100644 --- a/parsing_camera.c +++ b/parsing_camera.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/14 20:02:29 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:24:12 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:49:01 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ t_scene parsing_camera(t_scene scene) { char **tokens; - tokens = get_tokens_secure(scene, scene.numCamera, MAX_CAMERA, 4); + tokens = get_tokens_secure(scene, scene.num_camera, MAX_CAMERA, 4); scene.token_if_exit = tokens; scene.camera.camPos = parse_vector(tokens[1], scene); scene.camera.camDir = parse_vector_normalize(tokens[2], scene); @@ -24,6 +24,6 @@ t_scene parsing_camera(t_scene scene) scene.camera.yaw = atan2f(scene.camera.camDir.x, -scene.camera.camDir.z); scene.camera.pitch = asinf(scene.camera.camDir.y); ft_free_array(tokens); - scene.numCamera++; + scene.num_camera++; return (scene); } diff --git a/parsing_cylinder.c b/parsing_cylinder.c index 757d697..770c188 100644 --- a/parsing_cylinder.c +++ b/parsing_cylinder.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:54:13 by yantoine #+# #+# */ -/* Updated: 2025/02/18 17:24:08 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:53:04 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,17 @@ 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.num_cylinders, MAX_CYLINDERS, 6); scene.token_if_exit = tokens; - scene.cylinders[scene.numCylinders].center = parse_vector(tokens[1], scene); - scene.cylinders[scene.numCylinders].axis = parse_vector_normalize(tokens[2], + scene.cylinders[scene.num_cylinders].center = \ + parse_vector(tokens[1], scene); + scene.cylinders[scene.num_cylinders].axis = \ + parse_vector_normalize(tokens[2], scene); - scene.cylinders[scene.numCylinders].radius = ft_atof(tokens[3]); - scene.cylinders[scene.numCylinders].height = ft_atof(tokens[4]); - scene.cylinders[scene.numCylinders].color = parse_color(tokens[5], scene); + scene.cylinders[scene.num_cylinders].radius = ft_atof(tokens[3]); + scene.cylinders[scene.num_cylinders].height = ft_atof(tokens[4]); + scene.cylinders[scene.num_cylinders].color = parse_color(tokens[5], scene); ft_free_array(tokens); - scene.numCylinders++; + scene.num_cylinders++; return (scene); } diff --git a/parsing_light.c b/parsing_light.c index 9b06b15..cf580f1 100644 --- a/parsing_light.c +++ b/parsing_light.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:39:08 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:29:55 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:49:57 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,12 @@ t_scene parsing_light(t_scene scene) { char **tokens; - tokens = get_tokens_secure(scene, scene.numLights, MAX_LIGHTS, 4); + tokens = get_tokens_secure(scene, scene.num_lights, MAX_LIGHTS, 4); scene.token_if_exit = tokens; - scene.lights[scene.numLights].pos = parse_vector(tokens[1], scene); - scene.lights[scene.numLights].brightness = ft_atof(tokens[2]); - scene.lights[scene.numLights].color = parse_color(tokens[3], scene); + scene.lights[scene.num_lights].pos = parse_vector(tokens[1], scene); + scene.lights[scene.num_lights].brightness = ft_atof(tokens[2]); + scene.lights[scene.num_lights].color = parse_color(tokens[3], scene); ft_free_array(tokens); - scene.numLights++; + scene.num_lights++; return (scene); } diff --git a/parsing_plane.c b/parsing_plane.c index 9be62ad..60e2a99 100644 --- a/parsing_plane.c +++ b/parsing_plane.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:49:41 by yantoine #+# #+# */ -/* Updated: 2025/02/18 20:34:02 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:50:10 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,14 @@ t_scene parsing_plane(t_scene scene) { char **tokens; - tokens = get_tokens_secure(scene, scene.numPlanes, MAX_PLANES, 4); + tokens = get_tokens_secure(scene, scene.num_planes, MAX_PLANES, 4); scene.token_if_exit = tokens; - scene.planes[scene.numPlanes].point = parse_vector(tokens[1], scene); - scene.planes[scene.numPlanes].normal = parse_vector_normalize(tokens[2], + scene.planes[scene.num_planes].point = parse_vector(tokens[1], scene); + scene.planes[scene.num_planes].normal = parse_vector_normalize(tokens[2], scene); - scene.planes[scene.numPlanes].color = parse_color(tokens[3], scene); + scene.planes[scene.num_planes].color = parse_color(tokens[3], scene); ft_free_array(tokens); - scene.numPlanes++; + scene.num_planes++; return (scene); } diff --git a/parsing_sphere.c b/parsing_sphere.c index 19bacf0..46d8526 100644 --- a/parsing_sphere.c +++ b/parsing_sphere.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/15 19:46:16 by yantoine #+# #+# */ -/* Updated: 2025/02/18 20:39:35 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:50:19 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,13 @@ t_scene parsing_sphere(t_scene scene) { char **tokens; - tokens = get_tokens_secure(scene, scene.numSpheres, MAX_SPHERES, 4); + tokens = get_tokens_secure(scene, scene.num_spheres, MAX_SPHERES, 4); scene.token_if_exit = tokens; - scene.spheres[scene.numSpheres].center = parse_vector(tokens[1], scene); - scene.spheres[scene.numSpheres].radius = ft_atof(tokens[2]) / 2.0f; - scene.spheres[scene.numSpheres].color = parse_color(tokens[3], scene); + scene.spheres[scene.num_spheres].center = parse_vector(tokens[1], scene); + scene.spheres[scene.num_spheres].radius = ft_atof(tokens[2]) / 2.0f; + scene.spheres[scene.num_spheres].color = parse_color(tokens[3], scene); ft_free_array(tokens); - scene.numSpheres++; + scene.num_spheres++; return (scene); } diff --git a/shadows.c b/shadows.c index 86922d1..8dd5d95 100644 --- a/shadows.c +++ b/shadows.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 18:58:42 by yantoine #+# #+# */ -/* Updated: 2025/02/18 20:32:30 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:50:54 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ static bool check_shadow_sphere(const t_ray shadow_ray, float max_t, float t; i = 0; - while (i < scene.numSpheres) + while (i < scene.num_spheres) { t = intersect_sphere(shadow_ray, scene.spheres[i], &dummy); if (t > epsilon && t < max_t) @@ -42,7 +42,7 @@ static bool check_shadow_plane(const t_ray shadow_ray, float max_t, float t; i = 0; - while (i < scene.numPlanes) + while (i < scene.num_planes) { t = intersect_plane(shadow_ray, scene.planes[i], &dummy); if (t > epsilon && t < max_t) @@ -62,7 +62,7 @@ static bool check_shadow_cylinder(const t_ray shadow_ray, float max_t, float t; i = 0; - while (i < scene.numCylinders) + while (i < scene.num_cylinders) { t = intersect_cylinder(shadow_ray, scene.cylinders[i], &dummy); if (t > epsilon && t < max_t) diff --git a/tags b/tags index e729cf1..686118f 100644 --- a/tags +++ b/tags @@ -284,8 +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_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 +calc_light_contribution trace.c /^static t_vec3 calc_light_contribution(t_hit_info hit, t_light light,$/;" 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,$/;" 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 @@ -298,7 +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: +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 @@ -572,12 +572,12 @@ next minilibx-linux/mlx_int.h /^ struct s_win_list *next;$/;" m struct:s_win_lis 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 -numLights miniRT.h /^ int numLights;$/;" m struct:s_scene typeref:typename:int -numPlanes miniRT.h /^ int numPlanes;$/;" m struct:s_scene typeref:typename:int -numSpheres miniRT.h /^ int numSpheres;$/;" m struct:s_scene typeref:typename:int +num_ambient miniRT.h /^ int num_ambient;$/;" m struct:s_scene typeref:typename:int +num_camera miniRT.h /^ int num_camera;$/;" m struct:s_scene typeref:typename:int +num_cylinders miniRT.h /^ int num_cylinders;$/;" m struct:s_scene typeref:typename:int +num_lights miniRT.h /^ int num_lights;$/;" m struct:s_scene typeref:typename:int +num_planes miniRT.h /^ int num_planes;$/;" m struct:s_scene typeref:typename:int +num_spheres miniRT.h /^ int num_spheres;$/;" m struct:s_scene typeref:typename:int oc miniRT.h /^ t_vec3 oc;$/;" m struct:s_calc typeref:typename:t_vec3 oc_dot_v miniRT.h /^ float oc_dot_v;$/;" m struct:s_calc typeref:typename:float oc_perp miniRT.h /^ t_vec3 oc_perp;$/;" m struct:s_calc typeref:typename:t_vec3 diff --git a/trace.c b/trace.c index f65fb01..f405e67 100644 --- a/trace.c +++ b/trace.c @@ -6,52 +6,64 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 19:07:07 by yantoine #+# #+# */ -/* Updated: 2025/02/19 16:37:40 by yantoine ### ########.fr */ +/* Updated: 2025/02/19 16:52:23 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #include "miniRT.h" -static t_vec3 clamp_color(t_vec3 color) +static t_vec3 clamp_color(t_vec3 color) { - 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 (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); } -static t_vec3 calc_light_contribution(t_hit_info hit, t_light light, t_camera camera, t_scene scene) +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 l; + float diff; + t_vec3 view_dir; + t_vec3 half_dir; + float spec; + + l = vec3_normalize(vec3_sub(light.pos, hit.point)); + if (is_in_shadow(hit.point, light.pos, scene)) + return ((t_vec3){0, 0, 0}); + diff = fmaxf(0.0f, vec3_dot(hit.normal, l)); + view_dir = vec3_normalize(vec3_sub(camera.camPos, hit.point)); + half_dir = vec3_normalize(vec3_add(l, view_dir)); + spec = powf(fmaxf(0.0f, vec3_dot(hit.normal, half_dir)), 32.0f); + 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 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); + t_vec3 color; + t_hit_info hit; + int i; + + color = vec3_scale(obj_color, scene.ambient.ratio); + hit.point = hit_point; + hit.normal = hit_normal; + hit.color = obj_color; + i = 0; + while (i < scene.num_lights) + { + 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) @@ -68,8 +80,8 @@ t_vec3 trace(t_ray ray, t_scene scene) scene.ray = ray; if (intersect_objects(&t_min, &hit_normal, &obj_color, scene)) { - hit_point = vec3_add(scene.ray.origin, \ - vec3_scale(scene.ray.dir, t_min)); + 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});