-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.h
59 lines (53 loc) · 1.83 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
#ifndef CAMERAH
#define CAMERAH
#include <curand_kernel.h>
#include "ray.h"
#include "precision_types.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
__device__ vec3 random_in_unit_disk(curandState *local_rand_state) {
vec3 p;
do {
p = real_t(2.0f)*vec3(curand_uniform(local_rand_state),curand_uniform(local_rand_state),0) - vec3(1,1,0);
} while (dot(p,p) >= real_t(1.0f));
return p;
}
class camera {
public:
__device__ camera(vec3 lookfrom, vec3 lookat, vec3 vup, real_t vfov, real_t aspect, real_t aperture, real_t focus_dist) { // vfov is top to bottom in degrees
lens_radius = aperture / real_t(2.0f);
real_t theta = vfov*((real_t)M_PI)/real_t(180.0f);
// real_t half_height = tan(theta/2.0f);
real_t arg = theta/real_t(2.0f);
#ifdef __CUDA_ARCH__
#ifdef USE_FP16
real_t half_height = real_t(hsin(arg.val) / hcos(arg.val));
#else
real_t half_height = tan(arg);
#endif
#else
real_t half_height = tan(arg);
#endif
real_t 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 = real_t(2.0f)*half_width*focus_dist*u;
vertical = real_t(2.0f)*half_height*focus_dist*v;
}
__device__ ray get_ray(real_t s, real_t t, curandState *local_rand_state) {
vec3 rd = lens_radius*random_in_unit_disk(local_rand_state);
vec3 offset = u * rd.x() + v * rd.y();
return ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset);
}
vec3 origin;
vec3 lower_left_corner;
vec3 horizontal;
vec3 vertical;
vec3 u, v, w;
real_t lens_radius;
};
#endif