-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.h
91 lines (74 loc) · 2.13 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include "game.h"
#include "ray.h"
#include <complex>
#include "precomp.h"
namespace Tmpl8
{
class camera
{
public:
camera(
vec3 lookFrom,
vec3 lookAt,
vec3 vup,
float vfov,
float aspect_ratio_of_cam,
float aperture,
float focus_dist)
{
auto theta = degrees_to_radians(vfov);
auto h = tanf(theta / 2.f);
auto viewport_height = 2.0f * h;
auto viewport_width = aspect_ratio_of_cam * viewport_height;
//complete orthonormal basis
w = unit_vector(lookFrom - lookAt);
u = unit_vector(cross(vup, w));
v = cross(w, u);
origin = lookFrom;
focus_distance = focus_dist;
horizontal = viewport_width * u * focus_dist;
vertical = viewport_height * v * focus_dist;
lower_left_corner = origin - horizontal / 2 - vertical / 2 - focus_dist * w;
lens_radius = aperture / 2;
}
void setCameraPosition(vec3 lookFrom, vec3 lookAt, vec3 Rotation, float vFOV, bool key_depthOfField)
{
const float theta = degrees_to_radians(vFOV);
float h = tanf(theta / 2.f);
float viewport_height = 2.0f * h;
float viewport_width = aspect_ratio * viewport_height;
defocusBlurON = key_depthOfField;
//complete orthonormal basis
w = unit_vector(lookFrom - lookAt);
u = unit_vector(cross(Rotation, w));
v = cross(w, u);
origin = lookFrom;
horizontal = viewport_width * u * focus_distance;
vertical = viewport_height * v * focus_distance;
lower_left_corner = origin - horizontal / 2 - vertical / 2 - w * focus_distance;
}
void setFocusDist(float newFocusDist) { focus_distance = newFocusDist; }
ray get_ray(float s, float t) const
{
vec3 rd = vec3(0.f);
vec3 offset = vec3(0.f);
if (defocusBlurON) {
rd = lens_radius * vec3::random_in_unit_sphere();
offset = u * rd.x + v * rd.y;
}
return ray(origin + offset, lower_left_corner + s * horizontal + t * vertical - origin - offset);
}
private:
bool defocusBlurON = false;
std::complex<float> mycomplex = (2.20f, 2.0f);
point3 origin;
point3 lower_left_corner;
vec3 horizontal;
vec3 vertical;
vec3 u, v, w;
float lens_radius;
float focus_distance;
float rW, rX, rY, rZ;
};
}