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
390
391
392
393
394
395
396
397
398
399
400
/*
   Copyright (c) 2014 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy of this software
   and associated documentation files (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _CAMERA_H
#define _CAMERA_H

#include "core.h"

#include "Frustum.h"
#include "Core/Math/Headers/Quaternion.h"
#include "Core/Resources/Headers/Resource.h"
#include "Hardware/Input/Headers/EventHandler.h"

namespace Divide {

class Camera : public Resource {
public:
    enum CameraType {
        FREE_FLY,
        FIRST_PERSON,
        THIRD_PERSON,
        ORBIT,
        SCRIPTED
    };

public:
    Camera(const CameraType& type, const vec3<F32>& eye = VECTOR3_ZERO);
    virtual ~Camera();

    void fromCamera(const Camera& camera);

    virtual void update(const U64 deltaTime);

    /// Rendering calls
    /// Creates a viewMatrix and passes it to the rendering API
    void renderLookAt();
    /// Creates a reflection matrix using the specified plane height and passes it to the rendering API
    void renderLookAtReflected(const Plane<F32>& reflectionPlane);
    /// Creates the appropriate eye offset and frustum depending on the desired eye.
    /// This method does not calls "renderLookAt"
    /// rightEye = false => left eye's frustum+view; rightEye = true => right eye's frustum + view.
    void setAnaglyph(bool rightEye = false);
    /// Moves the camera by the specified offsets in each direction
    virtual void move(F32 dx, F32 dy, F32 dz);
    /// Global rotations are applied relative to the world axis, not the camera's
    void setGlobalRotation(F32 yaw, F32 pitch, F32 roll = 0.0f);
    /// Sets the camera's position, target and up directions
    const mat4<F32>& lookAt(const vec3<F32>& eye, 
                            const vec3<F32>& target, 
                            const vec3<F32>& up = WORLD_Y_AXIS);
    /// Rotates the camera (changes its orientation) by the specified quaternion (_orientation *= q)
    void rotate(const Quaternion<F32>& q);
    /// Sets the camera to point at the specified target point
    inline const mat4<F32>&  lookAt(const vec3<F32>& target) { 
        return lookAt(_eye, target, _yAxis); 
    }
    /// Sets the camera's orientation to match the specified yaw, pitch and roll values;
    /// Creates a quaternion based on the specified euler angles and calls "rotate" to change 
    /// the orientation
    virtual void rotate(F32 yaw, F32 pitch, F32 roll);
    /// Creates a quaternion based on the specified axis-angle and calls "rotate" to change 
    /// the orientation
    inline void rotate(const vec3<F32>& axis, F32 angle) { 
        rotate(Quaternion<F32>(axis,
                               angle * _cameraTurnSpeed)); 
    }
    /// Yaw, Pitch and Roll call "rotate" with a apropriate quaternion for  each rotation.
    /// Because the camera is facing the -Z axis, a positive angle will create a positive Yaw
    /// behind the camera and a negative one in front of the camera 
    /// (so we invert the angle - left will turn left when facing -Z)
    inline void rotateYaw(F32 angle) { 
        rotate(Quaternion<F32>(_yawFixed ? _fixedYawAxis : _orientation * WORLD_Y_AXIS,
                               -angle * _cameraTurnSpeed)); 
    }
    /// Change camera's roll.
    inline void rotateRoll(F32 angle) {
        rotate(Quaternion<F32>(_orientation * WORLD_Z_AXIS,
                               -angle * _cameraTurnSpeed)); 
    }
    /// Change camera's pitch
    inline void rotatePitch(F32 angle) { 
        rotate(Quaternion<F32>(_orientation * WORLD_X_AXIS,
                               -angle * _cameraTurnSpeed)); 
    }
    /// Sets the camera's Yaw angle.
    /// This creates a new orientation quaternion for the camera and extracts the euler angles
    inline void setYaw(F32 angle) { 
        setRotation(angle, _euler.pitch, _euler.roll); 
    }
    /// Sets the camera's Pitch angle. Yaw and Roll are previous extracted values
    inline void setPitch(F32 angle) {
        setRotation(_euler.yaw, angle, _euler.roll); 
    }
    /// Sets the camera's Roll angle. Yaw and Pitch are previous extracted values
    inline void setRoll(F32 angle) { 
        setRotation(_euler.yaw, _euler.pitch, angle); 
    }
    /// Sets the camera's Yaw angle. 
    /// This creates a new orientation quaternion for the camera and extracts the euler angles
    inline void setGlobalYaw(F32 angle) { 
        setGlobalRotation(angle, _euler.pitch, _euler.roll); 
    }
    /// Sets the camera's Pitch angle. Yaw and Roll are previous extracted values
    inline void setGlobalPitch(F32 angle) { 
        setGlobalRotation(_euler.yaw, angle, _euler.roll); 
    }
    /// Sets the camera's Roll angle. Yaw and Pitch are previous extracted values
    inline void setGlobalRoll(F32 angle) { 
        setGlobalRotation(_euler.yaw, _euler.pitch, angle); 
    }
    /// Moves the camera forward or backwards
    inline void moveForward(F32 factor) { 
        move(0.0f, 0.0f, factor * _cameraMoveSpeed); 
    }
    /// Moves the camera left or right
    inline void moveStrafe(F32 factor) { 
        move(factor * _cameraMoveSpeed, 0.0f, 0.0f); 
    }
    /// Moves the camera up or down
    inline void moveUp(F32 factor) { 
        move(0.0f, factor * _cameraMoveSpeed, 0.0f); 
    }
    /// Anaglyph rendering: Set intra-ocular distance
    inline void setIOD(F32 distance) { 
        _camIOD = distance; 
    }

    inline void setEye(F32 x, F32 y, F32 z) { 
        _eye.set(x,y,z);
        _viewMatrixDirty = true; 
    }

    inline void setEye(const vec3<F32>& position) { 
        _eye = position;
        _viewMatrixDirty = true;
    }

    inline void setRotation(const Quaternion<F32>& q) { 
        _orientation = q;
        _viewMatrixDirty = true; 
    }

    inline void setRotation(F32 yaw, F32 pitch, F32 roll = 0.0f) { 
        setRotation(Quaternion<F32>(-pitch, -yaw, -roll)); 
    }

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

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

    inline void setHorizontalFoV(F32 horizontalFoV) { 
        _verticalFoV = Util::xfov_to_yfov(horizontalFoV, _aspectRatio); 
        _projectionDirty = true;
    }

    /// Mouse sensitivity: amount of pixels per radian (this should be moved out of the camera class)
    inline void setMouseSensitivity(F32 sensitivity) {
        _mouseSensitivity = sensitivity;
    }

    inline void setMoveSpeedFactor(F32 moveSpeedFactor) {
        _moveSpeedFactor = moveSpeedFactor;
    }

    inline void setTurnSpeedFactor(F32 turnSpeedFactor) { 
        _turnSpeedFactor = turnSpeedFactor;
    }

    inline void setZoomSpeedFactor(F32 zoomSpeedFactor) { 
        _zoomSpeedFactor = zoomSpeedFactor;
    }

    /// Exactly as in Ogre3D: locks the yaw movement to the specified axis
    inline void setFixedYawAxis(bool useFixed, const vec3<F32>& fixedAxis = WORLD_Y_AXIS) { 
        _yawFixed = useFixed; 
        _fixedYawAxis = fixedAxis; 
    }

    /// Getter methods.
    inline F32 getMouseSensitivity() const {
        return _mouseSensitivity;
    }

    inline F32 getMoveSpeedFactor() const {
        return _moveSpeedFactor;
    }

    inline F32 getTurnSpeedFactor() const {
        return _turnSpeedFactor;
    }

    inline F32 getZoomSpeedFactor() const {
        return _zoomSpeedFactor;
    }

    inline const CameraType& getType() const {
        return _type; 
    }

    inline const vec3<F32>&  getEye() const {
        return _reflectionRendering ? _reflectedEye : _eye; 
    }

    inline vec3<F32> getViewDir() const {
        return _reflectionRendering ? _reflectedViewMatrix * _viewDir : _viewDir; 
    }

    inline vec3<F32> getUpDir() const {
        return _reflectionRendering ? _reflectedViewMatrix * _yAxis : _yAxis; 
    }

    inline vec3<F32> getRightDir() const {
        return _reflectionRendering ? _reflectedViewMatrix * _xAxis : _xAxis; 
    }

    inline const vec3<F32>&  getEuler() const {
        return _euler;
    }

    inline const vec3<F32>&  getTarget() const {
        return _target;
    }

    inline const mat4<F32>&  getProjectionMatrix() const { 
        return _projectionMatrix; 
    }

    inline const vec2<F32>&  getZPlanes() const { 
        return _zPlanes; 
    }

    inline const F32 getVerticalFoV() const { 
        return _verticalFoV; 
    }

    inline const F32 getHorizontalFoV() const { 
        return Util::yfov_to_xfov(_verticalFoV, _aspectRatio); 
    }

    inline const F32 getAspectRatio() const { 
        return _aspectRatio; 
    }

    inline const mat4<F32>&  getViewMatrix() { 
        updateViewMatrix();
        return _reflectionRendering ? _reflectedViewMatrix : _viewMatrix;
    }

    inline const mat4<F32>&  getWorldMatrix() { 
        if ( updateViewMatrix() ) {
            _viewMatrix.getInverse( _worldMatrix );
        }

        return _worldMatrix; 
    }

    /// Nothing really to unload
    virtual bool unload() {
        return true;
    }
    /// Add an event listener called after every RenderLookAt or RenderLookAtCube call
	virtual void addUpdateListener(const DELEGATE_CBK<>& f) { 
        _listeners.push_back(f); 
    }
    /// Informs all listeners of a new event
    virtual void updateListeners();
    /// Clear all listeners from the current camera
    virtual void clearListeners() { 
        _listeners.clear(); 
    }
    /// Inject mouse events
    virtual bool mouseMoved(const Input::MouseEvent& arg) {
        return true;
    }
    /// Called when the camera becomes active
    virtual void onActivate();
    /// Called when the camera becomes inactive
    virtual void onDeactivate();

    void setProjection(F32 aspectRatio, 
                       F32 verticalFoV, 
                       const vec2<F32>& zPlanes, 
                       bool updateOnSet = false);

    void setProjection(const vec4<F32>& rect, 
                       const vec2<F32>& zPlanes, 
                       bool updateOnSet = false);
    /// Extract the frustum associated with our current PoV
    virtual bool updateFrustum();
    /// Get the camera's current frustum
    inline const Frustum& getFrustumConst() const { 
        return *_frustum; 
    }

    inline Frustum& getFrustum() { 
        return *_frustum; 
    }

    inline void lockMovement(bool state) { 
        _movementLocked = state;
    }

    inline void lockRotation(bool state) { 
        _rotationLocked = state; 
    }

    inline void lockView(bool state) { 
        _viewMatrixLocked = state; 
    }

    inline void lockFrustum(bool state) { 
        _frustumLocked = state; 
    }

    /// Get the world space pozition from the specified screen coordinates 
    /// (use winCoords.z for depth from 0 to 1)
    vec3<F32> unProject(const vec3<F32>& winCoords) const;

    /// Get the world space pozition from the specified screen coordinates 
    /// (use winCoords.z for depth from 0 to 1)
    vec3<F32> unProject(const vec3<F32>& winCoords, 
                        const vec4<I32>& viewport) const;

protected:
    virtual void lookAtInternal();
    virtual bool updateViewMatrix();
    virtual void updateProjection(bool force = false);

    inline  void updateMatrices() {
        updateViewMatrix();
        updateProjection();
    }

protected:
    mat4<F32>       _viewMatrix;
    mat4<F32>       _worldMatrix;
    mat4<F32>       _projectionMatrix;
    mat4<F32>       _reflectedViewMatrix;
    Quaternion<F32> _orientation, _tempOrientation;
    vec3<F32> _eye, _reflectedEye, _viewDir, _target;
    vec3<F32> _xAxis, _yAxis, _zAxis;
    vec3<F32> _euler, _fixedYawAxis;
    vec2<F32>  _zPlanes;
    vec4<F32>  _orthoRect;

    F32 _verticalFoV;
    F32 _aspectRatio;
    F32 _accumPitchDegrees;
    F32 _turnSpeedFactor;
    F32 _moveSpeedFactor;
    F32 _mouseSensitivity;
    F32 _zoomSpeedFactor;
    F32 _cameraMoveSpeed;
    F32 _cameraTurnSpeed;
    F32 _cameraZoomSpeed;
    F32 _camIOD;
    CameraType	_type;

	vectorImpl<DELEGATE_CBK<> > _listeners;
    bool            _isActive;
    bool            _projectionDirty;
    bool            _viewMatrixDirty;
    bool            _viewMatrixLocked;
    bool            _frustumLocked;
    bool            _rotationLocked;
    bool            _movementLocked;
    bool            _yawFixed;
    bool            _isOrthoCamera;
    bool            _reflectionRendering;
    bool            _frustumDirty;
    Frustum*        _frustum;
};

}; //namespace Divide
#endif

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

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)

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)

309 Diff Diff IonutCava picture IonutCava Sat 16 Aug, 2014 22:57:33 +0000

[Ionut]
- Added an InputAggregator interface to standardize input event handling

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]]

259 Diff Diff k1ngp1n picture k1ngp1n Sun 27 Apr, 2014 18:24:52 +0000

[Ionut][[BR]]
- Implemented Exponential Stable Cascaded Shadow Maps for directional lights (still buggy, but basically working) [[BR]]
— CSM settings (resolution, number of splits, etc) are part of the scene render state [[BR]]
- SGN can skip rendering for the current frame if they are not ready (e.g. OnDraw can now return false or materials can fail to bind) [[BR]]
- Frustum class is now using the Plane class [[BR]]
- Removed Math SIMD implementation as the compiler generated code will always be faster and safer [[BR]]
- Added a “STUBBED” macro for outputing to the error stream of important messages (instead of using #pragma message) [[BR]]
- Added “freeze time” logic to pause/resume updates with or without freezing the GUI too [[BR]]
- Added “processGUI” method the every Scene class to separate time-sensitive GUI updates from game logic loop [[BR]]
- GLSL <-> Vertex Buffer bind locations are automatically set for every vertex shader [[BR]]
- Added basic support for compute shaders [[BR]]
- Removed most OS define checks to make porting to a new platform easier [[BR]]
- Texture transparency check per-pixel is now using OpenMP [[BR]]
- Texture samplers can now set a border color for CLAMP_TO_BORDER wrap mode [[BR]]
- Removed “Object” suffix from GFX Classes (pixel buffer, vertex buffer, frame buffer, etc) [[BR]]
- Stop processing scene input when application looses focus [[BR]]
- SceneGraphNode class is now responsible for updating SGNComponents on draw and calling its _node’s OnDraw method [[BR]]
- UBOs can now list all of the active uniforms inside a block with info such as type and offset [[BR]]
- Removed deprecated ‘GLCheck’ macro [[BR]]
- A lot of bug fixes and performance improvements in various places [[BR]]

242 Diff Diff k1ngp1n picture k1ngp1n Mon 03 Feb, 2014 21:05:28 +0000

[Ionut] [[BR]]
- Smoother third person camera [[BR]]
- Smoother Character movement [[BR]]
- Limit third person camera rotations (-179 ... + 179 yaw, -31.5 ... +60 pitch) [[BR]]
- Character and third person camera performance optimizations [[BR]]

241 k1ngp1n picture k1ngp1n Sun 02 Feb, 2014 22:07:51 +0000

[Ionut] [[BR]]
- Implemented a basic ThirdPerson camera [[BR]]
— Does not limit rotations yet [[BR]]
— Does not have on click and drag functionality yet [[BR]]