cone_base.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* cone_base.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: salee2 <salee2@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/05/24 20:21:32 by salee2 #+# #+# */ 9 /* Updated: 2023/05/24 20:21:33 by salee2 ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "include/minirt.h" 14 15 static double ray_disk_intersection(const t_cone *cone, t_ray *ray, \ 16 t_hit *hit) 17 { 18 double root; 19 double numerator; 20 double denominator; 21 22 denominator = dot(ray->dir, cone->h_normal); 23 if (fabs(denominator) < EPSILON) 24 return (INVALID_ROOT); 25 numerator = dot(sub(cone->base_center, ray->origin), cone->h_normal); 26 root = numerator / denominator; 27 if (root < hit->t_min || hit->t_max < root) 28 return (INVALID_ROOT); 29 if (mag_sq(sub(ray_at(root, ray), cone->base_center)) > cone->radius_sq) 30 return (INVALID_ROOT); 31 return (root); 32 } 33 34 t_bool intersect_cone_base(t_object *object, t_ray *ray, t_hit *hit) 35 { 36 const t_cone *cone = object->element; 37 const double root = ray_disk_intersection(cone, ray, hit); 38 39 if (root == INVALID_ROOT) 40 return (FALSE); 41 hit->t = root; 42 hit->t_max = hit->t; 43 hit->point = ray_at(hit->t, ray); 44 hit->normal = cone->h_normal; 45 if (dot(ray->dir, hit->normal) > 0) 46 hit->normal = scl_mul(-1, hit->normal); 47 hit->albedo = object->albedo; 48 return (TRUE); 49 }