|
From: <cn...@us...> - 2009-09-03 04:04:42
|
Revision: 533
http://hgengine.svn.sourceforge.net/hgengine/?rev=533&view=rev
Author: cnlohr
Date: 2009-09-03 04:04:30 +0000 (Thu, 03 Sep 2009)
Log Message:
-----------
add various HG1 toolset files
Added Paths:
-----------
Mercury2/tools/collada2hgmdl/MercuryMath.cpp
Mercury2/tools/collada2hgmdl/MercuryMath.h
Mercury2/tools/collada2hgmdl/MercuryMatrix.cpp
Mercury2/tools/collada2hgmdl/MercuryMatrix.h
Mercury2/tools/collada2hgmdl/MercuryTypes.cpp
Mercury2/tools/collada2hgmdl/MercuryTypes.h
Mercury2/tools/collada2hgmdl/ModelContainer.cpp
Mercury2/tools/collada2hgmdl/ModelContainer.h
Added: Mercury2/tools/collada2hgmdl/MercuryMath.cpp
===================================================================
--- Mercury2/tools/collada2hgmdl/MercuryMath.cpp (rev 0)
+++ Mercury2/tools/collada2hgmdl/MercuryMath.cpp 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,316 @@
+#include "MercuryMath.h"
+
+#if !defined( USE_SSE )
+
+//Generic Math functions. Compile these if you can not use optimized functions.
+
+void Mul4f(const float* first, const float* second, float* 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];
+}
+
+void Div4f(const float* first, const float* second, float* 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];
+}
+
+void Add4f(const float* first, const float* second, float* 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];
+}
+
+void Sub4f(const float* first, const float* second, float* 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];
+}
+
+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];
+}
+
+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];
+}
+
+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];
+}
+
+void R_ConcatTransforms4 ( const float* in1, const float* in2, float* out)
+{
+ 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] +
+ in1[2] * in2[9] + in1[3] * in2[13];
+ out[2] = in1[0] * in2[2] + in1[1] * in2[6] +
+ in1[2] * in2[10] + in1[3] * in2[14];
+ out[3] = in1[0] * in2[3] + in1[1] * in2[7] +
+ in1[2] * in2[11] + in1[3] * in2[15];
+
+ out[4] = in1[4] * in2[0] + in1[5] * in2[4] +
+ in1[6] * in2[8] + in1[7] * in2[12];
+ out[5] = in1[4] * in2[1] + in1[5] * in2[5] +
+ in1[6] * in2[9] + in1[7] * in2[13];
+ out[6] = in1[4] * in2[2] + in1[5] * in2[6] +
+ in1[6] * in2[10] + in1[7] * in2[14];
+ out[7] = in1[4] * in2[3] + in1[5] * in2[7] +
+ in1[6] * in2[11] + in1[7] * in2[15];
+
+ out[8] = in1[8] * in2[0] + in1[9] * in2[4] +
+ in1[10] * in2[8] + in1[11] * in2[12];
+ out[9] = in1[8] * in2[1] + in1[9] * in2[5] +
+ in1[10] * in2[9] + in1[11] * in2[13];
+ out[10] = in1[8] * in2[2] + in1[9] * in2[6] +
+ in1[10] * in2[10] + in1[11] * in2[14];
+ out[11] = in1[8] * in2[3] + in1[9] * in2[7] +
+ in1[10] * in2[11] + in1[11] * in2[15];
+
+ out[12] = in1[12] * in2[0] + in1[13] * in2[4] +
+ in1[14] * in2[8] + in1[15] * in2[12];
+ out[13] = in1[12] * in2[1] + in1[13] * in2[5] +
+ in1[14] * in2[9] + in1[15] * in2[13];
+ out[14] = in1[12] * in2[2] + in1[13] * in2[6] +
+ in1[14] * in2[10] + in1[15] * in2[14];
+ out[15] = in1[12] * in2[3] + in1[13] * in2[7] +
+ in1[14] * in2[11] + in1[15] * in2[15];
+}
+
+void VectorMultiply4f( const float * m, float *p, float *out )
+{
+ 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];
+ out[3] = p[0] * m[12] + p[1] * m[13] + p[2] * m[14] + p[3] * m[15];
+}
+
+#else
+#include <xmmintrin.h>
+
+inline __m128 Hadd4(__m128 x);
+__m128 Hadd4(__m128 x)
+{
+ //add the low and high components of x
+ x = _mm_add_ps(x, _mm_movehl_ps(x, x));
+ //add x[0] and x[1]
+ 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)
+{
+ __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]);
+}
+
+void Div4f(const float* first, const float* second, float* 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]);
+}
+
+void Add4f(const float* first, const float* second, float* 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]);
+
+}
+
+void Sub4f(const float* first, const float* second, float* 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]);
+
+}
+
+void Copy4f( void * dest, const void * source )
+{
+ __m128 xmm;
+
+ xmm = _mm_load_ps((float*)source);
+ _mm_store_ps((float*)dest, xmm);
+}
+
+void Copy8f( void * dest, const void * source )
+{
+ __m128 xmm[2];
+
+ xmm[0] = _mm_load_ps((float*)source);
+ _mm_store_ps((float*)dest, xmm[0]);
+
+ xmm[1] = _mm_load_ps((float*)&(((float*)source)[4]));
+ _mm_store_ps((float*)&(((float*)dest)[4]), xmm[1]);
+}
+
+void Copy16f( void * dest, const void * source )
+{
+ __m128 xmm[4];
+
+ xmm[0] = _mm_load_ps((float*)source);
+ _mm_store_ps((float*)dest, xmm[0]);
+
+ xmm[1] = _mm_load_ps((float*)&(((float*)source)[4]));
+ _mm_store_ps((float*)&(((float*)dest)[4]), xmm[1]);
+
+ xmm[2] = _mm_load_ps((float*)&(((float*)source)[8]));
+ _mm_store_ps((float*)&(((float*)dest)[8]), xmm[2]);
+
+ xmm[3] = _mm_load_ps((float*)&(((float*)source)[12]));
+ _mm_store_ps((float*)&(((float*)dest)[12]), xmm[3]);
+}
+
+void R_ConcatTransforms4 ( const float* in1, const float* in2, float* 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]));
+
+ 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]);
+
+ 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_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]);
+ }
+}
+
+//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 )
+{
+ __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]));
+
+ //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);
+
+ //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);
+
+ //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);
+}
+
+#endif
+
+/*
+ * (c) 2006 Joshua Allen, Charles Lohr
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
Added: Mercury2/tools/collada2hgmdl/MercuryMath.h
===================================================================
--- Mercury2/tools/collada2hgmdl/MercuryMath.h (rev 0)
+++ Mercury2/tools/collada2hgmdl/MercuryMath.h 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,74 @@
+#ifndef _MERCURYMATH_H
+#define _MERCURYMATH_H
+
+#include <math.h>
+
+#define DEGRAD 0.01745329251994329576f //degree to radian
+#define RADDEG 57.2957795130823208767f //radian to degree
+#define Q_PI 3.14159265358979323846f
+
+#if defined(WIN32)
+//In win32, sin works faster than sinf and similar functions
+#define SIN( x ) float( sin ( x ) )
+#define COS( x ) float( cos ( x ) )
+#define ATAN2( x, y ) float( atan2( x, y ) )
+#define ASIN( x ) float( asin ( x ) )
+#define ACOS( x ) float( acos ( x ) )
+#define SQRT( x ) float( sqrt ( x ) )
+#define TAN( x ) float( tan ( x ) )
+#define ABS( x ) float( ((x<0)?(-x):(x)) )
+inline int LRINTF(float x) { int r = (int)x; (x-r)>=0.5?++r:0; return r; };
+#else
+//On other OSes in general, sinf works faster than floating a sin
+#define SIN( x ) sinf( x )
+#define COS( x ) cosf( x )
+#define ATAN2( x, y ) atan2f( x, y )
+#define ASIN( x ) asinf ( x )
+#define ACOS( x ) acosf ( x )
+#define SQRT( x ) sqrtf( x )
+#define TAN( x ) tanf( x )
+#define ABS( x ) ((x<0)?(-x):(x))
+#define LRINTF( x ) lrintf( x )
+#endif
+
+#define SQ(x) ((x)*(x));
+
+#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 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 VectorMultiply4f(const float *m, float *p, float *out );
+
+#endif
+
+/*
+ * (c) 2006 Joshua Allen
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
Added: Mercury2/tools/collada2hgmdl/MercuryMatrix.cpp
===================================================================
--- Mercury2/tools/collada2hgmdl/MercuryMatrix.cpp (rev 0)
+++ Mercury2/tools/collada2hgmdl/MercuryMatrix.cpp 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,274 @@
+#include "MercuryMatrix.h"
+#include "MercuryMath.h"
+#include "MercuryTypes.h"
+
+MercuryMatrix::MercuryMatrix()
+{
+ Identity();
+}
+
+const MercuryMatrix& MercuryMatrix::operator=(const MercuryMatrix& m)
+{
+ Copy16f(m_matrix, m.m_matrix);
+ return *this;
+}
+
+const MercuryMatrix& MercuryMatrix::operator=(const float* m)
+{
+ Copy16f(m_matrix, m);
+ return *this;
+}
+
+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;
+}
+
+void MercuryMatrix::Identity()
+{
+ m_matrix[0][0] = 1;
+ m_matrix[0][1] = 0;
+ m_matrix[0][2] = 0;
+ m_matrix[0][3] = 0;
+
+ m_matrix[1][0] = 0;
+ m_matrix[1][1] = 1;
+ m_matrix[1][2] = 0;
+ m_matrix[1][3] = 0;
+
+ m_matrix[2][0] = 0;
+ m_matrix[2][1] = 0;
+ m_matrix[2][2] = 1;
+ m_matrix[2][3] = 0;
+
+ m_matrix[3][0] = 0;
+ m_matrix[3][1] = 0;
+ m_matrix[3][2] = 0;
+ m_matrix[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;
+ *this *= m;
+}
+
+void MercuryMatrix::RotateXYZ(float x, float y, float z)
+{
+ //x,y,z must be negated for some reason
+ float X = -x*2*Q_PI/360; //Reduced calulation for speed
+ float Y = -y*2*Q_PI/360;
+ float Z = -z*2*Q_PI/360;
+ float cx = COS(X);
+ float sx = SIN(X);
+ float cy = COS(Y);
+ float sy = SIN(Y);
+ float cz = COS(Z);
+ float sz = SIN(Z);
+
+ MercuryMatrix matrix;
+
+ //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.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.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.m_matrix[0][3] = 0;
+ matrix.m_matrix[1][3] = 0;
+ matrix.m_matrix[2][3] = 0;
+ matrix.m_matrix[3][3] = 1;
+
+ *this *= matrix;
+}
+
+void MercuryMatrix::RotateAngAxis( float fAngle, float ix, float iy, float iz )
+{
+ float c = COS( fAngle*Q_PI/180 );
+ float s = SIN( fAngle*Q_PI/180 );
+ float absin = SQRT( ix*ix + iy*iy + iz*iz );
+ float x = ix/absin;
+ 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;
+
+ 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;
+
+ 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;
+
+ m_matrix[3][0] = 0;
+ m_matrix[3][1] = 0;
+ m_matrix[3][2] = 0;
+ m_matrix[3][3] = 1;
+}
+
+void MercuryMatrix::Transotale( float tX, float tY, float tZ, float rX, float rY, float rZ, float sX, float sY, float sZ )
+{
+ //x,y,z must be negated for some reason
+ float X = -rX*DEGRAD; //Reduced calulation for speed
+ float Y = -rY*DEGRAD;
+ float Z = -rZ*DEGRAD;
+ float cx = COS(X);
+ float sx = SIN(X);
+ float cy = COS(Y);
+ float sy = SIN(Y);
+ float cz = COS(Z);
+ float sz = SIN(Z);
+
+ MercuryMatrix matrix;
+
+ //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.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.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.m_matrix[0][3] = tX;
+ matrix.m_matrix[1][3] = tY;
+ matrix.m_matrix[2][3] = tZ;
+ matrix.m_matrix[3][3] = 1;
+
+ *this *= matrix;
+}
+
+
+void MercuryMatrix::Scale(float x, float y, float z)
+{
+ MercuryMatrix m;
+
+ m.m_matrix[0][0] = x;
+ m.m_matrix[1][1] = y;
+ m.m_matrix[2][2] = z;
+
+ *this *= m;
+}
+
+MercuryMatrix MercuryMatrix::operator*(const MercuryMatrix& m) const
+{
+ MercuryMatrix r(*this);
+ R_ConcatTransforms4 ( (float*)&m_matrix, (float*)&m.m_matrix, (float*)&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);
+ return *this;
+}
+
+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.
+ *
+ * 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/tools/collada2hgmdl/MercuryMatrix.h
===================================================================
--- Mercury2/tools/collada2hgmdl/MercuryMatrix.h (rev 0)
+++ Mercury2/tools/collada2hgmdl/MercuryMatrix.h 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,75 @@
+#ifndef MERCURYMATRIX_H
+#define MERCURYMATRIX_H
+
+//This matrix will work identically to float[16]
+#include "MercuryMath.h"
+
+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
+{
+private:
+ ///[row][column] (The internal matrix)
+ float m_matrix[4][4];
+public:
+ MercuryMatrix();
+ inline MercuryMatrix(const MercuryMatrix& m) { *this = m; }
+ inline float* operator[](unsigned int i) { return m_matrix[i]; }
+ ///Allow typecasting to float * for use in APIs
+ inline const float* operator[](unsigned int i) const { return m_matrix[i]; }
+ const MercuryMatrix& operator=(const MercuryMatrix& m);
+ const MercuryMatrix& operator=(const float* m);
+ inline float* Ptr() { return (float*)&m_matrix; }
+ inline const float* Ptr() const { return (float*)&m_matrix; }
+
+ MercuryMatrix operator*(const MercuryMatrix& m) const;
+ MercuryMatrix& operator*=(const MercuryMatrix& m);
+
+ void Translate(float x, float y, float z);
+ ///Rotate along the 3 primariy axes by given amounts (in deg)
+ void RotateXYZ(float x, float y, float z);
+ ///Rotate a given amount (fAngle) in degrees around pAxis
+ 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); }
+
+ void Zero();
+ void Identity();
+};
+//XXX: NOTE: TODO: This is normally aligned.
+
+#endif
+
+/*
+ * Copyright (c) 2006 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/tools/collada2hgmdl/MercuryTypes.cpp
===================================================================
--- Mercury2/tools/collada2hgmdl/MercuryTypes.cpp (rev 0)
+++ Mercury2/tools/collada2hgmdl/MercuryTypes.cpp 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,584 @@
+#include <cmath>
+#include <string.h>
+#include "MercuryTypes.h"
+#include "MercuryMath.h"
+//#include "MercuryUtil.h"
+
+const float MercuryPoint::operator[] ( const int rhs ) const
+{
+ switch (rhs)
+ {
+ case 0: return x;
+ case 1: return y;
+ case 2: return z;
+ }
+ return x; //haha we won't even get here.
+}
+
+float & MercuryPoint::operator [] ( const int rhs )
+{
+ switch (rhs)
+ {
+ case 0: return x;
+ case 1: return y;
+ case 2: return z;
+ }
+ return x; //haha we won't even get here.
+}
+
+MercuryPoint MercuryPoint::operator*(const MercuryPoint& p) const
+{
+ MercuryPoint tmp;
+ tmp.x = x * p.x;
+ tmp.y = y * p.y;
+ tmp.z = z * p.z;
+ return tmp;
+}
+
+MercuryPoint MercuryPoint::operator/(const MercuryPoint& p) const
+{
+ MercuryPoint tmp;
+ tmp.x = x / p.x;
+ tmp.y = y / p.y;
+ tmp.z = z / p.z;
+ return tmp;
+}
+
+bool MercuryPoint::operator==(const MercuryPoint& p) const
+{
+ if ((x == p.x) && (y == p.y) && (z == p.z))
+ return true;
+
+ return false;
+}
+
+bool MercuryPoint::operator==(const float f) const
+{
+ if ((x == f) && (y == f) && (z == f))
+ return true;
+
+ return false;
+}
+
+MercuryPoint MercuryPoint::CrossProduct(const MercuryPoint& p) const
+{
+ MercuryPoint ret;
+
+ ret[0] = y*p.z - z*p.y;
+ ret[1] = z*p.x - x*p.z;
+ ret[2] = x*p.y - y*p.x;
+
+ return ret;
+}
+
+void MercuryPoint::NormalizeSelf()
+{
+ float imag = 1.0f/Magnitude();
+ x *= imag; y *= imag; z *= imag;
+}
+
+const MercuryPoint MercuryPoint::Normalize() const
+{
+ MercuryPoint t(*this);
+ t.NormalizeSelf();
+ return t;
+}
+
+float MercuryPoint::Magnitude() const
+{
+ float length = 0;
+ length += x*x;
+ length += y*y;
+ length += z*z;
+ return SQRT(length);
+}
+
+MercuryPoint Rotate2DPoint( float fAngle, MercuryPoint pIn )
+{
+ MercuryPoint ret;
+ ret.x += SIN(fAngle)*pIn.y + COS(-fAngle)*pIn.x;
+ ret.y += COS(fAngle)*pIn.y + SIN(-fAngle)*pIn.x;
+ return ret;
+}
+
+void AngleMatrix (const MercuryPoint & angles, MercuryMatrix & matrix )
+{
+ float X = angles[0]*2*Q_PI/360; //Reduced calulation for speed
+ float Y = angles[1]*2*Q_PI/360;
+ float Z = angles[2]*2*Q_PI/360;
+ float cx = COS(X);
+ float sx = SIN(X);
+ float cy = COS(Y);
+ float sy = SIN(Y);
+ float cz = COS(Z);
+ float sz = SIN(Z);
+
+ //Row major
+ matrix[0][0] = cy*cz;
+ matrix[0][1] = (sx*sy*cz)-(cx*sz);
+ matrix[0][2] = (cx*sy*cz)+(sx*sz);
+ matrix[0][3] = 0;
+
+ matrix[1][0] = cy*sz;
+ matrix[1][1] = (sx*sy*sz)+(cx*cz);
+ matrix[1][2] = (cx*sy*sz)-(sx*cz);
+ matrix[1][3] = 0;
+
+ matrix[2][0] = -sy;
+ matrix[2][1] = sx*cy;
+ matrix[2][2] = cx*cy;
+ matrix[2][3] = 0;
+
+ matrix[3][0] = 0;
+ matrix[3][1] = 0;
+ matrix[3][2] = 0;
+ matrix[3][3] = 1;
+}
+
+void R_ConcatTransforms3 ( MercuryMatrix in1, MercuryMatrix in2, MercuryMatrix & out)
+{
+ out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
+ in1[0][2] * in2[2][0];
+ out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
+ in1[0][2] * in2[2][1];
+ out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
+ in1[0][2] * in2[2][2];
+ out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] +
+ in1[0][2] * in2[2][3] + in1[0][3];
+ out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
+ in1[1][2] * in2[2][0];
+ out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
+ in1[1][2] * in2[2][1];
+ out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
+ in1[1][2] * in2[2][2];
+ out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] +
+ in1[1][2] * in2[2][3] + in1[1][3];
+ out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
+ in1[2][2] * in2[2][0];
+ out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
+ in1[2][2] * in2[2][1];
+ out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
+ in1[2][2] * in2[2][2];
+ out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] +
+ in1[2][2] * in2[2][3] + in1[2][3];
+}
+
+//rewrite
+//Get rid of vectors and use Quaternion.rotateAbout(Quaternion)
+void VectorRotate (const MercuryPoint &in1, const MercuryMatrix &in2, MercuryPoint &out) {
+ out[0] = DotProduct(in1, in2[0]);
+ out[1] = DotProduct(in1, in2[1]);
+ out[2] = DotProduct(in1, in2[2]);
+}
+
+//Must rewrite
+// rotate by the inverse of the matrix
+void VectorIRotate (const MercuryPoint &in1, MercuryMatrix &in2, MercuryPoint & out)
+{
+ out[0] = in1[0]*in2[0][0] + in1[1]*in2[1][0] + in1[2]*in2[2][0];
+ out[1] = in1[0]*in2[0][1] + in1[1]*in2[1][1] + in1[2]*in2[2][1];
+ out[2] = in1[0]*in2[0][2] + in1[1]*in2[1][2] + in1[2]*in2[2][2];
+}
+
+void TranslationMatrix( const MercuryPoint & position, MercuryMatrix & mat )
+{
+ mat[0][0] = 1;
+ mat[1][0] = 0;
+ mat[2][0] = 0;
+ mat[3][0] = 0;
+ mat[0][1] = 0;
+ mat[1][1] = 1;
+ mat[2][1] = 0;
+ mat[3][1] = 0;
+ mat[0][2] = 0;
+ mat[1][2] = 0;
+ mat[2][2] = 1;
+ mat[3][2] = 0;
+ mat[0][3] = position.x;
+ mat[1][3] = position.y;
+ mat[2][3] = position.z;
+ mat[3][3] = 1;
+}
+
+void VectorMultiply( MercuryMatrix &m, const MercuryPoint &p, MercuryPoint &out )
+{
+ out[0] = p[0] * m[0][0] + p[1] * m[0][1] + p[2] * m[0][2] + m[0][3];
+ out[1] = p[0] * m[1][0] + p[1] * m[1][1] + p[2] * m[1][2] + m[1][3];
+ out[2] = p[0] * m[2][0] + p[1] * m[2][1] + p[2] * m[2][2] + m[2][3];
+}
+
+/* CODE FOR COMPLETING MATRIX-MATRIX MULTIPLICATION (Thank god for MATH 221 w/ Dr. Yoon Song @ UMBC ) */
+void ScaleSector ( MercuryMatrix &in, MercuryMatrix &out, int iLine, float fAmount )
+{
+ out[iLine][0] = in[iLine][0] * fAmount;
+ out[iLine][1] = in[iLine][1] * fAmount;
+ out[iLine][2] = in[iLine][2] * fAmount;
+ out[iLine][3] = in[iLine][3] * fAmount;
+}
+
+void TranslateSector ( MercuryMatrix &in, MercuryMatrix &out, int iLine, int iLine2, float fAmount )
+{
+ out[iLine][0] += in[iLine2][0] * fAmount;
+ out[iLine][1] += in[iLine2][1] * fAmount;
+ out[iLine][2] += in[iLine2][2] * fAmount;
+ out[iLine][3] += in[iLine2][3] * fAmount;
+}
+
+void InvertMatrix( MercuryMatrix &in, MercuryMatrix &out )
+{
+ MercuryMatrix tmp;
+ memcpy( &tmp[0][0], &in[0][0], sizeof(float) * 16 );
+
+ out[0][1] = out[0][2] = out[0][3] = 0;
+ out[1][0] = out[1][2] = out[1][3] = 0;
+ out[2][0] = out[2][1] = out[2][3] = 0;
+ out[3][0] = out[3][1] = out[3][2] = 0;
+ out[0][0] = out[1][1] = out[2][2] = out[3][3] = 1;
+
+ for ( int i = 0; i < 4; i++ )
+ for ( int j = 0; j < 4; j++ )
+ {
+ //Ok, this is a pretty damn discusting trick.
+ //On matricies with one of the pivots having tiny values
+ // there's a big issue when it tries to get inverted.
+ // in many cases it can produce a matrix off by as much as 1 or 2 hundred
+ // the only way around this is to pick another row and add it to the
+ // row with an issue ahead of time, bringing all of the numbers to
+ // usable numbers.
+ if( ABS( 1.0f/tmp[i][i] ) > 1000.0f )
+ {
+ TranslateSector( out, out, i, (i+1)%4, 1 );
+ TranslateSector( tmp, tmp, i, (i+1)%4, 1 );
+ }
+
+ float fMt;
+ if ( i == j )
+ {
+ fMt = 1/tmp[i][i];
+ ScaleSector( tmp, tmp, j, fMt );
+ ScaleSector( out, out, j, fMt );
+ }
+ else
+ {
+ fMt = -tmp[j][i]/tmp[i][i];
+ TranslateSector( tmp, tmp, j, i, fMt );
+ TranslateSector( out, out, j, i, fMt );
+ }
+ }
+
+ tmp.Ptr();
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+float & MQuaternion::operator [] (int i)
+{
+ switch (i)
+ {
+ case 0: return x;
+ case 1: return y;
+ case 2: return z;
+ case 3: return w;
+ }
+ return x; //haha we won't even get here.
+}
+
+void MQuaternion::SetEuler(const MercuryPoint& angles)
+{
+ float X = angles[0]/2.0f; //roll
+ float Y = angles[1]/2.0f; //pitch
+ float Z = angles[2]/2.0f; //yaw
+
+ float cx = COS(X);
+ float sx = SIN(X);
+ float cy = COS(Y);
+ float sy = SIN(Y);
+ float cz = COS(Z);
+ float sz = SIN(Z);
+
+ //Correct according to
+ //http://en.wikipedia.org/wiki/Conversion_between_MQuaternions_and_Euler_angles
+ w = cx*cy*cz+sx*sy*sz;//q1
+ x = sx*cy*cz-cx*sy*sz;//q2
+ y = cx*sy*cz+sx*cy*sz;//q3
+ z = cx*cy*sz-sx*sy*cz;//q4
+}
+
+void MQuaternion::CreateFromAxisAngle(const MercuryPoint& p, const float radians)
+{
+ float sn = SIN(radians/2.0f);
+ w = COS(radians/2.0f);
+ x = sn * p.x;
+ y = sn * p.y;
+ z = sn * p.z;
+}
+
+//Returns the magnitude
+float MQuaternion::magnitude() const {
+ return SQRT((w*w)+(x*x)+(y*y)+(z*z));
+}
+
+//Returns the normalized MQuaternion
+MQuaternion MQuaternion::normalize() const {
+ return (*this)/magnitude();
+}
+
+//Returns the conjugate MQuaternion
+MQuaternion MQuaternion::conjugate() const {
+ MQuaternion c(w,-x,-y,-z);
+ return c;
+}
+
+//Returns the reciprocal MQuaternion
+MQuaternion MQuaternion::reciprocal() const {
+ float m = magnitude();
+ return conjugate()/(m*m);
+}
+
+//Rotate MQuaternion about another MQuaternion
+MQuaternion MQuaternion::rotateAbout(const MQuaternion &spinAxis) const {
+ return (*this)*spinAxis;
+}
+
+//Converts MQuaternion to 4x4 Matrix(3x3 Spatial)
+void MQuaternion::toMatrix( MercuryMatrix &matrix ) const {
+ float X = 2*x*x; //Reduced calulation for speed
+ float Y = 2*y*y;
+ float Z = 2*z*z;
+ float a = 2*w*x;
+ float b = 2*w*y;
+ float c = 2*w*z;
+ float d = 2*x*y;
+ float e = 2*x*z;
+ float f = 2*y*z;
+
+ //row major
+ matrix[0][0] = 1-Y-Z;
+ matrix[0][1] = d-c;
+ matrix[0][2] = e+b;
+
+ matrix[1][0] = d+c;
+ matrix[1][1] = 1-X-Z;
+ matrix[1][2] = f-a;
+
+ matrix[2][0] = e-b;
+ matrix[2][1] = f+a;
+ matrix[2][2] = 1-X-Y;
+}
+
+void MQuaternion::toMatrix4( MercuryMatrix &matrix ) const {
+ toMatrix( matrix );
+
+ //row major (even though it looks like column)
+ matrix[0][3] = 0;
+ matrix[1][3] = 0;
+ matrix[2][3] = 0;
+ matrix[3][3] = 1;
+
+ matrix[3][0] = 0;
+ matrix[3][1] = 0;
+ matrix[3][2] = 0;
+}
+
+MQuaternion MQuaternion::operator + (const MQuaternion &rhs) const
+{
+ MQuaternion result;
+ result.w = w + rhs.w;
+ result.x = x + rhs.x;
+ result.y = y + rhs.y;
+ result.z = z + rhs.z;
+ return result;
+}
+
+MQuaternion MQuaternion::operator - (const MQuaternion &rhs) const
+{
+ MQuaternion result;
+ result.w = w - rhs.w;
+ result.x = x - rhs.x;
+ result.y = y - rhs.y;
+ result.z = z - rhs.z;
+ return result;
+}
+
+MQuaternion MQuaternion::operator * (const MQuaternion &rhs) const
+{
+ MQuaternion result;
+ result.w = (w*rhs.w)-(x*rhs.x)-(y*rhs.y)-(z*rhs.z);
+ result.x = (w*rhs.x)+(x*rhs.w)+(y*rhs.z)-(z*rhs.y);
+ result.y = (w*rhs.y)-(x*rhs.z)+(y*rhs.w)+(z*rhs.x);
+ result.z = (w*rhs.z)+(x*rhs.y)-(y*rhs.x)+(z*rhs.w);
+ return result;
+}
+
+MQuaternion& MQuaternion::operator = (const MQuaternion &rhs)
+{
+ w = rhs.w;
+ x = rhs.x;
+ y = rhs.y;
+ z = rhs.z;
+ return (*this);
+}
+
+MQuaternion& MQuaternion::operator += (const MQuaternion &rhs) {
+ w += rhs.w;
+ x += rhs.x;
+ y += rhs.y;
+ z += rhs.z;
+ return (*this);
+}
+
+MQuaternion& MQuaternion::operator -= (const MQuaternion &rhs) {
+ w -= rhs.w;
+ x -= rhs.x;
+ y -= rhs.y;
+ z -= rhs.z;
+ return (*this);
+}
+
+MQuaternion& MQuaternion::operator *= (const MQuaternion &rhs) {
+ w = (w*rhs.w)-(x*rhs.x)-(y*rhs.y)-(z*rhs.z);
+ x = (w*rhs.x)+(x*rhs.w)+(y*rhs.z)-(z*rhs.y);
+ y = (w*rhs.y)-(x*rhs.z)+(y*rhs.w)+(z*rhs.x);
+ z = (w*rhs.z)+(x*rhs.y)-(y*rhs.x)+(z*rhs.w);
+ return (*this);
+}
+
+MQuaternion MQuaternion::operator * (const float &rhs) const {
+ MQuaternion result;
+ result.w = w*rhs;
+ result.x = x*rhs;
+ result.y = y*rhs;
+ result.z = z*rhs;
+ return result;
+}
+
+MQuaternion MQuaternion::operator / (const float &rhs) const {
+ MQuaternion result;
+ result.w = w/rhs;
+ result.x = x/rhs;
+ result.y = y/rhs;
+ result.z = z/rhs;
+ return result;
+}
+
+MQuaternion& MQuaternion::operator *= (const float &rhs) {
+ w *= rhs;
+ x *= rhs;
+ y *= rhs;
+ z *= rhs;
+ return (*this);
+}
+
+MQuaternion& MQuaternion::operator /= (const float &rhs) {
+ w /= rhs;
+ x /= rhs;
+ y /= rhs;
+ z /= rhs;
+ return (*this);
+}
+
+//Returns the Euclidian Inner Product of two MQuaternions (Similar to Vector Dot-Product)
+float innerProduct(const MQuaternion & a, const MQuaternion &b)
+{
+ return (a.w*b.w)+(a.x*b.x)+(a.y*b.y)+(a.z*b.z);
+}
+
+//Returns the Euclidian Outer Product of two MQuaternions
+MercuryPoint outerProduct(MQuaternion a,MQuaternion b)
+{
+ MercuryPoint result;
+ result.x = (a.w*b.x)-(a.x*b.w)-(a.y*b.z)+(a.z*b.y);
+ result.y = (a.w*b.y)+(a.x*b.z)-(a.y*b.w)-(a.z*b.x);
+ result.z = (a.w*b.z)-(a.x*b.y)+(a.y*b.x)-(a.z*b.w);
+ return result;
+}
+
+//Returns the Even Product of two MQuaternions
+MQuaternion evenProduct(MQuaternion a,MQuaternion b) {
+ MQuaternion result;
+ result.w = (a.w*b.w)-(a.x*b.x)-(a.y*b.y)-(a.z*b.z);
+ result.x = (a.w*b.x)+(a.x*b.w);
+ result.y = (a.w*b.y)+(a.y*b.w);
+ result.z = (a.w*b.z)+(a.z*b.w);
+ return result;
+}
+
+//Returns the Odd Product of two MQuaternions (Similar to Vector Cross-Product)
+MercuryPoint oddProduct(MQuaternion a,MQuaternion b) {
+ MercuryPoint result;
+ result.x = (a.y*b.z)-(a.z*b.y);
+ result.y = (a.z*b.x)-(a.x*b.z);
+ result.z = (a.x*b.y)-(a.y*b.x);
+ return result;
+}
+
+//Spherical Linear Interpolation between two MQuaternions at t percent completion(0-1)
+MQuaternion SLERP( const MQuaternion &a, const MQuaternion &b,float t) {
+ MQuaternion an = a.normalize(), bn = b.normalize();
+ float cosTheta = innerProduct(MQuaternion(an),bn);
+ float sinTheta;
+
+ //Careful: If cosTheta is exactly one, or even if it's infinitesimally over, it'll
+ // cause SQRT to produce not a number, and screw everything up.
+ if ( 1 - (cosTheta*cosTheta) <= 0 )
+ sinTheta = 0;
+ else
+ sinTheta = SQRT(1 - (cosTheta*cosTheta));
+
+ float Theta = ACOS(cosTheta); //Theta is half the angle between the 2 MQuaternions
+
+ if(fabs(Theta) < 0.01)
+ return a;
+ if(fabs(sinTheta) < 0.01)
+ return (a+b)/2;
+ return ( (a*SIN((1-t)*Theta)) + (b*SIN(t*Theta)) ) / sinTheta;
+}
+
+const MercuryPoint gpZero = MercuryPoint( 0,0,0 );
+const MercuryPoint gpOne = MercuryPoint( 1,1,1 );
+
+
+
+/*
+ * Copyright (c) 2005-2006, Joshua Allen, Charles Lohr, Adam Lowman
+ * 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/tools/collada2hgmdl/MercuryTypes.h
===================================================================
--- Mercury2/tools/collada2hgmdl/MercuryTypes.h (rev 0)
+++ Mercury2/tools/collada2hgmdl/MercuryTypes.h 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,212 @@
+#ifndef _MERCURYTYPES_H
+#define _MERCURYTYPES_H
+
+#include "MercuryMath.h"
+#include "MercuryMatrix.h"
+
+///A point in space/vector
+class MercuryPoint
+{
+public:
+ MercuryPoint() : x(0), y(0), z(0) { };
+ MercuryPoint( float ix, float iy, float iz ) : x(ix), y(iy), z(iz) { };
+ MercuryPoint( const float * in ) : x(in[0]), y(in[1]), z(in[2]) { };
+
+ ///Direct conversion to float*
+ operator float* () { return &x; }
+ ///Direct conversion to const float*
+ operator const float* () const { return &x; }
+
+ ///Get X value
+ inline const float GetX() const { return x; }
+ ///Get Y value
+ inline const float GetY() const { return y; }
+ ///Get Z value
+ inline const float GetZ() const { return z; }
+ ///Set X value
+ inline bool SetX(const float ix) { if (ix == x) { return false; } x = ix; return true; }
+ ///Set Y value
+ inline bool SetY(const float iy) { if (iy == y) { return false; } y = iy; return true; }
+ ///Set Z value
+ inline bool SetZ(const float iz) { if (iz == z) { return false; } z = iz; return true; }
+ ///Zero the vector
+ inline void Clear() { x = 0; y = 0; z = 0; }
+
+ //allow [] to access
+ float & operator[] ( const int rhs );
+ const float operator[] ( const int rhs ) const;
+
+ ///Normalize (make |point| = 1)
+ void NormalizeSelf();
+ ///Return a normalized point
+ const MercuryPoint Normalize() const;
+ ///Return the magnitude of |this|
+ float Magnitude() const;
+
+ float GetBiggestElement() const { if( x > y ) return (x>z)?x:z; else return (y>z)?y:z; }
+
+ ///Write out to be = to this point
+ inline void ConvertToVector3( float* out ) const { out[0] = x; out[1] = y; out[2] = z; }
+ ///Write out to be = to this point, however the 4th element will be 0
+ inline void ConvertToVector4( float* out ) const { out[0] = x; out[1] = y; out[2] = z; out[3] = 0; }
+ ///Write out to be = - to this point, however the 4th element will be 0
+ inline void ConvertToIVector4( float* out ) const { out[0] = -x; out[1] = -y; out[2] = -z; out[3] = 0; }
+
+ ///Component-wise multiply
+ MercuryPoint operator*(const MercuryPoint& p) const;
+ ///Component-wise divide
+ MercuryPoint operator/(const MercuryPoint& p) const;
+
+ inline MercuryPoint& operator += ( const MercuryPoint& other ) { x+=other.x; y+=other.y; z+=other.z; return *this; }
+ inline MercuryPoint& operator -= ( const MercuryPoint& other ) { x-=other.x; y-=other.y; z-=other.z; return *this; }
+ inline MercuryPoint& operator *= ( float f ) { x*=f; y*=f; z*=f; return *this; }
+ inline MercuryPoint& operator /= ( float f ) { x/=f; y/=f; z/=f; return *this; }
+
+ inline MercuryPoint operator + ( const MercuryPoint& other ) const { return MercuryPoint( x+other.x, y+other.y, z+other.z ); }
+ inline MercuryPoint operator - ( const MercuryPoint& other ) const { return MercuryPoint( x-other.x, y-other.y, z-other.z ); }
+ inline MercuryPoint operator * ( float f ) const { return MercuryPoint( x*f, y*f, z*f ); }
+ inline MercuryPoint operator / ( float f ) const { return MercuryPoint( x/f, y/f, z/f ); }
+
+ friend MercuryPoint operator * ( float f, const MercuryPoint& other ) { return other*f; }
+
+ bool operator==(const MercuryPoint& p) const;
+ inline bool operator!=(const MercuryPoint& p) const { return !(*this == p); }
+
+ bool operator==(const float f) const;
+ inline bool operator!=(const float f) const { return !(*this == f); }
+
+ ///Obtain the cross product (*this) x p
+ MercuryPoint CrossProduct(const MercuryPoint& p) const;
+
+ float x;
+ float y;
+ float z;
+};
+
+///Vector of all 0's (0,0,0)
+extern const MercuryPoint gpZero;
+///Vector of all 1's (1,1,1)
+extern const MercuryPoint gpOne;
+
+
+
+///Rotate (in 2D) the X and Y components of a MercuryPoint
+MercuryPoint Rotate2DPoint( float fAngle, MercuryPoint pIn );
+
+///Produce a matrix out of a rotation x, then y then z (how Mercury does it)
+void AngleMatrix (const MercuryPoint & angles, MercuryMatrix & mat );
+///Produce a translation matrix, by (x,y,z)
+void TranslationMatrix( const MercuryPoint & position, MercuryMatrix & mat );
+///Concatenate matrices, not using the last row. (faster when applicable, and breaks things that don't expect that last row.)
+void R_ConcatTransforms3 ( MercuryMatrix in1, MercuryMatrix in2, MercuryMatrix & out );
+
+///Legacy vector rotation only functions. It's similar to vector-matrix multiplication
+void VectorIRotate (const MercuryPoint & in1, MercuryMatrix &in2, MercuryPoint & out);
+void VectorRotate (const MercuryPoint & in1, const MercuryMatrix &in2, MercuryPoint & out);
+
+///Vector-Matrix multiplication
+void VectorMultiply( MercuryMatrix &m, const MercuryPoint &p, MercuryPoint &out );
+
+///Matrix Inverse of a 4x4 matrix.
+void InvertMatrix( MercuryMatrix &in, MercuryMatrix & out );
+
+///Mathematical Quaternion (Used for Rotation)
+class MQuaternion {
+public:
+ //Defines a Quaternion such that q = w + xi + yj + zk
+ float w,x,y,z;
+ MQuaternion() : w(0), x(0), y(0), z(0) { };
+ MQuaternion(float W, float X, float Y, float Z) : w(W), x(X), y(Y), z(Z) { };
+ MQuaternion(float* wxyz) : w(wxyz[0]), x(wxyz[1]), y(wxyz[2]), z(wxyz[3]) { };
+ MQuaternion(const MercuryPoint& p) : w(0), x(p.GetX()), y(p.GetY()), z(p.GetZ()) {};
+
+ ///Make this quaternion represent to a set of euler angles
+ void SetEuler(const MercuryPoint& angles);
+
+ ///Make the quaternion represent a given angle radians around an axis p
+ void CreateFromAxisAngle(const MercuryPoint& p, const float radians);
+
+ ///Access a component of the quaternion with the [] operator
+ float & operator[] ( const int rhs );
+ const float & operator[] ( const int rhs ) const;
+
+ ///Returns the magnitude
+ float magnitude() const;
+ ///Returns the normalized Quaternion
+ MQuaternion normalize() const;
+ ///Returns the conjugate Quaternion
+ MQuaternion conjugate() const;
+ ///Returns the reciprocal Quaternion
+ MQuaternion reciprocal() const;
+ ///Rotates Quaternion about another Quaternion
+ MQuaternion rotateAbout(const MQuaternion &spinAxis) const;
+ ///Converts Quaternion to 4x4 Matrix(3x3 Spatial)
+ void toMatrix( MercuryMatrix & mat ) const;
+ ///Converts Quaternion to complete 4x4 Matrix
+ void toMatrix4( MercuryMatrix & mat ) const;
+ ///Convert the quaternion to a point.
+ MercuryPoint ToPoint() { return MercuryPoint( x,y,z ); }
+ /******************************************************
+ * NOTE: Quaternion multipication is not commutative *
+ * Therefore the / operator could imply for a/b *
+ * a*b.reciprocal() or b.reciprocal()*a *
+ ******************************************************/
+ MQuaternion operator + (const MQuaternion &rhs) const;
+ MQuaternion operator - (const MQuaternion &rhs) const;
+ MQuaternion operator * (const MQuaternion &rhs) const;
+ MQuaternion& operator = (const MQuaternion &rhs);
+ MQuaternion& operator += (const MQuaternion &rhs);
+ MQuaternion& operator -= (const MQuaternion &rhs);
+ MQuaternion& operator *= (const MQuaternion &rhs);
+ MQuaternion operator * (const float &rhs) const;
+ MQuaternion operator / (const float &rhs) const;
+ MQuaternion& operator *= (const float &rhs);
+ MQuaternion& operator /= (const float &rhs);
+};
+//Normally aligned.
+
+///Returns the Euclidian Inner Product of two Quaternions (Similar to Vector Dot-Product)
+float innerProduct( const MQuaternion &a, const MQuaternion &b );
+
+///Returns the Euclidian Outer Product of two Quaternions
+MercuryPoint outerProduct( const MQuaternion &a, const MQuaternion &b );
+
+///Returns the Even Product of two Quaternions
+MQuaternion evenProduct(const MQuaternion &a, const MQuaternion &b );
+
+///Returns the Odd Product of two Quaternions (Similar to Vector Cross-Product)
+MercuryPoint oddProduct( const MQuaternion &a, const MQuaternion &b );
+
+///Spherical Linear Interpolation between two Quaternions at t percent completion(0-1)
+MQuaternion SLERP( const MQuaternion &a, const MQuaternion &b, float t );
+
+#endif
+
+
+/*
+ * Copyright (c) 2005-2006, Joshua Allen, Charles Lohr, Adam Lowman
+ * 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/tools/collada2hgmdl/ModelContainer.cpp
===================================================================
--- Mercury2/tools/collada2hgmdl/ModelContainer.cpp (rev 0)
+++ Mercury2/tools/collada2hgmdl/ModelContainer.cpp 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,299 @@
+#include "ModelContainer.h"
+
+//Utility functions, these should be written depending on the endian of the system.
+
+char Read1( FILE * f )
+{
+ char iTmp;
+ fread( &iTmp, 1, 1, f );
+ return iTmp;
+}
+
+void Write1( FILE * f, char c )
+{
+ fwrite( &c, 1, 1, f );
+}
+
+int Read4( FILE * f )
+{
+ int iTmp;
+ fread( &iTmp, 4, 1, f );
+ return iTmp;
+}
+
+void Write4( FILE * f, int i )
+{
+ fwrite( &i, 4, 1, f );
+}
+
+string ReadString( FILE * f )
+{
+ int iTmp = Read4( f );
+ char * sTmp = new char[iTmp+1];
+ fread( sTmp, iTmp, 1, f );
+ sTmp[iTmp] = '\0';
+ string sRet = sTmp;
+ delete sTmp;
+ return sRet;
+}
+
+//Actual code
+
+bool Mesh::Load( FILE * f )
+{
+ unsigned i;
+ unsigned j;
+
+ sName = ReadString( f );
+ iBoneAttached = -1;
+
+ vVerticies.resize( Read4( f ) );
+ vStrips.resize( Read4( f ) );
+ vFans.resize( Read4( f ) );
+ vTriangles.resize( Read4( f ) );
+ iMaterialNumber = Read4( f );
+ bCache = Read1( f ) != '\0';
+
+ for( i = 0; i < vVerticies.size(); i++ )
+ {
+ fread( &vVerticies[i], 8*4, 1, f );
+ vVerticies[i].bInUse = true;
+ vVerticies[i].bInUse2 = true;
+ }
+
+ for( i = 0; i < vStrips.size(); i++ )
+ {
+ vStrips[i].resize( Read4( f ) );
+ for( j = 0; j < vStrips[i].size(); j++ )
+ vStrips[i][j] = Read4( f );
+ }
+
+ for( i = 0; i < vFans.size(); i++ )
+ {
+ vFans[i].resize( Read4( f ) );
+ for( j = 0; j < vFans[i].size(); j++ )
+ vFans[i][j] = Read4( f );
+ }
+
+ for( i = 0; i < vTriangles.size(); i++ )
+ {
+ vTriangles[i].bInUse = true;
+ for ( j = 0; j < 3; j++ )
+ vTriangles[i].iFace[j] = Read4( f );
+ }
+
+ return !feof( f );
+}
+
+void Mesh::Save( FILE * f )
+{
+ unsigned i;
+ unsigned j;
+
+ Write4( f, sName.length() );
+ fwrite( sName.c_str(), sName.length(), 1, f );
+ Write4( f, vVerticies.size() );
+ Write4( f, vStrips.size() );
+ Write4( f, vFans.size() );
+ Write4( f, vTriangles.size() );
+ Write4( f, iMaterialNumber );
+ Write1( f, bCache );
+
+ for( i = 0; i < vVerticies.size(); i++ )
+ fwrite( &vVerticies[i], 8*4, 1, f );
+
+ for( i = 0; i < vStrips.size(); i++ )
+ for ( j = 0; j < vStrips[i].size(); j++ )
+ Write4( f, vStrips[i][j] );
+
+ for( i = 0; i < vFans.size(); i++ )
+ for ( j = 0; j < vFans[i].size(); j++ )
+ Write4( f, vFans[i][j] );
+
+ for( i = 0; i < vTriangles.size(); i++ )
+ for ( j = 0; j < 3; j++ )
+ Write4( f, vTriangles[i].iFace[j] );
+}
+
+bool Bone::Load( FILE * f )
+{
+ sName = ReadString( f );
+ iParentIndex = Read4( f );
+ iAssociatedMesh = Read4( f );
+ fread( &pJointPos, 12, 1, f );
+ fread( &qJointRot, 16, 1, f );
+ vAssignments.resize( Read4( f ) );
+
+ for( unsigned i = 0; i < vAssignments.size(); i++ )
+ {
+ vAssignments[i].iMesh = Read4( f );
+ vAssignments[i].iVertex = Read4( f );
+ fread( &vAssignments[i].fWeight, 4, 1, f );
+ }
+
+ return !feof( f );
+}
+
+void Bone::Save( FILE * f )
+{
+ Write4( f, sName.length() );
+ fwrite( sName.c_str(), sName.length(), 1, f );
+ Write4( f, iParentIndex );
+ Write4( f, iAssociatedMesh );
+ fwrite( &pJointPos, 12, 1, f );
+ fwrite( &qJointRot, 16, 1, f );
+
+ Write4( f, vAssignments.size() );
+ for( unsigned i = 0; i < vAssignments.size(); i++ )
+ {
+ Write4( f, vAssignments[i].iMesh );
+ Write4( f, vAssignments[i].iVertex );
+ fwrite( &vAssignments[i].fWeight, 4, 1, f );
+ }
+}
+
+bool AnimationTrack::Load( FILE * f )
+{
+ iBone = Read4( f );
+ vKeys.resize( Read4( f ) );
+ fread( &vKeys[0], 8*4*vKeys.size(), 1, f );
+ return !feof( f );
+}
+
+void AnimationTrack::Save( FILE * f )
+{
+ Write4( f, iBone );
+ Write4( f, vKeys.size() );
+ fwrite( &vKeys[0], 8*4*vKeys.size(), 1, f );
+}
+
+bool Animation::Load( FILE * f )
+{
+ sName = ReadString( f );
+ fread( &fDuration, 4, 1, f );
+ vTracks.resize( Read4( f ) );
+ for( unsigned i = 0; i < vTracks.size(); i++ )
+ vTracks[i].Load( f );
+ return !feof( f );
+}
+
+void Animation::Save( FILE * f )
+{
+ Write4( f, sName.length() );
+ fwrite( sName.c_str(), sName.length(), 1, f );
+ fwrite( &fDuration, 4, 1, f );
+ Write4( f, vTracks.size() );
+ for( unsigned i = 0; i < vTracks.size(); i++ )
+ vTracks[i].Save( f );
+}
+
+bool Model::LoadModel( const char * sFileName )
+{
+ FILE * f = fopen( sFileName, "rb" );
+ if ( !f )
+ return false;
+
+ char sMercuryHeadder[4];
+ int iVersion;
+ unsigned i;
+
+ fread( sMercuryHeadder, 4, 1, f );
+ iVersion = Read4( f );
+
+ vMeshes.resize( Read4( f ) );
+ for( i = 0; i < vMeshes.size(); i++ )
+ vMeshes[i].Load( f );
+
+ vBones.resize( Read4( f ) );
+ for( i = 0; i < vBones.size(); i++ )
+ vBones[i].Load( f );
+
+ //Compute bone xformations, and antixformations
+ for( i = 0; i < vBones.size(); i++ )
+ {
+ MercuryMatrix m,n,o;
+
+ vBones[i].qJointRot.toMatrix4( o );
+ o[0][3] = vBones[i].pJointPos.x;
+ o[1][3] = vBones[i].pJointPos.y;
+ o[2][3] = vBones[i].pJointPos.z;
+
+ if ( vBones[i].iParentIndex >= 0 )
+ {
+ R_ConcatTransforms4( (const float*)&vBones[vBones[i].iParentIndex].pXFormMatrix, (const float*)&o, (float*)&m );
+ Copy16f( &vBones[i].pXFormMatrix, &m);
+ }
+ else
+ Copy16f(&vBones[i].pXFormMatrix, &o);
+
+ InvertMatrix( vBones[i].pXFormMatrix, vBones[i].pAntiXFormMatrix );
+ }
+
+ vAnimations.resize( Read4( f ) );
+ for( i = 0; i < vAnimations.size(); i++ )
+ vAnimations[i].Load( f );
+
+ vMaterials.resize( Read4( f ) );
+ for( i = 0; i < vMaterials.size(); i++ )
+ vMaterials[i] = ReadString( f );
+
+ fclose(f);
+ return true;
+}
+
+void Model::SaveModel( const char * sFileName )
+{
+ unsigned i;
+ FILE * f = fopen( sFileName, "wb" );
+ if ( !f )
+ return;
+ const char * sMercuryKey = "MBMF";
+ fwrite( sMercuryKey, 4, 1, f );
+ Write4( f, 1 );
+
+ Write4( f, vMeshes.size() );
+ for( i = 0; i < vMeshes.size(); i++ )
+ vMeshes[i].Save( f );
+
+ Write4( f, vBones.size() );
+ for( i = 0; i < vBones.size(); i++ )
+ vBones[i].Save( f );
+
+ Write4( f, vAnimations.size() );
+ for( i = 0; i < vAnimations.size(); i++ )
+ vAnimations[i].Save( f );
+
+ Write4( f, vMaterials.size() );
+ for( i = 0; i < vMaterials.size(); i++ )
+ {
+ Write4( f, vMaterials[i].length() );
+ fwrite( vMaterials[i].c_str(), vMaterials[i].length(), 1, f );
+ }
+
+ fclose(f);
+}
+
+/*
+ * (c) 2006 Charles Lohr
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
Added: Mercury2/tools/collada2hgmdl/ModelContainer.h
===================================================================
--- Mercury2/tools/collada2hgmdl/ModelContainer.h (rev 0)
+++ Mercury2/tools/collada2hgmdl/ModelContainer.h 2009-09-03 04:04:30 UTC (rev 533)
@@ -0,0 +1,151 @@
+#ifndef _MODEL_CONTAINER_H
+#define _MODEL_CONTAINER_H
+
+#include "MercuryTypes.h"
+#include <vector>
+#include <string>
+
+using namespace std;
+
+//****MESH****//
+
+struct Vert
+{
+ bool operator < ( const Vert & rhs ) const
+ {
+ if( pPosition.z != rhs.pPosition.z )
+ return pPosition.z < rhs.pPosition.z;
+ if( pPosition.y != rhs.pPosition.y )
+ return pPosition.y < rhs.pPosition.y;
+ if( pPosition.x != rhs.pPosition.x )
+ return pPosition.x < rhs.pPosition.x;
+ if( uv[0] != rhs.uv[0] )
+ return uv[0] < uv[0];
+ return uv[1] < uv[1];
+ }
+
+ float uv[2];
+ MercuryPoint pNormal;
+ MercuryPoint pPosition;
+ vector< int > vUsedBones; //IGNORED BY SAVE AND NOT LOADED.
+ bool bInUse; //IGNORED BY SAVE (Load sets this to true.)
+ bool bInUse2; //IGNORED BY SAVE (Load sets this to true.).
+};
+
+struct Triangle
+{
+ int iFace[3];
+ bool bInUse; //IGNORED BY SAVE (Load sets this to true.)
+};
+
+struct Mesh
+{
+ bool Load( FILE * f );
+ void Save( FILE * f );
+
+ string sName;
+ vector< Vert > vVerticies;
+ vector< vector < int > > vStrips;
+ vector< vector < int > > vFans;
+ vector< Triangle > vTriangles;
+ int iMaterialNumber;
+ bool bCache;
+
+ int iBoneAttached; //IGNORED BY SAVE
+};
+
+//****BONE****//
+
+struct BoneAssignment
+{
+ int iMesh;
+ int iVertex;
+ float fWeight;
+};
+
+struct Bone
+{
+ bool Load( FILE * f );
+ void Save( FILE * f );
+
+ string sName;
+ int iParentIndex;
+ int iAssociatedMesh;
+
+ MercuryPoint pJointPos;;
+ MQuaternion qJointRot;
+ vector< BoneAssignment > vAssignments;
+
+ MercuryMatrix pXFormMatrix; //Performs that bone's xformation IGNORED BY SAVE
+ MercuryMatrix pAntiXFormMatrix; //Inverse of XFormMatrix IGNORED BY SAVE
+};
+
+//****ANIMATION****//
+
+struct AnimationKey
+{
+ float fTime;
+ MercuryPoint pPos;
+ MQuaternion qRot;
+};
+
+struct AnimationTrack
+{
+ bool Load( FILE * f );
+ void Save( FILE * f );
+
+ int iBone;
+ vector< AnimationKey > vKeys;
+};
+
+struct Animation
+{
+ bool Load( FILE * f );
+ void Save( FILE * f );
+
+ string sName;
+ float fDuration;
+ vector< AnimationTrack > vTracks;
+};
+
+
+class Model
+{
+public:
+ bool LoadModel( const char * sFileName );
+ void SaveModel( const char * sFileName );
+
+ vector< Mesh ...
[truncated message content] |