Subversion Repository Public Repository

Divide-Framework

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

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
#include "Headers/Camera.h"

#include "Platform/Video/Headers/GFXDevice.h"
#include "Managers/Headers/SceneManager.h"

namespace Divide {

Camera::Camera(const stringImpl& name, const CameraType& type, const vec3<F32>& eye)
    : Resource(name),
      _isActive(false),
      _isOrthoCamera(false),
      _projectionDirty(true),
      _viewMatrixDirty(true),
      _viewMatrixLocked(false),
      _rotationLocked(false),
      _movementLocked(false),
      _frustumLocked(false),
      _frustumDirty(true),
      _mouseSensitivity(1.0f),
      _zoomSpeedFactor(35.0f),
      _moveSpeedFactor(35.0f),
      _turnSpeedFactor(35.0f),
      _cameraMoveSpeed(35.0f),
      _cameraZoomSpeed(35.0f),
      _cameraTurnSpeed(35.0f),
      _aspectRatio(1.77f),
      _verticalFoV(60.0f),
      _type(type)
{
    _eye.set(eye);
    _fixedYawAxis.set(WORLD_Y_AXIS);
    _accumPitchDegrees = 0.0f;
    _orientation.identity();
    _viewMatrix.identity();
    _yawFixed = false;
    _zPlanes.set(0.1f, 1.0f);
    _frustum = MemoryManager_NEW Frustum(*this);
}

Camera::~Camera()
{
    MemoryManager::DELETE(_frustum);
}

void Camera::fromCamera(const Camera& camera) {
    if (camera._isOrthoCamera) {
        setProjection(camera._orthoRect,
                      camera.getZPlanes());
    } else {
        setProjection(camera.getAspectRatio(),
                      camera.getVerticalFoV(),
                      camera.getZPlanes());
    }

    lookAt(camera.getEye(), camera.getTarget(), camera.getUpDir());
    setMoveSpeedFactor(camera.getMoveSpeedFactor());
    setTurnSpeedFactor(camera.getTurnSpeedFactor());
    setZoomSpeedFactor(camera.getZoomSpeedFactor());
    setFixedYawAxis(camera._yawFixed, camera._fixedYawAxis);
    lockView(camera._viewMatrixLocked);
    lockFrustum(camera._frustumLocked);

    _accumPitchDegrees = camera._accumPitchDegrees;
}

void Camera::update(const U64 deltaTime) {
    F32 timeFactor = Time::MicrosecondsToSeconds<F32>(deltaTime);
    _cameraMoveSpeed = _moveSpeedFactor * timeFactor;
    _cameraTurnSpeed = _turnSpeedFactor * timeFactor;
    _cameraZoomSpeed = _zoomSpeedFactor * timeFactor;
}

void Camera::updateProjection(bool updateGPU) {
    assert(_isActive);

    if (updateGPU) {
        GFXDevice& gfx = GFX_DEVICE;

        F32* projectionMatrixData = 
            _isOrthoCamera ? gfx.setProjection(_orthoRect,
                                               _zPlanes)

                           : gfx.setProjection(_verticalFoV,
                                               _aspectRatio,
                                               _zPlanes);
        if (_projectionDirty) {
            _projectionMatrix.set(projectionMatrixData);
            _frustumDirty = true;
            _projectionDirty = false;
        }

    } else {
        if (_projectionDirty) {
            if (_isOrthoCamera) {
                _projectionMatrix.ortho(_orthoRect.x,
                                        _orthoRect.y,
                                        _orthoRect.z,
                                        _orthoRect.w,
                                        _zPlanes.x,
                                        _zPlanes.y);
            } else {
                _projectionMatrix.perspective(Angle::DegreesToRadians(_verticalFoV),
                                              _aspectRatio,
                                              _zPlanes.x,
                                              _zPlanes.y);
            }
            _frustumDirty = true;
            _projectionDirty = false;
        }
    }
}

void Camera::onActivate() {
    _isActive = true;
    updateProjection(false);
}

void Camera::onDeactivate() {
    _isActive = false;
}

void Camera::setGlobalRotation(F32 yaw, F32 pitch, F32 roll) {
    if (_rotationLocked) {
        return;
    }

    Quaternion<F32> pitchRot(WORLD_X_AXIS, -pitch);
    Quaternion<F32> yawRot(WORLD_Y_AXIS, -yaw);

    if (!IS_ZERO(roll)) {
        Quaternion<F32> tempOrientation(WORLD_Z_AXIS, -roll);
        setRotation(yawRot * pitchRot * tempOrientation);
    } else {
        setRotation(yawRot * pitchRot);
    }
}

void Camera::rotate(const Quaternion<F32>& q) {
    if (_rotationLocked) {
        return;
    }

    if (_type == CameraType::FIRST_PERSON) {
        vec3<F32> euler;
        q.getEuler(euler);
        rotate(euler.yaw, euler.pitch, euler.roll);
    } else {
        Quaternion<F32> tempOrientation(q);
        tempOrientation.normalize();
        _orientation = tempOrientation * _orientation;
    }

    _viewMatrixDirty = true;
}

void Camera::rotate(F32 yaw, F32 pitch, F32 roll) {
    if (_rotationLocked) {
        return;
    }

    yaw = -yaw * _cameraTurnSpeed;
    pitch = -pitch * _cameraTurnSpeed;
    roll = -roll * _cameraTurnSpeed;

    Quaternion<F32> tempOrientation;
    if (_type == CameraType::FIRST_PERSON) {
        _accumPitchDegrees += pitch;

        if (_accumPitchDegrees > 90.0f) {
            pitch = 90.0f - (_accumPitchDegrees - pitch);
            _accumPitchDegrees = 90.0f;
        }

        if (_accumPitchDegrees < -90.0f) {
            pitch = -90.0f - (_accumPitchDegrees - pitch);
            _accumPitchDegrees = -90.0f;
        }

        // Rotate camera about the world y axis.
        // Note the order the quaternions are multiplied. That is important!
        if (!IS_ZERO(yaw)) {
            tempOrientation.fromAxisAngle(WORLD_Y_AXIS, yaw);
            _orientation = tempOrientation * _orientation;
        }

        // Rotate camera about its local x axis.
        // Note the order the quaternions are multiplied. That is important!
        if (!IS_ZERO(pitch)) {
            tempOrientation.fromAxisAngle(WORLD_X_AXIS, pitch);
            _orientation = _orientation * tempOrientation;
        }
    } else {
        tempOrientation.fromEuler(pitch, yaw, roll);
        _orientation *= tempOrientation;
    }

    _viewMatrixDirty = true;
}

void Camera::move(F32 dx, F32 dy, F32 dz) {
    if (_movementLocked) {
        return;
    }
    dx *= _cameraMoveSpeed;
    dy *= _cameraMoveSpeed;
    dz *= _cameraMoveSpeed;

    _eye += getRightDir() * dx;
    _eye += WORLD_Y_AXIS * dy;

    if (_type == CameraType::FIRST_PERSON) {
        // Calculate the forward 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.
        vec3<F32> forward;
        forward.cross(WORLD_Y_AXIS, getRightDir());
        forward.normalize();
        _eye += forward * dz;
    } else {
        _eye += getForwardDir() * dz;
    }

    _viewMatrixDirty = true;
}

const mat4<F32>& Camera::lookAt(const vec3<F32>& eye,
                                const vec3<F32>& target,
                                const vec3<F32>& up) {
    vec3<F32> xAxis, yAxis, zAxis;

    _eye.set(eye);
    _target.set(target);
    zAxis.set(eye - target);

    zAxis.normalize();
    xAxis.cross(up, zAxis);
    xAxis.normalize();
    yAxis.cross(zAxis, xAxis);
    yAxis.normalize();
    
    _viewMatrix.m[0][0] = xAxis.x;
    _viewMatrix.m[1][0] = xAxis.y;
    _viewMatrix.m[2][0] = xAxis.z;
    _viewMatrix.m[3][0] = xAxis.dot(eye);

    _viewMatrix.m[0][1] = yAxis.x;
    _viewMatrix.m[1][1] = yAxis.y;
    _viewMatrix.m[2][1] = yAxis.z;
    _viewMatrix.m[3][1] = yAxis.dot(eye);

    _viewMatrix.m[0][2] = zAxis.x;
    _viewMatrix.m[1][2] = zAxis.y;
    _viewMatrix.m[2][2] = zAxis.z;
    _viewMatrix.m[3][2] = zAxis.dot(eye);

    // Extract the pitch angle from the view matrix.
    _accumPitchDegrees = Angle::RadiansToDegrees(asinf(zAxis.y));

    _orientation.fromMatrix(_viewMatrix);

    _viewMatrixDirty = false;
    _frustumDirty = true;

    return _viewMatrix;
}

/// Tell the rendering API to set up our desired PoV
void Camera::renderLookAt(mat4<F32>& viewMatrixOut, vec3<F32>& eyeVecOut) {
    updateViewMatrix();
    updateProjection();
    updateFrustum();
    // Inform all listeners of a new event
    updateListeners();

    viewMatrixOut = _viewMatrix;
    eyeVecOut = _eye;
}

void Camera::reflect(const Plane<F32>& reflectionPlane) {
    mat4<F32> reflectedMatrix(getViewMatrix());
    reflectedMatrix.reflect(reflectionPlane);
    lookAt(reflectedMatrix * getEye(), getTarget(), reflectedMatrix * getUpDir());
}

void Camera::setProjection(F32 aspectRatio, F32 verticalFoV,
                           const vec2<F32>& zPlanes, bool updateOnSet) {
    _zPlanes = zPlanes;
    _aspectRatio = aspectRatio;
    _verticalFoV = verticalFoV;
    _isOrthoCamera = false;
    _projectionDirty = true;

    if (updateOnSet) {
        _isActive = true;
        updateProjection();
    }
}

void Camera::setProjection(const vec4<F32>& rect, const vec2<F32>& zPlanes, bool updateOnSet) {
    _zPlanes = zPlanes;
    _orthoRect = rect;
    _isOrthoCamera = true;
    _projectionDirty = true;

    if (updateOnSet) {
        _isActive = true;
        updateProjection();
    }
}

bool Camera::updateViewMatrix() {
    if (!_viewMatrixDirty || _viewMatrixLocked) {
        return false;
    }

    vec3<F32> xAxis, yAxis, zAxis;

    _orientation.normalize();

    // Reconstruct the view matrix.
    _orientation.getMatrix(_viewMatrix);

    xAxis.set(_viewMatrix.m[0][0], _viewMatrix.m[1][0], _viewMatrix.m[2][0]);
    yAxis.set(_viewMatrix.m[0][1], _viewMatrix.m[1][1], _viewMatrix.m[2][1]);
    zAxis.set(_viewMatrix.m[0][2], _viewMatrix.m[1][2], _viewMatrix.m[2][2]);

    _target = -zAxis + _eye;

    _viewMatrix.m[3][0] = -xAxis.dot(_eye);
    _viewMatrix.m[3][1] = -yAxis.dot(_eye);
    _viewMatrix.m[3][2] = -zAxis.dot(_eye);
    _orientation.getEuler(_euler, true);

    _viewMatrixDirty = false;
    _frustumDirty = true;

    return true;
}

void Camera::updateListeners() {
    assert(_isActive);

    for (const DELEGATE_CBK_PARAM<Camera&>& listener : _listeners) {
        listener(*this);
    }
}

bool Camera::updateFrustum() {
    assert(_isActive);

    if (_frustumLocked) {
        return true;
    }

    if (!_frustumDirty) {
        return false;
    }

    _frustum->Extract(_viewMatrix, _projectionMatrix);

    _frustumDirty = false;

    return true;
}

vec3<F32> Camera::unProject(F32 winCoordsX, F32 winCoordsY, F32 winCoordsZ) const {
    const vec4<I32>& viewport = GFX_DEVICE.getCurrentViewport();

    return unProject(winCoordsX, winCoordsY, winCoordsZ, viewport);
}

vec3<F32> Camera::unProject(F32 winCoordsX, F32 winCoordsY, F32 winCoordsZ,
                            const vec4<I32>& viewport) const {
    vec4<F32> temp(winCoordsX, winCoordsY, winCoordsZ, 1.0f);
    temp.x = (temp.x - F32(viewport[0])) / F32(viewport[2]);
    temp.y = (temp.y - F32(viewport[1])) / F32(viewport[3]);

    temp = _frustum->_viewProjectionMatrixCache.getInverse() * (2.0f * temp - 1.0f);
    temp /= temp.w;

    return temp.xyz();
}
};

