00001 
00032 #ifndef LL_M4MATH_H
00033 #define LL_M4MATH_H
00034 
00035 #include "v3math.h"
00036 
00037 class LLVector4;
00038 class LLMatrix3;
00039 class LLQuaternion;
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 static const U32 NUM_VALUES_IN_MAT4 = 4;
00098 
00099 class LLMatrix4
00100 {
00101 public:
00102         F32     mMatrix[NUM_VALUES_IN_MAT4][NUM_VALUES_IN_MAT4];
00103 
00104         LLMatrix4();                                                                            
00105         explicit LLMatrix4(const F32 *mat);                                                             
00106         explicit LLMatrix4(const LLMatrix3 &mat);                                               
00107         explicit LLMatrix4(const LLQuaternion &q);                                              
00108 
00109         LLMatrix4(const LLMatrix3 &mat, const LLVector4 &pos);  
00110 
00111         
00112         LLMatrix4(const LLQuaternion &q, const LLVector4 &pos); 
00113         LLMatrix4(F32 angle,
00114                           const LLVector4 &vec, 
00115                           const LLVector4 &pos);                                                
00116         LLMatrix4(F32 angle, const LLVector4 &vec);                             
00117         LLMatrix4(const F32 roll, const F32 pitch, const F32 yaw, 
00118                           const LLVector4 &pos);                                                
00119         LLMatrix4(const F32 roll, const F32 pitch, const F32 yaw);                              
00120 
00121         ~LLMatrix4(void);                                                                               
00122 
00123 
00125         
00126         
00127         
00128 
00129         void initRows(const LLVector4 &row0,
00130                                   const LLVector4 &row1,
00131                                   const LLVector4 &row2,
00132                                   const LLVector4 &row3);
00133 
00134         
00135         const LLMatrix4& identity();                                    
00136         const LLMatrix4& zero();                                                
00137 
00138         const LLMatrix4& initRotation(const F32 angle, const F32 x, const F32 y, const F32 z);  
00139         const LLMatrix4& initRotation(const F32 angle, const LLVector4 &axis);  
00140         const LLMatrix4& initRotation(const F32 roll, const F32 pitch, const F32 yaw);          
00141         const LLMatrix4& initRotation(const LLQuaternion &q);                   
00142         
00143         
00144         const LLMatrix4& initMatrix(const LLMatrix3 &mat); 
00145         const LLMatrix4& initMatrix(const LLMatrix3 &mat, const LLVector4 &translation);
00146 
00147         
00148         
00149         const LLMatrix4& initRotTrans(const F32 angle,
00150                                                                   const F32 rx, const F32 ry, const F32 rz,
00151                                                                   const F32 px, const F32 py, const F32 pz);
00152 
00153         const LLMatrix4& initRotTrans(const F32 angle, const LLVector3 &axis, const LLVector3 &translation);     
00154         const LLMatrix4& initRotTrans(const F32 roll, const F32 pitch, const F32 yaw, const LLVector4 &pos); 
00155         const LLMatrix4& initRotTrans(const LLQuaternion &q, const LLVector4 &pos);     
00156 
00157 
00158         
00159         const LLMatrix4& initAll(const LLVector3 &scale, const LLQuaternion &q, const LLVector3 &pos);  
00160 
00161 
00163         
00164         
00165         
00166 
00167         const LLMatrix4& setTranslation(const F32 x, const F32 y, const F32 z); 
00168 
00169         void setFwdRow(const LLVector3 &row);
00170         void setLeftRow(const LLVector3 &row);
00171         void setUpRow(const LLVector3 &row);
00172 
00173         void setFwdCol(const LLVector3 &col);
00174         void setLeftCol(const LLVector3 &col);
00175         void setUpCol(const LLVector3 &col);
00176 
00177         const LLMatrix4& setTranslation(const LLVector4 &translation);
00178         const LLMatrix4& setTranslation(const LLVector3 &translation);
00179 
00181         
00182         
00183         
00184 
00185         F32                      determinant(void) const;                                               
00186         LLQuaternion quaternion(void) const;                    
00187 
00188         LLVector4 getFwdRow4() const;
00189         LLVector4 getLeftRow4() const;
00190         LLVector4 getUpRow4() const;
00191 
00192         LLMatrix3 getMat3() const;
00193 
00194         const LLVector3& getTranslation() const { return *(LLVector3*)&mMatrix[3][0]; }
00195 
00197         
00198         
00199         
00200 
00201         const LLMatrix4& transpose();                                           
00202         const LLMatrix4& invert();                                              
00203 
00204         
00205         
00206         const LLMatrix4& rotate(const F32 angle, const F32 x, const F32 y, const F32 z);                
00207         const LLMatrix4& rotate(const F32 angle, const LLVector4 &vec);         
00208         const LLMatrix4& rotate(const F32 roll, const F32 pitch, const F32 yaw);                
00209         const LLMatrix4& rotate(const LLQuaternion &q);                         
00210 
00211         
00212         
00213         const LLMatrix4& translate(const LLVector3 &vec);                               
00214         
00215 
00216 
00217 
00219         
00220         
00221         
00222 
00223 
00224 
00225 
00226 
00227         friend LLVector4 operator*(const LLVector4 &a, const LLMatrix4 &b);             
00228         friend LLVector3 operator*(const LLVector3 &a, const LLMatrix4 &b);             
00229         friend LLVector4 rotate_vector(const LLVector4 &a, const LLMatrix4 &b); 
00230         friend LLVector3 rotate_vector(const LLVector3 &a, const LLMatrix4 &b); 
00231 
00232         friend bool operator==(const LLMatrix4 &a, const LLMatrix4 &b);                 
00233         friend bool operator!=(const LLMatrix4 &a, const LLMatrix4 &b);                 
00234 
00235         friend const LLMatrix4& operator+=(LLMatrix4 &a, const LLMatrix4 &b);   
00236         friend const LLMatrix4& operator-=(LLMatrix4 &a, const LLMatrix4 &b);   
00237         friend const LLMatrix4& operator*=(LLMatrix4 &a, const LLMatrix4 &b);   
00238         friend const LLMatrix4& operator*=(LLMatrix4 &a, const F32 &b);                 
00239 
00240         friend std::ostream&     operator<<(std::ostream& s, const LLMatrix4 &a);       
00241 };
00242 
00243 
00244 inline LLMatrix4::LLMatrix4()
00245 {
00246         identity();
00247 }
00248 
00249 inline const LLMatrix4& LLMatrix4::identity()
00250 {
00251         mMatrix[0][0] = 1.f;
00252         mMatrix[0][1] = 0.f;
00253         mMatrix[0][2] = 0.f;
00254         mMatrix[0][3] = 0.f;
00255 
00256         mMatrix[1][0] = 0.f;
00257         mMatrix[1][1] = 1.f;
00258         mMatrix[1][2] = 0.f;
00259         mMatrix[1][3] = 0.f;
00260 
00261         mMatrix[2][0] = 0.f;
00262         mMatrix[2][1] = 0.f;
00263         mMatrix[2][2] = 1.f;
00264         mMatrix[2][3] = 0.f;
00265 
00266         mMatrix[3][0] = 0.f;
00267         mMatrix[3][1] = 0.f;
00268         mMatrix[3][2] = 0.f;
00269         mMatrix[3][3] = 1.f;
00270         return (*this);
00271 }
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 inline const LLMatrix4& operator*=(LLMatrix4 &a, const LLMatrix4 &b)
00295 {
00296         U32             i, j;
00297         LLMatrix4       mat;
00298         for (i = 0; i < NUM_VALUES_IN_MAT4; i++)
00299         {
00300                 for (j = 0; j < NUM_VALUES_IN_MAT4; j++)
00301                 {
00302                         mat.mMatrix[j][i] = a.mMatrix[j][0] * b.mMatrix[0][i] + 
00303                                                             a.mMatrix[j][1] * b.mMatrix[1][i] + 
00304                                                             a.mMatrix[j][2] * b.mMatrix[2][i] +
00305                                                                 a.mMatrix[j][3] * b.mMatrix[3][i];
00306                 }
00307         }
00308         a = mat;
00309         return a;
00310 }
00311 
00312 inline const LLMatrix4& operator*=(LLMatrix4 &a, const F32 &b)
00313 {
00314         U32             i, j;
00315         LLMatrix4       mat;
00316         for (i = 0; i < NUM_VALUES_IN_MAT4; i++)
00317         {
00318                 for (j = 0; j < NUM_VALUES_IN_MAT4; j++)
00319                 {
00320                         mat.mMatrix[j][i] = a.mMatrix[j][i] * b;
00321                 }
00322         }
00323         a = mat;
00324         return a;
00325 }
00326 
00327 inline const LLMatrix4& operator+=(LLMatrix4 &a, const LLMatrix4 &b)
00328 {
00329         LLMatrix4 mat;
00330         U32             i, j;
00331         for (i = 0; i < NUM_VALUES_IN_MAT4; i++)
00332         {
00333                 for (j = 0; j < NUM_VALUES_IN_MAT4; j++)
00334                 {
00335                         mat.mMatrix[j][i] = a.mMatrix[j][i] + b.mMatrix[j][i];
00336                 }
00337         }
00338         a = mat;
00339         return a;
00340 }
00341 
00342 inline const LLMatrix4& operator-=(LLMatrix4 &a, const LLMatrix4 &b)
00343 {
00344         LLMatrix4 mat;
00345         U32             i, j;
00346         for (i = 0; i < NUM_VALUES_IN_MAT4; i++)
00347         {
00348                 for (j = 0; j < NUM_VALUES_IN_MAT4; j++)
00349                 {
00350                         mat.mMatrix[j][i] = a.mMatrix[j][i] - b.mMatrix[j][i];
00351                 }
00352         }
00353         a = mat;
00354         return a;
00355 }
00356 
00357 #endif
00358 
00359 
00360