-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.h
52 lines (43 loc) · 1.73 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
#ifndef CAMERA_H
#define CAMERA_H
#include "ray.h"
#include <curand_kernel.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
__device__ Vector random_in_unit_disk(curandState* local_rand_state) {
Vector p;
do {
p = 2.f * Vector(curand_uniform(local_rand_state), curand_uniform(local_rand_state), 0) - Vector(1, 1, 0);
} while (dot(p, p) >= 1.f);
return p;
}
class Camera {
public:
Vector origin;
Vector lower_left_corner;
Vector horizontal;
Vector vertical;
Vector u, v, w;
double lens_radius;
__device__ Camera(Vector look_from, Vector look_at, Vector v_up, double v_fov, double aspect, double aperture, double focus_dist) {
// v_fov is top to bottom in degrees
lens_radius = aperture / 2.f;
double theta = v_fov * ((double)M_PI) / 180.f;
double half_height = tan(theta / 2.f);
double half_width = aspect * half_height;
origin = look_from;
w = unit_vector(look_from - look_at);
u = unit_vector(cross(v_up, w));
v = cross(w, u);
lower_left_corner = origin - half_width * focus_dist * u - half_height * focus_dist * v - focus_dist * w;
horizontal = 2.f * half_width * focus_dist * u;
vertical = 2.f * half_height * focus_dist * v;
}
__device__ Ray get_ray(float s, float t, curandState* local_rand_state) {
Vector rd = lens_radius * random_in_unit_disk(local_rand_state);
Vector offset = u * rd.x() + v * rd.y();
return Ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset);
}
};
#endif // CAMERA_H