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
/*
   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.

 */

/*Code references:
    Matrix inverse: http://www.devmaster.net/forums/showthread.php?t=14569
    Matrix multiply:  http://fhtr.blogspot.com/2010/02/4x4-float-matrix-multiplication-using.html
    Square root: http://www.codeproject.com/KB/cpp/Sqrt_Prec_VS_Speed.aspx
*/

#ifndef _MATH_HELPER_H_
#define _MATH_HELPER_H_

#include "Hardware/Platform/Headers/PlatformDefines.h"
#include <sstream>
#include <cctype>
#include <algorithm>

#define M_PIDIV2			1.570796326794896619231321691639f		//  PI / 2
#define M_2PI				6.283185307179586476925286766559f		//  2 * PI
#define M_PI2				9.869604401089358618834490999876f		//  PI ^ 2
#define M_PIDIV180			0.01745329251994329576923690768488f		//  PI / 180
#define M_180DIVPI			57.295779513082320876798154814105f		//  180 / PI
#define M_PIDIV360          0.00872664625997164788461845384244f     //  PI / 180 / 2 - PI / 360

namespace Divide {

template<typename T>
inline void DegToRad(T& a) { a*=M_PIDIV180; }
template<typename T>
inline void RadToDeg(T& a) { a*=M_180DIVPI; }
template<typename T>
inline F32 RADIANS(T a)	   { return a*M_PIDIV180;}
template<typename T>
inline F32 DEGREES(T a)	   { return a*M_180DIVPI; }
template<typename T>
inline F32 kilometre(T a)  { return a*1000.0f; }
template<typename T>
inline F32 metre(T a)	   { return a*1.0f; }
template<typename T>
inline F32 decimetre(T a)  { return a*0.1f; }
template<typename T>
inline F32 centimetre(T a) { return a*0.01f; }
template<typename T>
inline F32 millimeter(T a) { return a*0.001f; }
template<typename T>
inline D32 getUsToSec(T a) { return a * 0.000001; }
template<typename T>
inline D32 getUsToMs(T a)  { return getMsToSec(a); }
template<typename T>
inline D32 getMsToSec(T a) { return a*0.001; }
template<typename T>
inline D32 getSecToMs(T a) { return a*1000.0; }
template<typename T>
inline U64 getSecToUs(T a) { return static_cast<U64>(a * 1000000); }
template<typename T>
inline U64 getMsToUs(T a)  { return static_cast<U64>(getSecToMs(a)); }
template<typename T>
inline void MsToSec(T& a)  { a*=0.001; }
template<typename T>
inline void SecToMs(T& a)  { a*=1000; }
template<typename T>
inline void UsToSec(T& a)  { a*=0.000001; }
template<typename T>
inline void SecToUs(T& a)  { a*=1000000; }
template<typename T>
inline void UsToMs(T& a)   { MsToSec(a); }
template<typename T>
inline void MsToUs(T& a)   { SecToMs(a); }

const  F32 INV_RAND_MAX = 1.0 / (RAND_MAX + 1);

inline F32 random(F32 max=1.0)      { 
    return max * rand() * INV_RAND_MAX; 
}

inline F32 random(F32 min, F32 max) { 
    return min + (max - min) * INV_RAND_MAX * rand(); 
}

inline I32 random(I32 max = RAND_MAX) { 
    return rand()%(max+1); 
}

inline bool bitCompare(U32 bitMask, U32 bit) {
    return ((bitMask & bit) == bit);
}

template<typename T>
inline T squared(T n){
    return n*n;
}

/// Clamps value n between min and max
template<typename T>
inline void CLAMP(T& n, T min, T max){
    n = std::min(std::max(n, min), max);
}

template<typename T>
inline T CLAMPED(const T& n, T min, T max){
    return std::min(std::max(n, min), max);
}

// bit manipulation
#define ToBit(posn)      (1 << posn)
#define BitSet(arg,posn) (arg |=  1 << posn)
#define BitClr(arg,posn) (arg &= ~(1 << (posn)))
#define BitTst(arg,posn) ((arg &   1 << (posn)) != 0)

#define BitDiff(arg1,arg2) (arg1 ^ arg2)
#define BitCmp(arg1,arg2,posn) ((arg1<<posn) == (arg2<<posn))

// bitmask manipulation
#define BitMaskSet(arg,mask) ((arg) |= (mask))
#define BitMaskClear(arg,mask) ((arg) &= (~(mask)))
#define BitMaskFlip(arg,mask) ((arg) ^= (mask))
#define BitMaskCheck(arg,mask) ((arg) & (mask))

template<typename T>
class mat4;
template<typename T>
class vec3;
template<typename T>
class vec4;
template<typename T>
class Quaternion;

namespace Util {
    inline void replaceStringInPlace(stringImpl& subject, 
                                     const stringImpl& search, 
                                     const stringImpl& replace ) {
        stringAlg::stringSize pos = 0;
        while ( ( pos = subject.find( search, pos ) ) != stringImpl::npos ) {
            subject.replace( pos, search.length(), replace );
            pos += replace.length();
        }
    }

