Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

KX_Matrix4x4.h

Go to the documentation of this file.
00001 
00002 #ifndef KETSJI_MATRIX4X4
00003 #define KETSJI_MATRIX4X4
00004 
00005 #include "MT_Point3.h"
00006 #include "MT_Vector3.h"
00007 
00008 class CMatrix4x4
00009 {
00010 public:
00011 
00012         // creators.
00013         CMatrix4x4()                                                                                                            { Identity(); }
00014         CMatrix4x4(const float value[4][4]) {
00015                 for (int i=0;i<4;i++)
00016                         for (int j=0;j<4;j++)
00017                                 m_V[i][j] = value[i][j];
00018 
00019         }
00020         CMatrix4x4(const double value[16]) {
00021                 for (int i=0;i<16;i++)
00022                                 m_Vflat[i] = value[i];
00023 
00024         }
00025         
00026 
00027         CMatrix4x4(const CMatrix4x4 & other)                                                                    { SetMatrix(other); }
00028         CMatrix4x4(const MT_Point3 & orig,const MT_Vector3 & dir,const MT_Vector3 up)

00029         {
00030                         MT_Vector3 z = -(dir.normalized());
00031                         MT_Vector3 x = (up.cross(z)).normalized();
00032                         MT_Vector3 y = (z.cross(x));
00033 
00034         m_V[0][0] = x.x();
00035         m_V[0][1] = y.x();
00036         m_V[0][2] = z.x();
00037         m_V[0][3] = 0.0f;
00038 
00039         m_V[1][0] = x.y();
00040         m_V[1][1] = y.y();
00041         m_V[1][2] = z.y();
00042         m_V[1][3] = 0.0f;
00043 
00044         m_V[2][0] = x.z();
00045         m_V[2][1] = y.z();
00046         m_V[2][2] = z.z();
00047         m_V[2][3] = 0.0f;
00048 
00049         m_V[3][0] = orig.x();//0.0f;
00050         m_V[3][1] = orig.y();//0.0f;
00051         m_V[3][2] = orig.z();//0.0f;
00052         m_V[3][3] = 1.0f;
00053 
00054         //Translate(-orig);
00055         }
00056         // setting.
00057         void Identity()                                                                                                                 { for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { m_V[i][j] = (i==j?1.0f:0.0f); } } }
00058         void SetMatrix(const CMatrix4x4 & other)                                                                                { for (int i=0; i<16; i++) { m_Vflat[i] = other.m_Vflat[i]; } }
00059         double* getPointer() { return &m_V[0][0];}
00060         
00061         
00062         //inline CMatrix4x4&            Rotate(const MT_Vector3 & axis, float angle)                    { CMatrix4x4 tmp(*this); Multiply(tmp, CMatrix4x4(CQuaternion(axis, angle)), *this); return *this; }
00063         //CMatrix4x4&                           Translate(const MT_Vector3 & v);
00064 
00065         inline MT_Vector3                       GetRight()      const                                                                   { return MT_Vector3(m_V[0][0], m_V[0][1], m_V[0][2]); }
00066         inline MT_Vector3                       GetUp()         const                                                                   { return MT_Vector3(m_V[1][0], m_V[1][1], m_V[1][2]); }
00067         inline MT_Vector3                       GetDir()        const                                                                   { return MT_Vector3(m_V[2][0], m_V[2][1], m_V[2][2]); }
00068         inline MT_Point3                        GetPos()        const                                                                   { return MT_Point3(m_V[3][0], m_V[3][1], m_V[3][2]); }
00069         double& operator () (int row,int col) { 
00070                 return m_V[col][row];
00071         };
00072         inline void                             SetPos(const MT_Vector3 & v)                                                            { m_V[3][0] = v.x(); m_V[3][1] = v.y(); m_V[3][2] = v.z(); }
00073 
00074          static CMatrix4x4 Perspective(MT_Scalar inLeft, MT_Scalar inRight, MT_Scalar inBottom, MT_Scalar inTop, MT_Scalar inNear, MT_Scalar inFar)

00075 {
00076         
00077         CMatrix4x4 mat;
00078 
00079         // Column 0
00080         mat(0, 0) = -(2.0*inNear)                       / (inRight-inLeft);
00081         mat(1, 0) = 0;                                          
00082         mat(2, 0) = 0;                                          
00083         mat(3, 0) = 0;                                          
00084                                                                                 
00085         // Column 1                                                     
00086         mat(0, 1) = 0;                                          
00087         mat(1, 1) = (2.0*inNear)                        / (inTop-inBottom);
00088         mat(2, 1) = 0;                                          
00089         mat(3, 1) = 0;                                          
00090                                                                                 
00091         // Column 2                                                     
00092         mat(0, 2) =  (inRight+inLeft)           / (inRight-inLeft);
00093         mat(1, 2) =  (inTop+inBottom)           / (inTop-inBottom);
00094         mat(2, 2) = -(inFar+inNear)                     / (inFar-inNear);
00095         mat(3, 2) = -1;                                         
00096                                                                                 
00097         // Column 3                                                     
00098         mat(0, 3) = 0;                                          
00099         mat(1, 3) = 0;                                          
00100         mat(2, 3) = -(2.0*inFar*inNear)         / (inFar-inNear);
00101         mat(3, 3) = 0;
00102 
00103         return mat;
00104 }
00105 
00106 protected:
00107         
00108         union
00109         {
00110                 double m_V[4][4];
00111                 double m_Vflat[16];
00112         };
00113 };
00114 
00115 #endif //KETSJI_MATRIX4X4

Generated at Thu Feb 1 13:03:05 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000