Commits for Divide-Framework/trunk/Source Code/Rendering/Camera/Camera.cpp

Diff revisions: vs.
Revision Author Commited Message
779 Diff Diff IonutCava picture IonutCava Mon 10 Oct, 2016 15:31:45 +0000

[IonutCava]
- Frustum calculation bug fix

769 Diff Diff IonutCava picture IonutCava Fri 16 Sep, 2016 15:18:09 +0000

[IonutCava]
- Clean up camera class:
— Remove “reflection” specific code and add a generic “reflect” method to allow the calling code to handle camera placements and reflections. This got rid of the “reflectionPlane” member in the RenderParams struct
— Remove some member variables and replace them with on-demand computed variables on the stack.
— Add a “destroyCamera” call to the CameraManager to allow better resource cleanup when needed
- Prepare a rendering system overhaul:
— Add dual screen render targets: current frame and previous frame. Used for future features such as TXAA and Motion Blur.
— Prepare code for an implementation similar to the Doom (2016) system: http://www.adriancourreges.com/blog/2016/09/09/doom-2016-graphics-study/ (the code was already going in that direction)

750 Diff Diff IonutCava picture IonutCava Thu 07 Jul, 2016 16:02:03 +0000

[IonutCava]
- Static analysis based fixes and improvements

744 Diff Diff IonutCava picture IonutCava Tue 28 Jun, 2016 21:43:58 +0000