    inline void swap( char* first, char* second ) {
        char temp = *first;
        *first = *second;
        *second = temp;
    }

    inline void permute(char* input, 
                        U32 startingIndex, 
                        U32 stringLength, 
                        vectorImpl<stringImpl>& container ) {
        if ( startingIndex == stringLength - 1 ) {
            container.push_back( input );
        } else {
            for ( U32 i = startingIndex; i < stringLength; i++ ) {
                swap( &input[startingIndex], &input[i] );
                permute( input, startingIndex + 1, stringLength, container );
                swap( &input[startingIndex], &input[i] );
            }
        }
    }

    inline void getPermutations(const stringImpl& inputString, 
                                vectorImpl<stringImpl>& permutationContainer ) {
        permutationContainer.clear();
        permute( (char*)inputString.c_str(), 0, (U32)inputString.length() - 1, permutationContainer );
    }

    static std::stringstream _ssBuffer;

    inline bool isNumber( const stringImpl& s ) {
        _ssBuffer.str( "" );
        _ssBuffer << s.c_str();
        F32 number;
        _ssBuffer >> number;
        if ( _ssBuffer.good() ) {
            return false;
        } 
        if ( number == 0 && s[0] != 0 ) {
            return false;
        }
        return true;
    }

    template<typename T>
    inline std::string toString( const T& data ) {
        _ssBuffer.str( "" );
        _ssBuffer << data;
        return _ssBuffer.str();
    }

    template<> inline std::string toString(const U8&  data) { return std::to_string(static_cast<U32>(data)); }
    template<> inline std::string toString(const U16& data) { return std::to_string(static_cast<U32>(data)); }
    template<> inline std::string toString(const U32& data) { return std::to_string(data); }
    template<> inline std::string toString(const U64& data) { return std::to_string(data); }
    template<> inline std::string toString(const I8&  data) { return std::to_string(static_cast<I32>(data)); }
    template<> inline std::string toString(const I16& data) { return std::to_string(static_cast<I32>(data)); }
    template<> inline std::string toString(const I32& data) { return std::to_string(data); }
    template<> inline std::string toString(const I64& data) { return std::to_string(data); }
    template<> inline std::string toString(const F32& data) { return std::to_string(data); }
    template<> inline std::string toString(const D32& data) { return std::to_string(data); }

    //U = to data type, T = from data type
    template<typename U, typename T>
    inline U convertData(const T& data){
        std::istringstream iStream(data);
        U targetValue;
        iStream >> targetValue;
        return targetValue;
    }

    /// http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c
    inline void split(const stringImpl& input, 
                      const char* delimiter, 
                      vectorImpl<stringImpl>& outputVector ) {
        stringAlg::stringSize delLen = static_cast<stringAlg::stringSize>( strlen( delimiter ) );
        assert( !input.empty() && delLen > 0 );

        stringAlg::stringSize start = 0, end = 0;
        while ( end != stringImpl::npos ) {
            end = input.find( delimiter, start );
            // If at end, use length=maxLength.  Else use length=end-start.
            outputVector.push_back(input.substr(start, 
                                                (end == stringImpl::npos) ? stringImpl::npos : 
                                                                            end - start ) );
            // If at end, use start=maxSize.  Else use start=end+delimiter.
            start = ( ( end > ( stringImpl::npos - delLen ) ) ? stringImpl::npos : end + delLen );
        }
    }

