-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.h
50 lines (40 loc) · 1.22 KB
/
Camera.h
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
50
#pragma once
#include "utils.h"
class Camera {
public:
Camera(
Point3 lookfrom,
Point3 lookat,
Vec3 vup,
double vfov,
double aspect_ratio,
double aperture,
double focus_dist
) {
// Camera ( right handed coordinate system )
double theta = degrees_to_radians(vfov);
double h = focus_dist * tan(theta / 2);
double viewport_height = 2.0 * h;
double viewport_width = aspect_ratio * viewport_height;
w = unit_vector(lookfrom - lookat);
u = unit_vector(cross(vup, w));
v = cross(w, u);
origin = lookfrom;
horizontal = viewport_width * u;
vertical = viewport_height * v;
lower_left_corner = origin - horizontal / 2 - vertical / 2 - focus_dist * w;
lens_radius = aperture / 2;
}
Ray get_ray(double s, double t) const {
Vec3 rd = lens_radius * random_in_unit_disk();
Vec3 offset = u * rd.x() + v * rd.y();
return Ray(origin + offset, lower_left_corner + s * horizontal + t * vertical - origin - offset);
}
private:
Point3 origin;
Point3 lower_left_corner;
Vec3 horizontal;
Vec3 vertical;
Vec3 u, v, w;
double lens_radius;
};