camera.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* camera.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: salee2 <salee2@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/05/24 20:20:48 by salee2 #+# #+# */ 9 /* Updated: 2023/05/24 20:20:51 by salee2 ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "include/minirt.h" 14 15 double degree_to_radian(double degree) 16 { 17 return (degree * M_PI / 180.0); 18 } 19 20 double get_width(double h_fov) 21 { 22 const double theta = degree_to_radian(h_fov); 23 const double width = tan(theta / 2); 24 25 return (width); 26 } 27 28 t_camera camera_(t_canvas *canvas, t_point3 look_from, t_point3 look_at, \ 29 double h_fov) 30 { 31 t_camera this; 32 const t_vec3 jup = vec3_(0, 1, 0); 33 const t_vec3 k = norm(sub(look_from, look_at)); 34 const t_vec3 i = norm(cross(jup, k)); 35 const t_vec3 j = cross(k, i); 36 37 this.viewport_w = 2.0 * get_width(h_fov); 38 this.viewport_h = this.viewport_w / canvas->aspect_ratio; 39 this.origin = look_from; 40 this.ai = scl_mul(this.viewport_w, i); 41 this.bj = scl_mul(this.viewport_h, j); 42 this.lower_left_corner = sub(sub(sub(this.origin, \ 43 scl_mul(1.0 / 2.0, this.ai)), scl_mul(1.0 / 2.0, this.bj)), k); 44 return (this); 45 }