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

namespace Divide {

Camera::Camera(const stringImpl& name, const CameraType& type, const vec3<F32>& eye)
    : Resource(ResourceType::DEFAULT, name),
      _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(Camera& camera) {
    camera.updateLookAt();

    setMoveSpeedFactor(camera.getMoveSpeedFactor());
    setTurnSpeedFactor(camera.getTurnSpeedFactor());
    setZoomSpeedFactor(camera.getZoomSpeedFactor());
    setFixedYawAxis(camera._yawFixed, camera._fixedYawAxis);
    lockMovement(camera._movementLocked);
    lockRotation(camera._rotationLocked);
    _mouseSensitivity = camera._mouseSensitivity;
    _cameraMoveSpeed = camera._cameraMoveSpeed;
    _cameraTurnSpeed = camera._cameraTurnSpeed;
    _cameraZoomSpeed = camera._cameraZoomSpeed;

    lockView(camera._viewMatrixLocked);
    lockFrustum(camera._frustumLocked);
    _accumPitchDegrees = camera._accumPitchDegrees;

    if (camera._isOrthoCamera) {
        setAspectRatio(camera.getAspectRatio());
        setVerticalFoV(camera.getVerticalFoV());

        setProjection(camera._orthoRect,
                      camera.getZPlanes());
    } else {
        _orthoRect.set(camera._orthoRect);

        setProjection(camera.getAspectRatio(),
                      camera.getVerticalFoV(),
                      camera.getZPlanes());
    }

    setEye(camera.getEye());
    setRotation(camera._orientation);
    updateLookAt();
}

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

void Camera::setActiveInternal(bool state) {
    // nothing yet;
    ACKNOWLEDGE_UNUSED(state);
}


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)) {
        setRotation(yawRot * pitchRot * Quaternion<F32>(WORLD_Z_AXIS, -roll));
    } 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(Angle::DEGREES<F32> yaw, Angle::DEGREES<F32> pitch, Angle::DEGREES<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) {
    _eye.set(eye);
    _target.set(target);
    _viewMatrix.lookAt(_eye, _target, up);
    // Extract the pitch angle from the view matrix.
    _accumPitchDegrees = Angle::to_DEGREES(std::asinf(getForwardDir().y));
    _orientation.fromLookAt(_eye - _target, up);

    _viewMatrixDirty = false;
    _frustumDirty = true;

    return _viewMatrix;
}

/// Tell the rendering API to set up our desired PoV
void Camera::updateLookAt() {
    bool viewMatrixUpdated = updateViewMatrix();
    bool projMatrixUpdated = updateProjection();
    bool frustumUpdated = updateFrustum();
    if (viewMatrixUpdated || projMatrixUpdated || frustumUpdated) {
        onUpdate(*this);
    }
}

void Camera::reflect(const Plane<F32>& reflectionPlane) {
    mat4<F32> reflectedMatrix;
    reflectedMatrix.reflect(reflectionPlane);

    lookAt(reflectedMatrix * getEye(),
           getTarget(),
           reflectedMatrix * getUpDir());
}

bool Camera::updateProjection() {
    if (_projectionDirty) {
        if (_isOrthoCamera) {
            _projectionMatrix.ortho(_orthoRect.x,
                                    _orthoRect.y,
                                    _orthoRect.z,
                                    _orthoRect.w,
                                    _zPlanes.x,
                                    _zPlanes.y);
        } else {
            _projectionMatrix.perspective(Angle::to_RADIANS(_verticalFoV),
                                          _aspectRatio,
                                          _zPlanes.x,
                                          _zPlanes.y);
        }

        _frustumDirty = true;
        _projectionDirty = false;
        return true;
    }

    return false;
}

const mat4<F32>& Camera::setProjection(F32 aspectRatio, F32 verticalFoV, const vec2<F32>& zPlanes) {
    setAspectRatio(_aspectRatio);
    setVerticalFoV(verticalFoV);

    _zPlanes = zPlanes;
    _isOrthoCamera = false;
    _projectionDirty = true;
    updateProjection();

    return getProjectionMatrix();
}

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

     return getProjectionMatrix();
}

void Camera::setAspectRatio(F32 ratio) {
    _aspectRatio = ratio;
    _projectionDirty = true;
}

void Camera::setVerticalFoV(F32 verticalFoV) {
    _verticalFoV = verticalFoV;
    _projectionDirty = true;
}

void Camera::setHorizontalFoV(F32 horizontalFoV) {
    _verticalFoV = Angle::to_DEGREES(2.0f * std::atan(tan(Angle::to_RADIANS(horizontalFoV) * 0.5f) / _aspectRatio));
    _projectionDirty = true;
}

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);
    _euler = Angle::to_DEGREES(_euler);

    _viewMatrixDirty = false;
    _frustumDirty = true;

    return true;
}

bool Camera::updateFrustum() {
    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 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
868 Diff Diff IonutCava picture IonutCava Sat 25 Feb, 2017 21:46:55 +0000

[IonutCava]
- Fix buggy SSE Quaternion math
- Fix some typos and naming issues

867 Diff Diff IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.

853 Diff Diff IonutCava picture IonutCava Tue 07 Feb, 2017 21:43:58 +0000

[IonutCava]
- More work on water rendering.

850 Diff Diff IonutCava picture IonutCava Mon 06 Feb, 2017 17:04:39 +0000

[IonutCava]
- More code rot fixes
- Separate Planar reflectors/refractors from Cube R/R
- Now passing detail levels to shaders

824 Diff Diff IonutCava picture IonutCava Thu 19 Jan, 2017 17:18:03 +0000

[IonutCava]
- Prev Frame attachment system removed from RenderTargets
— Concept too high level for RT. Moved prev depth buffer to GFX class
- Other small optimizations.

823 Diff Diff IonutCava picture IonutCava Wed 18 Jan, 2017 17:11:00 +0000

[IonutCava]
- Fix some issues with Camera initialisation code.
- ToDo: Cleanup camera class. (e.g. setRotation / setGlobalRotation calls)

818 Diff Diff IonutCava picture IonutCava Mon 16 Jan, 2017 17:23:20 +0000

[IonutCava]
- Units get their bound node setup by the SGN UnitComponent and not during creation
- Profile guided optimizations

801 Diff Diff IonutCava picture IonutCava Sun 27 Nov, 2016 21:28:01 +0000

[IonutCava]
- More performance analysis guided optimizations.
- Some refactoring to allow later removal of Singletons status for: GFXDevice, SFXDevice, PXDevice, GUI and Input.
— Pass a PlatformContext around objects to access these classes.

788 Diff Diff IonutCava picture IonutCava Fri 21 Oct, 2016 16:11:37 +0000

[IonutCava]
- Added support for the Arena Allocator by Mamasha Knows (http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf)
— Used for GFX Related objects: Textures, shaders, etc

784 IonutCava picture IonutCava Mon 17 Oct, 2016 16:20:49 +0000

[IonutCava]
- Removed the CameraManager and move most of its functionality into static members of the Camera class
- Removed Camera push/pop system in favour of a stateless ptr based system to better fit the RenderPass structure