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