/
camera.cpp
732 lines (582 loc) · 21.3 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include <algorithm>
#include "camera.h"
const float Camera::DEFAULT_ROTATION_SPEED = 0.3f;
const float Camera::DEFAULT_FOVX = 90.0f;
const float Camera::DEFAULT_ZNEAR = 0.1f;
const float Camera::DEFAULT_ZFAR = 1000.0f;
const float Camera::DEFAULT_ORBIT_MIN_ZOOM = DEFAULT_ZNEAR + 1.0f;
const float Camera::DEFAULT_ORBIT_MAX_ZOOM = DEFAULT_ZFAR * 0.5f;
const float Camera::DEFAULT_ORBIT_OFFSET_DISTANCE = DEFAULT_ORBIT_MIN_ZOOM +
(DEFAULT_ORBIT_MAX_ZOOM - DEFAULT_ORBIT_MIN_ZOOM) * 0.25f;
const Vector3 Camera::WORLD_XAXIS(1.0f, 0.0f, 0.0f);
const Vector3 Camera::WORLD_YAXIS(0.0f, 1.0f, 0.0f);
const Vector3 Camera::WORLD_ZAXIS(0.0f, 0.0f, 1.0f);
Camera::Camera()
{
m_behavior = CAMERA_BEHAVIOR_FLIGHT;
m_preferTargetYAxisOrbiting = true;
m_accumPitchDegrees = 0.0f;
m_savedAccumPitchDegrees = 0.0f;
m_rotationSpeed = DEFAULT_ROTATION_SPEED;
m_fovx = DEFAULT_FOVX;
m_aspectRatio = 0.0f;
m_znear = DEFAULT_ZNEAR;
m_zfar = DEFAULT_ZFAR;
m_orbitMinZoom = DEFAULT_ORBIT_MIN_ZOOM;
m_orbitMaxZoom = DEFAULT_ORBIT_MAX_ZOOM;
m_orbitOffsetDistance = DEFAULT_ORBIT_OFFSET_DISTANCE;
m_eye.set(0.0f, 0.0f, 0.0f);
m_savedEye.set(0.0f, 0.0f, 0.0f);
m_target.set(0.0f, 0.0f, 0.0f);
m_xAxis.set(1.0f, 0.0f, 0.0f);
m_yAxis.set(0.0f, 1.0f, 0.0f);
m_targetYAxis.set(0.0f, 1.0f, 0.0f);
m_zAxis.set(0.0f, 0.0f, 1.0f);
m_viewDir.set(0.0f, 0.0f, -1.0f);
m_acceleration.set(0.0f, 0.0f, 0.0f);
m_currentVelocity.set(0.0f, 0.0f, 0.0f);
m_velocity.set(0.0f, 0.0f, 0.0f);
m_orientation.identity();
m_savedOrientation.identity();
m_viewMatrix.identity();
m_projMatrix.identity();
m_viewProjMatrix.identity();
}
Camera::~Camera()
{
}
void Camera::lookAt(const Vector3 &target)
{
lookAt(m_eye, target, m_yAxis);
}
void Camera::lookAt(const Vector3 &eye, const Vector3 &target, const Vector3 &up)
{
m_eye = eye;
m_target = target;
m_zAxis = eye - target;
m_zAxis.normalize();
m_viewDir = -m_zAxis;
m_xAxis = Vector3::cross(up, m_zAxis);
m_xAxis.normalize();
m_yAxis = Vector3::cross(m_zAxis, m_xAxis);
m_yAxis.normalize();
m_xAxis.normalize();
m_viewMatrix[0][0] = m_xAxis.x;
m_viewMatrix[1][0] = m_xAxis.y;
m_viewMatrix[2][0] = m_xAxis.z;
m_viewMatrix[3][0] = -Vector3::dot(m_xAxis, eye);
m_viewMatrix[0][1] = m_yAxis.x;
m_viewMatrix[1][1] = m_yAxis.y;
m_viewMatrix[2][1] = m_yAxis.z;
m_viewMatrix[3][1] = -Vector3::dot(m_yAxis, eye);
m_viewMatrix[0][2] = m_zAxis.x;
m_viewMatrix[1][2] = m_zAxis.y;
m_viewMatrix[2][2] = m_zAxis.z;
m_viewMatrix[3][2] = -Vector3::dot(m_zAxis, eye);
// Extract the pitch angle from the view matrix.
m_accumPitchDegrees = Math::radiansToDegrees(asinf(m_viewMatrix[1][2]));
m_orientation.fromMatrix(m_viewMatrix);
}
void Camera::move(float dx, float dy, float dz)
{
// Moves the camera by dx world units to the left or right; dy
// world units upwards or downwards; and dz world units forwards
// or backwards.
if (m_behavior == CAMERA_BEHAVIOR_ORBIT)
{
// Orbiting camera is always positioned relative to the
// target position. See updateViewMatrix().
return;
}
Vector3 eye = m_eye;
Vector3 forwards;
if (m_behavior == CAMERA_BEHAVIOR_FIRST_PERSON)
{
// Calculate the forwards direction. Can't just use the camera's local
// z axis as doing so will cause the camera to move more slowly as the
// camera's view approaches 90 degrees straight up and down.
forwards = Vector3::cross(WORLD_YAXIS, m_xAxis);
forwards.normalize();
}
else
{
forwards = m_viewDir;
}
eye += m_xAxis * dx;
eye += WORLD_YAXIS * dy;
eye += forwards * dz;
setPosition(eye);
}
void Camera::move(const Vector3 &direction, const Vector3 &amount)
{
// Moves the camera by the specified amount of world units in the specified
// direction in world space.
if (m_behavior == CAMERA_BEHAVIOR_ORBIT)
{
// Orbiting camera is always positioned relative to the
// target position. See updateViewMatrix().
return;
}
m_eye.x += direction.x * amount.x;
m_eye.y += direction.y * amount.y;
m_eye.z += direction.z * amount.z;
updateViewMatrix();
}
void Camera::perspective(float fovx, float aspect, float znear, float zfar)
{
// Construct a projection matrix based on the horizontal field of view
// 'fovx' rather than the more traditional vertical field of view 'fovy'.
float e = 1.0f / tanf(Math::degreesToRadians(fovx) / 2.0f);
float aspectInv = 1.0f / aspect;
float fovy = 2.0f * atanf(aspectInv / e);
float xScale = 1.0f / tanf(0.5f * fovy);
float yScale = xScale / aspectInv;
m_projMatrix[0][0] = xScale;
m_projMatrix[0][1] = 0.0f;
m_projMatrix[0][2] = 0.0f;
m_projMatrix[0][3] = 0.0f;
m_projMatrix[1][0] = 0.0f;
m_projMatrix[1][1] = yScale;
m_projMatrix[1][2] = 0.0f;
m_projMatrix[1][3] = 0.0f;
m_projMatrix[2][0] = 0.0f;
m_projMatrix[2][1] = 0.0f;
m_projMatrix[2][2] = (zfar + znear) / (znear - zfar);
m_projMatrix[2][3] = -1.0f;
m_projMatrix[3][0] = 0.0f;
m_projMatrix[3][1] = 0.0f;
m_projMatrix[3][2] = (2.0f * zfar * znear) / (znear - zfar);
m_projMatrix[3][3] = 0.0f;
m_viewProjMatrix = m_viewMatrix * m_projMatrix;
m_fovx = fovx;
m_aspectRatio = aspect;
m_znear = znear;
m_zfar = zfar;
}
void Camera::rotate(float headingDegrees, float pitchDegrees, float rollDegrees)
{
// Rotates the camera based on its current behavior.
// Note that not all behaviors support rolling.
pitchDegrees = -pitchDegrees;
headingDegrees = -headingDegrees;
rollDegrees = -rollDegrees;
switch (m_behavior)
{
default:
break;
case CAMERA_BEHAVIOR_FIRST_PERSON:
case CAMERA_BEHAVIOR_SPECTATOR:
rotateFirstPerson(headingDegrees, pitchDegrees);
break;
case CAMERA_BEHAVIOR_FLIGHT:
rotateFlight(headingDegrees, pitchDegrees, rollDegrees);
break;
case CAMERA_BEHAVIOR_ORBIT:
rotateOrbit(headingDegrees, pitchDegrees, rollDegrees);
break;
}
updateViewMatrix();
}
void Camera::rotateSmoothly(float headingDegrees, float pitchDegrees, float rollDegrees)
{
// This method applies a scaling factor to the rotation angles prior to
// using these rotation angles to rotate the camera. This method is usually
// called when the camera is being rotated using an input device (such as a
// mouse or a joystick).
headingDegrees *= m_rotationSpeed;
pitchDegrees *= m_rotationSpeed;
rollDegrees *= m_rotationSpeed;
rotate(headingDegrees, pitchDegrees, rollDegrees);
}
void Camera::undoRoll()
{
// Undo any camera rolling by leveling the camera. When the camera is
// orbiting this method will cause the camera to become level with the
// orbit target.
if (m_behavior == CAMERA_BEHAVIOR_ORBIT)
lookAt(m_eye, m_target, m_targetYAxis);
else
lookAt(m_eye, m_eye + m_viewDir, WORLD_YAXIS);
}
void Camera::updatePosition(const Vector3 &direction, float elapsedTimeSec)
{
// Moves the camera using Newton's second law of motion. Unit mass is
// assumed here to somewhat simplify the calculations. The direction vector
// is in the range [-1,1].
if (m_currentVelocity.magnitudeSq() != 0.0f)
{
// Only move the camera if the velocity vector is not of zero length.
// Doing this guards against the camera slowly creeping around due to
// floating point rounding errors.
Vector3 displacement = (m_currentVelocity * elapsedTimeSec) +
(0.5f * m_acceleration * elapsedTimeSec * elapsedTimeSec);
// Floating point rounding errors will slowly accumulate and cause the
// camera to move along each axis. To prevent any unintended movement
// the displacement vector is clamped to zero for each direction that
// the camera isn't moving in. Note that the updateVelocity() method
// will slowly decelerate the camera's velocity back to a stationary
// state when the camera is no longer moving along that direction. To
// account for this the camera's current velocity is also checked.
if (direction.x == 0.0f && Math::closeEnough(m_currentVelocity.x, 0.0f))
displacement.x = 0.0f;
if (direction.y == 0.0f && Math::closeEnough(m_currentVelocity.y, 0.0f))
displacement.y = 0.0f;
if (direction.z == 0.0f && Math::closeEnough(m_currentVelocity.z, 0.0f))
displacement.z = 0.0f;
move(displacement.x, displacement.y, displacement.z);
}
// Continuously update the camera's velocity vector even if the camera
// hasn't moved during this call. When the camera is no longer being moved
// the camera is decelerating back to its stationary state.
updateVelocity(direction, elapsedTimeSec);
}
void Camera::zoom(float zoom, float minZoom, float maxZoom)
{
if (m_behavior == CAMERA_BEHAVIOR_ORBIT)
{
// Moves the camera closer to or further away from the orbit
// target. The zoom amounts are in world units.
m_orbitMaxZoom = maxZoom;
m_orbitMinZoom = minZoom;
Vector3 offset = m_eye - m_target;
m_orbitOffsetDistance = offset.magnitude();
offset.normalize();
m_orbitOffsetDistance += zoom;
m_orbitOffsetDistance = std::min(std::max(m_orbitOffsetDistance, minZoom), maxZoom);
offset *= m_orbitOffsetDistance;
m_eye = offset + m_target;
updateViewMatrix();
}
else
{
// For the other behaviors zoom is interpreted as changing the
// horizontal field of view. The zoom amounts refer to the horizontal
// field of view in degrees.
zoom = std::min(std::max(zoom, minZoom), maxZoom);
perspective(zoom, m_aspectRatio, m_znear, m_zfar);
}
}
void Camera::setAcceleration(const Vector3 &acceleration)
{
m_acceleration = acceleration;
}
void Camera::setBehavior(CameraBehavior newBehavior)
{
// Switch to a new camera mode (i.e., behavior).
// This method is complicated by the fact that it tries to save the current
// behavior's state prior to making the switch to the new camera behavior.
// Doing this allows seamless switching between camera behaviors.
CameraBehavior prevBehavior = m_behavior;
if (prevBehavior == newBehavior)
return;
m_behavior = newBehavior;
switch (newBehavior)
{
case CAMERA_BEHAVIOR_FIRST_PERSON:
switch (prevBehavior)
{
default:
break;
case CAMERA_BEHAVIOR_FLIGHT:
m_eye.y = m_firstPersonYOffset;
updateViewMatrix();
break;
case CAMERA_BEHAVIOR_SPECTATOR:
m_eye.y = m_firstPersonYOffset;
updateViewMatrix();
break;
case CAMERA_BEHAVIOR_ORBIT:
m_eye.x = m_savedEye.x;
m_eye.z = m_savedEye.z;
m_eye.y = m_firstPersonYOffset;
m_orientation = m_savedOrientation;
m_accumPitchDegrees = m_savedAccumPitchDegrees;
updateViewMatrix();
break;
}
undoRoll();
break;
case CAMERA_BEHAVIOR_SPECTATOR:
switch (prevBehavior)
{
default:
break;
case CAMERA_BEHAVIOR_FLIGHT:
updateViewMatrix();
break;
case CAMERA_BEHAVIOR_ORBIT:
m_eye = m_savedEye;
m_orientation = m_savedOrientation;
m_accumPitchDegrees = m_savedAccumPitchDegrees;
updateViewMatrix();
break;
}
undoRoll();
break;
case CAMERA_BEHAVIOR_FLIGHT:
if (prevBehavior == CAMERA_BEHAVIOR_ORBIT)
{
m_eye = m_savedEye;
m_orientation = m_savedOrientation;
m_accumPitchDegrees = m_savedAccumPitchDegrees;
updateViewMatrix();
}
else
{
m_savedEye = m_eye;
updateViewMatrix();
}
break;
case CAMERA_BEHAVIOR_ORBIT:
if (prevBehavior == CAMERA_BEHAVIOR_FIRST_PERSON)
m_firstPersonYOffset = m_eye.y;
m_savedEye = m_eye;
m_savedOrientation = m_orientation;
m_savedAccumPitchDegrees = m_accumPitchDegrees;
m_targetYAxis = m_yAxis;
Vector3 newEye = m_eye + m_zAxis * m_orbitOffsetDistance;
Vector3 newTarget = m_eye;
lookAt(newEye, newTarget, m_targetYAxis);
break;
}
}
void Camera::setCurrentVelocity(const Vector3 ¤tVelocity)
{
m_currentVelocity = currentVelocity;
}
void Camera::setCurrentVelocity(float x, float y, float z)
{
m_currentVelocity.set(x, y, z);
}
void Camera::setOrbitMaxZoom(float orbitMaxZoom)
{
m_orbitMaxZoom = orbitMaxZoom;
}
void Camera::setOrbitMinZoom(float orbitMinZoom)
{
m_orbitMinZoom = orbitMinZoom;
}
void Camera::setOrbitOffsetDistance(float orbitOffsetDistance)
{
m_orbitOffsetDistance = orbitOffsetDistance;
}
void Camera::setOrientation(const Quaternion &newOrientation)
{
Matrix4 m = newOrientation.toMatrix4();
// Store the pitch for this new orientation.
// First person and spectator behaviors limit pitching to
// 90 degrees straight up and down.
m_accumPitchDegrees = Math::radiansToDegrees(asinf(m[1][2]));
// First person and spectator behaviors don't allow rolling.
// Negate any rolling that might be encoded in the new orientation.
m_orientation = newOrientation;
if (m_behavior == CAMERA_BEHAVIOR_FIRST_PERSON || m_behavior == CAMERA_BEHAVIOR_SPECTATOR)
lookAt(m_eye, m_eye + m_viewDir, WORLD_YAXIS);
updateViewMatrix();
}
void Camera::setPosition(const Vector3 &newEye)
{
m_eye = newEye;
updateViewMatrix();
}
void Camera::setPreferTargetYAxisOrbiting(bool preferTargetYAxisOrbiting)
{
// Determines the behavior of Y axis rotations when the camera is
// orbiting a target. When preferTargetYAxisOrbiting is true all
// Y axis rotations are about the orbit target's local Y axis.
// When preferTargetYAxisOrbiting is false then the camera's
// local Y axis is used instead.
m_preferTargetYAxisOrbiting = preferTargetYAxisOrbiting;
if (m_preferTargetYAxisOrbiting)
undoRoll();
}
void Camera::setRotationSpeed(float rotationSpeed)
{
// This is just an arbitrary value used to scale rotations
// when rotateSmoothly() is called.
m_rotationSpeed = rotationSpeed;
}
void Camera::setVelocity(const Vector3 &velocity)
{
m_velocity = velocity;
}
void Camera::setVelocity(float x, float y, float z)
{
m_velocity.set(x, y, z);
}
void Camera::rotateFirstPerson(float headingDegrees, float pitchDegrees)
{
// Implements the rotation logic for the first person style and
// spectator style camera behaviors. Roll is ignored.
m_accumPitchDegrees += pitchDegrees;
if (m_accumPitchDegrees > 90.0f)
{
pitchDegrees = 90.0f - (m_accumPitchDegrees - pitchDegrees);
m_accumPitchDegrees = 90.0f;
}
if (m_accumPitchDegrees < -90.0f)
{
pitchDegrees = -90.0f - (m_accumPitchDegrees - pitchDegrees);
m_accumPitchDegrees = -90.0f;
}
Quaternion rot;
// Rotate camera about the world y axis.
// Note the order the quaternions are multiplied. That is important!
if (headingDegrees != 0.0f)
{
rot.fromAxisAngle(WORLD_YAXIS, headingDegrees);
m_orientation = rot * m_orientation;
}
// Rotate camera about its local x axis.
// Note the order the quaternions are multiplied. That is important!
if (pitchDegrees != 0.0f)
{
rot.fromAxisAngle(WORLD_XAXIS, pitchDegrees);
m_orientation = m_orientation * rot;
}
}
void Camera::rotateFlight(float headingDegrees, float pitchDegrees, float rollDegrees)
{
// Implements the rotation logic for the flight style camera behavior.
m_accumPitchDegrees += pitchDegrees;
if (m_accumPitchDegrees > 360.0f)
m_accumPitchDegrees -= 360.0f;
if (m_accumPitchDegrees < -360.0f)
m_accumPitchDegrees += 360.0f;
Quaternion rot;
rot.fromHeadPitchRoll(headingDegrees, pitchDegrees, rollDegrees);
m_orientation *= rot;
}
void Camera::rotateOrbit(float headingDegrees, float pitchDegrees, float rollDegrees)
{
// Implements the rotation logic for the orbit style camera behavior.
// Roll is ignored for target Y axis orbiting.
//
// Briefly here's how this orbit camera implementation works. Switching to
// the orbit camera behavior via the setBehavior() method will set the
// camera's orientation to match the orbit target's orientation. Calls to
// rotateOrbit() will rotate this orientation. To turn this into a third
// person style view the updateViewMatrix() method will move the camera
// position back 'm_orbitOffsetDistance' world units along the camera's
// local z axis from the orbit target's world position.
Quaternion rot;
if (m_preferTargetYAxisOrbiting)
{
if (headingDegrees != 0.0f)
{
rot.fromAxisAngle(m_targetYAxis, headingDegrees);
m_orientation = rot * m_orientation;
}
if (pitchDegrees != 0.0f)
{
rot.fromAxisAngle(WORLD_XAXIS, pitchDegrees);
m_orientation = m_orientation * rot;
}
}
else
{
rot.fromHeadPitchRoll(headingDegrees, pitchDegrees, rollDegrees);
m_orientation *= rot;
}
}
void Camera::updateVelocity(const Vector3 &direction, float elapsedTimeSec)
{
// Updates the camera's velocity based on the supplied movement direction
// and the elapsed time (since this method was last called). The movement
// direction is in the range [-1,1].
if (direction.x != 0.0f)
{
// Camera is moving along the x axis.
// Linearly accelerate up to the camera's max speed.
m_currentVelocity.x += direction.x * m_acceleration.x * elapsedTimeSec;
if (m_currentVelocity.x > m_velocity.x)
m_currentVelocity.x = m_velocity.x;
else if (m_currentVelocity.x < -m_velocity.x)
m_currentVelocity.x = -m_velocity.x;
}
else
{
// Camera is no longer moving along the x axis.
// Linearly decelerate back to stationary state.
if (m_currentVelocity.x > 0.0f)
{
if ((m_currentVelocity.x -= m_acceleration.x * elapsedTimeSec) < 0.0f)
m_currentVelocity.x = 0.0f;
}
else
{
if ((m_currentVelocity.x += m_acceleration.x * elapsedTimeSec) > 0.0f)
m_currentVelocity.x = 0.0f;
}
}
if (direction.y != 0.0f)
{
// Camera is moving along the y axis.
// Linearly accelerate up to the camera's max speed.
m_currentVelocity.y += direction.y * m_acceleration.y * elapsedTimeSec;
if (m_currentVelocity.y > m_velocity.y)
m_currentVelocity.y = m_velocity.y;
else if (m_currentVelocity.y < -m_velocity.y)
m_currentVelocity.y = -m_velocity.y;
}
else
{
// Camera is no longer moving along the y axis.
// Linearly decelerate back to stationary state.
if (m_currentVelocity.y > 0.0f)
{
if ((m_currentVelocity.y -= m_acceleration.y * elapsedTimeSec) < 0.0f)
m_currentVelocity.y = 0.0f;
}
else
{
if ((m_currentVelocity.y += m_acceleration.y * elapsedTimeSec) > 0.0f)
m_currentVelocity.y = 0.0f;
}
}
if (direction.z != 0.0f)
{
// Camera is moving along the z axis.
// Linearly accelerate up to the camera's max speed.
m_currentVelocity.z += direction.z * m_acceleration.z * elapsedTimeSec;
if (m_currentVelocity.z > m_velocity.z)
m_currentVelocity.z = m_velocity.z;
else if (m_currentVelocity.z < -m_velocity.z)
m_currentVelocity.z = -m_velocity.z;
}
else
{
// Camera is no longer moving along the z axis.
// Linearly decelerate back to stationary state.
if (m_currentVelocity.z > 0.0f)
{
if ((m_currentVelocity.z -= m_acceleration.z * elapsedTimeSec) < 0.0f)
m_currentVelocity.z = 0.0f;
}
else
{
if ((m_currentVelocity.z += m_acceleration.z * elapsedTimeSec) > 0.0f)
m_currentVelocity.z = 0.0f;
}
}
}
void Camera::updateViewMatrix()
{
// Reconstruct the view matrix.
m_viewMatrix = m_orientation.toMatrix4();
m_xAxis.set(m_viewMatrix[0][0], m_viewMatrix[1][0], m_viewMatrix[2][0]);
m_yAxis.set(m_viewMatrix[0][1], m_viewMatrix[1][1], m_viewMatrix[2][1]);
m_zAxis.set(m_viewMatrix[0][2], m_viewMatrix[1][2], m_viewMatrix[2][2]);
m_viewDir = -m_zAxis;
if (m_behavior == CAMERA_BEHAVIOR_ORBIT)
{
// Calculate the new camera position based on the current
// orientation. The camera must always maintain the same
// distance from the target. Use the current offset vector
// to determine the correct distance from the target.
m_eye = m_target + m_zAxis * m_orbitOffsetDistance;
}
m_viewMatrix[3][0] = -Vector3::dot(m_xAxis, m_eye);
m_viewMatrix[3][1] = -Vector3::dot(m_yAxis, m_eye);
m_viewMatrix[3][2] = -Vector3::dot(m_zAxis, m_eye);
}