-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_ray.c
49 lines (45 loc) · 1.78 KB
/
camera_ray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* camera_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shongou <shongou@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 23:32:58 by shongou #+# #+# */
/* Updated: 2023/01/04 23:33:01 by shongou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
static void get_basis_vector(t_vec *esx, t_vec *esy, t_vec cam_dir)
{
if (cam_dir.x == 0 && cam_dir.z == 0 && cam_dir.y)
{
esx->x = 1.0;
esx->y = 0.0;
esx->z = 0.0;
esy->x = 0.0;
esy->y = 0.0;
esy->z = 1.0;
return ;
}
esx->x = cam_dir.z / sqrt(pow(cam_dir.z, 2) + pow(cam_dir.x, 2));
esx->y = 0.0;
esx->z = -(cam_dir.x) / sqrt(pow(cam_dir.z, 2) + pow(cam_dir.x, 2));
*esx = normalize(*esx);
*esy = normalize(cross(cam_dir, *esx));
}
t_ray get_camera_ray(double x, double y, t_camera camera)
{
t_ray ray;
t_vec screen_pos;
t_vec esx;
t_vec esy;
double distance;
distance = (WIDTH / 2.0) / tan(camera.fov / 2 * M_PI / 180.0);
get_basis_vector(&esx, &esy, camera.orientation);
screen_pos = add(mul(x - (WIDTH / 2), esx), mul((HEIGHT / 2 - y), esy));
ray.start = camera.pos;
ray.direction \
= normalize(add(screen_pos, mul(distance, camera.orientation)));
return (ray);
}