forked from RayTracing/TheNextWeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.h
60 lines (54 loc) · 2.27 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
51
52
53
54
55
56
57
58
59
//==================================================================================================
// Written in 2016 by Peter Shirley <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related and
// neighboring rights to this software to the public domain worldwide. This software is distributed
// without any warranty.
//
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==================================================================================================
#ifndef CAMERAH
#define CAMERAH
#include "ray.h"
vec3 random_in_unit_disk() {
vec3 p;
do {
p = 2.0*vec3(drand48(),drand48(),0) - vec3(1,1,0);
} while (dot(p,p) >= 1.0);
return p;
}
class camera {
public:
// new: add t0 and t1
camera(vec3 lookfrom, vec3 lookat, vec3 vup, float vfov, float aspect, float aperture, float focus_dist, float t0, float t1) { // vfov is top to bottom in degrees
time0 = t0;
time1 = t1;
lens_radius = aperture / 2;
float theta = vfov*M_PI/180;
float half_height = tan(theta/2);
float half_width = aspect * half_height;
origin = lookfrom;
w = unit_vector(lookfrom - lookat);
u = unit_vector(cross(vup, w));
v = cross(w, u);
lower_left_corner = origin - half_width*focus_dist*u -half_height*focus_dist*v - focus_dist*w;
horizontal = 2*half_width*focus_dist*u;
vertical = 2*half_height*focus_dist*v;
}
// new: add time to construct ray
ray get_ray(float s, float t) {
vec3 rd = lens_radius*random_in_unit_disk();
vec3 offset = u * rd.x() + v * rd.y();
float time = time0 + drand48()*(time1-time0);
return ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset, time);
}
vec3 origin;
vec3 lower_left_corner;
vec3 horizontal;
vec3 vertical;
vec3 u, v, w;
float time0, time1; // new variables for shutter open/close times
float lens_radius;
};
#endif