From: <axl...@us...> - 2009-01-04 16:38:05
|
Revision: 143 http://hgengine.svn.sourceforge.net/hgengine/?rev=143&view=rev Author: axlecrusher Date: 2009-01-04 16:37:53 +0000 (Sun, 04 Jan 2009) Log Message: ----------- First stage of overhauling mercury math so squeeze out a little more speed Use our own FloatRow to define rows for matrices instead of float arrays. We can define these differently if we are using SSE or not. So far this makes matrix multiplies about %14 faster. Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Mercury2/src/MercuryMatrix.h Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-01-03 17:50:45 UTC (rev 142) +++ Mercury2/src/MercuryMath.cpp 2009-01-04 16:37:53 UTC (rev 143) @@ -4,6 +4,11 @@ //Generic Math functions. Compile these if you can not use optimized functions. +void ZeroFloatRow(FloatRow& r) +{ + Copy4f(&r, (FloatRow){ 0.0f, 0.0f, 0.0f, 0.0f }); +} + void Mul4f(const float* first, const float* second, float* out) { out[0] = first[0] * second[0]; @@ -80,8 +85,14 @@ ((float*)dest)[15] = ((float*)source)[15]; } -void R_ConcatTransforms4 ( const float* in1, const float* in2, float* out) +void R_ConcatTransforms4 ( const FloatRow* in1a, const FloatRow* in2a, FloatRow* outa) { + float *in1, *in2, *out; + + in1 = (float*)in1a; + in2 = (float*)in2a; + out = (float*)outa; + out[0] = in1[0] * in2[0] + in1[1] * in2[4] + in1[2] * in2[8] + in1[3] * in2[12]; out[1] = in1[0] * in2[1] + in1[1] * in2[5] + @@ -128,9 +139,8 @@ } #else -#include <xmmintrin.h> -inline __m128 Hadd4(__m128 x); +//inline __m128 Hadd4(__m128 x); __m128 Hadd4(__m128 x) { //add the low and high components of x @@ -225,33 +235,27 @@ _mm_store_ps((float*)&(((float*)dest)[12]), xmm[3]); } -void R_ConcatTransforms4 ( const float* in1, const float* in2, float* out) +void R_ConcatTransforms4 ( const FloatRow* in1, const FloatRow* in2, FloatRow* out) { - unsigned int x, y; - __m128 xmm[8]; - - xmm[1] = _mm_load_ps((float*)&(in2[0])); - xmm[3] = _mm_load_ps((float*)&(in2[4])); - xmm[5] = _mm_load_ps((float*)&(in2[8])); - xmm[7] = _mm_load_ps((float*)&(in2[12])); - + unsigned int y; + __m128 xmm[4]; + for (y = 0; y < 4; ++y) { - xmm[0] = _mm_set_ps1(in1[(y*4)+0]); - xmm[2] = _mm_set_ps1(in1[(y*4)+1]); - xmm[4] = _mm_set_ps1(in1[(y*4)+2]); - xmm[6] = _mm_set_ps1(in1[(y*4)+3]); + //load columns + xmm[3] = _mm_shuffle_ps (in1[y], in1[y], 0xff); + xmm[2] = _mm_shuffle_ps (in1[y], in1[y], 0xaa); + xmm[1] = _mm_shuffle_ps (in1[y], in1[y], 0x55); + xmm[0] = _mm_shuffle_ps (in1[y], in1[y], 0x00); - xmm[0] = _mm_mul_ps( xmm[0], xmm[1] ); - xmm[2] = _mm_mul_ps( xmm[2], xmm[3] ); - xmm[4] = _mm_mul_ps( xmm[4], xmm[5] ); - xmm[6] = _mm_mul_ps( xmm[6], xmm[7] ); + xmm[0] = _mm_mul_ps( xmm[0], in2[0] ); + xmm[1] = _mm_mul_ps( xmm[1], in2[1] ); + xmm[2] = _mm_mul_ps( xmm[2], in2[2] ); + xmm[3] = _mm_mul_ps( xmm[3], in2[3] ); - xmm[0] = _mm_add_ps( xmm[0], xmm[2] ); - xmm[4] = _mm_add_ps( xmm[4], xmm[6] ); - xmm[0] = _mm_add_ps( xmm[0], xmm[4] ); - - _mm_store_ps(&(out[(y*4)]), xmm[0]); + xmm[0] = _mm_add_ps( xmm[0], xmm[1] ); + xmm[2] = _mm_add_ps( xmm[2], xmm[3] ); + out[y] = _mm_add_ps( xmm[0], xmm[2] ); } } @@ -288,6 +292,11 @@ _mm_store_ps(out, tmp); } +void ZeroFloatRow(FloatRow& r) +{ + r = (FloatRow)_mm_setzero_ps(); +} + #endif /* Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-01-03 17:50:45 UTC (rev 142) +++ Mercury2/src/MercuryMath.h 2009-01-04 16:37:53 UTC (rev 143) @@ -3,6 +3,15 @@ #include <math.h> +#ifdef USE_SSE +#include <xmmintrin.h> +typedef __m128 FloatRow __attribute__((aligned(16))); +#else +typedef float FloatRow[4]; +#endif + +void ZeroFloatRow(FloatRow& r); + #define DEGRAD 0.01745329251994329576f //degree to radian #define RADDEG 57.2957795130823208767f //radian to degree #define Q_PI 3.14159265358979323846f @@ -42,7 +51,8 @@ void Copy4f( void * dest, const void * source ); void Copy8f( void * dest, const void * source ); void Copy16f( void * dest, const void * source ); -void R_ConcatTransforms4 ( const float* in1, const float* in2, float* out ); +//void R_ConcatTransforms4 ( const float* in1, const float* in2, float* out ); +void R_ConcatTransforms4 ( const FloatRow* in1, const FloatRow* in2, FloatRow* out ); void VectorMultiply4f(const float *m, float *p, float *out ); #endif Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-01-03 17:50:45 UTC (rev 142) +++ Mercury2/src/MercuryMatrix.cpp 2009-01-04 16:37:53 UTC (rev 143) @@ -25,56 +25,47 @@ void MercuryMatrix::Zero() { - m_matrix[0][0] = 0; - m_matrix[0][1] = 0; - m_matrix[0][2] = 0; - m_matrix[0][3] = 0; - - m_matrix[1][0] = 0; - m_matrix[1][1] = 0; - m_matrix[1][2] = 0; - m_matrix[1][3] = 0; - - m_matrix[2][0] = 0; - m_matrix[2][1] = 0; - m_matrix[2][2] = 0; - m_matrix[2][3] = 0; - - m_matrix[3][0] = 0; - m_matrix[3][1] = 0; - m_matrix[3][2] = 0; - m_matrix[3][3] = 0; + ZeroFloatRow( m_matrix[0] ); + ZeroFloatRow( m_matrix[1] ); + ZeroFloatRow( m_matrix[2] ); + ZeroFloatRow( m_matrix[3] ); } void MercuryMatrix::Identity() { - m_matrix[0][0] = 1; - m_matrix[0][1] = 0; - m_matrix[0][2] = 0; - m_matrix[0][3] = 0; + Copy4f(&m_matrix[0], (void*)&((FloatRow){ 1.0f, 0.0f, 0.0f, 0.0f })); + Copy4f(&m_matrix[1], (void*)&((FloatRow){ 0.0f, 1.0f, 0.0f, 0.0f })); + Copy4f(&m_matrix[2], (void*)&((FloatRow){ 0.0f, 0.0f, 1.0f, 0.0f })); + Copy4f(&m_matrix[3], (void*)&((FloatRow){ 0.0f, 0.0f, 0.0f, 1.0f })); +/* + (*this)[0][0] = 1; + (*this)[0][1] = 0; + (*this)[0][2] = 0; + (*this)[0][3] = 0; - m_matrix[1][0] = 0; - m_matrix[1][1] = 1; - m_matrix[1][2] = 0; - m_matrix[1][3] = 0; + (*this)[1][0] = 0; + (*this)[1][1] = 1; + (*this)[1][2] = 0; + (*this)[1][3] = 0; - m_matrix[2][0] = 0; - m_matrix[2][1] = 0; - m_matrix[2][2] = 1; - m_matrix[2][3] = 0; + (*this)[2][0] = 0; + (*this)[2][1] = 0; + (*this)[2][2] = 1; + (*this)[2][3] = 0; - m_matrix[3][0] = 0; - m_matrix[3][1] = 0; - m_matrix[3][2] = 0; - m_matrix[3][3] = 1; + (*this)[3][0] = 0; + (*this)[3][1] = 0; + (*this)[3][2] = 0; + (*this)[3][3] = 1; + */ } void MercuryMatrix::Translate(float x, float y, float z) { MercuryMatrix m; - m.m_matrix[0][3] = x; - m.m_matrix[1][3] = y; - m.m_matrix[2][3] = z; + m[0][3] = x; + m[1][3] = y; + m[2][3] = z; *this *= m; } @@ -95,25 +86,25 @@ //Row major //manually transposed - matrix.m_matrix[0][0] = cy*cz; - matrix.m_matrix[1][0] = (sx*sy*cz)-(cx*sz); - matrix.m_matrix[2][0] = (cx*sy*cz)+(sx*sz); - matrix.m_matrix[3][0] = 0; + matrix[0][0] = cy*cz; + matrix[1][0] = (sx*sy*cz)-(cx*sz); + matrix[2][0] = (cx*sy*cz)+(sx*sz); + matrix[3][0] = 0; - matrix.m_matrix[0][1] = cy*sz; - matrix.m_matrix[1][1] = (sx*sy*sz)+(cx*cz); - matrix.m_matrix[2][1] = (cx*sy*sz)-(sx*cz); - matrix.m_matrix[3][1] = 0; + matrix[0][1] = cy*sz; + matrix[1][1] = (sx*sy*sz)+(cx*cz); + matrix[2][1] = (cx*sy*sz)-(sx*cz); + matrix[3][1] = 0; - matrix.m_matrix[0][2] = -sy; - matrix.m_matrix[1][2] = sx*cy; - matrix.m_matrix[2][2] = cx*cy; - matrix.m_matrix[3][2] = 0; + matrix[0][2] = -sy; + matrix[1][2] = sx*cy; + matrix[2][2] = cx*cy; + matrix[3][2] = 0; - matrix.m_matrix[0][3] = 0; - matrix.m_matrix[1][3] = 0; - matrix.m_matrix[2][3] = 0; - matrix.m_matrix[3][3] = 1; + matrix[0][3] = 0; + matrix[1][3] = 0; + matrix[2][3] = 0; + matrix[3][3] = 1; *this *= matrix; } @@ -127,25 +118,25 @@ float y = iy/absin; float z = iz/absin; - m_matrix[0][0] = x*x*(1-c)+c; - m_matrix[0][1] = x*y*(1-c)-z*s; - m_matrix[0][2] = x*z*(1-c)+y*s; - m_matrix[0][3] = 0; + (*this)[0][0] = x*x*(1-c)+c; + (*this)[0][1] = x*y*(1-c)-z*s; + (*this)[0][2] = x*z*(1-c)+y*s; + (*this)[0][3] = 0; - m_matrix[1][0] = y*x*(1-c)+z*s; - m_matrix[1][1] = y*y*(1-c)+c; - m_matrix[1][2] = y*z*(1-c)-x*s; - m_matrix[1][3] = 0; + (*this)[1][0] = y*x*(1-c)+z*s; + (*this)[1][1] = y*y*(1-c)+c; + (*this)[1][2] = y*z*(1-c)-x*s; + (*this)[1][3] = 0; - m_matrix[2][0] = x*z*(1-c)-y*s; - m_matrix[2][1] = y*z*(1-c)+x*s; - m_matrix[2][2] = z*z*(1-c)+c; - m_matrix[2][3] = 0; + (*this)[2][0] = x*z*(1-c)-y*s; + (*this)[2][1] = y*z*(1-c)+x*s; + (*this)[2][2] = z*z*(1-c)+c; + (*this)[2][3] = 0; - m_matrix[3][0] = 0; - m_matrix[3][1] = 0; - m_matrix[3][2] = 0; - m_matrix[3][3] = 1; + (*this)[3][0] = 0; + (*this)[3][1] = 0; + (*this)[3][2] = 0; + (*this)[3][3] = 1; } void MercuryMatrix::Transotale( float tX, float tY, float tZ, float rX, float rY, float rZ, float sX, float sY, float sZ ) @@ -165,25 +156,25 @@ //Row major //manually transposed - matrix.m_matrix[0][0] = sX*cy*cz; - matrix.m_matrix[1][0] = sX*((sx*sy*cz)-(cx*sz)); - matrix.m_matrix[2][0] = sX*((cx*sy*cz)+(sx*sz)); - matrix.m_matrix[3][0] = 0; + matrix[0][0] = sX*cy*cz; + matrix[1][0] = sX*((sx*sy*cz)-(cx*sz)); + matrix[2][0] = sX*((cx*sy*cz)+(sx*sz)); + matrix[3][0] = 0; - matrix.m_matrix[0][1] = sY*cy*sz; - matrix.m_matrix[1][1] = sY*((sx*sy*sz)+(cx*cz)); - matrix.m_matrix[2][1] = sY*((cx*sy*sz)-(sx*cz)); - matrix.m_matrix[3][1] = 0; + matrix[0][1] = sY*cy*sz; + matrix[1][1] = sY*((sx*sy*sz)+(cx*cz)); + matrix[2][1] = sY*((cx*sy*sz)-(sx*cz)); + matrix[3][1] = 0; - matrix.m_matrix[0][2] = sZ*(-sy); - matrix.m_matrix[1][2] = sZ*sx*cy; - matrix.m_matrix[2][2] = sZ*cx*cy; - matrix.m_matrix[3][2] = 0; + matrix[0][2] = sZ*(-sy); + matrix[1][2] = sZ*sx*cy; + matrix[2][2] = sZ*cx*cy; + matrix[3][2] = 0; - matrix.m_matrix[0][3] = tX; - matrix.m_matrix[1][3] = tY; - matrix.m_matrix[2][3] = tZ; - matrix.m_matrix[3][3] = 1; + matrix[0][3] = tX; + matrix[1][3] = tY; + matrix[2][3] = tZ; + matrix[3][3] = 1; *this *= matrix; } @@ -193,9 +184,9 @@ { MercuryMatrix m; - m.m_matrix[0][0] = x; - m.m_matrix[1][1] = y; - m.m_matrix[2][2] = z; + m[0][0] = x; + m[1][1] = y; + m[2][2] = z; *this *= m; } @@ -203,14 +194,16 @@ MercuryMatrix MercuryMatrix::operator*(const MercuryMatrix& m) const { MercuryMatrix r(*this); - R_ConcatTransforms4 ( (float*)&m_matrix, (float*)&m.m_matrix, (float*)&r.m_matrix); +// R_ConcatTransforms4 ( (float*)&m_matrix, (float*)&m.m_matrix, (float*)&r.m_matrix); + R_ConcatTransforms4 ( m_matrix, m.m_matrix, r.m_matrix); return r; } MercuryMatrix& MercuryMatrix::operator*=(const MercuryMatrix& m) { MercuryMatrix r(*this); - R_ConcatTransforms4 ( (float*)&r.m_matrix, (float*)&m.m_matrix, (float*)&m_matrix); +// R_ConcatTransforms4 ( (float*)&r.m_matrix, (float*)&m.m_matrix, (float*)&m_matrix); + R_ConcatTransforms4 ( r.m_matrix, m.m_matrix, m_matrix); return *this; } @@ -218,7 +211,7 @@ { for (int i = 0; i < 4; ++i) { - printf( "%f %f %f %f\n", m_matrix[i][0], m_matrix[i][1], m_matrix[i][2], m_matrix[i][3] ); + printf( "%f %f %f %f\n", (*this)[i][0], (*this)[i][1], (*this)[i][2], (*this)[i][3] ); } printf("\n"); } Modified: Mercury2/src/MercuryMatrix.h =================================================================== --- Mercury2/src/MercuryMatrix.h 2009-01-03 17:50:45 UTC (rev 142) +++ Mercury2/src/MercuryMatrix.h 2009-01-04 16:37:53 UTC (rev 143) @@ -15,13 +15,14 @@ { private: ///[row][column] (The internal matrix) - float m_matrix[4][4]; +// float m_matrix[4][4]; + FloatRow m_matrix[4]; public: MercuryMatrix(); inline MercuryMatrix(const MercuryMatrix& m) { *this = m; } - inline float* operator[](unsigned int i) { return m_matrix[i]; } + inline float* operator[](unsigned int i) { return (float*)&m_matrix[i]; } ///Allow typecasting to float * for use in APIs - inline const float* operator[](unsigned int i) const { return m_matrix[i]; } + inline const float* operator[](unsigned int i) const { return (float*)&m_matrix[i]; } const MercuryMatrix& operator=(const MercuryMatrix& m); const MercuryMatrix& operator=(const float* m); inline float* Ptr() { return (float*)&m_matrix; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-01-04 17:50:50
|
Revision: 144 http://hgengine.svn.sourceforge.net/hgengine/?rev=144&view=rev Author: axlecrusher Date: 2009-01-04 17:50:45 +0000 (Sun, 04 Jan 2009) Log Message: ----------- continuing improvements on the SSE functions Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-01-04 16:37:53 UTC (rev 143) +++ Mercury2/src/MercuryMath.cpp 2009-01-04 17:50:45 UTC (rev 144) @@ -85,7 +85,7 @@ ((float*)dest)[15] = ((float*)source)[15]; } -void R_ConcatTransforms4 ( const FloatRow* in1a, const FloatRow* in2a, FloatRow* outa) +void MatrixMultiply4f ( const FloatRow* in1a, const FloatRow* in2a, FloatRow* outa) { float *in1, *in2, *out; @@ -149,54 +149,24 @@ return _mm_add_ps( x, _mm_shuffle_ps(x, x, _MM_SHUFFLE(1,1,1,1)) ); } -void Mul4f(const float* first, const float* second, float* out) +void Mul4f(const FloatRow* first, const FloatRow* second, FloatRow* out) { - __m128 xmm[2]; - - xmm[0] = _mm_load_ps(first); - xmm[1] = _mm_load_ps(second); - - xmm[0] = _mm_mul_ps( xmm[0], xmm[1] ); - - _mm_store_ps(out, xmm[0]); + *out = _mm_mul_ps( *first, *second ); } -void Div4f(const float* first, const float* second, float* out) +void Div4f(const FloatRow* first, const FloatRow* second, FloatRow* out) { - __m128 xmm[2]; - - xmm[0] = _mm_load_ps(first); - xmm[1] = _mm_load_ps(second); - - xmm[0] = _mm_div_ps( xmm[0], xmm[1] ); - - _mm_store_ps(out, xmm[0]); + *out = _mm_div_ps( *first, *second ); } -void Add4f(const float* first, const float* second, float* out) +void Add4f(const FloatRow* first, const FloatRow* second, FloatRow* out) { - __m128 xmm[2]; - - xmm[0] = _mm_load_ps(first); - xmm[1] = _mm_load_ps(second); - - xmm[0] = _mm_add_ps( xmm[0], xmm[1] ); - - _mm_store_ps(out, xmm[0]); - + *out = _mm_add_ps( *first, *second ); } -void Sub4f(const float* first, const float* second, float* out) +void Sub4f(const FloatRow* first, const FloatRow* second, FloatRow* out) { - __m128 xmm[2]; - - xmm[0] = _mm_load_ps(first); - xmm[1] = _mm_load_ps(second); - - xmm[0] = _mm_sub_ps( xmm[0], xmm[1] ); - - _mm_store_ps(out, xmm[0]); - + *out = _mm_sub_ps( *first, *second ); } void Copy4f( void * dest, const void * source ) @@ -235,7 +205,7 @@ _mm_store_ps((float*)&(((float*)dest)[12]), xmm[3]); } -void R_ConcatTransforms4 ( const FloatRow* in1, const FloatRow* in2, FloatRow* out) +void MatrixMultiply4f( const FloatRow* in1, const FloatRow* in2, FloatRow* out) { unsigned int y; __m128 xmm[4]; Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-01-04 16:37:53 UTC (rev 143) +++ Mercury2/src/MercuryMath.h 2009-01-04 17:50:45 UTC (rev 144) @@ -44,15 +44,14 @@ #define DotProduct(x,y) ((x)[0]*(y)[0]+(x)[1]*(y)[1]+(x)[2]*(y)[2]) -void Mul4f(const float* first, const float* second, float* out); -void Div4f(const float* first, const float* second, float* out); -void Add4f(const float* first, const float* second, float* out); -void Sub4f(const float* first, const float* second, float* out); +void Mul4f(const FloatRow* first, const FloatRow* second, FloatRow* out); +void Div4f(const FloatRow* first, const FloatRow* second, FloatRow* out); +void Add4f(const FloatRow* first, const FloatRow* second, FloatRow* out); +void Sub4f(const FloatRow* first, const FloatRow* second, FloatRow* out); void Copy4f( void * dest, const void * source ); void Copy8f( void * dest, const void * source ); void Copy16f( void * dest, const void * source ); -//void R_ConcatTransforms4 ( const float* in1, const float* in2, float* out ); -void R_ConcatTransforms4 ( const FloatRow* in1, const FloatRow* in2, FloatRow* out ); +void MatrixMultiply4f ( const FloatRow* in1, const FloatRow* in2, FloatRow* out ); void VectorMultiply4f(const float *m, float *p, float *out ); #endif Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-01-04 16:37:53 UTC (rev 143) +++ Mercury2/src/MercuryMatrix.cpp 2009-01-04 17:50:45 UTC (rev 144) @@ -194,16 +194,14 @@ MercuryMatrix MercuryMatrix::operator*(const MercuryMatrix& m) const { MercuryMatrix r(*this); -// R_ConcatTransforms4 ( (float*)&m_matrix, (float*)&m.m_matrix, (float*)&r.m_matrix); - R_ConcatTransforms4 ( m_matrix, m.m_matrix, r.m_matrix); + MatrixMultiply4f ( m_matrix, m.m_matrix, r.m_matrix); return r; } MercuryMatrix& MercuryMatrix::operator*=(const MercuryMatrix& m) { MercuryMatrix r(*this); -// R_ConcatTransforms4 ( (float*)&r.m_matrix, (float*)&m.m_matrix, (float*)&m_matrix); - R_ConcatTransforms4 ( r.m_matrix, m.m_matrix, m_matrix); + MatrixMultiply4f ( r.m_matrix, m.m_matrix, m_matrix); return *this; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-01-04 18:07:45
|
Revision: 145 http://hgengine.svn.sourceforge.net/hgengine/?rev=145&view=rev Author: axlecrusher Date: 2009-01-04 18:07:42 +0000 (Sun, 04 Jan 2009) Log Message: ----------- improve the vector multiply Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-01-04 17:50:45 UTC (rev 144) +++ Mercury2/src/MercuryMath.cpp 2009-01-04 18:07:42 UTC (rev 145) @@ -130,8 +130,11 @@ in1[14] * in2[11] + in1[15] * in2[15]; } -void VectorMultiply4f( const float * m, float *p, float *out ) +void VectorMultiply4f( const FloatRow* matrix, const FloatRow* pa, FloatRow* outa ) { + float *m = (float*)matrix; + float *p = (float*)pa; + float *out = (float*)outa; out[0] = p[0] * m[0] + p[1] * m[1] + p[2] * m[2] + p[3] * m[3]; out[1] = p[0] * m[4] + p[1] * m[5] + p[2] * m[6] + p[3] * m[7]; out[2] = p[0] * m[8] + p[1] * m[9] + p[2] * m[10] + p[3] * m[11]; @@ -231,35 +234,25 @@ //This is an SSE matrix vector multiply, see the standard C++ code //for a clear algorithim. This seems like it works. -void VectorMultiply4f( const float * m, float *p, float *out ) +void VectorMultiply4f( const FloatRow* matrix, const FloatRow* p, FloatRow* out ) { - __m128 xmm[5], outxmm[2], tmp; - - xmm[0] = _mm_load_ps((float*)p); //the vector - - //store the matrix - xmm[1] = _mm_load_ps((float*)&(m[0])); - xmm[2] = _mm_load_ps((float*)&(m[4])); - xmm[3] = _mm_load_ps((float*)&(m[8])); - xmm[4] = _mm_load_ps((float*)&(m[12])); - + __m128 tmp; + //compute term 1 and term 2 and store them in the low order //of outxmm[0] - outxmm[0] = Hadd4( _mm_mul_ps( xmm[1], xmm[0] ) ); - tmp = Hadd4( _mm_mul_ps( xmm[2], xmm[0] ) ); - outxmm[0] = _mm_unpacklo_ps(outxmm[0], tmp); + out[0] = Hadd4( _mm_mul_ps( matrix[1], *p ) ); + tmp = Hadd4( _mm_mul_ps( matrix[2], *p ) ); + out[0] = _mm_unpacklo_ps(out[0], tmp); //compute term 3 and term 4 and store them in the high order //of outxmm[1] - outxmm[1] = Hadd4( _mm_mul_ps( xmm[3], xmm[0] ) ); - tmp = Hadd4( _mm_mul_ps( xmm[4], xmm[0] ) ); - outxmm[1] = _mm_unpacklo_ps(outxmm[1], tmp); + out[1] = Hadd4( _mm_mul_ps( matrix[3], *p ) ); + tmp = Hadd4( _mm_mul_ps( matrix[4], *p ) ); + out[1] = _mm_unpacklo_ps(out[1], tmp); //shuffle the low order of outxmm[0] into the loworder of tmp //and shuffle the low order of outxmm[1] into the high order of tmp - tmp = _mm_movelh_ps(outxmm[0], outxmm[1]); - - _mm_store_ps(out, tmp); + *out = _mm_movelh_ps(out[0], out[1]); } void ZeroFloatRow(FloatRow& r) Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-01-04 17:50:45 UTC (rev 144) +++ Mercury2/src/MercuryMath.h 2009-01-04 18:07:42 UTC (rev 145) @@ -52,7 +52,7 @@ void Copy8f( void * dest, const void * source ); void Copy16f( void * dest, const void * source ); void MatrixMultiply4f ( const FloatRow* in1, const FloatRow* in2, FloatRow* out ); -void VectorMultiply4f(const float *m, float *p, float *out ); +void VectorMultiply4f(const FloatRow* matrix, const FloatRow* p, FloatRow* out ); #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-01-04 19:10:57
|
Revision: 147 http://hgengine.svn.sourceforge.net/hgengine/?rev=147&view=rev Author: axlecrusher Date: 2009-01-04 19:10:52 +0000 (Sun, 04 Jan 2009) Log Message: ----------- move the transpose function out into math Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Mercury2/src/MercuryMatrix.h Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-01-04 18:32:00 UTC (rev 146) +++ Mercury2/src/MercuryMath.cpp 2009-01-04 19:10:52 UTC (rev 147) @@ -1,5 +1,34 @@ #include "MercuryMath.h" +//the SSE version of this was really slow, this is quicker +void TransposeMatrix( FloatRow* m ) +{ + float tmp; + float *_m = (float*)m; + + tmp = _m[1]; + _m[1] = _m[4]; + _m[4] = tmp; + + tmp = _m[2]; + _m[2] = _m[8]; + _m[8] = tmp; + tmp = _m[3]; + _m[3] = _m[12]; + _m[12] = tmp; + + tmp = _m[6]; + _m[6] = _m[9]; + _m[9] = tmp; + tmp = _m[7]; + _m[7] = _m[13]; + _m[13] = tmp; + + tmp = _m[11]; + _m[11] = _m[14]; + _m[14] = tmp; +} + #ifndef USE_SSE //Generic Math functions. Compile these if you can not use optimized functions. Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-01-04 18:32:00 UTC (rev 146) +++ Mercury2/src/MercuryMath.h 2009-01-04 19:10:52 UTC (rev 147) @@ -53,6 +53,7 @@ void Copy16f( void * dest, const void * source ); void MatrixMultiply4f ( const FloatRow* in1, const FloatRow* in2, FloatRow* out ); void VectorMultiply4f(const FloatRow* matrix, const FloatRow* p, FloatRow* out ); +void TransposeMatrix( FloatRow* m ); #endif Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-01-04 18:32:00 UTC (rev 146) +++ Mercury2/src/MercuryMatrix.cpp 2009-01-04 19:10:52 UTC (rev 147) @@ -214,40 +214,6 @@ printf("\n"); } -void TransposeMatrix( const MercuryMatrix &in, MercuryMatrix &out ) -{ - float tmp; - const float* _in = (const float*)in.Ptr(); - float* _out = out.Ptr(); - - //unchanging - _out[0] = _in[0]; - _out[5] = _in[5]; - _out[10] = _in[10]; - _out[15] = _in[15]; - - tmp = _in[1]; - _out[1] = _in[4]; - _out[4] = tmp; - tmp = _in[2]; - _out[2] = _in[8]; - _out[8] = tmp; - tmp = _in[3]; - _out[3] = _in[12]; - _out[12] = tmp; - - tmp = _in[6]; - _out[6] = _in[9]; - _out[9] = tmp; - tmp = _in[7]; - _out[7] = _in[13]; - _out[13] = tmp; - - tmp = _in[11]; - _out[11] = _in[14]; - _out[14] = tmp; -} - /* * Copyright (c) 2006 Joshua Allen * All rights reserved. Modified: Mercury2/src/MercuryMatrix.h =================================================================== --- Mercury2/src/MercuryMatrix.h 2009-01-04 18:32:00 UTC (rev 146) +++ Mercury2/src/MercuryMatrix.h 2009-01-04 19:10:52 UTC (rev 147) @@ -7,9 +7,6 @@ class MercuryMatrix; -//Matrix in1 will be copied, n2 will not. n2 better never be the same as out -void TransposeMatrix( const MercuryMatrix& in, MercuryMatrix &out ); - ///General Purpose 4x4 row-major matrix class MercuryMatrix { @@ -38,7 +35,7 @@ void RotateAngAxis( float fAngle, float x, float y, float z ); void Scale(float x, float y, float z); void Transotale( float tX, float tY, float tZ, float rX, float rY, float rZ, float sX, float sY, float sZ ); - inline void Transpose() { TransposeMatrix(*this, *this); } + inline void Transpose() { TransposeMatrix( m_matrix ); } void Zero(); void Identity(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cn...@us...> - 2009-01-22 16:29:03
|
Revision: 150 http://hgengine.svn.sourceforge.net/hgengine/?rev=150&view=rev Author: cnlohr Date: 2009-01-22 16:28:56 +0000 (Thu, 22 Jan 2009) Log Message: ----------- Cleanup non-sse code. Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-01-22 16:27:44 UTC (rev 149) +++ Mercury2/src/MercuryMath.cpp 2009-01-22 16:28:56 UTC (rev 150) @@ -35,7 +35,7 @@ void ZeroFloatRow(FloatRow& r) { - Copy4f(&r, (FloatRow){ 0.0f, 0.0f, 0.0f, 0.0f }); + Copy4f(&r, &gfrZero ); } void Mul4f(const FloatRow* first, const FloatRow* second, FloatRow* out) Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-01-22 16:27:44 UTC (rev 149) +++ Mercury2/src/MercuryMath.h 2009-01-22 16:28:56 UTC (rev 150) @@ -55,6 +55,8 @@ void VectorMultiply4f(const FloatRow* matrix, const FloatRow* p, FloatRow* out ); void TransposeMatrix( FloatRow* m ); +const FloatRow gfrZero = { 0.f, 0.f, 0.f, 0.f }; + #endif /* Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-01-22 16:27:44 UTC (rev 149) +++ Mercury2/src/MercuryMatrix.cpp 2009-01-22 16:28:56 UTC (rev 150) @@ -33,10 +33,12 @@ void MercuryMatrix::Identity() { - Copy4f(&m_matrix[0], (void*)&((FloatRow){ 1.0f, 0.0f, 0.0f, 0.0f })); - Copy4f(&m_matrix[1], (void*)&((FloatRow){ 0.0f, 1.0f, 0.0f, 0.0f })); - Copy4f(&m_matrix[2], (void*)&((FloatRow){ 0.0f, 0.0f, 1.0f, 0.0f })); - Copy4f(&m_matrix[3], (void*)&((FloatRow){ 0.0f, 0.0f, 0.0f, 1.0f })); + const static float Identity[16] = { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f }; + Copy16f(&m_matrix[0], Identity ); /* (*this)[0][0] = 1; (*this)[0][1] = 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-02 19:35:25
|
Revision: 158 http://hgengine.svn.sourceforge.net/hgengine/?rev=158&view=rev Author: axlecrusher Date: 2009-03-02 19:35:20 +0000 (Mon, 02 Mar 2009) Log Message: ----------- Add Plane Added Paths: ----------- Mercury2/src/MercuryPlane.cpp Mercury2/src/MercuryPlane.h Added: Mercury2/src/MercuryPlane.cpp =================================================================== --- Mercury2/src/MercuryPlane.cpp (rev 0) +++ Mercury2/src/MercuryPlane.cpp 2009-03-02 19:35:20 UTC (rev 158) @@ -0,0 +1,65 @@ +#include <MercuryPlane.h> +#include <stdio.h> +#include <MercuryMath.h> + +bool MercuryPlane::IsBehindPlane(const MercuryVertex& point) const +{ + return m_normal.DotProduct( point ) < 0; +} + +bool MercuryPlane::IsBehindPlane(const BoundingBox& bb) const +{ + const MercuryVertex& center = bb.GetCenter(); + const MercuryVertex& extends = bb.GetExtend(); + + MercuryVertex A1, A2, A3, tmp; + + tmp = center+MercuryVertex( extends.GetX(), 0, 0 ); + A1 = (center-tmp).Normalize(); + tmp = center+MercuryVertex( 0, extends.GetY(), 0 ); + A2 = (center-tmp).Normalize(); + tmp = center+MercuryVertex( 0, 0, extends.GetZ() ); + A3 = (center-tmp).Normalize(); + + float x = ABS( extends.GetX() * m_normal.DotProduct( A1 ) ); + x += ABS( extends.GetY() * m_normal.DotProduct( A2 ) ); + x += ABS( extends.GetZ() * m_normal.DotProduct( A3 ) ); + + float d = m_normal.DotProduct( m_center ); + + return ABS( d ) > x; +} + + + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ Added: Mercury2/src/MercuryPlane.h =================================================================== --- Mercury2/src/MercuryPlane.h (rev 0) +++ Mercury2/src/MercuryPlane.h 2009-03-02 19:35:20 UTC (rev 158) @@ -0,0 +1,58 @@ +#ifndef MERCURYPLANE_H +#define MERCURYPLANE_H + +#include <MercuryVertex.h> +#include <BoundingBox.h> + +class MercuryPlane +{ + public: + inline void Setup( const MercuryVertex& center, const MercuryVertex& normal) + { + m_center = center; + SetNormal(normal); + } + + inline void SetCenter(const MercuryVertex& center) { m_center = center; } + inline void SetNormal(const MercuryVertex& normal) { m_normal = normal.Normalize(); } + + bool IsBehindPlane(const MercuryVertex& point) const; + bool IsBehindPlane(const BoundingBox& bb) const; + private: + MercuryVertex m_center; + MercuryVector m_normal; +}; + +#endif + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-02 19:41:42
|
Revision: 159 http://hgengine.svn.sourceforge.net/hgengine/?rev=159&view=rev Author: axlecrusher Date: 2009-03-02 19:41:32 +0000 (Mon, 02 Mar 2009) Log Message: ----------- update Modified Paths: -------------- Mercury2/src/MercuryVertex.cpp Mercury2/src/MercuryVertex.h Modified: Mercury2/src/MercuryVertex.cpp =================================================================== --- Mercury2/src/MercuryVertex.cpp 2009-03-02 19:35:20 UTC (rev 158) +++ Mercury2/src/MercuryVertex.cpp 2009-03-02 19:41:32 UTC (rev 159) @@ -17,7 +17,7 @@ MercuryVertex::MercuryVertex( const float * in ) { for (unsigned int i = 0; i < 3; ++i) - m_xyz[i] *= in[i]; + m_xyz[i] = in[i]; } void MercuryVertex::NormalizeSelf() @@ -86,7 +86,18 @@ return ret; } +float MercuryVertex::DotProduct(const MercuryVertex& rhs) const +{ + return (m_xyz[0]*rhs.m_xyz[0]+m_xyz[1]*rhs.m_xyz[1]+m_xyz[2]*rhs.m_xyz[2]); +} +void MercuryVertex::Print() const +{ + printf("%f %f %f\n", m_xyz[0], m_xyz[1], m_xyz[2]); +} + + + /**************************************************************************** * Copyright (C) 2009 by Joshua Allen * * * Modified: Mercury2/src/MercuryVertex.h =================================================================== --- Mercury2/src/MercuryVertex.h 2009-03-02 19:35:20 UTC (rev 158) +++ Mercury2/src/MercuryVertex.h 2009-03-02 19:41:32 UTC (rev 159) @@ -61,6 +61,9 @@ ///Obtain the cross product (*this) x p MercuryVertex CrossProduct(const MercuryVertex& p) const; + + float DotProduct(const MercuryVertex& rhs) const; + void Print() const; float m_xyz[3]; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-02 20:36:10
|
Revision: 165 http://hgengine.svn.sourceforge.net/hgengine/?rev=165&view=rev Author: axlecrusher Date: 2009-03-02 20:36:05 +0000 (Mon, 02 Mar 2009) Log Message: ----------- fix matrix vector multiply Modified Paths: -------------- Mercury2/src/BoundingBox.cpp Mercury2/src/MercuryMatrix.cpp Modified: Mercury2/src/BoundingBox.cpp =================================================================== --- Mercury2/src/BoundingBox.cpp 2009-03-02 20:19:52 UTC (rev 164) +++ Mercury2/src/BoundingBox.cpp 2009-03-02 20:36:05 UTC (rev 165) @@ -20,12 +20,8 @@ const BoundingBox& bb = *m_bb; MercuryVertex c = GetGlobalMatrix() * m_bb->GetCenter(); - -// GetGlobalMatrix().Print(); - c.Print(); - BoundingBox gbb( c, bb.GetExtend() ); - + c.Print(); // printf("clip %d\n", FRUSTUM->Clip(gbb) ); const float* center = m_bb->GetCenter(); Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-03-02 20:19:52 UTC (rev 164) +++ Mercury2/src/MercuryMatrix.cpp 2009-03-02 20:36:05 UTC (rev 165) @@ -201,6 +201,7 @@ FloatRow r, tvo; v.ConvertToVector4( tmp ); + tmp[3] = 1; Float2FloatRow( tmp, &r ); VectorMultiply4f( m_matrix, &r, &tvo); FloatRow2Float( &tvo, tmp ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-03 02:02:43
|
Revision: 168 http://hgengine.svn.sourceforge.net/hgengine/?rev=168&view=rev Author: axlecrusher Date: 2009-03-03 02:02:34 +0000 (Tue, 03 Mar 2009) Log Message: ----------- Fix broken SSE math, I have to make these math functions safer Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-03-02 21:59:02 UTC (rev 167) +++ Mercury2/src/MercuryMath.cpp 2009-03-03 02:02:34 UTC (rev 168) @@ -278,25 +278,23 @@ //This is an SSE matrix vector multiply, see the standard C++ code //for a clear algorithim. This seems like it works. -void VectorMultiply4f( const FloatRow* matrix, const FloatRow* p, FloatRow* out ) +void VectorMultiply4f( const FloatRow* matrix, const FloatRow& p, FloatRow& out ) { - __m128 tmp; + __m128 tmp, XY; - //compute term 1 and term 2 and store them in the low order - //of outxmm[0] - out[0] = Hadd4( _mm_mul_ps( matrix[0], *p ) ); - tmp = Hadd4( _mm_mul_ps( matrix[1], *p ) ); - out[0] = _mm_unpacklo_ps(out[0], tmp); + //compute term X and term Y and store them in the low order of XY + XY = Hadd4( _mm_mul_ps( matrix[0], p ) ); //compute X + tmp = Hadd4( _mm_mul_ps( matrix[1], p ) ); //compute Y + XY = _mm_unpacklo_ps(XY, tmp); - //compute term 3 and term 4 and store them in the high order - //of outxmm[1] - out[1] = Hadd4( _mm_mul_ps( matrix[2], *p ) ); - tmp = Hadd4( _mm_mul_ps( matrix[3], *p ) ); - out[1] = _mm_unpacklo_ps(out[1], tmp); + //compute term Z and term W and store them in the low order of out + out = Hadd4( _mm_mul_ps( matrix[2], p ) ); //compute Z + tmp = Hadd4( _mm_mul_ps( matrix[3], p ) ); //compute W + out = _mm_unpacklo_ps(out, tmp); - //shuffle the low order of outxmm[0] into the loworder of tmp - //and shuffle the low order of outxmm[1] into the high order of tmp - *out = _mm_movelh_ps(out[0], out[1]); + //shuffle the low order of XY into the loworder of out + //and shuffle the low order of out into the high order of out + out = _mm_movelh_ps(XY, out); } void ZeroFloatRow(FloatRow& r) Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-03-02 21:59:02 UTC (rev 167) +++ Mercury2/src/MercuryMath.h 2009-03-03 02:02:34 UTC (rev 168) @@ -52,7 +52,7 @@ void Copy8f( void * dest, const void * source ); void Copy16f( void * dest, const void * source ); void MatrixMultiply4f ( const FloatRow* in1, const FloatRow* in2, FloatRow* out ); -void VectorMultiply4f(const FloatRow* matrix, const FloatRow* p, FloatRow* out ); +void VectorMultiply4f(const FloatRow* matrix, const FloatRow& p, FloatRow& out ); void TransposeMatrix( FloatRow* m ); void Float2FloatRow(const float* f, FloatRow* r); Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-03-02 21:59:02 UTC (rev 167) +++ Mercury2/src/MercuryMatrix.cpp 2009-03-03 02:02:34 UTC (rev 168) @@ -203,9 +203,9 @@ v.ConvertToVector4( tmp ); tmp[3] = 1; Float2FloatRow( tmp, &r ); - VectorMultiply4f( m_matrix, &r, &tvo); + VectorMultiply4f( m_matrix, r, tvo); FloatRow2Float( &tvo, tmp ); - + printf("%f %f %f %f\n", tmp[0], tmp[1], tmp[2], tmp[3]); return MercuryVertex(tmp); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-03 13:37:34
|
Revision: 171 http://hgengine.svn.sourceforge.net/hgengine/?rev=171&view=rev Author: axlecrusher Date: 2009-03-03 13:37:30 +0000 (Tue, 03 Mar 2009) Log Message: ----------- Make mercury math a little safer to use Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-03-03 13:33:16 UTC (rev 170) +++ Mercury2/src/MercuryMath.cpp 2009-03-03 13:37:30 UTC (rev 171) @@ -1,3 +1,4 @@ +#include <stdint.h> #include "MercuryMath.h" //the SSE version of this was really slow, this is quicker @@ -38,80 +39,46 @@ Copy4f(&r, &gfrZero ); } -void Mul4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Mul4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - (*out)[0] = (*first)[0] * (*second)[0]; - (*out)[1] = (*first)[1] * (*second)[1]; - (*out)[2] = (*first)[2] * (*second)[2]; - (*out)[3] = (*first)[3] * (*second)[3]; + for (uint8_t i = 0; i < 4; ++i) + out[i] = first[i] * second[i]; } -void Div4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Div4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - (*out)[0] = (*first)[0] / (*second)[0]; - (*out)[1] = (*first)[1] / (*second)[1]; - (*out)[2] = (*first)[2] / (*second)[2]; - (*out)[3] = (*first)[3] / (*second)[3]; + for (uint8_t i = 0; i < 4; ++i) + out[i] = first[i] / second[i]; } -void Add4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Add4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - (*out)[0] = (*first)[0] + (*second)[0]; - (*out)[1] = (*first)[1] + (*second)[1]; - (*out)[2] = (*first)[2] + (*second)[2]; - (*out)[3] = (*first)[3] + (*second)[3]; + for (uint8_t i = 0; i < 4; ++i) + out[i] = first[i] + second[i]; } -void Sub4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Sub4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - (*out)[0] = (*first)[0] - (*second)[0]; - (*out)[1] = (*first)[1] - (*second)[1]; - (*out)[2] = (*first)[2] - (*second)[2]; - (*out)[3] = (*first)[3] - (*second)[3]; + for (uint8_t i = 0; i < 4; ++i) + out[i] = first[i] - second[i]; } void Copy4f( void * dest, const void * source ) { - ((float*)dest)[0] = ((float*)source)[0]; - ((float*)dest)[1] = ((float*)source)[1]; - ((float*)dest)[2] = ((float*)source)[2]; - ((float*)dest)[3] = ((float*)source)[3]; + for (uint8_t i = 0; i < 4; ++i) + ((float*)dest)[i] = ((float*)source)[i]; } void Copy8f( void * dest, const void * source ) { - ((float*)dest)[0] = ((float*)source)[0]; - ((float*)dest)[1] = ((float*)source)[1]; - ((float*)dest)[2] = ((float*)source)[2]; - ((float*)dest)[3] = ((float*)source)[3]; - - ((float*)dest)[4] = ((float*)source)[4]; - ((float*)dest)[5] = ((float*)source)[5]; - ((float*)dest)[6] = ((float*)source)[6]; - ((float*)dest)[7] = ((float*)source)[7]; + for (uint8_t i = 0; i < 8; ++i) + ((float*)dest)[i] = ((float*)source)[i]; } void Copy16f( void * dest, const void * source ) { - ((float*)dest)[0] = ((float*)source)[0]; - ((float*)dest)[1] = ((float*)source)[1]; - ((float*)dest)[2] = ((float*)source)[2]; - ((float*)dest)[3] = ((float*)source)[3]; - - ((float*)dest)[4] = ((float*)source)[4]; - ((float*)dest)[5] = ((float*)source)[5]; - ((float*)dest)[6] = ((float*)source)[6]; - ((float*)dest)[7] = ((float*)source)[7]; - - ((float*)dest)[8] = ((float*)source)[8]; - ((float*)dest)[9] = ((float*)source)[9]; - ((float*)dest)[10] = ((float*)source)[10]; - ((float*)dest)[11] = ((float*)source)[11]; - - ((float*)dest)[12] = ((float*)source)[12]; - ((float*)dest)[13] = ((float*)source)[13]; - ((float*)dest)[14] = ((float*)source)[14]; - ((float*)dest)[15] = ((float*)source)[15]; + for (uint8_t i = 0; i < 16; ++i) + ((float*)dest)[i] = ((float*)source)[i]; } void MatrixMultiply4f ( const FloatRow* in1a, const FloatRow* in2a, FloatRow* outa) @@ -168,21 +135,16 @@ out[3] = p[0] * m[12] + p[1] * m[13] + p[2] * m[14] + p[3] * m[15]; } -void Float2FloatRow(const float* f, FloatRow* r) +void Float2FloatRow(const float* f, FloatRow& r) { - float* row = *r; - row[0] = f[0]; - row[1] = f[1]; - row[2] = f[2]; - row[3] = f[3]; + for (uint8_t i = 0; i < 4; ++i) + r[i] = f[i]; } -void FloatRow2Float( const FloatRow* fr, float* f) +void FloatRow2Float( const FloatRow& r, float* f) { - f[0] = (*fr)[0]; - f[1] = (*fr)[1]; - f[2] = (*fr)[2]; - f[3] = (*fr)[3]; + for (uint8_t i = 0; i < 4; ++i) + f[i] = r[i]; } #else @@ -196,24 +158,24 @@ return _mm_add_ps( x, _mm_shuffle_ps(x, x, _MM_SHUFFLE(1,1,1,1)) ); } -void Mul4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Mul4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - *out = _mm_mul_ps( *first, *second ); + out = _mm_mul_ps( first, second ); } -void Div4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Div4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - *out = _mm_div_ps( *first, *second ); + out = _mm_div_ps( first, second ); } -void Add4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Add4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - *out = _mm_add_ps( *first, *second ); + out = _mm_add_ps( first, second ); } -void Sub4f(const FloatRow* first, const FloatRow* second, FloatRow* out) +void Sub4f(const FloatRow& first, const FloatRow& second, FloatRow& out) { - *out = _mm_sub_ps( *first, *second ); + out = _mm_sub_ps( first, second ); } void Copy4f( void * dest, const void * source ) @@ -302,14 +264,14 @@ r = (FloatRow)_mm_setzero_ps(); } -void Float2FloatRow(const float* f, FloatRow* r) +void Float2FloatRow(const float* f, FloatRow& r) { - *r = _mm_load_ps( f ); + r = _mm_load_ps( f ); } -void FloatRow2Float( const FloatRow* fr, float* f) +void FloatRow2Float( const FloatRow& r, float* f) { - _mm_store_ps( f, *fr ); + _mm_store_ps( f, r ); } #endif Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-03-03 13:33:16 UTC (rev 170) +++ Mercury2/src/MercuryMath.h 2009-03-03 13:37:30 UTC (rev 171) @@ -44,10 +44,10 @@ //#define DotProduct(x,y) ((x)[0]*(y)[0]+(x)[1]*(y)[1]+(x)[2]*(y)[2]) -void Mul4f(const FloatRow* first, const FloatRow* second, FloatRow* out); -void Div4f(const FloatRow* first, const FloatRow* second, FloatRow* out); -void Add4f(const FloatRow* first, const FloatRow* second, FloatRow* out); -void Sub4f(const FloatRow* first, const FloatRow* second, FloatRow* out); +void Mul4f(const FloatRow& first, const FloatRow& second, FloatRow& out); +void Div4f(const FloatRow& first, const FloatRow& second, FloatRow& out); +void Add4f(const FloatRow& first, const FloatRow& second, FloatRow& out); +void Sub4f(const FloatRow& first, const FloatRow& second, FloatRow& out); void Copy4f( void * dest, const void * source ); void Copy8f( void * dest, const void * source ); void Copy16f( void * dest, const void * source ); @@ -55,8 +55,8 @@ void VectorMultiply4f(const FloatRow* matrix, const FloatRow& p, FloatRow& out ); void TransposeMatrix( FloatRow* m ); -void Float2FloatRow(const float* f, FloatRow* r); -void FloatRow2Float( const FloatRow* fr, float* f); +void Float2FloatRow(const float* f, FloatRow& r); +void FloatRow2Float(const FloatRow& fr, float* f); const FloatRow gfrZero = { 0.f, 0.f, 0.f, 0.f }; Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-03-03 13:33:16 UTC (rev 170) +++ Mercury2/src/MercuryMatrix.cpp 2009-03-03 13:37:30 UTC (rev 171) @@ -200,12 +200,11 @@ float tmp[4]; FloatRow r, tvo; - v.ConvertToVector4( tmp ); - tmp[3] = 1; - Float2FloatRow( tmp, &r ); + v.ConvertToVector4( tmp, 1 ); + Float2FloatRow( tmp, r ); VectorMultiply4f( m_matrix, r, tvo); - FloatRow2Float( &tvo, tmp ); - printf("%f %f %f %f\n", tmp[0], tmp[1], tmp[2], tmp[3]); + FloatRow2Float( tvo, tmp ); +// printf("%f %f %f %f\n", tmp[0], tmp[1], tmp[2], tmp[3]); return MercuryVertex(tmp); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-03 21:44:11
|
Revision: 174 http://hgengine.svn.sourceforge.net/hgengine/?rev=174&view=rev Author: axlecrusher Date: 2009-03-03 21:44:02 +0000 (Tue, 03 Mar 2009) Log Message: ----------- compute box normals Modified Paths: -------------- Mercury2/src/BoundingBox.cpp Mercury2/src/BoundingBox.h Modified: Mercury2/src/BoundingBox.cpp =================================================================== --- Mercury2/src/BoundingBox.cpp 2009-03-03 21:41:55 UTC (rev 173) +++ Mercury2/src/BoundingBox.cpp 2009-03-03 21:44:02 UTC (rev 174) @@ -4,12 +4,45 @@ #include <Viewport.h> +BoundingBox::BoundingBox(const MercuryVertex& center, const MercuryVertex& extend) + :m_center(center), m_extend(extend) +{ + ComputeNormals(); +}; + void BoundingBox::LoadFromBinary(char* data) { memcpy(m_center, data, sizeof(float)*3); memcpy(m_extend, data+(sizeof(float)*3), sizeof(float)*3); + ComputeNormals(); } +void BoundingBox::ComputeNormals() +{ + MercuryVertex t(m_center); + t.SetX( t.GetX() + m_extend.GetX() ); + m_normals[0] = (m_center - t).Normalize(); + + t = m_center; + t.SetY( t.GetY() + m_extend.GetY() ); + m_normals[1] = (m_center - t).Normalize(); + + t = m_center; + t.SetZ( t.GetZ() + m_extend.GetZ() ); + m_normals[2] = (m_center - t).Normalize(); +} + +BoundingBox BoundingBox::Transform( const MercuryMatrix& m ) const +{ + BoundingBox bb; + bb.m_extend = m_center; + bb.m_center = m * m_center; + bb.m_normals[0] = (m * m_normals[0]).Normalize(); + bb.m_normals[1] = (m * m_normals[1]).Normalize(); + bb.m_normals[2] = (m * m_normals[2]).Normalize(); + return bb; +} + RenderableBoundingBox::RenderableBoundingBox(const BoundingBox* bb) :MercuryAsset(), m_bb(bb) { @@ -19,11 +52,11 @@ { const BoundingBox& bb = *m_bb; - MercuryVertex c = GetGlobalMatrix() * m_bb->GetCenter(); - BoundingBox gbb( c, bb.GetExtend() ); - c.Print(); -// printf("clip %d\n", FRUSTUM->Clip(gbb) ); + BoundingBox gbb = m_bb->Transform( GetGlobalMatrix() ); + FRUSTUM->m_planes[PFAR].IsBehindPlane( gbb.GetCenter() ); +// printf("clip %d\n", FRUSTUM->m_planes[PFAR].IsBehindPlane( gbb.GetCenter() )); + const float* center = m_bb->GetCenter(); const float* extend = m_bb->GetExtend(); Modified: Mercury2/src/BoundingBox.h =================================================================== --- Mercury2/src/BoundingBox.h 2009-03-03 21:41:55 UTC (rev 173) +++ Mercury2/src/BoundingBox.h 2009-03-03 21:44:02 UTC (rev 174) @@ -3,23 +3,29 @@ #include <MercuryAsset.h> #include <MercuryVertex.h> +#include <MercuryMatrix.h> class BoundingBox { public: BoundingBox() {}; - BoundingBox(const MercuryVertex& center, const MercuryVertex& extend) - :m_center(center), m_extend(extend) - {}; + BoundingBox(const MercuryVertex& center, const MercuryVertex& extend); void LoadFromBinary(char* data); inline const MercuryVertex& GetCenter() const { return m_center; } inline const MercuryVertex& GetExtend() const { return m_extend; } + BoundingBox Transform( const MercuryMatrix& m ) const; + const MercuryVector& Normal(uint8_t i) const { return m_normals[i]; } + private: + void ComputeNormals(); + MercuryVertex m_center; MercuryVertex m_extend; + + MercuryVector m_normals[3]; }; class RenderableBoundingBox : public MercuryAsset This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-15 20:38:42
|
Revision: 178 http://hgengine.svn.sourceforge.net/hgengine/?rev=178&view=rev Author: axlecrusher Date: 2009-03-15 20:38:32 +0000 (Sun, 15 Mar 2009) Log Message: ----------- lots of updates to math and vertex Modified Paths: -------------- Mercury2/src/BoundingBox.cpp Mercury2/src/BoundingBox.h Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Mercury2/src/MercuryPlane.cpp Mercury2/src/MercuryVertex.cpp Mercury2/src/MercuryVertex.h Mercury2/src/Viewport.h Modified: Mercury2/src/BoundingBox.cpp =================================================================== --- Mercury2/src/BoundingBox.cpp 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/BoundingBox.cpp 2009-03-15 20:38:32 UTC (rev 178) @@ -32,7 +32,7 @@ m_normals[2] = (m_center - t).Normalize(); } -BoundingBox BoundingBox::Transform( const MercuryMatrix& m ) const +void BoundingBox::Transform( const MercuryMatrix& m ) { BoundingBox bb; bb.m_extend = m_center; @@ -40,7 +40,8 @@ bb.m_normals[0] = (m * m_normals[0]).Normalize(); bb.m_normals[1] = (m * m_normals[1]).Normalize(); bb.m_normals[2] = (m * m_normals[2]).Normalize(); - return bb; + *this = bb; +// return bb; } RenderableBoundingBox::RenderableBoundingBox(const BoundingBox* bb) @@ -50,7 +51,8 @@ void RenderableBoundingBox::Render(MercuryNode* node) { - BoundingBox gbb = m_bb->Transform( GetGlobalMatrix() ); + BoundingBox gbb = *m_bb; + gbb.Transform( GetGlobalMatrix() ); if ( FRUSTUM->Clip( gbb ) ) return; const float* center = m_bb->GetCenter(); Modified: Mercury2/src/BoundingBox.h =================================================================== --- Mercury2/src/BoundingBox.h 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/BoundingBox.h 2009-03-15 20:38:32 UTC (rev 178) @@ -6,18 +6,25 @@ #include <MercuryMatrix.h> #include <stdint.h> -class BoundingBox +class BoundingVolume { public: + virtual void LoadFromBinary(char* data) = 0; + virtual void Transform( const MercuryMatrix& m ) = 0; +}; + +class BoundingBox : public BoundingVolume +{ + public: BoundingBox() {}; BoundingBox(const MercuryVertex& center, const MercuryVertex& extend); - void LoadFromBinary(char* data); + virtual void LoadFromBinary(char* data); inline const MercuryVertex& GetCenter() const { return m_center; } inline const MercuryVertex& GetExtend() const { return m_extend; } - BoundingBox Transform( const MercuryMatrix& m ) const; + virtual void Transform( const MercuryMatrix& m ); const MercuryVector& Normal(uint8_t i) const { return m_normals[i]; } private: Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/MercuryMath.h 2009-03-15 20:38:32 UTC (rev 178) @@ -55,9 +55,23 @@ void VectorMultiply4f(const FloatRow* matrix, const FloatRow& p, FloatRow& out ); void TransposeMatrix( FloatRow* m ); -void Float2FloatRow(const float* f, FloatRow& r); -void FloatRow2Float(const FloatRow& fr, float* f); +//void Float2FloatRow(const float* f, FloatRow& r); +//void FloatRow2Float(const FloatRow& fr, float* f); +inline void MMCrossProduct( const FloatRow& r1, const FloatRow& r2, FloatRow& result) +{ + __m128 a,b,c,d,r;//using more registers is faster + + a = _mm_shuffle_ps(r1, r1, 0xc9); + b = _mm_shuffle_ps(r2, r2, 0xd2); + r = _mm_mul_ps( a, b ); + + c = _mm_shuffle_ps(r2, r2, 0xc9); + d = _mm_shuffle_ps(r1, r1, 0xd2); + r -= _mm_mul_ps( c, d ); + result = r; +} + const FloatRow gfrZero = { 0.f, 0.f, 0.f, 0.f }; #endif Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/MercuryMatrix.cpp 2009-03-15 20:38:32 UTC (rev 178) @@ -197,15 +197,9 @@ MercuryVector MercuryMatrix::operator*(const MercuryVector& v) const { - float tmp[4]; - FloatRow r, tvo; - - v.ConvertToVector4( tmp, 1 ); - Float2FloatRow( tmp, r ); - VectorMultiply4f( m_matrix, r, tvo); - FloatRow2Float( tvo, tmp ); -// printf("%f %f %f %f\n", tmp[0], tmp[1], tmp[2], tmp[3]); - return MercuryVertex(tmp); + MercuryVector r; + VectorMultiply4f( m_matrix, v.ToFloatRow(), r.ToFloatRow() ); + return r; } Modified: Mercury2/src/MercuryPlane.cpp =================================================================== --- Mercury2/src/MercuryPlane.cpp 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/MercuryPlane.cpp 2009-03-15 20:38:32 UTC (rev 178) @@ -2,10 +2,14 @@ #include <stdio.h> #include <MercuryMath.h> +#define SIGNED_DIST(x) m_normal.DotProduct(x) + +// origional algorithim was -x<0 +#define BEHIND_PLANE(x) x>=0 + bool MercuryPlane::IsBehindPlane(const MercuryVertex& point) const { - // origional algorithim was -dotproduct < 0 - return m_normal.DotProduct( point+m_center ) >= 0; + return BEHIND_PLANE( SIGNED_DIST( point+m_center ) ); } bool MercuryPlane::IsBehindPlane(const BoundingBox& bb) const @@ -14,16 +18,17 @@ const MercuryVertex& extends = bb.GetExtend(); const MercuryVertex &A1(bb.Normal(0)), &A2(bb.Normal(1)), &A3(bb.Normal(2)); - - float x = ABS( extends.GetX() * m_normal.DotProduct( A1 ) ); - x += ABS( extends.GetY() * m_normal.DotProduct( A2 ) ); - x += ABS( extends.GetZ() * m_normal.DotProduct( A3 ) ); - float d = m_normal.DotProduct( center+m_center ); + MercuryVertex dp = m_normal.DotProduct3(A1, A2, A3); + float x = ABS( extends.GetX() * dp[0] ); + x += ABS( extends.GetY() * dp[1] ); + x += ABS( extends.GetZ() * dp[2] ); + + float d = SIGNED_DIST( center+m_center ); if ( ABS(d) <= x ) //intersection return false; - - return IsBehindPlane( center ); //if we don't intersect, just see what side we are on + + return BEHIND_PLANE(d); //if we don't intersect, just see what side we are on } Modified: Mercury2/src/MercuryVertex.cpp =================================================================== --- Mercury2/src/MercuryVertex.cpp 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/MercuryVertex.cpp 2009-03-15 20:38:32 UTC (rev 178) @@ -4,33 +4,33 @@ MercuryVertex::MercuryVertex() { - m_xyz[0] = m_xyz[1] = m_xyz[2] = 0; + (*this)[0] = (*this)[1] = (*this)[2] = 0; } MercuryVertex::MercuryVertex( float ix, float iy, float iz ) { - m_xyz[0] = ix; - m_xyz[1] = iy; - m_xyz[2] = iz; + (*this)[0] = ix; + (*this)[1] = iy; + (*this)[2] = iz; } MercuryVertex::MercuryVertex( const float * in ) { for (unsigned int i = 0; i < 3; ++i) - m_xyz[i] = in[i]; + (*this)[i] = in[i]; } MercuryVertex::MercuryVertex( const MercuryVertex& v) { for (unsigned int i = 0; i < 3; ++i) - m_xyz[i] = v.m_xyz[i]; + (*this)[i] = v[i]; } void MercuryVertex::NormalizeSelf() { float imag = 1.0f/Length(); for (unsigned int i = 0; i < 3; ++i) - m_xyz[i] *= imag; + (*this)[i] *= imag; } MercuryVertex MercuryVertex::Normalize() const @@ -42,64 +42,81 @@ float MercuryVertex::Length() const { - float length = m_xyz[0]*m_xyz[0]; - length += m_xyz[1]*m_xyz[1]; - length += m_xyz[2]*m_xyz[2]; + float length = (*this)[0]*(*this)[0]; + length += (*this)[1]*(*this)[1]; + length += (*this)[2]*(*this)[2]; return SQRT(length); } float MercuryVertex::GetBiggestElement() const { - float tmp = m_xyz[0]; - tmp = max<float>(tmp, m_xyz[1]); - return max<float>(tmp, m_xyz[2]); + float tmp = (*this)[0]; + tmp = max<float>(tmp, (*this)[1]); + return max<float>(tmp, (*this)[2]); } const MercuryVertex& MercuryVertex::operator *= (const MercuryVertex& p) { for (unsigned int i = 0; i < 3; ++i) - m_xyz[i] *= p.m_xyz[i]; + (*this)[i] *= p[i]; return *this; } const MercuryVertex& MercuryVertex::operator /= (const MercuryVertex& p) { for (unsigned int i = 0; i < 3; ++i) - m_xyz[i] /= p.m_xyz[i]; + (*this)[i] /= p[i]; return *this; } bool MercuryVertex::operator==(const MercuryVertex& p) const { for (unsigned int i = 0; i < 3; ++i) - if (m_xyz[i] != p.m_xyz[i]) return false; + if ((*this)[i] != p[i]) return false; return true; } bool MercuryVertex::operator==(const float f) const { for (unsigned int i = 0; i < 3; ++i) - if (m_xyz[i] != f) return false; + if ((*this)[i] != f) return false; return true; } MercuryVertex MercuryVertex::CrossProduct(const MercuryVertex& p) const { MercuryVertex ret; - ret.m_xyz[0] = m_xyz[1]*p.m_xyz[2] - m_xyz[2]*p.m_xyz[1]; - ret.m_xyz[1] = m_xyz[2]*p.m_xyz[0] - m_xyz[0]*p.m_xyz[2]; - ret.m_xyz[2] = m_xyz[0]*p.m_xyz[1] - m_xyz[1]*p.m_xyz[0]; + ret[0] = (*this)[1]*p[2] - (*this)[2]*p[1]; + ret[1] = (*this)[2]*p[0] - (*this)[0]*p[2]; + ret[2] = (*this)[0]*p[1] - (*this)[1]*p[0]; return ret; } +MercuryVertex MercuryVertex::CrossProductSSE(const MercuryVertex& p) const +{ + //right now this is a hare slower than the C cross product above + MercuryVertex r; + MMCrossProduct( m_xyzw, p.m_xyzw, r.m_xyzw ); + return r; +} + float MercuryVertex::DotProduct(const MercuryVertex& rhs) const { - return (m_xyz[0]*rhs.m_xyz[0]+m_xyz[1]*rhs.m_xyz[1]+m_xyz[2]*rhs.m_xyz[2]); + return ((*this)[0]*rhs[0]+(*this)[1]*rhs[1]+(*this)[2]*rhs[2]); } +MercuryVertex MercuryVertex::DotProduct3(const MercuryVertex& rhs1, const MercuryVertex& rhs2, const MercuryVertex& rhs3) const +{ + MercuryVertex dp; + dp[0] = DotProduct(rhs1); + dp[1] = DotProduct(rhs2); + dp[2] = DotProduct(rhs3); + return dp; +} + void MercuryVertex::Print() const { - printf("%f %f %f\n", m_xyz[0], m_xyz[1], m_xyz[2]); + printf("%f %f %f\n", (*this)[0], (*this)[1], (*this)[2]); } Modified: Mercury2/src/MercuryVertex.h =================================================================== --- Mercury2/src/MercuryVertex.h 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/MercuryVertex.h 2009-03-15 20:38:32 UTC (rev 178) @@ -1,6 +1,10 @@ #ifndef MERCURYVECTOR_H #define MERCURYVECTOR_H +#include <MercuryMath.h> + +# define __inline__ __inline__ __attribute__((always_inline)) + class MercuryVertex { public: @@ -10,18 +14,21 @@ MercuryVertex( const MercuryVertex& v); ///Direct conversion to float* - operator float* () { return m_xyz; } + __inline__ operator float* () { return (float*)&m_xyzw; } ///Direct conversion to const float* - operator const float* () const { return m_xyz; } + __inline__ operator const float* () const { return (float*)&m_xyzw; } - inline const float GetX() const { return m_xyz[0]; } - inline const float GetY() const { return m_xyz[1]; } - inline const float GetZ() const { return m_xyz[2]; } - inline void SetX(const float ix) { m_xyz[0] = ix; } - inline void SetY(const float iy) { m_xyz[1] = iy; } - inline void SetZ(const float iz) { m_xyz[2] = iz; } + inline FloatRow& ToFloatRow() { return m_xyzw; } + inline const FloatRow& ToFloatRow() const { return m_xyzw; } + + inline const float GetX() const { return (*this)[0]; } + inline const float GetY() const { return (*this)[1]; } + inline const float GetZ() const { return (*this)[2]; } + inline void SetX(const float ix) { (*this)[0] = ix; } + inline void SetY(const float iy) { (*this)[1] = iy; } + inline void SetZ(const float iz) { (*this)[2] = iz; } - inline void Zero() { m_xyz[0] = 0; m_xyz[1] = 0; m_xyz[2] = 0; } + inline void Zero() { (*this)[0] = 0; (*this)[1] = 0; (*this)[2] = 0; } ///Normalize (make |point| = 1) void NormalizeSelf(); @@ -33,26 +40,26 @@ float GetBiggestElement() const; ///Write out to be = to this point - inline void ConvertToVector3( float* out ) const { out[0] = m_xyz[0]; out[1] = m_xyz[1]; out[2] = m_xyz[2]; } + inline void ConvertToVector3( float* out ) const { out[0] = (*this)[0]; out[1] = (*this)[1]; out[2] = (*this)[2]; } ///Write out to be = to this point, however the 4th element will be 0 - inline void ConvertToVector4( float* out, float w = 0 ) const { out[0] = m_xyz[0]; out[1] = m_xyz[1]; out[2] = m_xyz[2]; out[3] = w; } + inline void ConvertToVector4( float* out, float w = 0 ) const { out[0] = (*this)[0]; out[1] = (*this)[1]; out[2] = (*this)[2]; out[3] = w; } ///Write out to be = - to this point, however the 4th element will be 0 - inline void ConvertToIVector4( float* out, float w = 0 ) const { out[0] = -m_xyz[0]; out[1] = -m_xyz[1]; out[2] = -m_xyz[2]; out[3] = w; } + inline void ConvertToIVector4( float* out, float w = 0 ) const { out[0] = -(*this)[0]; out[1] = -(*this)[1]; out[2] = -(*this)[2]; out[3] = w; } const MercuryVertex& operator *= (const MercuryVertex& p); const MercuryVertex& operator /= (const MercuryVertex& p); inline MercuryVertex operator * (const MercuryVertex& p) const { MercuryVertex r(*this); r*=p; return r; } inline MercuryVertex operator / (const MercuryVertex& p) const { MercuryVertex r(*this); r/=p; return r; } - inline MercuryVertex& operator += ( const MercuryVertex& other ) { m_xyz[0]+=other.m_xyz[0]; m_xyz[1]+=other.m_xyz[1]; m_xyz[2]+=other.m_xyz[2]; return *this; } - inline MercuryVertex& operator -= ( const MercuryVertex& other ) { m_xyz[0]-=other.m_xyz[0]; m_xyz[1]-=other.m_xyz[1]; m_xyz[2]-=other.m_xyz[2]; return *this; } - inline MercuryVertex& operator *= ( float f ) { m_xyz[0]*=f; m_xyz[1]*=f; m_xyz[2]*=f; return *this; } - inline MercuryVertex& operator /= ( float f ) { m_xyz[0]/=f; m_xyz[1]/=f; m_xyz[2]/=f; return *this; } + inline MercuryVertex& operator += ( const MercuryVertex& other ) { (*this)[0]+=other[0]; (*this)[1]+=other[1]; (*this)[2]+=other[2]; return *this; } + inline MercuryVertex& operator -= ( const MercuryVertex& other ) { (*this)[0]-=other[0]; (*this)[1]-=other[1]; (*this)[2]-=other[2]; return *this; } + inline MercuryVertex& operator *= ( float f ) { (*this)[0]*=f; (*this)[1]*=f; (*this)[2]*=f; return *this; } + inline MercuryVertex& operator /= ( float f ) { (*this)[0]/=f; (*this)[1]/=f; (*this)[2]/=f; return *this; } - inline MercuryVertex operator + ( const MercuryVertex& other ) const { return MercuryVertex( m_xyz[0]+other.m_xyz[0], m_xyz[1]+other.m_xyz[1], m_xyz[2]+other.m_xyz[2] ); } - inline MercuryVertex operator - ( const MercuryVertex& other ) const { return MercuryVertex( m_xyz[0]-other.m_xyz[0], m_xyz[1]-other.m_xyz[1], m_xyz[2]-other.m_xyz[2] ); } - inline MercuryVertex operator * ( float f ) const { return MercuryVertex( m_xyz[0]*f, m_xyz[1]*f, m_xyz[2]*f ); } - inline MercuryVertex operator / ( float f ) const { return MercuryVertex( m_xyz[0]/f, m_xyz[1]/f, m_xyz[2]/f ); } + inline MercuryVertex operator + ( const MercuryVertex& other ) const { return MercuryVertex( (*this)[0]+other[0], (*this)[1]+other[1], (*this)[2]+other[2] ); } + inline MercuryVertex operator - ( const MercuryVertex& other ) const { return MercuryVertex( (*this)[0]-other[0], (*this)[1]-other[1], (*this)[2]-other[2] ); } + inline MercuryVertex operator * ( float f ) const { return MercuryVertex( (*this)[0]*f, (*this)[1]*f, (*this)[2]*f ); } + inline MercuryVertex operator / ( float f ) const { return MercuryVertex( (*this)[0]/f, (*this)[1]/f, (*this)[2]/f ); } bool operator==(const MercuryVertex& p) const; inline bool operator!=(const MercuryVertex& p) const { return !(*this == p); } @@ -62,11 +69,15 @@ ///Obtain the cross product (*this) x p MercuryVertex CrossProduct(const MercuryVertex& p) const; + MercuryVertex CrossProductSSE(const MercuryVertex& p) const; float DotProduct(const MercuryVertex& rhs) const; + MercuryVertex DotProduct3(const MercuryVertex& rhs1, const MercuryVertex& rhs2, const MercuryVertex& rhs3) const; + void Print() const; - float m_xyz[3]; +// float (*this)[3]; + FloatRow m_xyzw; }; typedef MercuryVertex MercuryVector; Modified: Mercury2/src/Viewport.h =================================================================== --- Mercury2/src/Viewport.h 2009-03-08 04:19:56 UTC (rev 177) +++ Mercury2/src/Viewport.h 2009-03-15 20:38:32 UTC (rev 178) @@ -9,11 +9,11 @@ enum PlanePos { PTOP = 0, - PBOTTOM, - PLEFT, - PRIGHT, - PNEAR, - PFAR + PBOTTOM, + PLEFT, + PRIGHT, + PNEAR, + PFAR }; class Frustum @@ -23,13 +23,10 @@ void SetPerspective( float fov, float aspect, float znear, float zfar ); const MercuryMatrix& GetMatrix() const { return m_frustum; } void ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar); - void LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up); - bool Clip(const BoundingBox& bb) const; - - MercuryPlane m_planes[6]; private: + MercuryPlane m_planes[6]; MercuryMatrix m_frustum; float m_aspect, m_fov, m_zNear, m_zFar; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-15 20:47:06
|
Revision: 180 http://hgengine.svn.sourceforge.net/hgengine/?rev=180&view=rev Author: axlecrusher Date: 2009-03-15 20:46:54 +0000 (Sun, 15 Mar 2009) Log Message: ----------- replace crossproducts Modified Paths: -------------- Mercury2/src/MercuryVertex.cpp Mercury2/src/MercuryVertex.h Modified: Mercury2/src/MercuryVertex.cpp =================================================================== --- Mercury2/src/MercuryVertex.cpp 2009-03-15 20:42:29 UTC (rev 179) +++ Mercury2/src/MercuryVertex.cpp 2009-03-15 20:46:54 UTC (rev 180) @@ -85,16 +85,6 @@ MercuryVertex MercuryVertex::CrossProduct(const MercuryVertex& p) const { - MercuryVertex ret; - ret[0] = (*this)[1]*p[2] - (*this)[2]*p[1]; - ret[1] = (*this)[2]*p[0] - (*this)[0]*p[2]; - ret[2] = (*this)[0]*p[1] - (*this)[1]*p[0]; - return ret; -} - -MercuryVertex MercuryVertex::CrossProductSSE(const MercuryVertex& p) const -{ - //right now this is a hare slower than the C cross product above MercuryVertex r; MMCrossProduct( m_xyzw, p.m_xyzw, r.m_xyzw ); return r; Modified: Mercury2/src/MercuryVertex.h =================================================================== --- Mercury2/src/MercuryVertex.h 2009-03-15 20:42:29 UTC (rev 179) +++ Mercury2/src/MercuryVertex.h 2009-03-15 20:46:54 UTC (rev 180) @@ -69,7 +69,6 @@ ///Obtain the cross product (*this) x p MercuryVertex CrossProduct(const MercuryVertex& p) const; - MercuryVertex CrossProductSSE(const MercuryVertex& p) const; float DotProduct(const MercuryVertex& rhs) const; MercuryVertex DotProduct3(const MercuryVertex& rhs1, const MercuryVertex& rhs2, const MercuryVertex& rhs3) const; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-15 22:18:11
|
Revision: 181 http://hgengine.svn.sourceforge.net/hgengine/?rev=181&view=rev Author: axlecrusher Date: 2009-03-15 22:17:56 +0000 (Sun, 15 Mar 2009) Log Message: ----------- update to semaphore and count VBO draw commands Modified Paths: -------------- Mercury2/src/MSemaphore.cpp Mercury2/src/MSemaphore.h Mercury2/src/Mercury2.cpp Mercury2/src/MercuryVBO.cpp Mercury2/src/MercuryVBO.h Mercury2/src/RenderableNode.cpp Modified: Mercury2/src/MSemaphore.cpp =================================================================== --- Mercury2/src/MSemaphore.cpp 2009-03-15 20:46:54 UTC (rev 180) +++ Mercury2/src/MSemaphore.cpp 2009-03-15 22:17:56 UTC (rev 181) @@ -5,11 +5,16 @@ { } -unsigned long MSemaphore::ReadValue() +unsigned long MSemaphore::Read() { return __sync_or_and_fetch(&m_counter, 0); } +unsigned long MSemaphore::ReadAndClear() +{ + return __sync_fetch_and_and(&m_counter, 0); +} + unsigned long MSemaphore::Decrement() { return __sync_sub_and_fetch(&m_counter, 1); Modified: Mercury2/src/MSemaphore.h =================================================================== --- Mercury2/src/MSemaphore.h 2009-03-15 20:46:54 UTC (rev 180) +++ Mercury2/src/MSemaphore.h 2009-03-15 22:17:56 UTC (rev 181) @@ -6,7 +6,8 @@ public: MSemaphore(); - unsigned long ReadValue(); + unsigned long Read(); + unsigned long ReadAndClear(); unsigned long Decrement(); unsigned long Increment(); Modified: Mercury2/src/Mercury2.cpp =================================================================== --- Mercury2/src/Mercury2.cpp 2009-03-15 20:46:54 UTC (rev 180) +++ Mercury2/src/Mercury2.cpp 2009-03-15 22:17:56 UTC (rev 181) @@ -17,7 +17,7 @@ MSemaphore UpdateLoopGo; void* UpdateThread(void* node) { - while( UpdateLoopGo.ReadValue() < 1 ) + while( UpdateLoopGo.Read() < 1 ) { MercuryNode* n = (MercuryNode*)node; n->RecursiveUpdate(0.01f); @@ -69,7 +69,8 @@ fpsTimer.Touch(timer); if (fpsTimer.Age() > 1) { - printf("FPS: %f\n", m_count/fpsTimer.Age()); + float batches = MercuryVBO::ResetBatchCount()/(float)m_count; + printf("FPS: %f, VBO batches %f\n", m_count/fpsTimer.Age(), batches); m_count = 0; fpsTimer = timer; } Modified: Mercury2/src/MercuryVBO.cpp =================================================================== --- Mercury2/src/MercuryVBO.cpp 2009-03-15 20:46:54 UTC (rev 180) +++ Mercury2/src/MercuryVBO.cpp 2009-03-15 22:17:56 UTC (rev 181) @@ -50,6 +50,7 @@ } glDrawRangeElements(GL_TRIANGLES, 0, m_indexData.Length()-1, m_indexData.Length(), GL_UNSIGNED_SHORT, NULL); + m_vboBatches.Increment(); m_lastVBOrendered = this; @@ -88,6 +89,8 @@ } void* MercuryVBO::m_lastVBOrendered = NULL; +MSemaphore MercuryVBO::m_vboBatches; + /**************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/MercuryVBO.h =================================================================== --- Mercury2/src/MercuryVBO.h 2009-03-15 20:46:54 UTC (rev 180) +++ Mercury2/src/MercuryVBO.h 2009-03-15 22:17:56 UTC (rev 181) @@ -5,6 +5,8 @@ #include <AlignedBuffer.h> #include <BoundingBox.h> +#include <MSemaphore.h> + class MercuryVBO : public MercuryAsset { public: @@ -15,6 +17,9 @@ void AllocateVertexSpace(unsigned int count); void AllocateIndexSpace(unsigned int count); + + static uint32_t BatchCount() { return m_vboBatches.Read(); } + static uint32_t ResetBatchCount() { return m_vboBatches.ReadAndClear(); } private: virtual void InitVBO(); @@ -27,6 +32,8 @@ AlignedBuffer<float> m_vertexData; AlignedBuffer<uint16_t> m_indexData; BoundingBox* m_boundingBox; + + static MSemaphore m_vboBatches; }; #endif Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2009-03-15 20:46:54 UTC (rev 180) +++ Mercury2/src/RenderableNode.cpp 2009-03-15 22:17:56 UTC (rev 181) @@ -163,14 +163,14 @@ unsigned long RenderableNode::Spinlock( unsigned long value ) { unsigned long waited = 0; - while (m_semaphore.ReadValue() != value) ++waited; + while (m_semaphore.Read() != value) ++waited; return waited; } unsigned long RenderableNode::SpinlockWait( unsigned long value, unsigned long usec ) { unsigned long waited = 0; - while (m_semaphore.ReadValue() != value) + while (m_semaphore.Read() != value) { // waited=true; ++waited; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-16 00:09:09
|
Revision: 182 http://hgengine.svn.sourceforge.net/hgengine/?rev=182&view=rev Author: axlecrusher Date: 2009-03-16 00:09:00 +0000 (Mon, 16 Mar 2009) Log Message: ----------- clean up bounding boxes and rendering Modified Paths: -------------- Mercury2/src/BoundingBox.cpp Mercury2/src/BoundingBox.h Mercury2/src/HGMDLMesh.cpp Mercury2/src/MercuryAsset.cpp Mercury2/src/MercuryAsset.h Mercury2/src/MercuryVBO.cpp Mercury2/src/MercuryVBO.h Mercury2/src/RenderableNode.cpp Mercury2/src/RenderableNode.h Modified: Mercury2/src/BoundingBox.cpp =================================================================== --- Mercury2/src/BoundingBox.cpp 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/BoundingBox.cpp 2009-03-16 00:09:00 UTC (rev 182) @@ -41,22 +41,17 @@ bb.m_normals[1] = (m * m_normals[1]).Normalize(); bb.m_normals[2] = (m * m_normals[2]).Normalize(); *this = bb; -// return bb; } -RenderableBoundingBox::RenderableBoundingBox(const BoundingBox* bb) - :MercuryAsset(), m_bb(bb) +void BoundingBox::Render() { -} +// BoundingBox gbb = *this; -void RenderableBoundingBox::Render(MercuryNode* node) -{ - BoundingBox gbb = *m_bb; - gbb.Transform( GetGlobalMatrix() ); - if ( FRUSTUM->Clip( gbb ) ) return; +// gbb.Transform( ((RenderableNode*)node)->GetGlobalMatrix() ); +// if ( FRUSTUM->Clip( gbb ) ) return; - const float* center = m_bb->GetCenter(); - const float* extend = m_bb->GetExtend(); + const float* center = GetCenter(); + const float* extend = GetExtend(); glPushAttrib( GL_CURRENT_BIT ); glBegin(GL_LINES); Modified: Mercury2/src/BoundingBox.h =================================================================== --- Mercury2/src/BoundingBox.h 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/BoundingBox.h 2009-03-16 00:09:00 UTC (rev 182) @@ -1,7 +1,7 @@ #ifndef BOUNDINGBOX_H #define BOUNDINGBOX_H -#include <MercuryAsset.h> +#include <MercuryNode.h> #include <MercuryVertex.h> #include <MercuryMatrix.h> #include <stdint.h> @@ -9,8 +9,11 @@ class BoundingVolume { public: + virtual ~BoundingVolume() {}; + virtual void LoadFromBinary(char* data) = 0; virtual void Transform( const MercuryMatrix& m ) = 0; + virtual void Render() {}; }; class BoundingBox : public BoundingVolume @@ -18,6 +21,7 @@ public: BoundingBox() {}; BoundingBox(const MercuryVertex& center, const MercuryVertex& extend); + virtual ~BoundingBox() {}; virtual void LoadFromBinary(char* data); @@ -26,6 +30,8 @@ virtual void Transform( const MercuryMatrix& m ); const MercuryVector& Normal(uint8_t i) const { return m_normals[i]; } + + virtual void Render(); private: void ComputeNormals(); @@ -35,7 +41,7 @@ MercuryVector m_normals[3]; }; - +/* class RenderableBoundingBox : public MercuryAsset { public: @@ -45,7 +51,7 @@ private: const BoundingBox* m_bb; }; - +*/ #endif /**************************************************************************** Modified: Mercury2/src/HGMDLMesh.cpp =================================================================== --- Mercury2/src/HGMDLMesh.cpp 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/HGMDLMesh.cpp 2009-03-16 00:09:00 UTC (rev 182) @@ -87,9 +87,9 @@ { data = new char[length]; hgmdl->Read( data, length ); - - m_boundingBox = new BoundingBox(); - m_boundingBox->LoadFromBinary( data ); + + m_boundingVolume = new BoundingBox(); + m_boundingVolume->LoadFromBinary( data ); } SAFE_DELETE_ARRAY(data); Modified: Mercury2/src/MercuryAsset.cpp =================================================================== --- Mercury2/src/MercuryAsset.cpp 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/MercuryAsset.cpp 2009-03-16 00:09:00 UTC (rev 182) @@ -2,10 +2,14 @@ #include <RenderableNode.h> MercuryAsset::MercuryAsset() - :m_isInstanced(false) + :m_isInstanced(false), m_boundingVolume(NULL) { } +MercuryAsset::~MercuryAsset() +{ + SAFE_DELETE(m_boundingVolume); +} void MercuryAsset::Init(MercuryNode* node) { @@ -68,11 +72,6 @@ } -const MercuryMatrix& MercuryAsset::GetGlobalMatrix() const -{ - return GLOBALMATRIX; -} - std::map<MString, MercuryAsset*> AssetFactory::m_assetInstances; /*************************************************************************** Modified: Mercury2/src/MercuryAsset.h =================================================================== --- Mercury2/src/MercuryAsset.h 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/MercuryAsset.h 2009-03-16 00:09:00 UTC (rev 182) @@ -6,12 +6,13 @@ #include <MessageHandler.h> #include <map> #include <MercuryMatrix.h> +#include <BoundingBox.h> class MercuryAsset : public RefBase, MessageHandler { public: MercuryAsset(); - virtual ~MercuryAsset() {}; + virtual ~MercuryAsset(); virtual void Init(MercuryNode* node); @@ -23,9 +24,9 @@ virtual void LoadFromXML(const XMLNode& node) {}; inline void IsInstanced(bool b) { m_isInstanced = b; } - const MercuryMatrix& GetGlobalMatrix() const; protected: bool m_isInstanced; + BoundingVolume* m_boundingVolume; }; class AssetFactory Modified: Mercury2/src/MercuryVBO.cpp =================================================================== --- Mercury2/src/MercuryVBO.cpp 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/MercuryVBO.cpp 2009-03-16 00:09:00 UTC (rev 182) @@ -10,7 +10,7 @@ #define BUFFER_OFFSET(i) ((char*)NULL + (i)) MercuryVBO::MercuryVBO() - :MercuryAsset(), m_initiated(false), m_boundingBox(NULL) + :MercuryAsset(), m_initiated(false) { m_bufferIDs[0] = m_bufferIDs[1] = 0; } @@ -19,7 +19,6 @@ { if (m_bufferIDs[0]) glDeleteBuffersARB(2, m_bufferIDs); m_bufferIDs[0] = m_bufferIDs[1] = 0; - SAFE_DELETE(m_boundingBox); } void MercuryVBO::Render(MercuryNode* node) @@ -54,11 +53,7 @@ m_lastVBOrendered = this; - if (m_boundingBox) - { - RenderableBoundingBox rbb(m_boundingBox); - rbb.Render(node); - } + if (m_boundingVolume) m_boundingVolume->Render(); } void MercuryVBO::InitVBO() Modified: Mercury2/src/MercuryVBO.h =================================================================== --- Mercury2/src/MercuryVBO.h 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/MercuryVBO.h 2009-03-16 00:09:00 UTC (rev 182) @@ -31,7 +31,6 @@ protected: AlignedBuffer<float> m_vertexData; AlignedBuffer<uint16_t> m_indexData; - BoundingBox* m_boundingBox; static MSemaphore m_vboBatches; }; Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/RenderableNode.cpp 2009-03-16 00:09:00 UTC (rev 182) @@ -24,7 +24,8 @@ m_render.clear(); m_postrender.clear(); } - +/* +//this function exists for threaded rendering void RenderableNode::Update(float dTime) { static unsigned long waitTime = 0; @@ -41,7 +42,7 @@ UpdateWaited += waited; } - +*/ void RenderableNode::Render() { list< MercuryAsset* >::iterator i; @@ -116,17 +117,15 @@ RenderableNode* rn; if ( ( rn = Cast(n) ) ) { +/* MSemaphoreDecOnDestroy s( &(rn->m_semaphore) ); - int unsigned long waited = rn->SpinlockWait(1, waitTime); - if (waited > 0) - waitTime = (waitTime<1000000)?waitTime+1000:waitTime; - else - waitTime = (waitTime!=0)?waitTime-1000:0; - + if (waited > 0) waitTime = (waitTime<1000000)?waitTime+1000:waitTime; + else waitTime = (waitTime!=0)?waitTime-1000:0; RenderWaited += waited; - +*/ // ++RenderWaited += rn->Spinlock(1); + rn->Render(); } @@ -159,7 +158,7 @@ MercuryNode::LoadFromXML( node ); } - +/* unsigned long RenderableNode::Spinlock( unsigned long value ) { unsigned long waited = 0; @@ -178,9 +177,9 @@ } return waited; } +*/ - /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/RenderableNode.h =================================================================== --- Mercury2/src/RenderableNode.h 2009-03-15 22:17:56 UTC (rev 181) +++ Mercury2/src/RenderableNode.h 2009-03-16 00:09:00 UTC (rev 182) @@ -21,7 +21,7 @@ ~RenderableNode(); virtual void Render(); - virtual void Update(float dTime); +// virtual void Update(float dTime); ///Returnes true if N is of type RenderableNode // static bool IsMyType( MercuryNode* n ); @@ -44,8 +44,6 @@ bool m_hidden; private: bool IsInAssetList(MercuryAsset* asset) const; - unsigned long Spinlock( unsigned long value ); - unsigned long SpinlockWait( unsigned long value, unsigned long usec ); std::list< MAutoPtr< MercuryAsset > > m_assets; ///serves as a holder for memory @@ -55,8 +53,11 @@ std::list< MercuryAsset* > m_prerender; std::list< MercuryAsset* > m_render; std::list< MercuryAsset* > m_postrender; - - MSemaphore m_semaphore; + +//Below is stuff for threaded rendering +// unsigned long Spinlock( unsigned long value ); +// unsigned long SpinlockWait( unsigned long value, unsigned long usec ); +// MSemaphore m_semaphore; }; #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-16 00:42:28
|
Revision: 183 http://hgengine.svn.sourceforge.net/hgengine/?rev=183&view=rev Author: axlecrusher Date: 2009-03-16 00:42:19 +0000 (Mon, 16 Mar 2009) Log Message: ----------- make nodes constant in render functions Modified Paths: -------------- Mercury2/src/HGMDLModel.cpp Mercury2/src/HGMDLModel.h Mercury2/src/MercuryAsset.h Mercury2/src/MercuryVBO.cpp Mercury2/src/MercuryVBO.h Mercury2/src/Texture.cpp Mercury2/src/Texture.h Modified: Mercury2/src/HGMDLModel.cpp =================================================================== --- Mercury2/src/HGMDLModel.cpp 2009-03-16 00:09:00 UTC (rev 182) +++ Mercury2/src/HGMDLModel.cpp 2009-03-16 00:42:19 UTC (rev 183) @@ -58,7 +58,7 @@ } } -void HGMDLModel::Render(MercuryNode* node) +void HGMDLModel::Render(const MercuryNode* node) { for(uint16_t i = 0; i < m_meshes.size(); ++i) m_meshes[i]->Render(node); Modified: Mercury2/src/HGMDLModel.h =================================================================== --- Mercury2/src/HGMDLModel.h 2009-03-16 00:09:00 UTC (rev 182) +++ Mercury2/src/HGMDLModel.h 2009-03-16 00:42:19 UTC (rev 183) @@ -18,7 +18,7 @@ void LoadModel(MercuryFile* hgmdl); static HGMDLModel* Generate(); - virtual void Render(MercuryNode* node); + virtual void Render(const MercuryNode* node); private: void LoadHGMDL( const MString& path ); Modified: Mercury2/src/MercuryAsset.h =================================================================== --- Mercury2/src/MercuryAsset.h 2009-03-16 00:09:00 UTC (rev 182) +++ Mercury2/src/MercuryAsset.h 2009-03-16 00:42:19 UTC (rev 183) @@ -16,9 +16,9 @@ virtual void Init(MercuryNode* node); - virtual void PreRender(MercuryNode* node) {}; - virtual void Render(MercuryNode* node) = 0; - virtual void PostRender(MercuryNode* node) {}; + virtual void PreRender(const MercuryNode* node) {}; + virtual void Render(const MercuryNode* node) = 0; + virtual void PostRender(const MercuryNode* node) {}; ///Loads an asset from an XMLAsset representing itself virtual void LoadFromXML(const XMLNode& node) {}; Modified: Mercury2/src/MercuryVBO.cpp =================================================================== --- Mercury2/src/MercuryVBO.cpp 2009-03-16 00:09:00 UTC (rev 182) +++ Mercury2/src/MercuryVBO.cpp 2009-03-16 00:42:19 UTC (rev 183) @@ -21,7 +21,7 @@ m_bufferIDs[0] = m_bufferIDs[1] = 0; } -void MercuryVBO::Render(MercuryNode* node) +void MercuryVBO::Render(const MercuryNode* node) { uint8_t numTextures = Texture::NumberActiveTextures(); uint16_t stride = sizeof(float)*8; Modified: Mercury2/src/MercuryVBO.h =================================================================== --- Mercury2/src/MercuryVBO.h 2009-03-16 00:09:00 UTC (rev 182) +++ Mercury2/src/MercuryVBO.h 2009-03-16 00:42:19 UTC (rev 183) @@ -13,7 +13,7 @@ MercuryVBO(); virtual ~MercuryVBO(); - virtual void Render(MercuryNode* node); + virtual void Render(const MercuryNode* node); void AllocateVertexSpace(unsigned int count); void AllocateIndexSpace(unsigned int count); Modified: Mercury2/src/Texture.cpp =================================================================== --- Mercury2/src/Texture.cpp 2009-03-16 00:09:00 UTC (rev 182) +++ Mercury2/src/Texture.cpp 2009-03-16 00:42:19 UTC (rev 183) @@ -82,12 +82,12 @@ }; -void Texture::Render(MercuryNode* node) +void Texture::Render(const MercuryNode* node) { BindTexture(); } -void Texture::PostRender(MercuryNode* node) +void Texture::PostRender(const MercuryNode* node) { UnbindTexture(); } Modified: Mercury2/src/Texture.h =================================================================== --- Mercury2/src/Texture.h 2009-03-16 00:09:00 UTC (rev 182) +++ Mercury2/src/Texture.h 2009-03-16 00:42:19 UTC (rev 183) @@ -12,8 +12,8 @@ virtual void Init(MercuryNode* node); - virtual void Render(MercuryNode* node); - virtual void PostRender(MercuryNode* node); + virtual void Render(const MercuryNode* node); + virtual void PostRender(const MercuryNode* node); virtual void LoadFromXML(const XMLNode& node); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-03-19 22:46:03
|
Revision: 184 http://hgengine.svn.sourceforge.net/hgengine/?rev=184&view=rev Author: axlecrusher Date: 2009-03-19 22:45:56 +0000 (Thu, 19 Mar 2009) Log Message: ----------- updates Modified Paths: -------------- Mercury2/src/BoundingBox.cpp Mercury2/src/BoundingBox.h Mercury2/src/Mercury2.cpp Mercury2/src/MercuryAsset.h Mercury2/src/RenderableNode.cpp Mercury2/src/RenderableNode.h Mercury2/src/XMLParser.cpp Mercury2/src/XMLParser.h Modified: Mercury2/src/BoundingBox.cpp =================================================================== --- Mercury2/src/BoundingBox.cpp 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/BoundingBox.cpp 2009-03-19 22:45:56 UTC (rev 184) @@ -10,6 +10,18 @@ ComputeNormals(); }; +BoundingBox::BoundingBox(const BoundingBox& bb) + :m_center(bb.m_center), m_extend(bb.m_extend) +{ + for (uint8_t i = 0; i < 3; ++i) + m_normals[i] = bb.m_normals[i]; +} + +BoundingVolume* BoundingBox::SpawnClone() const +{ + return new BoundingBox(*this); +} + void BoundingBox::LoadFromBinary(char* data) { memcpy(m_center, data, sizeof(float)*3); Modified: Mercury2/src/BoundingBox.h =================================================================== --- Mercury2/src/BoundingBox.h 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/BoundingBox.h 2009-03-19 22:45:56 UTC (rev 184) @@ -14,6 +14,7 @@ virtual void LoadFromBinary(char* data) = 0; virtual void Transform( const MercuryMatrix& m ) = 0; virtual void Render() {}; + virtual BoundingVolume* SpawnClone() const = 0; }; class BoundingBox : public BoundingVolume @@ -21,6 +22,8 @@ public: BoundingBox() {}; BoundingBox(const MercuryVertex& center, const MercuryVertex& extend); + BoundingBox(const BoundingBox& bb); + virtual ~BoundingBox() {}; virtual void LoadFromBinary(char* data); @@ -32,6 +35,7 @@ const MercuryVector& Normal(uint8_t i) const { return m_normals[i]; } virtual void Render(); + virtual BoundingVolume* SpawnClone() const; private: void ComputeNormals(); Modified: Mercury2/src/Mercury2.cpp =================================================================== --- Mercury2/src/Mercury2.cpp 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/Mercury2.cpp 2009-03-19 22:45:56 UTC (rev 184) @@ -84,9 +84,9 @@ SAFE_DELETE(root); SAFE_DELETE(w); - uint64_t totalWaited = UpdateWaited + RenderWaited; - printf("Update wait %%%f\n", (UpdateWaited/double(totalWaited))*100.0f); - printf("Render wait %%%f\n", (RenderWaited/double(totalWaited))*100.0f); +// uint64_t totalWaited = UpdateWaited + RenderWaited; +// printf("Update wait %%%f\n", (UpdateWaited/double(totalWaited))*100.0f); +// printf("Render wait %%%f\n", (RenderWaited/double(totalWaited))*100.0f); return 0; } Modified: Mercury2/src/MercuryAsset.h =================================================================== --- Mercury2/src/MercuryAsset.h 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/MercuryAsset.h 2009-03-19 22:45:56 UTC (rev 184) @@ -24,6 +24,8 @@ virtual void LoadFromXML(const XMLNode& node) {}; inline void IsInstanced(bool b) { m_isInstanced = b; } + + inline const BoundingVolume* GetBoundingVolume() const { return m_boundingVolume; } protected: bool m_isInstanced; BoundingVolume* m_boundingVolume; Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/RenderableNode.cpp 2009-03-19 22:45:56 UTC (rev 184) @@ -7,12 +7,12 @@ using namespace std; REGISTER_NODE_TYPE(RenderableNode); - +/* uint64_t RenderWaited = 0; uint64_t UpdateWaited = 0; MercuryMatrix GLOBALMATRIX; - +*/ RenderableNode::RenderableNode() :m_hidden(false) { @@ -43,13 +43,32 @@ UpdateWaited += waited; } */ + +void RenderableNode::Update(float dTime) +{ + MercuryMatrix m = FindGlobalMatrix(); + + std::list< MAutoPtr< MercuryAsset > >::iterator i; + for (i = m_assets.begin(); i != m_assets.end(); ++i ) + { + const BoundingVolume* v = (*i)->GetBoundingVolume(); + if (v) + { + BoundingVolume* nv = v->SpawnClone(); + nv->Transform(m); + SAFE_DELETE(nv); + //do some stuff to figure out if this node is culled + } + } +} + void RenderableNode::Render() { list< MercuryAsset* >::iterator i; + MercuryMatrix m = FindGlobalMatrix(); if (m_hidden || IsCulled()) return; - MercuryMatrix m = GLOBALMATRIX = FindGlobalMatrix(); m.Transpose(); glLoadMatrixf( m.Ptr() ); @@ -113,7 +132,7 @@ void RenderableNode::RecursiveRender( MercuryNode* n ) { - static unsigned long waitTime = 0; +// static unsigned long waitTime = 0; RenderableNode* rn; if ( ( rn = Cast(n) ) ) { Modified: Mercury2/src/RenderableNode.h =================================================================== --- Mercury2/src/RenderableNode.h 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/RenderableNode.h 2009-03-19 22:45:56 UTC (rev 184) @@ -10,9 +10,9 @@ #define MCHECKASSETS -extern uint64_t RenderWaited; -extern uint64_t UpdateWaited; -extern MercuryMatrix GLOBALMATRIX; +//extern uint64_t RenderWaited; +//extern uint64_t UpdateWaited; +//extern MercuryMatrix GLOBALMATRIX; class RenderableNode : public MercuryNode { @@ -21,7 +21,7 @@ ~RenderableNode(); virtual void Render(); -// virtual void Update(float dTime); + virtual void Update(float dTime); ///Returnes true if N is of type RenderableNode // static bool IsMyType( MercuryNode* n ); Modified: Mercury2/src/XMLParser.cpp =================================================================== --- Mercury2/src/XMLParser.cpp 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/XMLParser.cpp 2009-03-19 22:45:56 UTC (rev 184) @@ -74,8 +74,49 @@ data = MString((const char*)d); xmlFree(d); } + else + { + d = xmlGetProp(m_node, (const xmlChar*)"fallback"); + if (d) + { + //start searching at the root + XMLNode root( xmlDocGetRootElement(m_doc) ); + //prevent infinite recursion on self + if ( root.m_node != m_node ) + { + XMLNode fall = root.FindFallbackNode( MString((const char*)d) ); + data = fall.Attribute(tag); + } + } + } + + if (d) xmlFree(d); + return data; } +/* +MString XMLNode::FindFallbackAttribute(const MString& path) +{ + + xmlChar* d = xmlGetProp(m_node, (const xmlChar*)tag.c_str()); + if (d) + { + + } + MString p = path; +} +*/ +XMLNode XMLNode::FindFallbackNode(const MString& path) const +{ + if (path.length() > 0) + { + MString name = path.substr(0, path.find(".")); + for (XMLNode n = this->Child(); n.IsValid(); n = n.NextNode()) + if (n.Attribute("name") == name) return n.FindFallbackNode(path); + return XMLNode(); + } + return *this; +} XMLDocument::XMLDocument() :m_doc(NULL) Modified: Mercury2/src/XMLParser.h =================================================================== --- Mercury2/src/XMLParser.h 2009-03-16 00:42:19 UTC (rev 183) +++ Mercury2/src/XMLParser.h 2009-03-19 22:45:56 UTC (rev 184) @@ -28,6 +28,9 @@ MString Name() const; MString Content() const; MString Attribute(const MString & tag) const; + +// MString FindFallbackAttribute(); + XMLNode FindFallbackNode(const MString& path) const; inline bool IsValid() const { return m_node!=NULL; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cn...@us...> - 2009-03-20 03:38:01
|
Revision: 187 http://hgengine.svn.sourceforge.net/hgengine/?rev=187&view=rev Author: cnlohr Date: 2009-03-20 03:37:43 +0000 (Fri, 20 Mar 2009) Log Message: ----------- add shaders (not yet working) Added Paths: ----------- Mercury2/src/Shader.cpp Mercury2/src/Shader.h Added: Mercury2/src/Shader.cpp =================================================================== --- Mercury2/src/Shader.cpp (rev 0) +++ Mercury2/src/Shader.cpp 2009-03-20 03:37:43 UTC (rev 187) @@ -0,0 +1,431 @@ +#include <Shader.h> +#include <RenderableNode.h> +#include <MercuryFile.h> + +#define GL_GLEXT_PROTOTYPES + +#include <GL/gl.h> +#include <GL/glext.h> + +using namespace std; + +REGISTER_ASSET_TYPE( Shader ); + +ShaderAttributesSet SHADERATTRIBUTES; +Shader * Shader::CurrentShader; + +ShaderAttribute * ShaderAttributesSet::GetHandle( const MString & sName ) +{ + ShaderAttribute * ret = m_AllShaderAttributes[sName]; + if( !ret ) + { + ret = new ShaderAttribute(); + m_AllShaderAttributes[sName] = ret; + } + return ret; +} + +Shader::Shader() +{ + iProgramID = (GLhandleARB)NULL; + vertexShader = (GLhandleARB)NULL; + fragmentShader = (GLhandleARB)NULL; + geometryShader = (GLhandleARB)NULL; + iTimeCode[0] = 0; + iTimeCode[1] = 0; + iTimeCode[2] = 0; + +} + +Shader::~Shader() +{ + DestroyShader( ); +} + +void Shader::Init(MercuryNode* node) +{ + MercuryAsset::Init( node ); + RenderableNode* rn; + if ( (rn=RenderableNode::Cast( node )) ) + rn->AddPostRender( this ); +} + +void Shader::Render(const MercuryNode* node) +{ + bool bApply = true; + + //If there's a currnet shader, we may want to abort switching shaders + if( CurrentShader ) + { + if( CurrentShader->fPriority > fPriority ) + bApply = false; + } + if( bApply ) + { + OriginalShader = CurrentShader; + CurrentShader = this; + ActivateShader(); + } +} + +void Shader::PostRender(const MercuryNode* node) +{ + CurrentShader = OriginalShader; + if( OriginalShader ) + OriginalShader->ActivateShader(); + else + DeactivateShader(); +} + +void Shader::LoadFromXML(const XMLNode& node) +{ + sShaderName = node.Attribute("file"); + fPriority = atof( node.Attribute("priority").c_str() ); + LoadShader( ); +} + +bool Shader::LoadShader( ) +{ + GetTimeCodes( iTimeCode ); + MString s1 = sShaderName, s2 = sShaderName, s3 = sShaderName; + MercuryFile * f1; + MercuryFile * f2; + MercuryFile * f3; //geometry + char * Buffer; + int i; + + s1 += ".frag"; + s2 += ".vert"; + s3 += ".geom"; + f1 = FILEMAN.Open( s1 ); + f2 = FILEMAN.Open( s2 ); + f3 = FILEMAN.Open( s3 ); + + if( f1 == 0 || f2 == 0 ) + { + if( !f1 ) + printf( "Could not open %s.\n", (char*)s1.c_str() ); + if( !f2 ) + printf( "Could not open %s.\n", (char*)s2.c_str() ); + return false; + } + if( f1 ) + { + i = f1->Length(); + Buffer = (char*)malloc( i+1 ); + f1->Read( Buffer, i ); + f1->Close(); + printf( "Compiling: %s\n", s1.c_str() ); + Buffer[i] = '\0'; + if( !LoadShaderFrag( Buffer ) ) + { + free( Buffer ); + if( f2 ) + f2->Close(); + if( f3 ) + f3->Close(); + printf( "Reporting failed shaderload. Not linking.\n" ); + return false; + } + free( Buffer ); + } + if( f2 ) + { + i = f2->Length(); + Buffer = (char*)malloc( i+1 ); + f2->Read( Buffer, i ); + f2->Close(); + Buffer[i] = '\0'; + printf( "Compiling: %s\n", s2.c_str() ); + if( !LoadShaderVert( Buffer ) ) + { + if( f3 ) + f3->Close(); + free( Buffer ); + printf( "Reporting failed shaderload. Not linking.\n" ); + return false; + } + free( Buffer ); + } + if( f3 ) + { + i = f3->Length(); + Buffer = (char*)malloc( i+1 ); + f3->Read( Buffer, i ); + f3->Close(); + Buffer[i] = '\0'; + printf( "Compiling: %s\n", s3.c_str() ); + if( !LoadShaderGeom( Buffer ) ) + { + free( Buffer ); + printf( "Reporting failed shaderload. Not linking.\n" ); + return false; + } + free( Buffer ); + } + return LinkShaders(); +} + +bool Shader::LoadShaderFrag( const char * sShaderCode ) +{ + if( strlen( sShaderCode ) < 5 ) + return false; + + GLint bFragCompiled; + GLint stringLength; + fragmentShader = glCreateShaderObjectARB( GL_FRAGMENT_SHADER_ARB ); + glShaderSourceARB( fragmentShader, 1, &sShaderCode, NULL ); + glCompileShaderARB( fragmentShader ); + + glGetObjectParameterivARB( fragmentShader, GL_OBJECT_COMPILE_STATUS_ARB, &bFragCompiled ); + glGetObjectParameterivARB( fragmentShader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &stringLength ); + if ( stringLength > 1 ) + { + char * tmpstr = (char*)malloc( stringLength + 1 ); + glGetInfoLogARB( fragmentShader, stringLength, NULL, tmpstr ); + puts( "Compiling Fragment Shader response follows:" ); + puts( tmpstr ); + free( tmpstr ); + return bFragCompiled!=0; + } + return true; +} + +bool Shader::LoadShaderVert( const char * sShaderCode ) +{ + if( strlen( sShaderCode ) < 5 ) + return false; + + GLint bVertCompiled; + GLint stringLength; + //Create a new vertex shader + vertexShader = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB ); + //Bind the shader to the text, setting that to be its source. + glShaderSourceARB( vertexShader, 1, &sShaderCode, NULL ); + //Compile the shader + glCompileShaderARB( vertexShader ); + //Did the shader compile? Were there any errors? + glGetObjectParameterivARB( vertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &bVertCompiled ); + glGetObjectParameterivARB( vertexShader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &stringLength ); + if( stringLength > 1 ) + { + char * tmpstr = (char*)malloc( stringLength + 1 ); + glGetInfoLogARB( vertexShader, stringLength, NULL, tmpstr ); + puts( "Compiling Vertex Shader response follows:" ); + puts( tmpstr ); + free( tmpstr ); + return bVertCompiled!=0; + } + + return true; +} + +bool Shader::LoadShaderGeom( const char * sShaderCode ) +{ + if( strlen( sShaderCode ) < 5 ) + return false; + + GLint bGeomCompiled; + GLint stringLength; + //Create a new geometry shader + geometryShader = glCreateShaderObjectARB( GL_GEOMETRY_SHADER_EXT ); + //Bind the shader to the text, setting that to be its source. + glShaderSourceARB( geometryShader, 1, &sShaderCode, NULL ); + //Compile the shader + glCompileShaderARB( geometryShader ); + //Did the shader compile? Were there any errors? + glGetObjectParameterivARB( geometryShader, GL_OBJECT_COMPILE_STATUS_ARB, &bGeomCompiled ); + glGetObjectParameterivARB( geometryShader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &stringLength ); + if( bGeomCompiled == 0 ) + { + char * tmpstr = (char*)malloc( stringLength + 1 ); + glGetInfoLogARB( geometryShader, stringLength, NULL, tmpstr ); + puts( "Compiling Geometry Shader response follows:" ); + puts( tmpstr ); + free( tmpstr ); + return bGeomCompiled!=0; + } + return true; +} + +bool Shader::LinkShaders() +{ + GLint bLinked; + GLint stringLength; + //Create the actual shader prgoram + iProgramID = glCreateProgramObjectARB(); + //Attach the fragment/vertex shader to it. + if( vertexShader ) + glAttachObjectARB( iProgramID, vertexShader ); + if( fragmentShader ) + glAttachObjectARB( iProgramID, fragmentShader ); + if( geometryShader ) + glAttachObjectARB( iProgramID, geometryShader ); + //Attempt to link the shader + glLinkProgramARB( iProgramID ); + + //If we're using a geometry shader, we have to do a little extra. + if( geometryShader ) + { + glProgramParameteriEXT( iProgramID, GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES ); + glProgramParameteriEXT( iProgramID, GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP ); + + int ierror, i; + GLint imaxvert; + glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT,&imaxvert); + if( (ierror = glGetError()) != 0 ) + { + puts( "ERROR: You cannot load a geometry shader when there are still errors left in OpenGL." ); + puts( "Please track down the error remaining by using glGetError() to cordon off your code." ); + printf( "The last error received was: %d\n", ierror ); + } + for( i = 1; i < imaxvert; i++ ) + { + glProgramParameteriEXT(iProgramID,GL_GEOMETRY_VERTICES_OUT_EXT,imaxvert/i); + if( glGetError() == 0 ) + break; + } + printf( "Geometry Shader loaded with a total of %d max verticies. Because there are %d max vertices, and %d preceived components per vert.\n", imaxvert/i, imaxvert, i ); + } + + + //See if there were any errors. + glGetObjectParameterivARB( iProgramID, GL_OBJECT_LINK_STATUS_ARB, &bLinked ); + glGetObjectParameterivARB( iProgramID, GL_OBJECT_INFO_LOG_LENGTH_ARB, &stringLength ); + + if ( stringLength > 1 || bLinked == 0 ) + { + char * tmpstr = (char*)malloc( stringLength + 1 ); + glGetInfoLogARB( iProgramID, stringLength, NULL, tmpstr ); + puts( "Linking shaders. response follows:" ); + puts( tmpstr ); + free( tmpstr ); + return bLinked!=0; + } + + //Build the list of uniform tabs. + int iNumUniforms; + glGetObjectParameterivARB( iProgramID, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &iNumUniforms ); + m_vShaderTabs.resize( iNumUniforms ); + for( int i = 0; i < iNumUniforms; ++i ) + { + char buffer[1024]; + int bufflen; + GLint size; + GLenum type; + glGetActiveUniformARB( iProgramID, i, 1024, &bufflen, &size, &type, buffer ); + buffer[bufflen] = 0; + m_vShaderTabs[i] = SHADERATTRIBUTES.GetHandle( buffer ); + } + return true; +} + +void Shader::DestroyShader() +{ + GLhandleARB *objects = NULL; + GLint i, count = -1; + + if( !iProgramID ) + return; + + //If we can't destroy the object, then don't try. + glGetObjectParameterivARB(iProgramID, GL_OBJECT_ATTACHED_OBJECTS_ARB, &count); + + //Iterate through all children. + if (count > 0) + { + objects = (GLhandleARB *)malloc(count*sizeof(GLhandleARB)); + glGetAttachedObjectsARB(iProgramID, count, NULL, objects); + } + else + return; + + for ( i = 0; i < count; ++i) + { + glDetachObjectARB(iProgramID, objects[i]); + } + + glDeleteObjectARB(iProgramID); + free( objects ); + return; +} + +void Shader::CheckForNewer( ) +{ + unsigned long iCurTimes[3]; + GetTimeCodes( iCurTimes ); + if( iCurTimes[0] != iTimeCode[0] || iCurTimes[1] != iTimeCode[1] || iCurTimes[2] != iTimeCode[2] ) + { + DestroyShader( ); + LoadShader( ); + } +} + +void Shader::GetTimeCodes( unsigned long * iOut ) +{ + MercuryFile * f = FILEMAN.Open( sShaderName + ".frag" ); + iOut[0] = f->GetModTime(); + f->Close(); + + f = FILEMAN.Open( sShaderName + ".vert" ); + iOut[1] = f->GetModTime(); + f->Close(); + + f = FILEMAN.Open( sShaderName + ".geom" ); + iOut[2] = f->GetModTime(); + f->Close(); +} + +void Shader::ActivateShader() +{ + glUseProgramObjectARB( iProgramID ); + + for( unsigned i = 0; i < m_vShaderTabs.size(); ++i ) + { + ShaderAttribute * sa = m_vShaderTabs[i]; + switch( sa->typ ) + { + case ShaderAttribute::TYPE_INT: + case ShaderAttribute::TYPE_SAMPLER: + glUniform1iARB( i, sa->sau.iInt ); + break; + case ShaderAttribute::TYPE_FLOAT: + case ShaderAttribute::TYPE_FLOATV4: + glUniform4fvARB( i, 4, &sa->sau.fFloatV4[0] ); + }; + } +} + +void Shader::DeactivateShader() +{ + glUseProgramObjectARB( 0 ); +} + + +/* + * Copyright (c) 2009 Charles Lohr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the distribution. + * - Neither the name of the Mercury Engine nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ Added: Mercury2/src/Shader.h =================================================================== --- Mercury2/src/Shader.h (rev 0) +++ Mercury2/src/Shader.h 2009-03-20 03:37:43 UTC (rev 187) @@ -0,0 +1,185 @@ +#ifndef SHADER_H +#define SHADER_H + +#include <MercuryAsset.h> +#include <map> +#include <vector> + +///Basic Attribute for all shaders +class ShaderAttribute +{ +public: + ShaderAttribute() : typ( TYPE_INT ) { sau.iInt = 0; } + + ///Type of ShaderAttribute for shader + enum ShaderAttributeTyp + { + TYPE_INT, ///Synonomous to 'int' when passing into a shader + TYPE_SAMPLER, ///Synonomous to 'sampler2D' when passing into a shader + TYPE_FLOAT, ///Synonomous to 'float' when passing into a shader + TYPE_FLOATV4 ///Synonomous to 'vec4' when passing into a shader + } typ; + + ///Actual data for value. + union ShaderAttributeVal + { + int iInt; ///Synonomous to 'int' + unsigned int iSampler; ///Synonomous to 'sampler2D' + float fFloat; ///Synonomous to 'float' + float fFloatV4[4]; ///Synonomous to 'vec4' + } sau; +}; + +///Shader Attribute Retainer +class ShaderAttributesSet +{ +public: + ShaderAttribute * GetHandle( const MString & sName ); +private: + ///All shader attributes + /** This contains a list of all attributes that are passed in. + If a shader does not have an attribute, it does not insert + it here. Note that if shaders change and an element is + no longer referenced, it will remain in the map, as to + prevent bad pointers. This list is all-enduring. + */ + std::map< MString, ShaderAttribute * > m_AllShaderAttributes; +}; + +extern ShaderAttributesSet SHADERATTRIBUTES; + +///Basic element for turning shaders on and off +/** This class helps aide in the loading and use of shaders. It allows loading of files + through the LoadShader() function that actively looks for .frag and .vert files. By use + of the CheckForNewer() function, the class looks for changes, if any changes are found + the shaders will be re-loaded, compiled and linked. Once this node has a shader loaded, + the shader can be activated or deactivated using the ActivateShader() and + DeactivateShader() functions respectively. Note that shaders do not stack. When you + call DeactiveShader(), it does not bring back previously activated shaders. It simply + turns all shaders off. */ +class Shader : public MercuryAsset +{ +public: + Shader(); + virtual ~Shader(); + + virtual void Init(MercuryNode* node); + virtual void Render(const MercuryNode* node); + virtual void PostRender(const MercuryNode* node); + static Shader* Generate() { return new Shader; } + virtual void LoadFromXML(const XMLNode& node); + + ///Explicitly get the OpenGL ProgramID in the event you need it for advanced techniques + unsigned int GetProgramID() { return iProgramID; } +private: + ///Suggested function for loading shaders. + /** This function looks for {sShaderName}.vert and {sShaderName}.frag. It will + attempt to load, compile and link the files. If any errors are found, they will + be announced at STDOUT. When Geometry shader support is added, it will search + for .geom files */ + bool LoadShader( ); + + ///Explicitly load a fragment shader + /** This function takes on raw code in the sShaderCode string and attemps to compile + it. Any errors it runs into will be displayed at STDOUT. Note you are + discouraged to use this function, in favor of using LoadShader(). */ + bool LoadShaderFrag( const char * sShaderCode ); + + ///Explicitly load a vertex shader + /** This function takes on raw code in the sShaderCode string and attemps to compile + it. Any errors it runs into will be displayed at STDOUT. You should use + LoadShader() instead of this function. */ + bool LoadShaderVert( const char * sShaderCode ); + + ///Explicitly load a geometry shader + /** This function takes on raw code in the sShaderCode string and attemps to compile + it. Any errors it runs into will be displayed at STDOUT. You should use + LoadShader() instead of this function. */ + bool LoadShaderGeom( const char * sShaderCode ); + + ///Explicitly link all shaders currently loaded + /** This takes vertexShader and fragmentShader and converts them into iProgramID. + this function is discouraged when using LoadShader(). */ + bool LinkShaders(); + + ///Check for newer version of 'this' shader + void CheckForNewer( ); + + ///Get the last modified time for sShaderName + /* This function takes on iOut as being where to put the last time the shader was modified. + this value is system dependent and is necessiarly not linked to anything like seconds. */ + void GetTimeCodes( unsigned long * iOut ); + + ///Activate Shader (apply to current OpenGL state) + void ActivateShader(); + + ///Turn all shaders off in OpenGL state. + void DeactivateShader(); + + ///Destroy this shader + void DestroyShader(); + + ///The last time codes (for vertex and fragment shaders respectively) + unsigned long iTimeCode[3]; + + ///The OpenGL Program ID (in OpenGL Land, contains all shaders, linked together) + unsigned int iProgramID; + + ///The OpenGL Geometry Program ID + unsigned int geometryShader; + + ///The OpenGL Vertex Program ID + unsigned int vertexShader; + + ///The OpenGL Fragment Program ID + unsigned int fragmentShader; + + ///Shader attributes + /** This is the system that helps make it possible to blast + through all attributes currently set up by dereferencing + the pointers in the attributes repository. + */ + std::vector< ShaderAttribute * > m_vShaderTabs; + + ///Name of the shader + MString sShaderName; + + ///Priority of THIS shader (if lower than currently active one, it does not become active) + float fPriority; + + ///Global static shader (so shaders can recursively activate and deactivate shaders) + static Shader * CurrentShader; + + ///Original Shader (to re-enable when leaving) + Shader * OriginalShader; +}; + +#endif + +/* + * Copyright (c) 2009 Charles Lohr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the distribution. + * - Neither the name of the Mercury Engine nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-04-01 01:23:03
|
Revision: 191 http://hgengine.svn.sourceforge.net/hgengine/?rev=191&view=rev Author: axlecrusher Date: 2009-04-01 01:22:53 +0000 (Wed, 01 Apr 2009) Log Message: ----------- keep pointers to siblings and use them for finding children Modified Paths: -------------- Mercury2/src/MercuryNode.cpp Mercury2/src/MercuryNode.h Modified: Mercury2/src/MercuryNode.cpp =================================================================== --- Mercury2/src/MercuryNode.cpp 2009-03-28 13:22:39 UTC (rev 190) +++ Mercury2/src/MercuryNode.cpp 2009-04-01 01:22:53 UTC (rev 191) @@ -7,7 +7,7 @@ REGISTER_NODE_TYPE(MercuryNode); MercuryNode::MercuryNode() - :m_parent(NULL) + :m_parent(NULL), m_prevSibling(NULL), m_nextSibling(NULL) { } @@ -24,11 +24,23 @@ void MercuryNode::AddChild(MercuryNode* n) { +// list< MercuryNode* >::iterator last = m_children.end(); + + n->m_prevSibling = NULL; + n->m_nextSibling = NULL; + if (m_children.end() != m_children.begin()) + { + MercuryNode* last = m_children.back(); + last->m_nextSibling = n; + n->m_prevSibling = last; + } + m_children.push_back(n); n->m_parent = this; OnAddChild(); n->OnAdded(); + m_rebuildRenderGraph = true; } void MercuryNode::RemoveChild(MercuryNode* n) @@ -38,48 +50,46 @@ { if (*i == n) { + MercuryNode* next = n->m_nextSibling; + MercuryNode* prev = n->m_prevSibling; + n->OnRemoved(); OnRemoveChild(); + + if (prev) prev->m_nextSibling = next; + if (next) next->m_prevSibling = prev; + + n->m_nextSibling = NULL; + n->m_prevSibling = NULL; n->m_parent = NULL; + m_children.erase(i); + m_rebuildRenderGraph = true; + return; } } } -MercuryNode* MercuryNode::Parent() const +MercuryNode* MercuryNode::FirstChild() const { - return m_parent; -} - -MercuryNode* MercuryNode::NextSibling() const -{ - if (m_parent) return m_parent->NextChild(this); + if (m_children.begin() != m_children.end()) + return *(m_children.begin()); return NULL; } -MercuryNode* MercuryNode::PrevSibling() const +MercuryNode* MercuryNode::NextChild(const MercuryNode* child) const { - if (m_parent) return m_parent->PrevChild(this); - return NULL; + if (child==NULL) return NULL; + return child->NextSibling(); } -MercuryNode* MercuryNode::NextChild(const MercuryNode* n) const +MercuryNode* MercuryNode::PrevChild(const MercuryNode* child) const { - list< MercuryNode* >::const_iterator i; - for (i = m_children.begin(); i != m_children.end(); ++i ) - if (*i == n) return *i; - return NULL; + if (child==NULL) return NULL; + return child->PrevSibling(); } -MercuryNode* MercuryNode::PrevChild(const MercuryNode* n) const -{ - list< MercuryNode* >::const_iterator i; - for (i = m_children.end(); i != m_children.begin(); --i ) - if (*i == n) return *i; - return NULL; -} - void MercuryNode::RecursiveUpdate(float dTime) { Update(dTime); @@ -140,6 +150,7 @@ return NULL; } +bool MercuryNode::m_rebuildRenderGraph = false; /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/MercuryNode.h =================================================================== --- Mercury2/src/MercuryNode.h 2009-03-28 13:22:39 UTC (rev 190) +++ Mercury2/src/MercuryNode.h 2009-04-01 01:22:53 UTC (rev 191) @@ -13,9 +13,9 @@ **/ #define GENRTTI(x) static const x* Cast(const MercuryNode* n) \ -{ return dynamic_cast<const x*>(n); } \ +{ if (n==NULL) return NULL; return dynamic_cast<const x*>(n); } \ static x* Cast(MercuryNode* n) \ -{ return dynamic_cast<x*>(n); } +{ if (n==NULL) return NULL; return dynamic_cast<x*>(n); } /* #define GENRTTI(x) static bool IsMyType(const MercuryNode* n) \ @@ -32,9 +32,10 @@ void AddChild(MercuryNode* n); void RemoveChild(MercuryNode* n); - MercuryNode* Parent() const; - MercuryNode* NextSibling() const; - MercuryNode* PrevSibling() const; + inline MercuryNode* Parent() const { return m_parent; } + inline MercuryNode* NextSibling() const { return m_nextSibling; } + inline MercuryNode* PrevSibling() const { return m_prevSibling; } + MercuryNode* FirstChild() const; MercuryNode* NextChild(const MercuryNode* n) const; ///Finds the next child in regards to n MercuryNode* PrevChild(const MercuryNode* n) const; ///Finds the previous child in regards to n const std::list< MercuryNode* >& Children() const { return m_children; } @@ -63,6 +64,11 @@ protected: std::list< MercuryNode* > m_children; //These nodes are unique, not instanced MercuryNode* m_parent; + MercuryNode* m_prevSibling; + MercuryNode* m_nextSibling; + + static bool m_rebuildRenderGraph; + }; class NodeFactory This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-04-04 23:23:26
|
Revision: 199 http://hgengine.svn.sourceforge.net/hgengine/?rev=199&view=rev Author: axlecrusher Date: 2009-04-04 23:23:21 +0000 (Sat, 04 Apr 2009) Log Message: ----------- new render graph setup Modified Paths: -------------- Mercury2/src/RenderGraph.cpp Mercury2/src/RenderableNode.cpp Mercury2/src/RenderableNode.h Mercury2/src/Viewport.cpp Mercury2/src/Viewport.h Modified: Mercury2/src/RenderGraph.cpp =================================================================== --- Mercury2/src/RenderGraph.cpp 2009-04-04 19:54:12 UTC (rev 198) +++ Mercury2/src/RenderGraph.cpp 2009-04-04 23:23:21 UTC (rev 199) @@ -1,15 +1,41 @@ +#include <MercuryMatrix.h> #include <RenderGraph.h> +#include <GL/gl.h> + void RenderGraphEntry::Render() { - if (m_node) m_node->Render(); + MercuryMatrix m; + + if (m_node) + { + if ( !(m_node->IsHidden() || m_node->IsCulled()) ) + { + m = m_node->FindGlobalMatrix(); + m.Transpose(); + glLoadMatrixf( m.Ptr() ); + m_node->PreRender( m ); + m_node->Render( m ); + } + } + std::list< RenderGraphEntry >::iterator i; for (i = m_children.begin(); i != m_children.end(); ++i ) i->Render(); + + if (m_node) + { + if ( !(m_node->IsHidden() || m_node->IsCulled()) ) + { + glLoadMatrixf( m.Ptr() ); + m_node->PostRender( m ); + } + } } void RenderGraph::Build( MercuryNode* node ) { + printf("rebuilding render graph\n"); m_root = RenderGraphEntry(); Build(node, m_root); } Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2009-04-04 19:54:12 UTC (rev 198) +++ Mercury2/src/RenderableNode.cpp 2009-04-04 23:23:21 UTC (rev 199) @@ -38,22 +38,23 @@ } } -void RenderableNode::Render() +void RenderableNode::PreRender(const MercuryMatrix& matrix) { list< MercuryAsset* >::iterator i; - MercuryMatrix m = FindGlobalMatrix(); - - if (m_hidden || IsCulled()) return; - - m.Transpose(); - glLoadMatrixf( m.Ptr() ); - for (i = m_prerender.begin(); i != m_prerender.end(); ++i ) (*i)->PreRender(this); +} +void RenderableNode::Render(const MercuryMatrix& matrix) +{ + list< MercuryAsset* >::iterator i; for (i = m_render.begin(); i != m_render.end(); ++i ) (*i)->Render(this); - +} + +void RenderableNode::PostRender(const MercuryMatrix& matrix) +{ + list< MercuryAsset* >::iterator i; for (i = m_postrender.begin(); i != m_postrender.end(); ++i ) (*i)->PostRender(this); } @@ -106,21 +107,6 @@ return false; } -void RenderableNode::RecursiveRender( MercuryNode* n ) -{ - RenderableNode* rn; - if ( ( rn = Cast(n) ) ) - { - rn->Render(); - } - - const list< MercuryNode* >& children = n->Children(); - list< MercuryNode* >::const_iterator i; - - for (i = children.begin(); i != children.end(); ++i ) - RenderableNode::RecursiveRender( *i ); -} - void RenderableNode::LoadFromXML(const XMLNode& node) { if ( !node.Attribute("hidden").empty() ) Modified: Mercury2/src/RenderableNode.h =================================================================== --- Mercury2/src/RenderableNode.h 2009-04-04 19:54:12 UTC (rev 198) +++ Mercury2/src/RenderableNode.h 2009-04-04 23:23:21 UTC (rev 199) @@ -16,21 +16,24 @@ RenderableNode(); ~RenderableNode(); - virtual void Render(); virtual void Update(float dTime); - + inline void AddAsset(MAutoPtr< MercuryAsset > asset) { m_assets.push_back(asset); } void AddPreRender(MercuryAsset* asset); void AddRender(MercuryAsset* asset); void AddPostRender(MercuryAsset* asset); - static void RecursiveRender( MercuryNode* n ); + virtual void PreRender(const MercuryMatrix& matrix); + virtual void Render(const MercuryMatrix& matrix); + virtual void PostRender(const MercuryMatrix& matrix); + virtual void LoadFromXML(const XMLNode& node); const MercuryMatrix& FindGlobalMatrix() const; virtual bool IsCulled() { return false; } + bool IsHidden() { return m_hidden; } GENRTTI(RenderableNode); protected: Modified: Mercury2/src/Viewport.cpp =================================================================== --- Mercury2/src/Viewport.cpp 2009-04-04 19:54:12 UTC (rev 198) +++ Mercury2/src/Viewport.cpp 2009-04-04 23:23:21 UTC (rev 199) @@ -3,22 +3,15 @@ REGISTER_NODE_TYPE(Viewport); -const Frustum* FRUSTUM = NULL; - -void Viewport::Render() +void Viewport::Render(const MercuryMatrix& matrix) { - FRUSTUM = &m_frustum; - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); - - MercuryMatrix m = FindGlobalMatrix(); - m.Transpose(); MercuryMatrix f = m_frustum.GetMatrix(); f.Transpose(); - glLoadMatrixf( (m * f).Ptr() ); + glLoadMatrixf( (matrix * f).Ptr() ); //The following 2 are equivelent to above // glLoadMatrixf( m_frustum.Ptr() ); // glMultMatrixf( m.Ptr() ); Modified: Mercury2/src/Viewport.h =================================================================== --- Mercury2/src/Viewport.h 2009-04-04 19:54:12 UTC (rev 198) +++ Mercury2/src/Viewport.h 2009-04-04 23:23:21 UTC (rev 199) @@ -40,7 +40,7 @@ class Viewport : public RenderableNode { public: - virtual void Render(); + virtual void Render(const MercuryMatrix& matrix); virtual void LoadFromXML(const XMLNode& node); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-04-05 04:10:36
|
Revision: 201 http://hgengine.svn.sourceforge.net/hgengine/?rev=201&view=rev Author: axlecrusher Date: 2009-04-05 04:10:31 +0000 (Sun, 05 Apr 2009) Log Message: ----------- update renderable's matrix logic Modified Paths: -------------- Mercury2/src/MercuryMatrix.cpp Mercury2/src/MercuryMatrix.h Mercury2/src/RenderGraph.cpp Mercury2/src/RenderGraph.h Mercury2/src/RenderableNode.cpp Mercury2/src/TransformNode.cpp Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-04-05 00:30:19 UTC (rev 200) +++ Mercury2/src/MercuryMatrix.cpp 2009-04-05 04:10:31 UTC (rev 201) @@ -3,7 +3,7 @@ namespace MercuryMath { - MercuryMatrix IdentityMatrix; + static MercuryMatrix IdentityMatrix; } MercuryMatrix::MercuryMatrix() @@ -202,6 +202,7 @@ return r; } +MercuryMatrix MercuryMatrix::IdentityMatrix; /* * Copyright (c) 2006 Joshua Allen Modified: Mercury2/src/MercuryMatrix.h =================================================================== --- Mercury2/src/MercuryMatrix.h 2009-04-05 00:30:19 UTC (rev 200) +++ Mercury2/src/MercuryMatrix.h 2009-04-05 04:10:31 UTC (rev 201) @@ -44,6 +44,7 @@ void Identity(); void Print() const; + static MercuryMatrix IdentityMatrix; } #if !defined( WIN32 ) || defined( _MSC_VER ) M_ALIGN(64); @@ -51,10 +52,9 @@ M_ALIGN(16); #endif -namespace MercuryMath -{ - extern MercuryMatrix IdentityMatrix; -} +//namespace MercuryMath +//{ +//} #endif Modified: Mercury2/src/RenderGraph.cpp =================================================================== --- Mercury2/src/RenderGraph.cpp 2009-04-05 00:30:19 UTC (rev 200) +++ Mercury2/src/RenderGraph.cpp 2009-04-05 04:10:31 UTC (rev 201) @@ -11,7 +11,7 @@ { if ( !(m_node->IsHidden() || m_node->IsCulled()) ) { - m = m_node->FindGlobalMatrix(); + m = *m_matrix; m.Transpose(); glLoadMatrixf( m.Ptr() ); m_node->PreRender( m ); @@ -49,7 +49,7 @@ { //found a new renderable printf("Found renderable %p\n", rn); - entry.m_children.push_back( RenderGraphEntry(rn) ); + entry.m_children.push_back( RenderGraphEntry(&rn->FindGlobalMatrix(), rn) ); lastEntry = &(entry.m_children.back()); } Modified: Mercury2/src/RenderGraph.h =================================================================== --- Mercury2/src/RenderGraph.h 2009-04-05 00:30:19 UTC (rev 200) +++ Mercury2/src/RenderGraph.h 2009-04-05 04:10:31 UTC (rev 201) @@ -8,8 +8,8 @@ { friend class RenderGraph; public: - RenderGraphEntry(RenderableNode* node = NULL) - :m_node(node) + RenderGraphEntry(const MercuryMatrix* matrix = &MercuryMatrix::IdentityMatrix, RenderableNode* node = NULL) + :m_node(node), m_matrix(matrix) {} void AddChild(RenderGraphEntry entry); @@ -17,6 +17,7 @@ private: RenderableNode* m_node; //we don't own this, no new or free std::list< RenderGraphEntry > m_children; + const MercuryMatrix* m_matrix; }; class RenderGraph Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2009-04-05 00:30:19 UTC (rev 200) +++ Mercury2/src/RenderableNode.cpp 2009-04-05 04:10:31 UTC (rev 201) @@ -67,7 +67,7 @@ if ( (tn = TransformNode::Cast(n)) ) return tn->GetGlobalMatrix(); - return MercuryMath::IdentityMatrix; + return MercuryMatrix::IdentityMatrix; } void RenderableNode::AddPreRender(MercuryAsset* asset) Modified: Mercury2/src/TransformNode.cpp =================================================================== --- Mercury2/src/TransformNode.cpp 2009-04-05 00:30:19 UTC (rev 200) +++ Mercury2/src/TransformNode.cpp 2009-04-05 04:10:31 UTC (rev 201) @@ -71,7 +71,7 @@ n = n->Parent(); } - return MercuryMath::IdentityMatrix; + return MercuryMatrix::IdentityMatrix; } void TransformNode::RippleTaintDown() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-04-12 14:18:35
|
Revision: 204 http://hgengine.svn.sourceforge.net/hgengine/?rev=204&view=rev Author: axlecrusher Date: 2009-04-12 14:18:33 +0000 (Sun, 12 Apr 2009) Log Message: ----------- Move around the bounding box test logic. This avoids having to use Cast to figure out which cull test to run Modified Paths: -------------- Mercury2/src/BoundingBox.cpp Mercury2/src/BoundingBox.h Mercury2/src/Mercury2.cpp Mercury2/src/MercuryPlane.cpp Mercury2/src/MercuryPlane.h Mercury2/src/RenderGraph.cpp Mercury2/src/RenderableNode.cpp Mercury2/src/RenderableNode.h Mercury2/src/Viewport.cpp Mercury2/src/Viewport.h Modified: Mercury2/src/BoundingBox.cpp =================================================================== --- Mercury2/src/BoundingBox.cpp 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/BoundingBox.cpp 2009-04-12 14:18:33 UTC (rev 204) @@ -4,6 +4,11 @@ #include <string.h> #include <Viewport.h> +#define SIGNED_DIST(x) m_normal.DotProduct(x) + +// origional algorithim was -x<0 +#define BEHIND_PLANE(x) x>=0 + BoundingBox::BoundingBox(const MercuryVertex& center, const MercuryVertex& extend) :m_center(center), m_extend(extend) { @@ -55,6 +60,32 @@ *this = bb; } +bool BoundingBox::Clip( const MercuryPlane& p ) +{ + MercuryVertex dp = p.GetNormal().DotProduct3(m_normals[0], m_normals[1], m_normals[2]); + float x = ABS( m_extend.GetX() * dp[0] ); + x += ABS( m_extend.GetY() * dp[1] ); + x += ABS( m_extend.GetZ() * dp[2] ); + + float d = p.GetNormal().DotProduct( m_center+p.GetCenter() ); //signed distance + if ( ABS(d) <= x ) //intersection + return false; + + return BEHIND_PLANE(d); //if we don't intersect, just see what side we are on +} + +bool BoundingBox::Clip( const Frustum& f ) +{ + bool inView = true; + for (uint8_t i = 0; (i < 6) && inView; ++i) + { + inView = Clip( f.GetPlane(i) )?false:inView; + } + + return !inView; +} + + void BoundingBox::Render() { // BoundingBox gbb = *this; Modified: Mercury2/src/BoundingBox.h =================================================================== --- Mercury2/src/BoundingBox.h 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/BoundingBox.h 2009-04-12 14:18:33 UTC (rev 204) @@ -4,7 +4,9 @@ #include <MercuryNode.h> #include <MercuryVertex.h> #include <MercuryMatrix.h> +#include <Frustum.h> #include <stdint.h> +#include <MercuryPlane.h> class BoundingVolume { @@ -15,6 +17,9 @@ virtual void Transform( const MercuryMatrix& m ) = 0; virtual void Render() {}; virtual BoundingVolume* SpawnClone() const = 0; + + virtual bool Clip( const MercuryPlane& p ) = 0; + virtual bool Clip( const Frustum& f ) = 0; }; class BoundingBox : public BoundingVolume @@ -37,6 +42,9 @@ virtual void Render(); virtual BoundingVolume* SpawnClone() const; + virtual bool Clip( const MercuryPlane& p ); + virtual bool Clip( const Frustum& f ); + private: void ComputeNormals(); @@ -45,17 +53,7 @@ MercuryVector m_normals[3]; }; -/* -class RenderableBoundingBox : public MercuryAsset -{ - public: - RenderableBoundingBox(const BoundingBox* bb); - virtual void Render(MercuryNode* node); - private: - const BoundingBox* m_bb; -}; -*/ #endif /**************************************************************************** Modified: Mercury2/src/Mercury2.cpp =================================================================== --- Mercury2/src/Mercury2.cpp 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/Mercury2.cpp 2009-04-12 14:18:33 UTC (rev 204) @@ -70,6 +70,7 @@ renderGraph.Build(root); } + renderGraph.Render(); // RenderableNode::RecursiveRender(root); w->SwapBuffers(); Modified: Mercury2/src/MercuryPlane.cpp =================================================================== --- Mercury2/src/MercuryPlane.cpp 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/MercuryPlane.cpp 2009-04-12 14:18:33 UTC (rev 204) @@ -12,27 +12,6 @@ return BEHIND_PLANE( SIGNED_DIST( point+m_center ) ); } -bool MercuryPlane::IsBehindPlane(const BoundingBox& bb) const -{ - const MercuryVertex& center = bb.GetCenter(); - const MercuryVertex& extends = bb.GetExtend(); - - const MercuryVertex &A1(bb.Normal(0)), &A2(bb.Normal(1)), &A3(bb.Normal(2)); - - MercuryVertex dp = m_normal.DotProduct3(A1, A2, A3); - float x = ABS( extends.GetX() * dp[0] ); - x += ABS( extends.GetY() * dp[1] ); - x += ABS( extends.GetZ() * dp[2] ); - - float d = SIGNED_DIST( center+m_center ); - if ( ABS(d) <= x ) //intersection - return false; - - return BEHIND_PLANE(d); //if we don't intersect, just see what side we are on -} - - - /**************************************************************************** * Copyright (C) 2009 by Joshua Allen * * * Modified: Mercury2/src/MercuryPlane.h =================================================================== --- Mercury2/src/MercuryPlane.h 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/MercuryPlane.h 2009-04-12 14:18:33 UTC (rev 204) @@ -2,7 +2,7 @@ #define MERCURYPLANE_H #include <MercuryVertex.h> -#include <BoundingBox.h> +//#include <BoundingBox.h> class MercuryPlane { @@ -16,8 +16,10 @@ inline void SetCenter(const MercuryVertex& center) { m_center = center; } inline void SetNormal(const MercuryVertex& normal) { m_normal = normal.Normalize(); } + inline const MercuryVector& GetNormal() const { return m_normal; } + inline const MercuryVector& GetCenter() const { return m_center; } + bool IsBehindPlane(const MercuryVertex& point) const; - bool IsBehindPlane(const BoundingBox& bb) const; private: MercuryVertex m_center; MercuryVector m_normal; Modified: Mercury2/src/RenderGraph.cpp =================================================================== --- Mercury2/src/RenderGraph.cpp 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/RenderGraph.cpp 2009-04-12 14:18:33 UTC (rev 204) @@ -6,17 +6,15 @@ void RenderGraphEntry::Render() { MercuryMatrix m; - + if (m_node) { - if ( !(m_node->IsHidden() || m_node->IsCulled()) ) - { - m = *m_matrix; - m.Transpose(); - glLoadMatrixf( m.Ptr() ); - m_node->PreRender( m ); - m_node->Render( m ); - } + if ( m_node->IsHidden() || m_node->IsCulled(*m_matrix) ) return; + m = *m_matrix; + m.Transpose(); + glLoadMatrixf( m.Ptr() ); + m_node->PreRender( m ); + m_node->Render( m ); } std::list< RenderGraphEntry >::iterator i; @@ -25,17 +23,14 @@ if (m_node) { - if ( !(m_node->IsHidden() || m_node->IsCulled()) ) - { - glLoadMatrixf( m.Ptr() ); - m_node->PostRender( m ); - } + glLoadMatrixf( m.Ptr() ); + m_node->PostRender( m ); } } void RenderGraph::Build( MercuryNode* node ) { - printf("rebuilding render graph\n"); +// printf("rebuilding render graph\n"); m_root = RenderGraphEntry(); Build(node, m_root); } @@ -48,7 +43,6 @@ if ( rn ) { //found a new renderable - printf("Found renderable %p\n", rn); entry.m_children.push_back( RenderGraphEntry(&rn->FindGlobalMatrix(), rn) ); lastEntry = &(entry.m_children.back()); } Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/RenderableNode.cpp 2009-04-12 14:18:33 UTC (rev 204) @@ -3,6 +3,7 @@ #include <GL/gl.h> #include <TransformNode.h> #include <unistd.h> +#include <Viewport.h> using namespace std; @@ -22,6 +23,7 @@ void RenderableNode::Update(float dTime) { + /* MercuryMatrix m = FindGlobalMatrix(); std::list< MAutoPtr< MercuryAsset > >::iterator i; @@ -36,6 +38,7 @@ //do some stuff to figure out if this node is culled } } + */ } void RenderableNode::PreRender(const MercuryMatrix& matrix) @@ -130,6 +133,28 @@ MercuryNode::LoadFromXML( node ); } +bool RenderableNode::IsCulled(const MercuryMatrix& matrix) +{ + bool clip = false; + + std::list< MAutoPtr< MercuryAsset > >::iterator i; + for (i = m_assets.begin(); i != m_assets.end(); ++i ) + { + const BoundingVolume* bv = (*i)->GetBoundingVolume(); + if (bv) + { + BoundingVolume* v = bv->SpawnClone(); + v->Transform( matrix ); + clip = v->Clip( *FRUSTUM ); + delete v; + if ( clip == false ) return false; + } + else + return false; + } + return clip; +} + /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/RenderableNode.h =================================================================== --- Mercury2/src/RenderableNode.h 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/RenderableNode.h 2009-04-12 14:18:33 UTC (rev 204) @@ -32,7 +32,7 @@ const MercuryMatrix& FindGlobalMatrix() const; - virtual bool IsCulled() { return false; } + virtual bool IsCulled(const MercuryMatrix& matrix); bool IsHidden() { return m_hidden; } GENRTTI(RenderableNode); Modified: Mercury2/src/Viewport.cpp =================================================================== --- Mercury2/src/Viewport.cpp 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/Viewport.cpp 2009-04-12 14:18:33 UTC (rev 204) @@ -2,9 +2,12 @@ #include <GL/gl.h> REGISTER_NODE_TYPE(Viewport); +const Frustum* FRUSTUM; void Viewport::Render(const MercuryMatrix& matrix) { + FRUSTUM = &m_frustum; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); @@ -31,119 +34,7 @@ RenderableNode::LoadFromXML(node); } -void Frustum::SetPerspective( float fov, float aspect, float znear, float zfar ) -{ - float xmin, xmax, ymin, ymax; - - m_fov = fov; - m_aspect = aspect; - m_zNear = znear; - m_zFar = zfar; - float tang = TAN(m_fov * Q_PI / 360.0); - - m_nh = ymax = m_zNear * tang; //nh - ymin = -ymax; - xmin = ymin * m_aspect; - m_nw = xmax = ymax * m_aspect; //nw - - m_fh = m_zFar*tang; - m_fw = m_fh*aspect; - - ComputeFrustum(xmin, xmax, ymin, ymax, m_zNear, m_zFar); -} - -void Frustum::ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar) -{ - float near2 = 2*zNear; - float rml = right-left; - float tmb = top - bottom; - float fmn = zFar - zNear; - - float A = (right+left)/rml; - float B = (top+bottom)/tmb; - float C = -(zFar+zNear)/fmn; - float D = -(near2*zFar)/fmn; - - m_frustum.Zero(); - - //row major - m_frustum[0][0] = near2/rml; - m_frustum[0][2] = A; - m_frustum[1][1] = near2/tmb; - m_frustum[1][2] = B; - m_frustum[2][2] = C; - m_frustum[2][3] = D; - m_frustum[3][2] = -1; - -// m_frustum.Transpose(); //XXX fix it to remove this -} - -void Frustum::LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up) -{ - //Right now this only builds the frustum planes - MercuryVector X,Y,Z; - - Z = (eye - look).Normalize(); //direction behind camera - X = (up.CrossProduct(Z)).Normalize(); //X axis - Y = Z.CrossProduct( X ); //real up - - m_nc = (eye - Z) * m_zNear; - m_fc = (eye - Z) * m_zFar; - - m_planes[PNEAR].Setup(m_nc, Z*(-1)); - m_planes[PFAR].Setup(m_fc, Z); -// m_fc.Print(); -// Z.Print(); - MercuryVector aux,normal; - - aux = (m_nc + Y*m_nh) - eye; - aux.NormalizeSelf(); - normal = aux * X; - m_planes[PTOP].Setup(m_nc+Y*m_nh,normal); - - aux = (m_nc - Y*m_nh) - eye; - aux.NormalizeSelf(); - normal = X * aux; - m_planes[PBOTTOM].Setup(m_nc-Y*m_nh,normal); - - aux = (m_nc - X*m_nw) - eye; - aux.NormalizeSelf(); - normal = aux * Y; - m_planes[PLEFT].Setup(m_nc-X*m_nw,normal); - - aux = (m_nc + X*m_nw) - eye; - aux.NormalizeSelf(); - normal = Y * aux; - m_planes[PRIGHT].Setup(m_nc+X*m_nw,normal); -} - -bool Frustum::Clip(const BoundingBox& bb) const -{ - bool inView = true; - for (uint8_t i = 0; (i < 6) && inView; ++i) - { - inView = m_planes[i].IsBehindPlane( bb )?false:inView; - } - - return !inView; -}; - -/* -void Frustum::LookAt() -{ - -} -*/ -/* -bool Frustum::IsPointInFrustum( float x, float y, float z ) -{ - for( uint16_t i = 0; i < 6; ++i ) - if( frustum[i][0] * x + frustum[i][1] * y + frustum[i][2] * z + frustum[i][3] <= 0 ) - return false; - return true; -}*/ - /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/Viewport.h =================================================================== --- Mercury2/src/Viewport.h 2009-04-05 23:21:37 UTC (rev 203) +++ Mercury2/src/Viewport.h 2009-04-12 14:18:33 UTC (rev 204) @@ -5,36 +5,8 @@ #include <MercuryMatrix.h> #include <MercuryVertex.h> #include <MercuryPlane.h> +#include <Frustum.h> -enum PlanePos -{ - PTOP = 0, - PBOTTOM, - PLEFT, - PRIGHT, - PNEAR, - PFAR -}; - -class Frustum -{ - - public: - void SetPerspective( float fov, float aspect, float znear, float zfar ); - const MercuryMatrix& GetMatrix() const { return m_frustum; } - void ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar); - void LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up); - bool Clip(const BoundingBox& bb) const; - private: - MercuryPlane m_planes[6]; - MercuryMatrix m_frustum; - - float m_aspect, m_fov, m_zNear, m_zFar; - float m_nh, m_nw, m_fh, m_fw; - - MercuryVector m_nc, m_fc; -}; - extern const Frustum* FRUSTUM; class Viewport : public RenderableNode This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-04-12 16:29:02
|
Revision: 205 http://hgengine.svn.sourceforge.net/hgengine/?rev=205&view=rev Author: axlecrusher Date: 2009-04-12 16:28:55 +0000 (Sun, 12 Apr 2009) Log Message: ----------- updates Modified Paths: -------------- Mercury2/src/Mercury2.cpp Mercury2/src/MercuryMatrix.cpp Mercury2/src/MercuryVBO.cpp Mercury2/src/MercuryVBO.h Mercury2/src/Texture.cpp Mercury2/src/Texture.h Mercury2/src/TransformNode.cpp Modified: Mercury2/src/Mercury2.cpp =================================================================== --- Mercury2/src/Mercury2.cpp 2009-04-12 14:18:33 UTC (rev 204) +++ Mercury2/src/Mercury2.cpp 2009-04-12 16:28:55 UTC (rev 205) @@ -14,7 +14,10 @@ #include <MercuryTimer.h> #include <RenderGraph.h> +#include <Texture.h> +bool SHOWBOUNDINGVOLUME = false; + MSemaphore UpdateLoopGo; void* UpdateThread(void* node) { @@ -80,7 +83,8 @@ if (fpsTimer.Age() > 1) { float batches = MercuryVBO::ResetBatchCount()/(float)m_count; - printf("FPS: %f, VBO batches %f\n", m_count/fpsTimer.Age(), batches); + float binds = Texture::ReadAndResetBindCount()/(float)m_count; + printf("FPS: %f, VBO batches %f, TBinds %f\n", m_count/fpsTimer.Age(), batches, binds); m_count = 0; fpsTimer = timer; } Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-04-12 14:18:33 UTC (rev 204) +++ Mercury2/src/MercuryMatrix.cpp 2009-04-12 16:28:55 UTC (rev 205) @@ -1,11 +1,6 @@ #include "MercuryMatrix.h" #include <stdio.h> -namespace MercuryMath -{ - static MercuryMatrix IdentityMatrix; -} - MercuryMatrix::MercuryMatrix() { Identity(); Modified: Mercury2/src/MercuryVBO.cpp =================================================================== --- Mercury2/src/MercuryVBO.cpp 2009-04-12 14:18:33 UTC (rev 204) +++ Mercury2/src/MercuryVBO.cpp 2009-04-12 16:28:55 UTC (rev 205) @@ -9,6 +9,8 @@ #define BUFFER_OFFSET(i) ((char*)NULL + (i)) +extern bool SHOWBOUNDINGVOLUME; + MercuryVBO::MercuryVBO() :MercuryAsset(), m_initiated(false) { @@ -49,11 +51,11 @@ } glDrawRangeElements(GL_TRIANGLES, 0, m_indexData.Length()-1, m_indexData.Length(), GL_UNSIGNED_SHORT, NULL); - m_vboBatches.Increment(); + m_vboBatches++; m_lastVBOrendered = this; - if (m_boundingVolume) m_boundingVolume->Render(); + if (m_boundingVolume && SHOWBOUNDINGVOLUME) m_boundingVolume->Render(); } void MercuryVBO::InitVBO() @@ -84,7 +86,7 @@ } void* MercuryVBO::m_lastVBOrendered = NULL; -MSemaphore MercuryVBO::m_vboBatches; +uint32_t MercuryVBO::m_vboBatches; /**************************************************************************** * Copyright (C) 2008 by Joshua Allen * Modified: Mercury2/src/MercuryVBO.h =================================================================== --- Mercury2/src/MercuryVBO.h 2009-04-12 14:18:33 UTC (rev 204) +++ Mercury2/src/MercuryVBO.h 2009-04-12 16:28:55 UTC (rev 205) @@ -18,8 +18,8 @@ void AllocateVertexSpace(unsigned int count); void AllocateIndexSpace(unsigned int count); - static uint32_t BatchCount() { return m_vboBatches.Read(); } - static uint32_t ResetBatchCount() { return m_vboBatches.ReadAndClear(); } + static uint32_t BatchCount() { return m_vboBatches; } + static uint32_t ResetBatchCount() { uint32_t t = m_vboBatches; m_vboBatches = 0; return t; } private: virtual void InitVBO(); @@ -32,7 +32,7 @@ AlignedBuffer<float> m_vertexData; AlignedBuffer<uint16_t> m_indexData; - static MSemaphore m_vboBatches; + static uint32_t m_vboBatches; }; #endif Modified: Mercury2/src/Texture.cpp =================================================================== --- Mercury2/src/Texture.cpp 2009-04-12 14:18:33 UTC (rev 204) +++ Mercury2/src/Texture.cpp 2009-04-12 16:28:55 UTC (rev 205) @@ -99,14 +99,15 @@ void Texture::BindTexture() { - m_textureResource = GL_TEXTURE0+m_activeTextures; - glActiveTexture( m_textureResource ); - glClientActiveTextureARB(m_textureResource); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnable( GL_TEXTURE_2D ); - glBindTexture(GL_TEXTURE_2D, m_textureID); - glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); - ++m_activeTextures; + m_textureResource = GL_TEXTURE0+m_activeTextures; + glActiveTexture( m_textureResource ); + glClientActiveTextureARB(m_textureResource); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnable( GL_TEXTURE_2D ); + glBindTexture(GL_TEXTURE_2D, m_textureID); + glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); + ++m_activeTextures; + ++m_textureBinds; } void Texture::UnbindTexture() @@ -145,8 +146,8 @@ bool Texture::m_initTextureSuccess = false; unsigned short Texture::m_activeTextures = 0; +uint32_t Texture::m_textureBinds = 0; - /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/Texture.h =================================================================== --- Mercury2/src/Texture.h 2009-04-12 14:18:33 UTC (rev 204) +++ Mercury2/src/Texture.h 2009-04-12 16:28:55 UTC (rev 205) @@ -20,6 +20,7 @@ void LoadFromRaw(const RawImageData* raw); inline static unsigned short NumberActiveTextures() { return m_activeTextures; } + inline static uint32_t ReadAndResetBindCount() { uint32_t t = m_textureBinds; m_textureBinds = 0; return t; } static Texture* Generate(); static MAutoPtr< Texture > LoadFromFile(const MString& path); @@ -35,6 +36,7 @@ static bool m_initTextureSuccess; static unsigned short m_activeTextures; + static uint32_t m_textureBinds; MString m_filename; }; Modified: Mercury2/src/TransformNode.cpp =================================================================== --- Mercury2/src/TransformNode.cpp 2009-04-12 14:18:33 UTC (rev 204) +++ Mercury2/src/TransformNode.cpp 2009-04-12 16:28:55 UTC (rev 205) @@ -52,7 +52,7 @@ m_tainted = false; MercuryMatrix local; - local.Identity(); +// local.Identity(); local.Transotale( m_position.x, m_position.y, m_position.z, m_rotation.x, m_rotation.y, m_rotation.z, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-04-12 16:32:57
|
Revision: 208 http://hgengine.svn.sourceforge.net/hgengine/?rev=208&view=rev Author: axlecrusher Date: 2009-04-12 16:32:55 +0000 (Sun, 12 Apr 2009) Log Message: ----------- Move frustum into own file Added Paths: ----------- Mercury2/src/Frustum.cpp Mercury2/src/Frustum.h Added: Mercury2/src/Frustum.cpp =================================================================== --- Mercury2/src/Frustum.cpp (rev 0) +++ Mercury2/src/Frustum.cpp 2009-04-12 16:32:55 UTC (rev 208) @@ -0,0 +1,135 @@ +#include <Frustum.h> + +void Frustum::SetPerspective( float fov, float aspect, float znear, float zfar ) +{ + float xmin, xmax, ymin, ymax; + + m_fov = fov; + m_aspect = aspect; + m_zNear = znear; + m_zFar = zfar; + + float tang = TAN(m_fov * Q_PI / 360.0); + + m_nh = ymax = m_zNear * tang; //nh + ymin = -ymax; + xmin = ymin * m_aspect; + m_nw = xmax = ymax * m_aspect; //nw + + m_fh = m_zFar*tang; + m_fw = m_fh*aspect; + + ComputeFrustum(xmin, xmax, ymin, ymax, m_zNear, m_zFar); +} + +void Frustum::ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar) +{ + float near2 = 2*zNear; + float rml = right-left; + float tmb = top - bottom; + float fmn = zFar - zNear; + + float A = (right+left)/rml; + float B = (top+bottom)/tmb; + float C = -(zFar+zNear)/fmn; + float D = -(near2*zFar)/fmn; + + m_frustum.Zero(); + + //row major + m_frustum[0][0] = near2/rml; + m_frustum[0][2] = A; + m_frustum[1][1] = near2/tmb; + m_frustum[1][2] = B; + m_frustum[2][2] = C; + m_frustum[2][3] = D; + m_frustum[3][2] = -1; + +// m_frustum.Transpose(); //XXX fix it to remove this +} + +void Frustum::LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up) +{ + //Right now this only builds the frustum planes + MercuryVector X,Y,Z; + + Z = (eye - look).Normalize(); //direction behind camera + X = (up.CrossProduct(Z)).Normalize(); //X axis + Y = Z.CrossProduct( X ); //real up + + m_nc = (eye - Z) * m_zNear; + m_fc = (eye - Z) * m_zFar; + + m_planes[PNEAR].Setup(m_nc, Z*(-1)); + m_planes[PFAR].Setup(m_fc, Z); +// m_fc.Print(); +// Z.Print(); + MercuryVector aux,normal; + + aux = (m_nc + Y*m_nh) - eye; + aux.NormalizeSelf(); + normal = aux * X; + m_planes[PTOP].Setup(m_nc+Y*m_nh,normal); + + aux = (m_nc - Y*m_nh) - eye; + aux.NormalizeSelf(); + normal = X * aux; + m_planes[PBOTTOM].Setup(m_nc-Y*m_nh,normal); + + aux = (m_nc - X*m_nw) - eye; + aux.NormalizeSelf(); + normal = aux * Y; + m_planes[PLEFT].Setup(m_nc-X*m_nw,normal); + + aux = (m_nc + X*m_nw) - eye; + aux.NormalizeSelf(); + normal = Y * aux; + m_planes[PRIGHT].Setup(m_nc+X*m_nw,normal); +} + +/* +void Frustum::LookAt() +{ + +} +*/ +/* +bool Frustum::IsPointInFrustum( float x, float y, float z ) +{ + for( uint16_t i = 0; i < 6; ++i ) + if( frustum[i][0] * x + frustum[i][1] * y + frustum[i][2] * z + frustum[i][3] <= 0 ) + return false; + return true; +}*/ + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ Added: Mercury2/src/Frustum.h =================================================================== --- Mercury2/src/Frustum.h (rev 0) +++ Mercury2/src/Frustum.h 2009-04-12 16:32:55 UTC (rev 208) @@ -0,0 +1,70 @@ +#ifndef FRUSTUM_H +#define FRUSTUM_H + +#include <MercuryMatrix.h> +#include <MercuryPlane.h> + +enum PlanePos +{ + PTOP = 0, + PBOTTOM, + PLEFT, + PRIGHT, + PNEAR, + PFAR +}; + +class Frustum +{ + + public: + void SetPerspective( float fov, float aspect, float znear, float zfar ); + const MercuryMatrix& GetMatrix() const { return m_frustum; } + void ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar); + void LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up); + + inline const MercuryPlane& GetPlane(int i) const { return m_planes[i]; } + private: + + MercuryPlane m_planes[6]; + MercuryMatrix m_frustum; + + float m_aspect, m_fov, m_zNear, m_zFar; + float m_nh, m_nw, m_fh, m_fw; + + MercuryVector m_nc, m_fc; +}; + +#endif + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <axl...@us...> - 2009-04-12 18:09:50
|
Revision: 209 http://hgengine.svn.sourceforge.net/hgengine/?rev=209&view=rev Author: axlecrusher Date: 2009-04-12 18:09:46 +0000 (Sun, 12 Apr 2009) Log Message: ----------- more updates to improve speed of basic math functions Modified Paths: -------------- Mercury2/src/MercuryMath.cpp Mercury2/src/MercuryMath.h Mercury2/src/MercuryMatrix.cpp Mercury2/src/MercuryMatrix.h Mercury2/src/RenderGraph.h Mercury2/src/RenderableNode.cpp Mercury2/src/TransformNode.cpp Modified: Mercury2/src/MercuryMath.cpp =================================================================== --- Mercury2/src/MercuryMath.cpp 2009-04-12 16:32:55 UTC (rev 208) +++ Mercury2/src/MercuryMath.cpp 2009-04-12 18:09:46 UTC (rev 209) @@ -65,20 +65,17 @@ void Copy4f( void * dest, const void * source ) { - for (uint8_t i = 0; i < 4; ++i) - ((float*)dest)[i] = ((float*)source)[i]; + COPY<float,4>((float*)source, (float*)dest); } void Copy8f( void * dest, const void * source ) { - for (uint8_t i = 0; i < 8; ++i) - ((float*)dest)[i] = ((float*)source)[i]; + COPY<float,8>((float*)source, (float*)dest); } void Copy16f( void * dest, const void * source ) { - for (uint8_t i = 0; i < 16; ++i) - ((float*)dest)[i] = ((float*)source)[i]; + COPY<float,16>((float*)source, (float*)dest); } void MatrixMultiply4f ( const FloatRow* in1a, const FloatRow* in2a, FloatRow* outa) Modified: Mercury2/src/MercuryMath.h =================================================================== --- Mercury2/src/MercuryMath.h 2009-04-12 16:32:55 UTC (rev 208) +++ Mercury2/src/MercuryMath.h 2009-04-12 18:09:46 UTC (rev 209) @@ -16,6 +16,18 @@ #define RADDEG 57.2957795130823208767f //radian to degree #define Q_PI 3.14159265358979323846f +template<typename t, unsigned i> +inline void COPY(const t* s, t*d) +{ + d[i-1] = s[i-1]; + COPY<t,i-1>(s,d); +} + +template<> inline void COPY<float,0>(const float* s, float* d) +{ + d[0] = s[0]; +} + #if defined(WIN32) //In win32, sin works faster than sinf and similar functions #define SIN( x ) float( sin ( x ) ) Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-04-12 16:32:55 UTC (rev 208) +++ Mercury2/src/MercuryMatrix.cpp 2009-04-12 18:09:46 UTC (rev 209) @@ -3,7 +3,7 @@ MercuryMatrix::MercuryMatrix() { - Identity(); + *this = Identity(); } const MercuryMatrix& MercuryMatrix::operator=(const MercuryMatrix& m) @@ -26,14 +26,19 @@ ZeroFloatRow( m_matrix[3] ); } -void MercuryMatrix::Identity() +const MercuryMatrix& MercuryMatrix::Identity() { - const static float Identity[16] = { - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f }; - Copy16f(&m_matrix[0], Identity ); + if (IdentityMatrix.m_matrix[0][0] != 1.0f) + { + float identity[16] = { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f }; + Copy16f(MercuryMatrix::IdentityMatrix.m_matrix[0], identity ); + } + + return IdentityMatrix; } void MercuryMatrix::Translate(float x, float y, float z) Modified: Mercury2/src/MercuryMatrix.h =================================================================== --- Mercury2/src/MercuryMatrix.h 2009-04-12 16:32:55 UTC (rev 208) +++ Mercury2/src/MercuryMatrix.h 2009-04-12 18:09:46 UTC (rev 209) @@ -15,6 +15,8 @@ ///[row][column] (The internal matrix) // float m_matrix[4][4]; FloatRow m_matrix[4]; + + static MercuryMatrix IdentityMatrix; public: MercuryMatrix(); inline MercuryMatrix(const MercuryMatrix& m) { *this = m; } @@ -41,10 +43,9 @@ inline void Transpose() { TransposeMatrix( m_matrix ); } void Zero(); - void Identity(); + static const MercuryMatrix& Identity(); void Print() const; - static MercuryMatrix IdentityMatrix; } #if !defined( WIN32 ) || defined( _MSC_VER ) M_ALIGN(64); Modified: Mercury2/src/RenderGraph.h =================================================================== --- Mercury2/src/RenderGraph.h 2009-04-12 16:32:55 UTC (rev 208) +++ Mercury2/src/RenderGraph.h 2009-04-12 18:09:46 UTC (rev 209) @@ -8,8 +8,14 @@ { friend class RenderGraph; public: - RenderGraphEntry(const MercuryMatrix* matrix = &MercuryMatrix::IdentityMatrix, RenderableNode* node = NULL) - :m_node(node), m_matrix(matrix) + RenderGraphEntry() + { + m_node = NULL; + m_matrix = &MercuryMatrix::Identity(); + } + + RenderGraphEntry(const MercuryMatrix* matrix, RenderableNode* node) + :m_node(node), m_matrix(matrix) {} void AddChild(RenderGraphEntry entry); Modified: Mercury2/src/RenderableNode.cpp =================================================================== --- Mercury2/src/RenderableNode.cpp 2009-04-12 16:32:55 UTC (rev 208) +++ Mercury2/src/RenderableNode.cpp 2009-04-12 18:09:46 UTC (rev 209) @@ -70,7 +70,7 @@ if ( (tn = TransformNode::Cast(n)) ) return tn->GetGlobalMatrix(); - return MercuryMatrix::IdentityMatrix; + return MercuryMatrix::Identity(); } void RenderableNode::AddPreRender(MercuryAsset* asset) Modified: Mercury2/src/TransformNode.cpp =================================================================== --- Mercury2/src/TransformNode.cpp 2009-04-12 16:32:55 UTC (rev 208) +++ Mercury2/src/TransformNode.cpp 2009-04-12 18:09:46 UTC (rev 209) @@ -71,7 +71,7 @@ n = n->Parent(); } - return MercuryMatrix::IdentityMatrix; + return MercuryMatrix::Identity(); } void TransformNode::RippleTaintDown() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |