-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
executable file
·240 lines (189 loc) · 4.85 KB
/
Camera.cpp
File metadata and controls
executable file
·240 lines (189 loc) · 4.85 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <cmath>
#include "Camera.h"
#include "Algebra.h"
#include <iostream>
using namespace std;
// ----------
//
// PROTECTED
//
// ----------
void Camera::setUVW(const Vector &look, const Vector &up) {
// w first, since opp look
wb = Vector(look);
wb.negate();
wb.normalize();
// u next, cross up and w
ub = cross(up, wb);
ub.normalize();
// v last, cross w and u (or u and look + normalize)
vb = cross(wb, ub); // no need to normalize
}
double Camera::widthAngle() {
// width angle = aspect ratio * height angle
return GetScreenWidthRatio() * heightAngle;
}
// ----------
// NORMALIZING
// ----------
Matrix Camera::unhingeNorm() {
double c = -near / far;
return Matrix(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1.0f / (c + 1), c / (c + 1),
0, 0, -1, 0
);
}
Matrix Camera::scaleNorm() {
return Matrix(
1.0f / (tan(widthAngle() / 2) * far), 0, 0, 0,
0, 1.0f / (tan(heightAngle / 2) * far), 0, 0,
0, 0, 1.0f / far, 0,
0, 0, 0, 1
);
}
Matrix Camera::rotateNorm() {
return Matrix(
ub[0], ub[1], ub[2], 0,
vb[0], vb[1], vb[2], 0,
wb[0], wb[1], wb[2], 0,
0, 0, 0, 1
);
}
Matrix Camera::translateNorm() {
return Matrix(
1, 0, 0, -eye[0],
0, 1, 0, -eye[1],
0, 0, 1, -eye[2],
0, 0, 0, 1
);
}
// ----------
//
// PUBLIC
//
// ----------
Camera::Camera() {
Reset();
}
Camera::~Camera() {
}
void Camera::Reset() {
// set defaults, somewhat meaningless tho
Point eyeP = Point(2, 2, 2);
Vector look = Vector(-2, -2, -2);
Vector up = Vector(0, 1, 0);
Orient(eyeP, look, up);
heightAngle = 45;
near = 0.001;
far = 30;
}
// ----------
// SETTERS
// ----------
void Camera::Orient(Point& eye, Point& focus, Vector& up) {
// compute look from eye and focus
Vector look = focus - eye;
this->eye = Point(eye);
setUVW(look, up);
}
void Camera::Orient(Point& eye, Vector& look, Vector& up) {
this->eye = Point(eye);
setUVW(look, up);
}
void Camera::SetViewAngle (double viewAngle) {
heightAngle = DEG_TO_RAD(viewAngle);
}
void Camera::SetNearPlane (double nearPlane) {
near = nearPlane;
}
void Camera::SetFarPlane (double farPlane) {
far = farPlane;
}
void Camera::SetScreenSize (int screenWidth, int screenHeight) {
this->screenWidth = screenWidth;
this->screenHeight = screenHeight;
}
// ----------
// GETTERS
// ----------
Point Camera::GetEyePoint() {
return eye;
}
Vector Camera::GetLookVector() {
return -wb;
}
Vector Camera::GetUpVector() {
return vb; // no necessarily the same as what was passed in via Orient, but
// direction is what matters
}
double Camera::GetViewAngle() {
return RAD_TO_DEG(heightAngle);
}
double Camera::GetNearPlane() {
return near;
}
double Camera::GetFarPlane() {
return far;
}
int Camera::GetScreenWidth() {
return screenWidth;
}
int Camera::GetScreenHeight() {
return screenHeight;
}
// dist from near to far
double Camera::GetFilmPlaneDepth() {
return far - near;
}
// aspect ratio
double Camera::GetScreenWidthRatio() {
return (double)screenWidth / (double)screenHeight;
}
// TODO: 4 matrices, most likely
// project world onto film plane
// should return MppSuvw
Matrix Camera::GetProjectionMatrix() {
return unhingeNorm() * scaleNorm();
}
// TODO
// position camera relative to scene (or orient world relative to camera)
// should return Rxyz2uvw * Txyz
Matrix Camera::GetModelViewMatrix() {
return rotateNorm() * translateNorm();
}
// ----------
// GEOMETRY
// ----------
// rotate around v, yaw
void Camera::RotateV(double angle) {
Matrix rotV = rot_mat(vb, DEG_TO_RAD(angle)); // mtx to rotate around v
ub = rotV * ub;
wb = rotV * wb;
}
// rotate around u, pith
void Camera::RotateU(double angle) {
Matrix rotU = rot_mat(ub, DEG_TO_RAD(angle)); // mtx to rotate around u
vb = rotU * vb;
wb = rotU * wb;
}
// rotate around w, roll
void Camera::RotateW(double angle) {
Matrix rotW = rot_mat(wb, DEG_TO_RAD(angle));
ub = rotW * ub;
vb = rotW * vb;
}
// translate eye
// TODO: currently in world space
// maybe translate in camera space?
void Camera::Translate(const Vector &dir) {
eye = eye + dir;
}
// rotate u, v, w around arbitrary axis
void Camera::Rotate(Point p, Vector axis, double degrees) {
Matrix rot = rot_mat(p, axis, DEG_TO_RAD(degrees));
ub = rot * ub;
vb = rot * vb;
wb = rot * wb;
}