-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcamera.cpp
More file actions
312 lines (286 loc) · 8.2 KB
/
camera.cpp
File metadata and controls
312 lines (286 loc) · 8.2 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "components/camera.h"
#include "components/actor.h"
#include "components/transform.h"
#include "resources/texture.h"
#include "pipelinecontext.h"
#include "gizmos.h"
Camera *s_currentCamera = nullptr;
/*!
\class Camera
\brief The Camera represents the player's point of view; how the player sees the world.
\inmodule Components
*/
Camera::Camera() :
m_fov(45.0), // 2*arctan(height/(2*distance))
m_near(0.1f),
m_far(1000.0f),
m_ratio(1.0f),
m_focal(1.0f),
m_orthoSize(1.0f),
m_ortho(false) {
}
/*!
Returns view matrix for the camera.
*/
Matrix4 Camera::viewMatrix() const {
Transform *t = transform();
Matrix4 m;
m.translate(-t->worldPosition());
return Matrix4(t->worldQuaternion().toMatrix()).inverse() * m;
}
/*!
Returns projection matrix for the camera.
*/
Matrix4 Camera::projectionMatrix() const {
return m_projection;
}
/*!
Transforms position from \a worldSpace into screen space.
Returns result of transformation.
*/
Vector2 Camera::project(const Vector3 &worldSpace) {
Vector4 in(worldSpace.x, worldSpace.y, worldSpace.z, 1.0f);
Vector4 out(viewMatrix() * in);
in = m_projection * out;
if(in.w == 0.0f) {
return Vector2(); // false;
}
in.w = 1.0f / in.w;
in.x *= in.w;
in.y *= in.w;
in.z *= in.w;
return Vector2((in.x * 0.5f + 0.5f), (in.y * 0.5f + 0.5f));
}
/*!
Transforms position from \a screenSpace into world space.
Returns result of transformation.
*/
Vector3 Camera::unproject(const Vector3 &screenSpace) {
Matrix4 final((m_projection * viewMatrix()).inverse());
Vector4 in;
in.x = (screenSpace.x) * 2.0f - 1.0f;
in.y = (screenSpace.y) * 2.0f - 1.0f;
in.z = 2.0f * screenSpace.z - 1.0f;
in.w = 1.0f;
Vector4 out(final * in);
if(out.w == 0.0f) {
return Vector3(); // false
}
return Vector3(out.x / out.w, out.y / out.w, out.z / out.w);
}
/*!
Returns ray with origin point in camera position and direction to projection plane with \a x and \a y coordinates.
*/
Ray Camera::castRay(float x, float y) {
Transform *t = transform();
Vector3 p = t->worldPosition();
Vector3 dir = t->worldQuaternion() * Vector3(0.0, 0.0,-1.0);
dir.normalize();
Vector3 view;
if(m_ortho) {
p += t->worldQuaternion() * Vector3((x - 0.5f) * m_orthoSize * m_ratio,
(y - 0.5f) * m_orthoSize, 0.0f);
} else {
float tang = tan(m_fov * DEG2RAD);
Vector3 right = dir.cross(Vector3(0.0f, 1.0f, 0.0f));
Vector3 up = right.cross(dir);
view = Vector3((x - 0.5f) * tang * m_ratio) * right +
Vector3((y - 0.5f) * tang) * up + p + dir;
dir = (view - p);
dir.normalize();
}
return Ray(p, dir);
}
/*!
Returns field of view angle for the camera in degrees.
*/
float Camera::fov() const {
return m_fov;
}
/*!
Sets field of view \a angle for the camera in degrees.
\note Applicable only for the perspective mode.
*/
void Camera::setFov(const float angle) {
m_fov = angle;
recalcProjection();
}
/*!
Returns a distance to near cut plane.
*/
float Camera::nearPlane() const {
return m_near;
}
/*!
Sets a \a distance to near cut plane.
*/
void Camera::setNear(const float distance) {
m_near = distance;
recalcProjection();
}
/*!
Returns a distance to far cut plane.
*/
float Camera::farPlane() const {
return m_far;
}
/*!
Sets a \a distance to far cut plane.
*/
void Camera::setFar(const float distance) {
m_far = distance;
recalcProjection();
}
/*!
Returns the aspect ratio (width divided by height).
*/
float Camera::ratio() const {
return m_ratio;
}
/*!
Sets the aspect \a ratio (width divided by height).
*/
void Camera::setRatio(float ratio) {
m_ratio = ratio;
recalcProjection();
}
/*!
Returns a focal distance for the camera.
*/
float Camera::focal() const {
return m_focal;
}
/*!
Sets a \a focal distance for the camera.
*/
void Camera::setFocal(const float focal) {
m_focal = focal;
recalcProjection();
}
/*!
Returns the color with which the screen will be cleared.
*/
Vector4 Camera::color() const {
return m_color;
}
/*!
Sets the \a color with which the screen will be cleared.
*/
void Camera::setColor(const Vector4 color) {
m_color = color;
}
/*!
Returns camera size for orthographic mode.
*/
float Camera::orthoSize() const {
return m_orthoSize;
}
/*!
Sets camera \a size for orthographic mode.
*/
void Camera::setOrthoSize(const float size) {
m_orthoSize = size;
recalcProjection();
}
/*!
Returns true for the orthographic mode; for the perspective mode, returns false.
*/
bool Camera::orthographic() const {
return m_ortho;
}
/*!
Sets orthographic \a mode.
*/
void Camera::setOrthographic(const bool mode) {
m_ortho = mode;
recalcProjection();
}
/*!
Returns current active camera.
*/
Camera *Camera::current() {
return s_currentCamera;
}
/*!
Sets \a current active camera.
*/
void Camera::setCurrent(Camera *current) {
s_currentCamera = current;
}
/*!
Returns frustum corners for the \a camera.
*/
array<Vector3, 8> Camera::frustumCorners(const Camera &camera) {
Transform *t = camera.transform();
return Camera::frustumCorners(camera.m_ortho, (camera.m_ortho) ?
camera.m_orthoSize :
camera.m_fov,
camera.m_ratio,
t->worldPosition(), t->worldRotation(),
camera.m_near, camera.m_far);
}
/*!
Returns frustum corners with provided parameters.
This function accepts a list of parameters:
\a ortho is a flag that points orthographic or perspective camera.
\a sigma is an angle of frustum or ortho size in the case of an orthographic camera.
\a ratio is an aspect ratio.
\a position of the frustum in world space.
\a rotation of frustum in world space.
\a nearPlane clipping plane.
\a farPlane clipping plane.
*/
array<Vector3, 8> Camera::frustumCorners(bool ortho, float sigma, float ratio, const Vector3 &position,
const Quaternion &rotation, float nearPlane, float farPlane) {
float nh;
float fh;
float nw;
float fw;
if(ortho) {
nh = sigma * 0.5f;
nw = nh * ratio;
fw = nw;
fh = nh;
} else {
float tang = tanf(sigma * DEG2RAD * 0.5f);
nh = nearPlane * tang;
fh = farPlane * tang;
nw = nh * ratio;
fw = fh * ratio;
}
Vector3 dir = rotation * Vector3(0.0f, 0.0f,-1.0f);
Vector3 right = dir.cross(rotation * Vector3(0.0f, 1.0f, 0.0f));
Vector3 up = right.cross(dir);
Vector3 nc = position + dir * nearPlane;
Vector3 fc = position + dir * farPlane;
return {nc + up * nh - right * nw,
nc + up * nh + right * nw,
nc - up * nh + right * nw,
nc - up * nh - right * nw,
fc + up * fh - right * fw,
fc + up * fh + right * fw,
fc - up * fh + right * fw,
fc - up * fh - right * fw};
}
void Camera::drawGizmos() {
Transform *t = transform();
Gizmos::drawIcon(t->worldPosition(), Vector2(0.5f), ".embedded/camera.png", Vector4(1.0f));
}
void Camera::drawGizmosSelected() {
Transform *t = transform();
array<Vector3, 8> a = frustumCorners(m_ortho, (m_ortho) ? m_orthoSize : m_fov,
m_ratio, t->worldPosition(), t->worldRotation(), nearPlane(), farPlane());
Vector3Vector points(a.begin(), a.end());
IndexVector indices = {0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4,
0, 4, 1, 5, 2, 6, 3, 7};
Gizmos::drawLines(points, indices, Vector4(0.5f, 0.5f, 0.5f, 1.0f));
}
void Camera::recalcProjection() {
if(m_ortho) {
float width = m_orthoSize * m_ratio;
m_projection = Matrix4::ortho(-width / 2, width / 2, -m_orthoSize / 2, m_orthoSize / 2, m_near, m_far);
} else {
m_projection = Matrix4::perspective(m_fov, m_ratio, m_near, m_far);
}
}