[IonutCava]
- More performance tweaks

716 Diff Diff IonutCava picture IonutCava Tue 31 May, 2016 16:08:29 +0000

[IonutCava]
- Multithreaded scene load/unload fixes:
— Added postLoadMainThread call for scenes for thread sensitive loading steps
— GUI element adding is a thread sensitive loading step (a fast one) because CEGUI doesn’t handle threading
- Added REALTIME_WITH_CALLBACK task priority for tasks that need an immediate callback when they complete instead of using the flushCallbackQueue system.
- Some work on shadow mapping for getting it to work again
- ShaderPrograms that fail to load can cause a LockManager infinite wait. Delete the lockManager if the shader didn’t load to avoid waiting on commands that never get called.

706 Diff Diff IonutCava picture IonutCava Fri 13 May, 2016 16:29:40 +0000

[IonutCava]
- Renamed LightManager to LightPool and changed it from a Singleton to a scene specific pointer
- More scene load / unload updates (changing scenes now works properly!)
- Allowed GUI interface to hold per-scene elements and only render relevant ones
- Reduced dependencies between nodes, scenemanager, scene, loaders and kernel
- Removed Reflector base class as it was useless and integrate Water class with the Reflection system already in place
- Extended RenderingComponent’s reflection system to allow refraction as well

