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
/*
   Copyright (c) 2016 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 "Frustum.h"
#include "Core/Math/Headers/Quaternion.h"
#include "Core/Resources/Headers/Resource.h"
#include "Platform/Input/Headers/EventHandler.h"

namespace Divide {

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

   public:

    void fromCamera(const Camera& camera);

    virtual void update(const U64 deltaTime);

    void updateLookAt();

    void reflect(const Plane<F32>& reflectionPlane);
    /// 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);
    inline void setGlobalRotation(const vec3<F32>& euler) {
        setGlobalRotation(euler.yaw, euler.pitch, euler.roll);
    }
    /// 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, getUpDir());
    }
    /// 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);
    }
    /// Moves the camera left or right
    inline void moveStrafe(F32 factor) {
        move(factor, 0.0f, 0.0f);
    }
    /// Moves the camera up or down
    inline void moveUp(F32 factor) {
        move(0.0f, factor, 0.0f);
    }

    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 = Angle::RadiansToDegrees(
            2.0f *
            std::atan(tan(Angle::DegreesToRadians(horizontalFoV) * 0.5f) /
                      _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 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 _eye;
    }

    inline vec3<F32> getUpDir() const {
        const mat4<F32>& viewMat = getViewMatrix();
        return vec3<F32>(viewMat.m[0][1], viewMat.m[1][1], viewMat.m[2][1]);
    }

    inline vec3<F32> getRightDir() const {
        const mat4<F32>& viewMat = getViewMatrix();
        return vec3<F32>(viewMat.m[0][0], viewMat.m[1][0], viewMat.m[2][0]);

    }

    inline vec3<F32> getForwardDir() const {
        const mat4<F32>& viewMat = getViewMatrix();
        return vec3<F32>(-viewMat.m[0][2], -viewMat.m[1][2], -viewMat.m[2][2]);
    }

    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 vec4<F32>& orthoRect() const { return _orthoRect; }

    inline bool isOrthoProjected() const { return _isOrthoCamera; }

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

    inline const F32 getHorizontalFoV() const {
        F32 halfFoV = Angle::DegreesToRadians(_verticalFoV) * 0.5f;
        return Angle::RadiansToDegrees(2.0f *
                                       std::atan(tan(halfFoV) * _aspectRatio));
    }

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

    inline const mat4<F32>& getViewMatrix() const {
        return _viewMatrix;
    }

    inline const mat4<F32>& getViewMatrix(bool update = false) {
        if (update) {
            updateViewMatrix();
        }

        return _viewMatrix;
    }

    inline void getWorldMatrix(mat4<F32>& worldMatOut) {
        getViewMatrix().getInverse(worldMatOut);
    }

    /// 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_PARAM<Camera&>& 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)
    inline vec3<F32> unProject(const vec3<F32>& winCoords) const {
        return unProject(winCoords.x, winCoords.y, winCoords.z);
    }

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

    vec3<F32> unProject(F32 winCoordsX, F32 winCoordsY, F32 winCoordsZ) const;
    vec3<F32> unProject(F32 winCoordsX, F32 winCoordsY, F32 winCoordsZ, const vec4<I32>& viewport) const;

   protected:
    virtual bool updateViewMatrix();
    virtual void updateProjection();

   protected:
    SET_DELETE_FRIEND
    SET_DELETE_HASHMAP_FRIEND
    friend class CameraManager;
    explicit Camera(const stringImpl& name, const CameraType& type, const vec3<F32>& eye = VECTOR3_ZERO);
    virtual ~Camera();

   protected:
    mat4<F32> _viewMatrix;
    mat4<F32> _projectionMatrix;
    Quaternion<F32> _orientation;
    vec3<F32> _eye, _target;
    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;
    CameraType _type;

    vectorImpl<DELEGATE_CBK_PARAM<Camera&> > _listeners;
    bool _projectionDirty;
    bool _viewMatrixDirty;
    bool _viewMatrixLocked;
    bool _frustumLocked;
    bool _rotationLocked;
    bool _movementLocked;
    bool _yawFixed;
    bool _isOrthoCamera;
    bool _frustumDirty;
    Frustum* _frustum;
};

TYPEDEF_SMART_POINTERS_FOR_CLASS(Camera);

};  // namespace Divide
#endif

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

Diff revisions: vs.
Revision Author Commited Message
781 Diff Diff IonutCava picture IonutCava Wed 12 Oct, 2016 16:03:46 +0000

[IonutCava]
- ShaderComputeQueue <-> Material shader request bug fix (element stuck in QUEUED state)
- More Camera class cleanups
- Add a method of removing EnvironmentProbes from their Pool

780 Diff Diff IonutCava picture IonutCava Tue 11 Oct, 2016 16:15:24 +0000

[IonutCava]
- AtomicCounter in glUniformBuffer is now based on a glGenericBuffer implementation
- GFXDevice <-> Camera relation cleaned up a bit
- Added a readback system for glGenericBuffer
- Fixed a bug with Z_PRE_PASS stage culling

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

709 Diff Diff IonutCava picture IonutCava Thu 19 May, 2016 16:21:46 +0000

[IonutCava]
- Massive rewrite of the resource system:
— ResourceCache now uses a map of weak pointers and passes shared pointers to the call point of CreateResource
— RemoveResource is no longer needed, but resource based smart pointers use a custom deleter that calls unload()
— Keep a shared_ptr of the resource in the entity that needs it and pass shared_ptr references from getter methods
-— All of this removed the need for calls to RemoveResource, REGISTER_TRACKED_DEPENDENCY and UNREGISTER_TRACKED_DEPENDENCY reducing the number of mutex locks and atomic exchanges
- Singleton cleanup:
— Removed ShaderManager singleton and merged it’s data and responsibilities in static methods in both ShaderProgram and Shader classes.
Explanation: Due to the complex interdependency chains in the system, copying, updating, tracking the cache in a thread safe way became a lot more slower than a straight forward smart pointer based implementation. (e.g. scene graph nodes have 3d objects and material instances, 3d objects have materials, materials have textures and shaders, etc)

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

648 Diff Diff IonutCava picture IonutCava Sun 21 Feb, 2016 16:32:52 +0000

[IonutCava]
- Update copyright notice
- Move BoundingBox and BoundingSphere to a new BoundsComponent
— Add a temp hack in SceneGraphNode to update these

632 IonutCava picture IonutCava Tue 26 Jan, 2016 17:18:10 +0000

[IonutCava]
- Some improvements to “ignoreView” nodes.
— BoundingBoxes update now
-— Still not perfect
- Small optimization to Task class
— Less atomic load calls