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
/*
   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 _TRANSFORM_H_
#define _TRANSFORM_H_

#include "Quaternion.h"
#include "Utility/Headers/GUIDWrapper.h"
   
namespace Divide {

struct TransformValues {
    /// The object's position in the world as a 3 component vector
    vec3<F32> _translation;
    /// Scaling is stored as a 3 component vector.
    /// This helps us check more easily if it's an uniform scale or not
    vec3<F32> _scale;
    /// All orientation/rotation info is stored in a Quaternion 
    /// (because they are awesome and also have an internal mat4 if needed)
    Quaternion<F32> _orientation;

    void operator=(const TransformValues& other) {
        _translation.set( other._translation );
        _scale.set( other._scale );
        _orientation.set( other._orientation );
    }

    bool operator==( const TransformValues& other ) const {
        return _scale.compare( other._scale ) &&
               _orientation.compare( other._orientation ) &&
               _translation.compare( other._translation );
    }
    bool operator!=( const TransformValues& other ) const {
        return !_scale.compare( other._scale ) ||
               !_orientation.compare( other._orientation ) ||
               !_translation.compare( other._translation );
    }
};

class Transform : public GUIDWrapper, private NonCopyable {
public:

    Transform();
    Transform(const Quaternion<F32>& orientation, 
              const vec3<F32>& translation, 
              const vec3<F32>& scale);

    ~Transform();

    /// Set the local X,Y and Z position
    void setPosition(const vec3<F32>& position) {
        WriteLock w_lock(this->_lock);
        this->_transformValues._translation.set(position);
        setDirty();
    }

    /// Set the local X,Y and Z scale factors
    void setScale(const vec3<F32>& scale) { 
        WriteLock w_lock(this->_lock); 
        this->_transformValues._scale.set(scale); 
        setDirty();
        rebuildMatrix(); 
    }

    /// Set the local orientation using the Axis-Angle system. 
    /// The angle can be in either degrees(default) or radians
    void setRotation(const vec3<F32>& axis, F32 degrees, bool inDegrees = true) { 
        setRotation(Quaternion<F32>(axis, degrees, inDegrees));
    }

    /// Set the local orientation using the Euler system.
    /// The angles can be in either degrees(default) or radians
    void setRotation(const vec3<F32>& euler, bool inDegrees = true)  { 
        setRotation(Quaternion<F32>(euler.pitch, euler.yaw, euler.roll, inDegrees));
    }

    /// Set the local orientation so that it matches the specified quaternion.
    void setRotation(const Quaternion<F32>& quat) {
        WriteLock w_lock(this->_lock); 
        this->_transformValues._orientation.set(quat);
        this->_transformValues._orientation.normalize();
        setDirty(); 
        rebuildMatrix();
    }

    /// Add the specified translation factors to the current local position
    void translate(const vec3<F32>& axisFactors) { 
        WriteLock w_lock(this->_lock); 
        this->_transformValues._translation += axisFactors;
        setDirty();
    }

    /// Add the specified scale factors to the current local position
    void scale(const vec3<F32>& axisFactors) { 
        WriteLock w_lock(this->_lock); 
        this->_transformValues._scale += axisFactors; 
        setDirty();
        rebuildMatrix(); 
    }

    /// Apply the specified Axis-Angle rotation starting from the current orientation. 
    /// The angles can be in either degrees(default) or radians
    void rotate(const vec3<F32>& axis, F32 degrees, bool inDegrees = true) { 
        rotate(Quaternion<F32>(axis, degrees, inDegrees));
    }

    /// Apply the specified Euler rotation starting from the current orientation. 
    /// The angles can be in either degrees(default) or radians
    void rotate(const vec3<F32>& euler, bool inDegrees = true) { 
        rotate(Quaternion<F32>(euler.pitch, euler.yaw, euler.roll, inDegrees));
    }

    /// Apply the specified Quaternion rotation starting from the current orientation.
    void rotate(const Quaternion<F32>& quat) {
        setRotation(this->_transformValues._orientation * quat);
    }

    /// Perform a SLERP rotation towards the specified quaternion
    void rotateSlerp(const Quaternion<F32>& quat, const D32 deltaTime) {
        WriteLock w_lock(this->_lock); 
        this->_transformValues._orientation.slerp(quat, deltaTime); 
        this->_transformValues._orientation.normalize();
        setDirty(); 
        rebuildMatrix();
    }

    /// If a non-uniform scaling factor is currently set (either locally or in the parent transform), 
    // return false
    inline bool isUniformScaled() const {
        return getScale().isUniform();
    }

    /// Transformation helper functions. These just call the normal translate/rotate/scale functions
    /// Set an uniform scale on all three axis
    inline void setScale(const F32 scale) {
        this->setScale(vec3<F32>(scale,scale,scale)); 
    }
    /// Set the scaling factor on the X axis
    inline void setScaleX(const F32 scale) {
        this->setScale(vec3<F32>(scale,
                                 this->_transformValues._scale.y,
                                 this->_transformValues._scale.z));
    }
    /// Set the scaling factor on the Y axis
    inline void setScaleY(const F32 scale) {
        this->setScale(vec3<F32>(this->_transformValues._scale.x,
                                 scale,
                                 this->_transformValues._scale.z));
    }
    /// Set the scaling factor on the Z axis
    inline void setScaleZ(const F32 scale) {
        this->setScale(vec3<F32>(this->_transformValues._scale.x,
                                 this->_transformValues._scale.y,
                                 scale));
    }
    /// Increase the scaling factor on all three axis by an uniform factor
    inline void scale(const F32 scale) {
        this->scale(vec3<F32>(scale, scale, scale));
    }
    /// Increase the scaling factor on the X axis by the specified factor
    inline void scaleX(const F32 scale) {
        this->scale(vec3<F32>(scale,
                              this->_transformValues._scale.y,
                              this->_transformValues._scale.z));
    }
    /// Increase the scaling factor on the Y axis by the specified factor
    inline void scaleY(const F32 scale) {
        this->scale(vec3<F32>(this->_transformValues._scale.x,
                              scale,
                              this->_transformValues._scale.z));
    }
    /// Increase the scaling factor on the Z axis by the specified factor
    inline void scaleZ(const F32 scale) {
        this->scale(vec3<F32>(this->_transformValues._scale.x,
                              this->_transformValues._scale.y,
                              scale));
    }
    /// Rotate on the X axis (Axis-Angle used) by the specified angle (either degrees or radians)
    inline void rotateX(const F32 angle, bool inDegrees = true) {
        this->rotate(vec3<F32>(1,0,0), angle, inDegrees);
    }
    /// Rotate on the Y axis (Axis-Angle used) by the specified angle (either degrees or radians)
    inline void rotateY(const F32 angle, bool inDegrees = true) {
        this->rotate(vec3<F32>(0,1,0), angle, inDegrees);
    }
    /// Rotate on the Z axis (Axis-Angle used) by the specified angle (either degrees or radians)
    inline void rotateZ(const F32 angle, bool inDegrees = true) {
        this->rotate(vec3<F32>(0,0,1), angle, inDegrees);
    }
    /// Set the rotation on the X axis (Axis-Angle used) by the specified angle (either degrees or radians)
    inline void setRotationX(const F32 angle, bool inDegrees = true) {
        this->setRotation(vec3<F32>(1,0,0), angle, inDegrees);
    }
    /// Set the rotation on the X axis (Euler used) by the specified angle (either degrees or radians)
    inline void setRotationXEuler(const F32 angle, bool inDegrees = true) {
        this->setRotation(angle, inDegrees);
    }
    /// Set the rotation on the Y axis (Axis-Angle used) by the specified angle (either degrees or radians)
    inline void setRotationY(const F32 angle, bool inDegrees = true) {
        this->setRotation(vec3<F32>(0,1,0), angle, inDegrees);
    }
    /// Set the rotation on the Z axis (Axis-Angle used) by the specified angle (either degrees or radians)
    inline void setRotationZ(const F32 angle, bool inDegrees = true) {
        this->setRotation(vec3<F32>(0,0,1), angle, inDegrees);
    }
    /// Translate the object on the X axis by the specified amount
    inline void translateX(const F32 positionX) {
        this->translate(vec3<F32>(positionX, 0.0f, 0.0f));
    }
    /// Translate the object on the Y axis by the specified amount
    inline void translateY(const F32 positionY) {
        this->translate(vec3<F32>(0.0f, positionY, 0.0f));
    }
    /// Translate the object on the Z axis by the specified amount
    inline void translateZ(const F32 positionZ) {
        this->translate(vec3<F32>(0.0f, 0.0f, positionZ));
    }
    /// Set the object's position on the X axis
    inline void setPositionX(const F32 positionX) {
        this->setPosition(vec3<F32>(positionX, 
                                    this->_transformValues._translation.y, 
                                    this->_transformValues._translation.z));
    }
    /// Set the object's position on the Y axis
    inline void setPositionY(const F32 positionY) {
        this->setPosition(vec3<F32>(this->_transformValues._translation.x, 
                                    positionY,
                                    this->_transformValues._translation.z));
    }
    /// Set the object's position on the Z axis
    inline void setPositionZ(const F32 positionZ) {
        this->setPosition(vec3<F32>(this->_transformValues._translation.x,
                                    this->_transformValues._translation.y, 
                                    positionZ));
    }
    /// Return the scale factor
    inline const vec3<F32>& getScale() const {
        ReadLock r_lock(this->_lock); 
        return this->_transformValues._scale; 
    }
    /// Return the position
    inline const vec3<F32>& getPosition() const {
        ReadLock r_lock(this->_lock); 
        return this->_transformValues._translation; 
    }
    /// Return the orientation quaternion
    inline const Quaternion<F32>& getOrientation() const {
        ReadLock r_lock(this->_lock);
        return this->_transformValues._orientation; 
    }
    /// Get the local transformation matrix
    const mat4<F32>& getMatrix();
    /// Sets the transform to match a certain transformation matrix.
    /// Scale, orientation and translation are extracted from the specified matrix
    inline void setTransforms(const mat4<F32>& transform) {
        WriteLock w_lock(this->_lock);
        Util::Mat4::decompose(transform, this->_transformValues._scale,
                                         this->_transformValues._orientation,
                                         this->_transformValues._translation);
        setDirty();
    }
    /// Set all of the internal values to match those of the specified transform
    inline void clone(Transform* const transform) {
        WriteLock w_lock(this->_lock);
        this->_transformValues._scale.set(transform->getScale());
        this->_transformValues._translation.set(transform->getPosition());
        this->_transformValues._orientation.set(transform->getOrientation());
        setDirty();
        w_lock.unlock();
    }
    /// Extract the 3 transform values (position, scale, rotation) from the current instance
    inline const TransformValues& getValues() const { return _transformValues; }
    /// Compares 2 transforms
    bool operator==( const Transform& other ) const {
        ReadLock r_lock( _lock );
        return _transformValues == other._transformValues;
    }
    bool operator!=( const Transform& other ) const {
        ReadLock r_lock( _lock );
        return _transformValues != other._transformValues;
    }
    ///Reset transform to identity
    void identity();

private:
    inline void setDirty() {
        this->_dirty = true;
    }

    inline void rebuildMatrix() {
        this->_rebuildMatrix = true;
    }

private:
    /// The actual scale, rotation and translation values
    TransformValues _transformValues;
    /// This is the actual model matrix, but it will not convert to world space as it depends on it's parent in graph
    mat4<F32> _worldMatrix;
    /// _dirty is set to true whenever a translation, rotation or scale is applied
    std::atomic_bool _dirty;
    /// _rebuildMatrix is true when a rotation or scale is applied to avoid rebuilding matrices on translation
    std::atomic_bool _rebuildMatrix;
    mutable SharedLock _lock;
};

}; //namespace Divide

#endif

Commits for Divide-Framework/trunk/Source Code/Core/Math/Headers/Transform.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)

329 Diff Diff IonutCava picture IonutCava Tue 07 Oct, 2014 12:51:37 +0000

[Ionut]
- Modified material class and shaders to support multiple BRDFs (currently, only Flat, Phong and Blinn-Phong supported with Cook-Torrance and Oren-Nayar in future commits)
- Optimized transform updates and queries
- Fixed an indirect draw command (no buffer was bound to GL_DRAW_INDIRECT_BUFFER, violating a core requirement)

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)

308 Diff Diff IonutCava picture IonutCava Sat 16 Aug, 2014 19:49:31 +0000

[Ionut]
- Moved Transform from SceneGraphNode to PhysicsComponent and modified BoundingBox update system based on transform update
- Added PhysicsAsset to PhysicsComponent for interfacing with PshysicsAPIs (e.g. PhysXActor inherits PhysicsAsset)
- PhysicsAsset keeps track of the parent PhysicsComponent to have direct access to a node’s transform
- Rewrote GUIConsole to only print items when visible, and use a single circular buffer for entries.
- Updated GUI input system to match the interface that both Kernel and SceneManager share (should probably abstract all the input methods in a class and have Kernel, SceneManager, Scene and GUI inherit from that. In next commit.)
- Updated CEGUI to probably match the OIS states(mouse, keyboard, etc) and properly inject time inputs.

306 Diff Diff IonutCava picture IonutCava Tue 12 Aug, 2014 12:47:31 +0000

[Ionut]
- More World Editor work:
— Move and Scale selected objects
— Disabling object specific controls when nothing is selected
— Toggle PostFX and Fog controls working
— Do not pass input to scene when clicking on any editor controls
— Granularity fields working
— Input fields only allow floating point inputs (via regex check)
- Cleaned up Transform class

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)

287 Diff Diff IonutCava picture IonutCava Tue 22 Jul, 2014 16:11:05 +0000

[Ionut]
- Unified matrix transforms and node info and made it a vertex-shader only buffer
- World matrix queries have to be issued from the SceneGraphNode instead of the SGN’s Transform class to allow future interpolation algorithms to be more easily implemented
- Moved all lighting calculations per pixel to save varying and register space (caused compilation issues both on nVidia and AMD)
- Stored GPU buffer info (view, projection, viewport etc) into a special struct to avoid multiple redundant buffer uploads and to remove redundant client side matrix caches
- Some work on shader buffer locking system
- Cleaned up MathClasses.h and Transform.h
- Updated ASSIMP to 3.1.1

284 IonutCava picture IonutCava Tue 01 Jul, 2014 20:54:08 +0000

[Ionut][[BR]]
- Per SGN shader data implementation part 1 / 2 [[BR]]
— Gather node info and transforms into custom struct for visible nodes and store them in a GFXDevice local vector [[BR]]
- Removed “prepareDepthMaterial” and added depthPass flag to “prepareMaterial” [[BR]]