698 Diff Diff IonutCava picture IonutCava Mon 09 May, 2016 20:21:34 +0000

[IonutCava]
- Remove all anaglyph rendering code. nVidia gained 1 sympathy point with me for VRWorks:
— Single Pass Stereo completely removes the need for double rendering the scene for each eye.
— LiquidVR also support similar technology
-— Once VR development starts for Divide, documentation for both SDKs should be pretty sorted.

692 Diff Diff IonutCava picture IonutCava Wed 27 Apr, 2016 16:24:26 +0000

[IonutCava]
- Resource system cleanup:
— Resource name and resource file location (if any) is now passed only via explicit constructors! (ResourceDescriptors already contain this data, so why use setters?)
- Fix a bug with ortho projection matrix calculation (set m[3][3] to 1. this was skipped when the identity() call was changed to zero() in the ortho() function)
— This also fixed text rendering

656 Diff Diff IonutCava picture IonutCava Fri 04 Mar, 2016 17:23:26 +0000

[Ionut]
- Separate resolution from window size
— Still buggy

601 IonutCava picture IonutCava Wed 09 Dec, 2015 17:12:38 +0000

[IonutCava]
- Shadow mapping system improvements:
— Point and spot light support
— Variable number of debug preview planes
- Light impostor rendering fix and update:
— Displays coloured icons for point and spot lights
- Camera system improvement:
— Camera manager is the only class allowed to instantiate new cameras
- Initial dual paraboloid rendering code
- Remove discard statements from shader code (should use blending if visual artefacts are encountered)