00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef GRINLIZ_MATRIX_DEFINED
00027 #define GRINLIZ_MATRIX_DEFINED
00028
00029 #include "gldebug.h"
00030 #include "glvector.h"
00031
00032 namespace grinliz {
00033
00045 class Matrix4
00046 {
00047 public:
00048 Matrix4() { SetIdentity(); }
00049 void SetIdentity() { x[0] = x[5] = x[10] = x[15] = 1.0f;
00050 x[1] = x[2] = x[3] = x[4] = x[6] = x[7] = x[8] = x[9] = x[11] = x[12] = x[13] = x[14] = 0.0f;
00051 }
00052 void SetTranslation( float _x, float _y, float _z )
00053 { x[12] = _x; x[13] = _y; x[14] = _z; }
00054
00055 void SetXRotation( float thetaDegree );
00056 void SetYRotation( float thetaDegree );
00057 void SetZRotation( float thetaDegree );
00058
00059 void SetXRotationNeg90();
00060 void SetZRotationNeg90();
00061
00062 void SetScale( float scale ) { x[0] = x[5] = x[10] = scale; }
00063
00064 void SetAxisAngle( const Vector3F& axis, float angle );
00065
00066 void StripTrans() { x[12] = x[13] = x[14] = 0.0f;
00067 x[15] = 1.0f;
00068 }
00069
00070 float* Base() { return x; }
00071
00072
00073 bool IsRotation() const;
00074
00075 friend void MultMatrix4( const Matrix4& a, const Matrix4& b, Matrix4* c );
00076 friend void MultMatrix4( const Matrix4& a, const Vector4F& b, Vector4F* c );
00077
00078 #ifdef DEBUG
00079 void Dump( const char* name ) const
00080 {
00081 GLOUTPUT(("Matrix %s:\n"
00082 " %6.2f %6.2f %6.2f %6.2f\n"
00083 " %6.2f %6.2f %6.2f %6.2f\n"
00084 " %6.2f %6.2f %6.2f %6.2f\n"
00085 " %6.2f %6.2f %6.2f %6.2f\n",
00086 name,
00087 x[0], x[4], x[8], x[12],
00088 x[1], x[5], x[9], x[13],
00089 x[2], x[6], x[10], x[14],
00090 x[3], x[7], x[11], x[15] ));
00091 }
00092 #endif
00093
00094
00095
00096
00097 union
00098 {
00099 float x[16];
00100 struct
00101 {
00102 float m11, m21, m31, m41, m12, m22, m32, m42, m13, m23, m33, m43, m14, m24, m34, m44;
00103 };
00104 };
00105
00106 friend Matrix4 operator*( const Matrix4& a, const Matrix4& b )
00107 {
00108 Matrix4 result;
00109 MultMatrix4( a, b, &result );
00110 return result;
00111 }
00112 };
00113
00114
00115 inline void MultMatrix4( const Matrix4& a, const Matrix4& b, Matrix4* c )
00116 {
00117
00118 GLASSERT( c != &a && c != &b && &a != &b );
00119
00120 for( int i=0; i<4; ++i )
00121 {
00122 for( int j=0; j<4; ++j )
00123 {
00124 *( c->x + i +4*j ) = a.x[i+0] * b.x[j*4+0]
00125 + a.x[i+4] * b.x[j*4+1]
00126 + a.x[i+8] * b.x[j*4+2]
00127 + a.x[i+12] * b.x[j*4+3];
00128 }
00129 }
00130 }
00131
00132
00133 inline void MultMatrix4( const Matrix4& a, const Vector3F& b, Vector3F* c )
00134 {
00135 GLASSERT( c != &b );
00136 for( int i=0; i<3; ++i )
00137 {
00138 *( &c->x + i ) = a.x[i+0] * b.x
00139 + a.x[i+4] * b.y
00140 + a.x[i+8] * b.z
00141 + a.x[i+12];
00142 }
00143 }
00144
00145
00146 inline void MultMatrix4( const Vector3F& b, const Matrix4& a, Vector3F* c )
00147 {
00148 GLASSERT( c != &b );
00149
00150 for( int i=0; i<3; ++i )
00151 {
00152 *( &c->x + i ) = a.x[i*4+0] * b.x
00153 + a.x[i*4+1] * b.y
00154 + a.x[i*4+2] * b.z
00155 + a.x[i*4+3];
00156 }
00157 }
00158
00159
00160 };
00161
00162 #endif