	/// http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
    inline stringImpl& ltrim( stringImpl& s ) {
        s.erase( s.begin(), std::find_if(s.begin(), s.end(), 
                                         std::not1( std::ptr_fun<int, int>( std::isspace ) ) ) );
        return s;
    }

	inline stringImpl& rtrim(stringImpl& s) {
		s.erase(std::find_if(s.rbegin(), s.rend(), 
                std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
		return s;
	}

	inline stringImpl& trim(stringImpl& s) {
		return ltrim(rtrim(s));
	}

    inline F32 xfov_to_yfov(F32 xfov, F32 aspect) {
        return DEGREES(2.0f * std::atan(tan(RADIANS(xfov) * 0.5f) / aspect));
    }

    inline F32 yfov_to_xfov(F32 yfov, F32 aspect) {
        return DEGREES(2.0f * std::atan(tan(RADIANS(yfov) * 0.5f) * aspect));
    }

    /** Ogre3D
    @brief Normalise the selected rotations to be within the +/-180 degree range.
    @details The normalise uses a wrap around, 
    @details so for example a yaw of 360 degrees becomes 0 degrees, and -190 degrees becomes 170.
    @param normYaw If false, the yaw isn't normalized.
    @param normPitch If false, the pitch isn't normalized.
    @param normRoll If false, the roll isn't normalized.
    */
    void normalize(vec3<F32>& inputRotation, 
                   bool degrees = false, 
                   bool normYaw = true, 
                   bool normPitch = true, 
                   bool normRoll = true);
    
    vec4<U8>  toByteColor(const vec4<F32>& floatColor);
    vec3<U8>  toByteColor(const vec3<F32>& floatColor);
    vec4<F32> toFloatColor(const vec4<U8>& byteColor);
    vec3<F32> toFloatColor(const vec3<U8>& byteColor);

    namespace Mat4 {
        // ----------------------------------------------------------------------------------------
        template<typename T>
        void decompose (const mat4<T>& matrix, vec3<T>& scale, Quaternion<T>& rotation, vec3<T>& position);
        template<typename T>
        void decomposeNoScaling(const mat4<T>& matrix, Quaternion<T>& rotation,	vec3<T>& position);

        template<typename T>
        inline T* Multiply( const T *a, const T *b, T *r = nullptr ) {
            static T temp[16];
            r = temp;
            U32 row, row_offset;
            for ( row = 0, row_offset = row * 4; row < 4; ++row, row_offset = row * 4 ) {
                r[row_offset + 0] = ( a[row_offset + 0] * b[0 + 0]  ) +
                                    ( a[row_offset + 1] * b[0 + 4]  ) +
                                    ( a[row_offset + 2] * b[0 + 8]  ) +
                                    ( a[row_offset + 3] * b[0 + 12] );

                r[row_offset + 1] = ( a[row_offset + 0] * b[1 + 0]  ) +
                                    ( a[row_offset + 1] * b[1 + 4]  ) +
                                    ( a[row_offset + 2] * b[1 + 8]  ) +
                                    ( a[row_offset + 3] * b[1 + 12] );

                r[row_offset + 2] = ( a[row_offset + 0] * b[2 + 0]  ) +
                                    ( a[row_offset + 1] * b[2 + 4]  ) +
                                    ( a[row_offset + 2] * b[2 + 8]  ) +
                                    ( a[row_offset + 3] * b[2 + 12] );

                r[row_offset + 3] = ( a[row_offset + 0] * b[3 + 0]  ) +
                                    ( a[row_offset + 1] * b[3 + 4]  ) +
                                    ( a[row_offset + 2] * b[3 + 8]  ) +
                                    ( a[row_offset + 3] * b[3 + 12] );
            }
            return r;
        }

        template<typename T>
        __forceinline T det( const T* mat ) {
            return ( ( mat[0] * mat[5] * mat[10] ) +
                     ( mat[4] * mat[9] * mat[2] ) +
                     ( mat[8] * mat[1] * mat[6] ) -
                     ( mat[8] * mat[5] * mat[2] ) -
                     ( mat[4] * mat[1] * mat[10] ) -
                     ( mat[0] * mat[9] * mat[6] ) );
        }

        // Copyright 2011 The Closure Library Authors. All Rights Reserved.
        template<typename T>
        __forceinline void Inverse(const T* in, T* out){
            T m00 = in[ 0], m10 = in[ 1], m20 = in[ 2], m30 = in[ 3];
            T m01 = in[ 4], m11 = in[ 5], m21 = in[ 6], m31 = in[ 7];
            T m02 = in[ 8], m12 = in[ 9], m22 = in[10], m32 = in[11];
            T m03 = in[12], m13 = in[13], m23 = in[14], m33 = in[15];

            T a0 = m00 * m11 - m10 * m01;
            T a1 = m00 * m21 - m20 * m01;
            T a2 = m00 * m31 - m30 * m01;
            T a3 = m10 * m21 - m20 * m11;
            T a4 = m10 * m31 - m30 * m11;
            T a5 = m20 * m31 - m30 * m21;
            T b0 = m02 * m13 - m12 * m03;
            T b1 = m02 * m23 - m22 * m03;
            T b2 = m02 * m33 - m32 * m03;
            T b3 = m12 * m23 - m22 * m13;
            T b4 = m12 * m33 - m32 * m13;
            T b5 = m22 * m33 - m32 * m23;

            T idet = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
            assert(!IS_ZERO(idet));
                
            idet = 1 / idet;

            out[ 0] = ( m11 * b5 - m21 * b4 + m31 * b3) * idet;
            out[ 1] = (-m10 * b5 + m20 * b4 - m30 * b3) * idet;
            out[ 2] = ( m13 * a5 - m23 * a4 + m33 * a3) * idet;
            out[ 3] = (-m12 * a5 + m22 * a4 - m32 * a3) * idet;
            out[ 4] = (-m01 * b5 + m21 * b2 - m31 * b1) * idet;
            out[ 5] = ( m00 * b5 - m20 * b2 + m30 * b1) * idet;
            out[ 6] = (-m03 * a5 + m23 * a2 - m33 * a1) * idet;
            out[ 7] = ( m02 * a5 - m22 * a2 + m32 * a1) * idet;
            out[ 8] = ( m01 * b4 - m11 * b2 + m31 * b0) * idet;
            out[ 9] = (-m00 * b4 + m10 * b2 - m30 * b0) * idet;
            out[10] = ( m03 * a4 - m13 * a2 + m33 * a0) * idet;
            out[11] = (-m02 * a4 + m12 * a2 - m32 * a0) * idet;
            out[12] = (-m01 * b3 + m11 * b1 - m21 * b0) * idet;
            out[13] = ( m00 * b3 - m10 * b1 + m20 * b0) * idet;
            out[14] = (-m03 * a3 + m13 * a1 - m23 * a0) * idet;
            out[15] = ( m02 * a3 - m12 * a1 + m22 * a0) * idet;
        }
    };
};

}; //namespace Divide

#endif

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

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

328 Diff Diff IonutCava picture IonutCava Sat 04 Oct, 2014 18:03:36 +0000

[Ionut]
- Added per SceneGraphNode material instances. (Cloned from the SceneNode’s material)
- Improved shader loading for materials

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

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

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)

302 Diff Diff IonutCava picture IonutCava Wed 06 Aug, 2014 22:09:55 +0000

[Ionut]
- GenericVertexData buffer size (for read/write buffering) is now configurable instead of a fixed 3x and also applies to non-persistently mapped buffers
- Moved OpenGL enum tables to Divide::GLUtil::GL_ENUM_TABLE
- More code cleanups (glResources, glError, glVertexArray, glGenericVertexData, glIMPrimitive)

272 IonutCava picture IonutCava Wed 11 Jun, 2014 11:30:51 +0000

[Ionut] [[BR]]
- Added initial code for Forward+ rendering (grid based light culling) [[BR]]
- Simplified Light class [[BR]]
- Moved more light uniform data to ShaderBuffers [[BR]]
- Double buffered grass rendering [[BR]]
- Added a better attribute interface to GenericVertexData [[BR]]
- Fixed screenshot capture [[BR]]
- Fixed texture double-bind checks [[BR]]