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
384
385
386
387
388
389
#include "Headers/Camera.h"

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

namespace Divide {
	
Camera::Camera(const CameraType& type, const vec3<F32>& eye) : Resource("temp_camera"),
                                                              _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.33f),
                                                              _verticalFoV(60.0f),
                                                              _camIOD(0.5f),
                                                              _type(type)
{
    _eye.set(eye);
    _reflectedEye.set(eye * vec3<F32>(1.0f, -1.0f, 1.0f));
    _xAxis.set(WORLD_X_AXIS);
    _yAxis.set(WORLD_Y_AXIS);
    _zAxis.set(WORLD_Z_AXIS);
    _viewDir.set(WORLD_Z_NEG_AXIS);
    _fixedYawAxis.set(WORLD_Y_AXIS);
    _accumPitchDegrees = 0.0f;
    _orientation.identity();
    _tempOrientation.identity();
    _viewMatrix.identity();
    _reflectedViewMatrix.identity();
    _yawFixed = false;
    _reflectionRendering = false;
    _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());
    }

    setMoveSpeedFactor(camera.getMoveSpeedFactor());
    setTurnSpeedFactor(camera.getTurnSpeedFactor());
    setZoomSpeedFactor(camera.getZoomSpeedFactor());
    setFixedYawAxis(camera._yawFixed, camera._fixedYawAxis);
    lockView(camera._viewMatrixLocked);
    lockFrustum(camera._frustumLocked);
}

void Camera::update(const U64 deltaTime) {
	if (Config::USE_FIXED_TIMESTEP) {
		F32 timeFactor = getUsToSec(Config::SKIP_TICKS);
		_cameraMoveSpeed = _moveSpeedFactor * timeFactor;
		_cameraTurnSpeed = _turnSpeedFactor * timeFactor;
		_cameraZoomSpeed = _zoomSpeedFactor * timeFactor;
	} else {
		F32 timeFactor = getUsToSec(deltaTime);
		_cameraMoveSpeed = _moveSpeedFactor * timeFactor;
		_cameraTurnSpeed = _turnSpeedFactor * timeFactor;
		_cameraZoomSpeed = _zoomSpeedFactor * timeFactor;
	}
}

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

    if (_projectionDirty || force) {
        if (_projectionDirty) {
            _projectionMatrix.set(_isOrthoCamera ? GFX_DEVICE.setProjection(_orthoRect, _zPlanes) : 
                                                   GFX_DEVICE.setProjection(_verticalFoV, _aspectRatio, _zPlanes));
            _projectionDirty = false;
            _frustumDirty = true;
        }else{
            _isOrthoCamera ? GFX_DEVICE.setProjection(_orthoRect, _zPlanes) : 
                             GFX_DEVICE.setProjection(_verticalFoV, _aspectRatio, _zPlanes);
        }
    }
}

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

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)) {
        _tempOrientation.fromAxisAngle(WORLD_Z_AXIS, -roll);
        setRotation(yawRot * pitchRot * _tempOrientation);
    } else {
        setRotation(yawRot * pitchRot);
    }
}

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

    if (_type == FIRST_PERSON) {
        vec3<F32> euler;
        q.getEuler(&euler);
        rotate(euler.yaw, euler.pitch,euler.roll);
    } else {
        _tempOrientation.set(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;

    if (_type == 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 (yaw != 0.0f) {
            _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 (pitch != 0.0f) {
            _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;
    }

    _eye += _xAxis * dx * _cameraMoveSpeed;
    _eye += WORLD_Y_AXIS * dy * _cameraMoveSpeed;

    if (_type == 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, _xAxis);
        forward.normalize();
        _eye += forward * dz * _cameraMoveSpeed;
    } else {
        _eye += _viewDir * dz * _cameraMoveSpeed;
    }

    _viewMatrixDirty = true;
}

const mat4<F32>& Camera::lookAt(const vec3<F32>& eye, 
                                const vec3<F32>& target, 
                                const vec3<F32>& up) {
    _eye = eye;

    _zAxis = eye - target;
    _zAxis.normalize();
    _xAxis.cross(up, _zAxis);
    _xAxis.normalize();
    _yAxis.cross(_zAxis, _xAxis);
    _yAxis.normalize();

    _viewDir = -_zAxis;

    _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 = DEGREES(asinf(_zAxis.y));

    _orientation.fromMatrix(_viewMatrix);

    _viewMatrixDirty = false;
    _frustumDirty = true;

    return _viewMatrix;
}

void Camera::setAnaglyph(bool rightEye) {
    assert(_isActive);

    GFX_DEVICE.setAnaglyphFrustum(_camIOD, _zPlanes, _aspectRatio, _verticalFoV, rightEye);
    rightEye ? _eye.x += _camIOD / 2 : _eye.x -= _camIOD / 2;

    _viewMatrixDirty = true;
}

/// Tell the rendering API to set up our desired PoV
void Camera::renderLookAt() {
    if (_reflectionRendering){
        _reflectionRendering = false;
        _frustumDirty = true;
    }

    lookAtInternal();
}

void Camera::renderLookAtReflected(const Plane<F32>& reflectionPlane) {
    _reflectionRendering = true;
    _reflectedViewMatrix.reflect(reflectionPlane);
    _reflectedEye        = _reflectedViewMatrix * _eye;
    _frustumDirty = true;

    lookAtInternal();
}

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

    updateMatrices();

    //Tell the Rendering API to draw from our desired PoV
    GFX_DEVICE.lookAt(_reflectionRendering ? (_reflectedViewMatrix * _viewMatrix) : _viewMatrix,
                      _reflectionRendering ? _reflectedEye : _eye);

    updateFrustum();
    //Inform all listeners of a new event
    updateListeners();

}

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;
    }

    _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]);
    _viewDir = -_zAxis;
    _target = _viewDir + _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<>& listener : _listeners) {
		listener();
	}
}

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

    if (_frustumLocked) {
        return true;
    }

    if (!_frustumDirty) {
        return false;
    }

    _frustum->Extract();

    _frustumDirty = false;

    return true;
}

