ray.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ray.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: salee2 <salee2@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/05/24 20:22:46 by salee2 #+# #+# */ 9 /* Updated: 2023/05/24 20:22:49 by salee2 ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "include/minirt.h" 14 15 t_ray ray_(t_point3 origin, t_vec3 dir) 16 { 17 t_ray this; 18 19 this.origin = origin; 20 this.dir = norm(dir); 21 return (this); 22 } 23 24 t_vec3 ray_at(double t, t_ray *ray) 25 { 26 t_vec3 this; 27 28 this = add(ray->origin, scl_mul(t, ray->dir)); 29 return (this); 30 } 31 32 t_ray primary_ray(t_camera *cam, t_vec2 xy) 33 { 34 t_ray this; 35 const t_vec3 ai = scl_mul(xy.x, cam->ai); 36 const t_vec3 bj = scl_mul(xy.y, cam->bj); 37 38 this.origin = cam->origin; 39 this.dir = norm(sub(add(add(cam->lower_left_corner, ai), bj), cam->origin)); 40 return (this); 41 } 42 43 int ray_color(t_scene *scene) 44 { 45 double t; 46 t_color3 color; 47 48 scene->hit = hit_(INFINITY); 49 if (hit_objects(scene->objects, &scene->ray, &scene->hit)) 50 color = phong_lighting(scene); 51 else 52 { 53 t = 0.5 * (scene->ray.dir.bj + 1.0); 54 color = add(scl_mul(1.0 - t, color3_(1.0, 1.0, 1.0)), scl_mul(t, color3_(0.5, 0.7, 1.0))); 55 } 56 return (create_trgb(color)); 57 } 58