-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathCamera.cpp
150 lines (121 loc) · 4.01 KB
/
Camera.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// code for depth-of-field, mouse + keyboard user interaction based on https://github.com/peterkutz/GPUPathTracer
#include <math.h>
#include "Camera.h"
#include "CudaRenderKernel.h"
InteractiveCamera::InteractiveCamera()
{
centerPosition = Vec3f(0, 0, 0);
yaw = 0;
pitch = 0.3;
radius = 4;
apertureRadius = 0.04;
focalDistance = 4.0f;
resolution = Vec2f(scrwidth, scrheight);
fov = Vec2f(40, 40);
}
InteractiveCamera::~InteractiveCamera() {}
void InteractiveCamera::changeYaw(float m){
yaw += m;
fixYaw();
}
void InteractiveCamera::changePitch(float m){
pitch += m;
fixPitch();
}
void InteractiveCamera::changeRadius(float m){
radius += radius * m; // Change proportional to current radius. Assuming radius isn't allowed to go to zero.
fixRadius();
}
void InteractiveCamera::changeAltitude(float m){
centerPosition.y += m;
//fixCenterPosition();
}
void InteractiveCamera::goForward(float m){
centerPosition += viewDirection * m;
}
void InteractiveCamera::strafe(float m){
Vec3f strafeAxis = cross(viewDirection, Vec3f(0, 1, 0));
strafeAxis.normalize();
centerPosition += strafeAxis * m;
}
void InteractiveCamera::rotateRight(float m){
float yaw2 = yaw;
yaw2 += m;
float pitch2 = pitch;
float xDirection = sin(yaw2) * cos(pitch2);
float yDirection = sin(pitch2);
float zDirection = cos(yaw2) * cos(pitch2);
Vec3f directionToCamera = Vec3f(xDirection, yDirection, zDirection);
viewDirection = directionToCamera * (-1.0);
}
void InteractiveCamera::changeApertureDiameter(float m){
apertureRadius += (apertureRadius + 0.01) * m; // Change proportional to current apertureRadius.
fixApertureRadius();
}
void InteractiveCamera::changeFocalDistance(float m){
focalDistance += m;
fixFocalDistance();
}
void InteractiveCamera::setResolution(float x, float y){
resolution = Vec2f(x, y);
setFOVX(fov.x);
}
float radiansToDegrees(float radians) {
float degrees = radians * 180.0 / M_PI;
return degrees;
}
float degreesToRadians(float degrees) {
float radians = degrees / 180.0 * M_PI;
return radians;
}
void InteractiveCamera::setFOVX(float fovx){
fov.x = fovx;
fov.y = radiansToDegrees(atan(tan(degreesToRadians(fovx) * 0.5) * (resolution.y / resolution.x)) * 2.0);
// resolution float division
}
void InteractiveCamera::buildRenderCamera(Camera* renderCamera){
float xDirection = sin(yaw) * cos(pitch);
float yDirection = sin(pitch);
float zDirection = cos(yaw) * cos(pitch);
Vec3f directionToCamera = Vec3f(xDirection, yDirection, zDirection);
viewDirection = directionToCamera * (-1.0);
Vec3f eyePosition = centerPosition + directionToCamera * radius;
//Vec3f eyePosition = centerPosition; // rotate camera from stationary viewpoint
renderCamera->position = eyePosition;
renderCamera->view = viewDirection;
renderCamera->up = Vec3f(0, 1, 0);
renderCamera->resolution = Vec2f(resolution.x, resolution.y);
renderCamera->fov = Vec2f(fov.x, fov.y);
renderCamera->apertureRadius = apertureRadius;
renderCamera->focalDistance = focalDistance;
}
float mod(float x, float y) { // Does this account for -y ???
return x - y * floorf(x / y);
}
void InteractiveCamera::fixYaw() {
yaw = mod(yaw, 2 * M_PI); // Normalize the yaw.
}
float clamp2(float n, float low, float high) {
n = fminf(n, high);
n = fmaxf(n, low);
return n;
}
void InteractiveCamera::fixPitch() {
float padding = 0.05;
pitch = clamp2(pitch, -PI_OVER_TWO + padding, PI_OVER_TWO - padding); // Limit the pitch.
}
void InteractiveCamera::fixRadius() {
float minRadius = 0.2;
float maxRadius = 100.0;
radius = clamp2(radius, minRadius, maxRadius);
}
void InteractiveCamera::fixApertureRadius() {
float minApertureRadius = 0.0;
float maxApertureRadius = 25.0;
apertureRadius = clamp2(apertureRadius, minApertureRadius, maxApertureRadius);
}
void InteractiveCamera::fixFocalDistance() {
float minFocalDist = 0.2;
float maxFocalDist = 100.0;
focalDistance = clamp2(focalDistance, minFocalDist, maxFocalDist);
}