Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 330 vs 331 for /trunk/Source Code/Rendering/Camera/Camera.cpp

Diff revisions: vs.
  @@ -5,28 +5,27 @@
5 5
6 6 namespace Divide {
7 7
8 - Camera::Camera(const CameraType& type, const vec3<F32>& eye) :
9 - Resource("temp_camera"),
10 - _isActive(false),
11 - _isOrthoCamera(false),
12 - _projectionDirty(true),
13 - _viewMatrixDirty(true),
14 - _viewMatrixLocked(false),
15 - _rotationLocked(false),
16 - _movementLocked(false),
17 - _frustumLocked(false),
18 - _frustumDirty(true),
19 - _mouseSensitivity(1.0f),
20 - _zoomSpeedFactor(35.0f),
21 - _moveSpeedFactor(35.0f),
22 - _turnSpeedFactor(35.0f),
23 - _cameraMoveSpeed(35.0f),
24 - _cameraZoomSpeed(35.0f),
25 - _cameraTurnSpeed(35.0f),
26 - _aspectRatio(1.33f),
27 - _verticalFoV(60.0f),
28 - _camIOD(0.5f),
29 - _type(type)
8 + Camera::Camera(const CameraType& type, const vec3<F32>& eye) : Resource("temp_camera"),
9 + _isActive(false),
10 + _isOrthoCamera(false),
11 + _projectionDirty(true),
12 + _viewMatrixDirty(true),
13 + _viewMatrixLocked(false),
14 + _rotationLocked(false),
15 + _movementLocked(false),
16 + _frustumLocked(false),
17 + _frustumDirty(true),
18 + _mouseSensitivity(1.0f),
19 + _zoomSpeedFactor(35.0f),
20 + _moveSpeedFactor(35.0f),
21 + _turnSpeedFactor(35.0f),
22 + _cameraMoveSpeed(35.0f),
23 + _cameraZoomSpeed(35.0f),
24 + _cameraTurnSpeed(35.0f),
25 + _aspectRatio(1.33f),
26 + _verticalFoV(60.0f),
27 + _camIOD(0.5f),
28 + _type(type)
30 29 {
31 30 _eye.set(eye);
32 31 _reflectedEye.set(eye * vec3<F32>(1.0f, -1.0f, 1.0f));
  @@ -42,19 +41,20 @@
42 41 _reflectedViewMatrix.identity();
43 42 _yawFixed = false;
44 43 _reflectionRendering = false;
45 - _frustum = New Frustum(*this);
44 + _frustum = MemoryManager_NEW Frustum(*this);
46 45 }
47 46
48 47 Camera::~Camera()
49 48 {
50 - MemoryManager::SAFE_DELETE( _frustum );
49 + MemoryManager::DELETE( _frustum );
51 50 }
52 51
53 - void Camera::fromCamera(const Camera& camera){
54 - if(camera._isOrthoCamera)
52 + void Camera::fromCamera(const Camera& camera) {
53 + if (camera._isOrthoCamera) {
55 54 setProjection(camera._orthoRect, camera.getZPlanes());
56 - else
55 + } else {
57 56 setProjection(camera.getAspectRatio(), camera.getVerticalFoV(), camera.getZPlanes());
57 + }
58 58
59 59 setMoveSpeedFactor(camera.getMoveSpeedFactor());
60 60 setTurnSpeedFactor(camera.getTurnSpeedFactor());
  @@ -65,7 +65,6 @@
65 65 }
66 66
67 67 void Camera::update(const U64 deltaTime) {
68 -
69 68 if (Config::USE_FIXED_TIMESTEP) {
70 69 F32 timeFactor = getUsToSec(Config::SKIP_TICKS);
71 70 _cameraMoveSpeed = _moveSpeedFactor * timeFactor;
  @@ -79,15 +78,18 @@
79 78 }
80 79 }
81 80
82 - void Camera::updateProjection(bool force){
81 + void Camera::updateProjection(bool force) {
83 82 assert(_isActive);
84 - if (_projectionDirty || force){
85 - if (_projectionDirty){
86 - _projectionMatrix.set(_isOrthoCamera ? GFX_DEVICE.setProjection(_orthoRect, _zPlanes) : GFX_DEVICE.setProjection(_verticalFoV, _aspectRatio, _zPlanes));
83 +
84 + if (_projectionDirty || force) {
85 + if (_projectionDirty) {
86 + _projectionMatrix.set(_isOrthoCamera ? GFX_DEVICE.setProjection(_orthoRect, _zPlanes) :
87 + GFX_DEVICE.setProjection(_verticalFoV, _aspectRatio, _zPlanes));
87 88 _projectionDirty = false;
88 89 _frustumDirty = true;
89 90 }else{
90 - _isOrthoCamera ? GFX_DEVICE.setProjection(_orthoRect, _zPlanes) : GFX_DEVICE.setProjection(_verticalFoV, _aspectRatio, _zPlanes);
91 + _isOrthoCamera ? GFX_DEVICE.setProjection(_orthoRect, _zPlanes) :
92 + GFX_DEVICE.setProjection(_verticalFoV, _aspectRatio, _zPlanes);
91 93 }
92 94 }
93 95 }
  @@ -102,26 +104,31 @@
102 104 }
103 105
104 106 void Camera::setGlobalRotation(F32 yaw, F32 pitch, F32 roll) {
105 - if (_rotationLocked) return;
107 + if (_rotationLocked) {
108 + return;
109 + }
106 110
107 111 Quaternion<F32> pitchRot(WORLD_X_AXIS, -pitch);
108 112 Quaternion<F32> yawRot(WORLD_Y_AXIS, -yaw);
109 - if(!IS_ZERO(roll)){
113 +
114 + if (!IS_ZERO(roll)) {
110 115 _tempOrientation.fromAxisAngle(WORLD_Z_AXIS, -roll);
111 116 setRotation(yawRot * pitchRot * _tempOrientation);
112 - }else{
117 + } else {
113 118 setRotation(yawRot * pitchRot);
114 119 }
115 120 }
116 121
117 122 void Camera::rotate(const Quaternion<F32>& q) {
118 - if(_rotationLocked) return;
123 + if (_rotationLocked) {
124 + return;
125 + }
119 126
120 - if(_type == FIRST_PERSON){
127 + if (_type == FIRST_PERSON) {
121 128 vec3<F32> euler;
122 129 q.getEuler(&euler);
123 130 rotate(euler.yaw, euler.pitch,euler.roll);
124 - }else{
131 + } else {
125 132 _tempOrientation.set(q);
126 133 _tempOrientation.normalize();
127 134 _orientation = _tempOrientation * _orientation;
  @@ -131,39 +138,41 @@
131 138 }
132 139
133 140 void Camera::rotate(F32 yaw, F32 pitch, F32 roll) {
134 - if (_rotationLocked) return;
141 + if (_rotationLocked) {
142 + return;
143 + }
135 144
136 145 yaw = -yaw * _cameraTurnSpeed;
137 146 pitch = -pitch * _cameraTurnSpeed;
138 147 roll = -roll * _cameraTurnSpeed;
139 148
140 - if(_type == FIRST_PERSON){
149 + if (_type == FIRST_PERSON) {
141 150 _accumPitchDegrees += pitch;
142 151
143 - if (_accumPitchDegrees > 90.0f){
152 + if (_accumPitchDegrees > 90.0f) {
144 153 pitch = 90.0f - (_accumPitchDegrees - pitch);
145 154 _accumPitchDegrees = 90.0f;
146 155 }
147 156
148 - if (_accumPitchDegrees < -90.0f){
157 + if (_accumPitchDegrees < -90.0f) {
149 158 pitch = -90.0f - (_accumPitchDegrees - pitch);
150 159 _accumPitchDegrees = -90.0f;
151 160 }
152 161
153 162 // Rotate camera about the world y axis.
154 163 // Note the order the quaternions are multiplied. That is important!
155 - if (yaw != 0.0f){
164 + if (yaw != 0.0f) {
156 165 _tempOrientation.fromAxisAngle(WORLD_Y_AXIS, yaw);
157 166 _orientation = _tempOrientation * _orientation;
158 167 }
159 168
160 169 // Rotate camera about its local x axis.
161 170 // Note the order the quaternions are multiplied. That is important!
162 - if (pitch != 0.0f){
171 + if (pitch != 0.0f) {
163 172 _tempOrientation.fromAxisAngle(WORLD_X_AXIS, pitch);
164 173 _orientation = _orientation * _tempOrientation;
165 174 }
166 - }else{
175 + } else {
167 176 _tempOrientation.fromEuler(pitch, yaw, roll);
168 177 _orientation *= _tempOrientation;
169 178 }
  @@ -179,7 +188,7 @@
179 188 _eye += _xAxis * dx * _cameraMoveSpeed;
180 189 _eye += WORLD_Y_AXIS * dy * _cameraMoveSpeed;
181 190
182 - if (_type == FIRST_PERSON){
191 + if (_type == FIRST_PERSON) {
183 192 // Calculate the forward direction. Can't just use the camera's local
184 193 // z axis as doing so will cause the camera to move more slowly as the
185 194 // camera's view approaches 90 degrees straight up and down.
  @@ -187,14 +196,16 @@
187 196 forward.cross(WORLD_Y_AXIS, _xAxis);
188 197 forward.normalize();
189 198 _eye += forward * dz * _cameraMoveSpeed;
190 - }else{
199 + } else {
191 200 _eye += _viewDir * dz * _cameraMoveSpeed;
192 201 }
193 202
194 203 _viewMatrixDirty = true;
195 204 }
196 205
197 - const mat4<F32>& Camera::lookAt(const vec3<F32>& eye, const vec3<F32>& target, const vec3<F32>& up) {
206 + const mat4<F32>& Camera::lookAt(const vec3<F32>& eye,
207 + const vec3<F32>& target,
208 + const vec3<F32>& up) {
198 209 _eye = eye;
199 210
200 211 _zAxis = eye - target;
  @@ -232,26 +243,26 @@
232 243 return _viewMatrix;
233 244 }
234 245
235 - void Camera::setAnaglyph(bool rightEye){
246 + void Camera::setAnaglyph(bool rightEye) {
236 247 assert(_isActive);
237 248
238 249 GFX_DEVICE.setAnaglyphFrustum(_camIOD, _zPlanes, _aspectRatio, _verticalFoV, rightEye);
239 - if(rightEye) _eye.x += _camIOD/2;
240 - else _eye.x -= _camIOD/2;
250 + rightEye ? _eye.x += _camIOD / 2 : _eye.x -= _camIOD / 2;
241 251
242 252 _viewMatrixDirty = true;
243 253 }
244 254
245 - ///Tell the rendering API to set up our desired PoV
255 + /// Tell the rendering API to set up our desired PoV
246 256 void Camera::renderLookAt() {
247 257 if (_reflectionRendering){
248 258 _reflectionRendering = false;
249 259 _frustumDirty = true;
250 260 }
261 +
251 262 lookAtInternal();
252 263 }
253 264
254 - void Camera::renderLookAtReflected(const Plane<F32>& reflectionPlane){
265 + void Camera::renderLookAtReflected(const Plane<F32>& reflectionPlane) {
255 266 _reflectionRendering = true;
256 267 _reflectedViewMatrix.reflect(reflectionPlane);
257 268 _reflectedEye = _reflectedViewMatrix * _eye;
  @@ -262,6 +273,7 @@
262 273
263 274 void Camera::lookAtInternal() {
264 275 assert(_isActive);
276 +
265 277 updateMatrices();
266 278
267 279 //Tell the Rendering API to draw from our desired PoV
  @@ -274,7 +286,10 @@
274 286
275 287 }
276 288
277 - void Camera::setProjection(F32 aspectRatio, F32 verticalFoV, const vec2<F32>& zPlanes, bool updateOnSet){
289 + void Camera::setProjection(F32 aspectRatio,
290 + F32 verticalFoV,
291 + const vec2<F32>& zPlanes,
292 + bool updateOnSet) {
278 293 _zPlanes = zPlanes;
279 294 _aspectRatio = aspectRatio;
280 295 _verticalFoV = verticalFoV;
  @@ -287,7 +302,9 @@
287 302 }
288 303 }
289 304
290 - void Camera::setProjection(const vec4<F32>& rect, const vec2<F32>& zPlanes, bool updateOnSet) {
305 + void Camera::setProjection(const vec4<F32>& rect,
306 + const vec2<F32>& zPlanes,
307 + bool updateOnSet) {
291 308 _zPlanes = zPlanes;
292 309 _orthoRect = rect;
293 310 _isOrthoCamera = true;
  @@ -299,10 +316,11 @@
299 316 }
300 317 }
301 318
302 - bool Camera::updateViewMatrix(){
303 - if (!_viewMatrixDirty || _viewMatrixLocked)
319 + bool Camera::updateViewMatrix() {
320 + if (!_viewMatrixDirty || _viewMatrixLocked) {
304 321 return false;
305 -
322 + }
323 +
306 324 _orientation.normalize();
307 325
308 326 // Reconstruct the view matrix.
  @@ -325,7 +343,7 @@
325 343 return true;
326 344 }
327 345
328 - void Camera::updateListeners(){
346 + void Camera::updateListeners() {
329 347 assert(_isActive);
330 348
331 349 for (const DELEGATE_CBK<>& listener : _listeners) {
  @@ -333,11 +351,16 @@
333 351 }
334 352 }
335 353
336 - bool Camera::updateFrustum(){
354 + bool Camera::updateFrustum() {
337 355 assert(_isActive);
338 - if (_frustumLocked) return true;
339 356
340 - if (!_frustumDirty) return false;
357 + if (_frustumLocked) {
358 + return true;
359 + }
360 +
361 + if (!_frustumDirty) {
362 + return false;
363 + }
341 364
342 365 _frustum->Extract();
343 366
  @@ -346,7 +369,7 @@
346 369 return true;
347 370 }
348 371
349 - vec3<F32> Camera::unProject(const vec3<F32>& winCoords) const{
372 + vec3<F32> Camera::unProject(const vec3<F32>& winCoords) const {
350 373 return unProject(winCoords, GFX_DEVICE.getCurrentViewport());
351 374 }
352 375