Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

glmatrix.h

00001 /*
00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
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         // Is this probably a rotation matrix?
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         // Row-Column notation is backwards from x,y regrettably. Very
00095         // confusing. Just uses array. Increment by one moves down to the next
00096         // row, so that the next columnt is at +4.
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         // This does not support the target being one of the sources.
00118         GLASSERT( c != &a && c != &b && &a != &b );
00119 
00120         for( int i=0; i<4; ++i )                        // target row
00121         {
00122                 for( int j=0; j<4; ++j )                // target column
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 )                        // target row
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];                  // assume 1.0
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 )                        // target row
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];                 // assume 1.0
00156         }
00157 }
00158 
00159 
00160 };      // namespace grinliz
00161 
00162 #endif

Generated on Sun Sep 25 16:25:49 2005 for Kyra by  doxygen 1.4.3