vec3<F32> Camera::unProject(const vec3<F32>& winCoords) const {
    return unProject(winCoords, GFX_DEVICE.getCurrentViewport());
}

vec3<F32> Camera::unProject(const vec3<F32>& winCoords, const vec4<I32>& viewport) const {
    mat4<F32> cameraVP =  _frustum->_viewProjectionMatrixCache.getInverse();

    vec4<F32> temp(winCoords, 1.0f);
    temp.x = (temp.x - F32(viewport[0])) / F32(viewport[2]);
    temp.y = (temp.y - F32(viewport[1])) / F32(viewport[3]);

    temp = cameraVP * (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
331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

326 Diff Diff IonutCava picture IonutCava Tue 30 Sep, 2014 21:11:32 +0000

[Ionut]
- Fixed more memory leaks
- Simplified Task interface and timing System
- Improved compatibility between Boost, STL and EASTL with random combinations of vectors, strings and maps
- Simplified Light class by removing the “slot” member
- Random optimizations

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

289 Diff Diff IonutCava picture IonutCava Fri 25 Jul, 2014 17:46:28 +0000

[Ionut]
- Added transform interpolation support to avoid object stuttering when frame rate varies (see Transform.cpp)
- Updated the quaternion class to be a wrapper around a regular vec4 to take advantage of it’s built in functions (length, dot, cross, etc)

283 Diff Diff IonutCava picture IonutCava Tue 01 Jul, 2014 18:16:16 +0000

[Ionut][[BR]]
- Upload Viewport, camera position and zPlanes data to the gpu using the _matricesBuffer instead of ShaderProgram’s “uploadNodeMatrices” method [[BR]]
- Removed most matrices constructed with the worldMatrix (worldView, worldViewProjection, etc) to allow better batching of transforms. [[BR]]
— Both changes will allow the elimination of the “uploadNodeMatrices” and “setLoD” methods [[BR]]

268 Diff Diff IonutCava picture IonutCava Wed 04 Jun, 2014 10:20:27 +0000

[IonutCava] [[BR]]
- Terrain and vegetation updates: [[BR]]
— Use standard phong lighting model [[BR]]
— Separate grass patches per chunk [[BR]]
— Reduce number of required texture bind points for terrain by adding both textures and normal maps in a single texture array [[BR]]
— Draw terrain with 2 drawcalls (using glMultiDrawElements twice, once for LoD == 0 and once for LoD > 0) [[BR]]
[[BR]]
- Buffer management updates: [[BR]]
— Unified buffer binding system [[BR]]
— Made Uniform buffer objects visible to all classes and moved lightUBO to LightManager, matricesUBO to GFXDevice etc [[BR]]
— Added initial support for Shader Buffer Objects [[BR]]
— Moved viewport management to GFXDevice class [[BR]]
[[BR]]
- Rendering improvements: [[BR]]
— Added Hi-Z occlusion culling to instanceCull shader (using Hi-Z depth buffer constructed in GFXDevice class) [[BR]]
— Resolved issues with shadow maps overriding texture bind locations [[[BR]]
— Improved shader and shader program loading [[BR]]
— Improved material transparency support [[BR]]
— Added buffer lock manager class from “Approaching Zero Driver Overhead in OpenGL (Presented by NVIDIA)” speech at GDC14 [[BR]]

264 Diff Diff k1ngp1n picture k1ngp1n Tue 27 May, 2014 13:04:38 +0000

[Ionut] [[BR]]
- New camera system: [[BR]]
— Stack based (push/pop active camera) [[BR]]
— Per camera frustum [[BR]]
— Per camera projection [[BR]]
— Shadow mapping now uses the new camera system (each light has a shadow camera) [[BR]]
[[BR]]
- New terrain, water and vegetation system: [[BR]]
— Direct import of terrain from L3DT both as 8-bit grayscale and 16-bit raw formats [[BR]]
— Multiple alpha maps for texture splatting support [[BR]]
— Per texture detail map support [[BR]]
— Transform feedback based visibility culling for vegetation [[BR]]
— Alpha-map based vegetation placing on terrain [[BR]]
— Water now uses a refraction map instead of alpha blending for better refraction effects [[BR]]
[[BR]]
- SceneGraph improvements: [[BR]]
— Better bounding box updates from child to parent [[BR]]
— Better onDraw / postDraw implementation [[BR]]
[[BR]]
- Performance improvements: [[BR]]
— Batch 2D rendering tasks together as much as possible[[BR]]
— Improve renderStateBlock hash generation for better sorting [[BR]]
— Move most state related gfx changes from API implementation to the GFXDevice class [[BR]]
— Move viewport stack to the GFXDevice class [[BR]]
— Added various performance counters (frame duration with round-robin query system, draw call count, etc) [[BR]]
— Fixed and improved clip plane management [[BR]]
— Delete client-side texture data after GPU upload [[BR]]
[[BR]]
- New features: [[BR]]
— Added callback support to the Task class so that a specific task may call a function in the main thread upon completion [[BR]]
— Added mouse-wheel zoom support to Orbit/ThirdPerson cameras [[BR]]
— Added 2D Texture Array support (e.g. used for terrain textures) [[BR]]

260 k1ngp1n picture k1ngp1n Sat 03 May, 2014 19:33:41 +0000

[Ionut] [[BR]]
- More work on CSM. Reverted back to VSM to allow easier blurring and filtering [[BR]]
- Rewrote FrameBuffer classes to fix MSAA and memory leaks that occurred on resize (double creation of textures) [[BR]]
— Removed FBType. FrameBuffer types depend only on attachment types and if it’s MSAA enabled or not [[BR]]
- Rewrote RenderState classes to be API agnostic [[BR]]
- Bug fixes in math classes [[BR]]
- Changed “resolution change” logic [[BR]]
- Simplified some options in config.xml [[BR]]
- Improved per-light shadow options [[BR]]