-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCamera.cpp
More file actions
440 lines (376 loc) · 14.9 KB
/
Camera.cpp
File metadata and controls
440 lines (376 loc) · 14.9 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#include "Camera.h"
// Init
void Camera::LookAt(vec3 cameraPos, vec3 cameraTarget, vec3 cameraUp)
{
this->mPosition = cameraPos;
this->mDirection = normalize(cameraTarget - cameraPos);
this->mUp = normalize(cameraUp);
this->mRight = normalize(cross(this->mUp, this->mDirection));
updateView();
}
void Camera::SetProjection(float fovy, int width, int height, float znear, float zfar)
{
mWindowW = width;
mWindowH = height;
mFovyDeg = fovy;
mZnear = znear;
mZfar = zfar;
mMatProjection = glm::perspective(glm::radians(fovy), float(width) / float(height), znear, zfar);
updateViewProjection();
}
void Camera::SetViewportSize(int width, int height)
{
mWindowW = width;
mWindowH = height;
mMatProjection = glm::perspective(glm::radians(mFovyDeg), float(mWindowW) / float(mWindowH), mZnear, mZfar);
updateViewProjection();
}
void Camera::SetPosition(vec3 posCamera)
{
this->mPosition = posCamera;
updateView();
}
void Camera::SetZoom(float fovy)
{
mFovyDeg = fovy;
mMatProjection = glm::perspective(glm::radians(fovy), float(mWindowW) / float(mWindowH), mZnear, mZfar);
updateViewProjection();
}
// Update
void Camera::RotateCamera(float yaw, float pitch, float roll)
{
mat4 rotationMatrix =
glm::rotate(mat4(1.0f), yaw, vec3(0.0f, 1.0f, 0.0f)) *
glm::rotate(mat4(1.0f), pitch, mRight) *
glm::rotate(mat4(1.0f), roll, mDirection);
mDirection = vec3(glm::normalize(rotationMatrix * vec4(mDirection, 0.f)));
mUp = vec3(0.0f, 1.0f, 0.0f);
mRight = glm::normalize(cross(mUp, mDirection));
}
void Camera::MoveCamera(const vec3& delta)
{
mPosition += delta;
}
void Camera::Animate(float deltaT, vec3& orbitalTarget, vec3& viewPos, vec3& viewTarget)
{
mIsUnchanged = true;
static eCameraMode previousMode = mCurrentMode;
float baseT = 0.15f;
float t = GetInterpolationValue(baseT);
// Mouse delta management
vec2 mouseMove = mMousePos - mMousePosPrev;
mMousePosPrev = mMousePos;
// Initializing targets in the first frame
if (bFirstUpdate)
{
mPositionTarget = mPosition;
mDirectionTarget = mDirection;
mUpTarget = mUp;
mRightTarget = mRight;
t = 1.0f; // No smooth transition, go directly to the new position
bFirstUpdate = false;
}
// Flag to know if the camera has changed mode
static bool bCameraModeChanged = false;
// Camera mode change management
if (mKeyboardState[KeyboardControls::Orbital])
{
mCurrentMode = ORBITAL;
mKeyboardState[KeyboardControls::Orbital] = false;
bCameraModeChanged = true;
mIsUnchanged = false;
}
else if (mKeyboardState[KeyboardControls::Bridge])
{
mCurrentMode = BRIDGE;
mKeyboardState[KeyboardControls::Bridge] = false;
bCameraModeChanged = true;
mIsUnchanged = false;
}
else if (mKeyboardState[KeyboardControls::Fps])
{
mCurrentMode = FPS;
mKeyboardState[KeyboardControls::Fps] = false;
bCameraModeChanged = true;
mIsUnchanged = false;
}
// If we just changed mode we reset the targets to the current state
if (bCameraModeChanged)
{
if (previousMode == FPS && mCurrentMode == ORBITAL)
{
// FPS -> ORBITAL :
mOrbitRadius = glm::length(orbitalTarget - mPosition);
mOrbitYaw = atan2(mPosition.z - orbitalTarget.z, mPosition.x - orbitalTarget.x);
mOrbitPitch = -asin((orbitalTarget.y - mPosition.y) / mOrbitRadius);
mTargetPos = orbitalTarget;
mDirection = glm::normalize(mTargetPos - mPosition);
mUp = vec3(0, 1, 0);
mRight = glm::normalize(glm::cross(mUp, mDirection));
mDirectionTarget = mDirection;
mUpTarget = mUp;
mRightTarget = mRight;
}
else
{
mPositionTarget = mPosition;
if (mCurrentMode == BRIDGE)
mDirectionTarget = mDirection = viewTarget;
else
mDirectionTarget = mDirection;
mUpTarget = vec3(0.0f, 1.0f, 0.0f);
mRightTarget = mRight;
}
bCameraModeChanged = false;
}
previousMode = mCurrentMode;
// ===== ORBITAL MODE =====
if (mCurrentMode == eCameraMode::ORBITAL)
{
if (mMouseButtonState[MouseButtons::Left] && (mouseMove.x || mouseMove.y))
{
// There is a new mouse position with a left clic
mOrbitYaw += mouseMove.x * mRotateSpeed;
mOrbitPitch += mouseMove.y * mRotateSpeed;
mOrbitPitch = glm::clamp(mOrbitPitch, -glm::pi<float>() / 2.f + 0.1f, glm::pi<float>() / 2.f - 0.1f);
mIsUnchanged = false;
}
mTargetPos = orbitalTarget;
float x = mTargetPos.x + mOrbitRadius * cos(mOrbitPitch) * cos(mOrbitYaw);
float y = mTargetPos.y + mOrbitRadius * sin(mOrbitPitch);
float z = mTargetPos.z + mOrbitRadius * cos(mOrbitPitch) * sin(mOrbitYaw);
vec3 orbitalPosTarget(x, y, z);
vec3 directionTarget = normalize(mTargetPos - orbitalPosTarget);
vec3 rightTarget = normalize(cross(vec3(0, 1, 0), directionTarget));
vec3 upTarget = vec3(0, 1, 0);
mPosition = glm::mix(mPosition, orbitalPosTarget, t);
mDirection = glm::normalize(glm::mix(mDirection, directionTarget, t));
mUp = vec3(0.0f, 1.0f, 0.0f);
mRight = glm::normalize(glm::mix(mRight, mRightTarget, t));
}
// ===== BRIDGE MODE =====
else if (mCurrentMode == eCameraMode::BRIDGE)
{
bool cameraDirty = false;
float yaw = 0, pitch = 0;
if (mMouseButtonState[MouseButtons::Left] && (mouseMove.x || mouseMove.y))
{
yaw = -mRotateSpeed * mouseMove.x;
pitch = mRotateSpeed * mouseMove.y;
cameraDirty = true;
}
if (cameraDirty)
{
mat4 rotationMatrix = glm::rotate(mat4(1.0f), yaw, vec3(0, 1.0f, 0)) * glm::rotate(mat4(1.0f), pitch, mRightTarget);
mDirectionTarget = glm::normalize(vec3(rotationMatrix * vec4(mDirectionTarget, 0.f)));
mUpTarget = glm::normalize(vec3(rotationMatrix * vec4(mUpTarget, 0.f)));
mRightTarget = glm::normalize(glm::cross(mUpTarget, mDirectionTarget));
mIsUnchanged = false;
}
// Interpolation to target
mPosition = viewPos;
mDirection = glm::normalize(glm::mix(mDirection, mDirectionTarget, t));
mUp = vec3(0.0f, 1.0f, 0.0f);
mRight = glm::normalize(glm::mix(mRight, mRightTarget, t));
}
// ===== FPS MODE =====
else if (mCurrentMode == eCameraMode::FPS)
{
bool cameraDirty = false;
float yaw = 0, pitch = 0, roll = 0;
vec3 moveVec{ 0.f };
float moveStep = deltaT * mMoveSpeed;
if (mKeyboardState[KeyboardControls::SpeedUp]) moveStep *= 10.f;
if (mKeyboardState[KeyboardControls::SlowDown]) moveStep *= 0.1f;
if (mMouseButtonState[MouseButtons::Left] && (mouseMove.x || mouseMove.y))
{
yaw = -mRotateSpeed * mouseMove.x;
pitch = mRotateSpeed * mouseMove.y;
cameraDirty = true;
}
if (mKeyboardState[KeyboardControls::MoveForward]) { moveVec += moveStep * mDirection; cameraDirty = true; }
if (mKeyboardState[KeyboardControls::MoveBackward]) { moveVec += -moveStep * mDirection; cameraDirty = true; }
if (mKeyboardState[KeyboardControls::MoveLeft]) { moveVec += moveStep * mRight; cameraDirty = true; }
if (mKeyboardState[KeyboardControls::MoveRight]) { moveVec -= moveStep * mRight; cameraDirty = true; }
if (mKeyboardState[KeyboardControls::MoveUp]) { moveVec += moveStep * mUp; cameraDirty = true; }
if (mKeyboardState[KeyboardControls::MoveDown]) { moveVec -= moveStep * mUp; cameraDirty = true; }
if (cameraDirty)
{
mPositionTarget += moveVec;
mat4 rotationMatrix =
glm::rotate(mat4(1.0f), yaw, vec3(0, 1.0f, 0)) *
glm::rotate(mat4(1.0f), pitch, mRightTarget) *
glm::rotate(mat4(1.0f), roll, mDirectionTarget);
mDirectionTarget = glm::normalize(vec3(rotationMatrix * vec4(mDirectionTarget, 0.f)));
mUpTarget = glm::normalize(vec3(rotationMatrix * vec4(mUpTarget, 0.f)));
mRightTarget = glm::normalize(glm::cross(mUpTarget, mDirectionTarget));
mIsUnchanged = false;
}
mPosition = glm::mix(mPosition, mPositionTarget, t);
mDirection = glm::normalize(glm::mix(mDirection, mDirectionTarget, t));
mUp = vec3(0.0f, 1.0f, 0.0f);
mRight = glm::normalize(glm::mix(mRight, mRightTarget, t));
}
updateView();
}
// Get
float Camera::GetInterpolationValue(float t) const
{
switch (mInterpolation)
{
case eInterpolation::Linear: return LinInterp(t);
case eInterpolation::SmoothStep: return SmoothStepInterp(t);
case eInterpolation::EaseIn: return EaseInInterp(t);
case eInterpolation::EaseOut: return EaseOutInterp(t);
case eInterpolation::EaseInOut: return EaseInOutInterp(t);
default: return t;
}
}
float Camera::GetNorthAngleDEG()
{
// Vector representing North (negative Z axis)
vec3 north(0.0f, 0.0f, -1.0f);
// Projection of the direction onto the XZ plane
vec3 directionXZ(mDirection.x, 0.0f, mDirection.z);
// Normalization of the projected vector
directionXZ = normalize(directionXZ);
// Calculating the angle between the projected direction and North
float North = glm::orientedAngle(north, directionXZ, vec3(0.0f, 1.0f, 0.0f));
// Converting angle to degrees
North = degrees(North);
North = 360.0f - North;
// Adjusting the angle to always be positive (0-360)
while (North < 0)
North += 360.0f;
while (North > 360.0f)
North -= 360.0f;
return North;
}
float Camera::GetAttitudeDEG()
{
// Projection of mDirection onto the horizontal plane (XZ)
vec3 horizontalDir = glm::normalize(vec3(mDirection.x, 0.0f, mDirection.z));
// Calculating the angle between mDirection and its horizontal projection
float pitchRadians = glm::acos(glm::dot(mDirection, horizontalDir));
// Determine if the angle is positive or negative
if (mDirection.y < 0)
pitchRadians = -pitchRadians;
// Conversion to degrees
float pitchDegrees = glm::degrees(pitchRadians);
return pitchDegrees;
}
float Camera::GetRollDEG()
{
// Camera "right" vector
vec3 cameraRight = glm::normalize(glm::cross(mDirection, vec3(0.0f, 1.0f, 0.0f)));
// Camera "up" vector in the plane perpendicular to mDirection
vec3 cameraUpProjected = glm::normalize(glm::cross(mDirection, cameraRight));
// Calculate the angle between cameraUpProjected and the world's "up" vector (0, 1, 0)
float rollRadians = glm::acos(glm::dot(cameraUpProjected, vec3(0.0f, 1.0f, 0.0f)));
// Determine if the angle is positive or negative
if (glm::dot(cameraRight, mUp) < 0)
rollRadians = -rollRadians;
// Conversion to degrees
float rollDegrees = glm::degrees(rollRadians);
return rollDegrees;
}
bool Camera::IsInViewFrustum(const vec3& position)
{
vec4 clipSpace = mMatViewProjection * vec4(position, 1.0f);
return std::abs(clipSpace.x) <= clipSpace.w && std::abs(clipSpace.y) <= clipSpace.w && clipSpace.z >= -clipSpace.w && clipSpace.z <= clipSpace.w;
}
float Camera::GetHorizonViewportY() const
{
// Position far away on the line of sight
glm::vec3 distantPoint = mPosition + mZfar * mDirection;
// Vertical projection on the ground (y=0)
glm::vec3 horizonPoint = glm::vec3(distantPoint.x, 0.0f, distantPoint.z);
// Passage into clip space (viewprojection)
glm::vec4 clipPos = mMatViewProjection * glm::vec4(horizonPoint, 1.0f);
// Homogeneous division
if (clipPos.w != 0.0f)
clipPos /= clipPos.w;
// Clamp between -1 and 1 (vertical NDC viewport)
float viewportY = glm::clamp(clipPos.y, -1.0f, 1.0f);
return viewportY;
}
// Inputs
void Camera::KeyboardUpdate(int key, int scancode, int action, int mods)
{
if (mKeyboardMap.find(key) == mKeyboardMap.end())
return;
auto cameraKey = mKeyboardMap.at(key);
if (action == GLFW_PRESS || action == GLFW_REPEAT)
mKeyboardState[cameraKey] = true;
else
mKeyboardState[cameraKey] = false;
}
void Camera::MousePosUpdate(double xpos, double ypos)
{
mMousePos = { float(xpos), float(ypos) };
}
void Camera::MouseButtonUpdate(int button, int action, int mods)
{
if (mMouseButtonMap.find(button) == mMouseButtonMap.end())
return;
auto cameraButton = mMouseButtonMap.at(button);
if (action == GLFW_PRESS)
mMouseButtonState[cameraButton] = true;
else
mMouseButtonState[cameraButton] = false;
}
// Orbital mode
void Camera::SetOrbitalMode()
{
mPreviousMode = mCurrentMode;
mCurrentMode = eCameraMode::ORBITAL;
vec3 direction = mTargetPos - mPosition;
mOrbitRadius = length(direction);
mOrbitYaw = -atan2(direction.z, direction.x);
mOrbitPitch = -asin(direction.y / mOrbitRadius);
}
void Camera::SetTarget(const vec3& target)
{
mTargetPos = target;
}
void Camera::AdjustOrbitRadius(float delta)
{
if (mCurrentMode == eCameraMode::ORBITAL)
mOrbitRadius = glm::max(0.1f, mOrbitRadius + delta);
}
void Camera::SetMode(eCameraMode mode)
{
this->mPreviousMode = mCurrentMode;
this->mCurrentMode = mode;
if (mCurrentMode == eCameraMode::ORBITAL)
{
vec3 direction = mTargetPos - mPosition;
mOrbitRadius = length(direction);
mOrbitYaw = -atan2(direction.z, direction.x);
mOrbitPitch = -asin(direction.y / mOrbitRadius);
}
}
void Camera::ReturnToPreviousMode()
{
mCurrentMode = mPreviousMode;
if (mCurrentMode == eCameraMode::ORBITAL)
{
vec3 direction = mTargetPos - mPosition;
mOrbitRadius = length(direction);
mOrbitYaw = -atan2(direction.z, direction.x);
mOrbitPitch = -asin(direction.y / mOrbitRadius);
}
}
void Camera::updateView()
{
mMatView = glm::lookAt(mPosition, mPosition + mDirection, mUp);
mMatViewReflexion = glm::scale(mMatView, vec3(1.0f, -1.0f, 1.0f));
updateViewProjection();
}
void Camera::updateViewProjection()
{
mMatViewProjection = mMatProjection * mMatView;
}