Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

IMatrix4x4.h

Go to the documentation of this file.
00001 /*
00002 
00003 AmnesiaEngine
00004 Copyright (c) 2006 John DiSanti.
00005 
00006 Permission is hereby granted, free of charge,
00007 to any person obtaining a copy of this software and associated
00008 documentation files (the "Software"), to deal in the Software
00009 without restriction, including without limitation the rights to
00010 use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to
00012 whom the Software is furnished to do so, subject to the following
00013 conditions:
00014 
00015 The above copyright notice and this permission notice shall be included
00016 in all copies or substantial portions of the Software.
00017 
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00019 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
00020 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00021 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00022 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00023 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00024 
00025 */
00026 
00027 #ifndef AMN_IMATRIX4X4_H
00028 #define AMN_IMATRIX4X4_H
00029 
00030 namespace amn
00031 {
00032 
00034 class IMatrix4x4
00035 {
00036 public:
00038         IMatrix4x4()
00039         {
00040                 loadIdentity(); 
00041         }
00043         IMatrix4x4(IMatrix4x4& copy)
00044         {
00045                 setMatrix(copy);
00046         }
00047 
00049         const float* getMatrixPointer()
00050         {
00051                 return mat;
00052         }
00053 
00055         void loadIdentity()
00056         {
00057                 mat[0] = 1; mat[4] = 0;
00058                 mat[1] = 0; mat[5] = 1;
00059                 mat[2] = 0; mat[6] = 0;
00060                 mat[3] = 0; mat[7] = 0;
00061                 mat[8] = 0; mat[12]= 0;
00062                 mat[9] = 0; mat[13]= 0;
00063                 mat[10]= 1; mat[14]= 0;
00064                 mat[11]= 0; mat[16]= 1;
00065         }
00066 
00068         void setMatrix(IMatrix4x4& copy)
00069         {
00070                 int i;
00071                 for(i = 0; i < 16; i++)
00072                         mat[i] = copy.getMatrixPointer()[i];
00073         }
00074 
00076         void setMatrix(const float* copy)
00077         {
00078                 if(!copy)
00079                         return;
00080 
00081                 int i;
00082                 for(i = 0; i < 16; i++)
00083                         mat[i] = copy[i];
00084         }
00085 
00087         float getElement(unsigned short id)
00088         {
00089                 return mat[id];
00090         }
00091 
00093         float operator[] (int id)
00094         {
00095                 return mat[id];
00096         }
00097 
00099         IMatrix4x4& operator *= (IMatrix4x4& m)
00100         {
00101                 /* Example of 3x3 multiplication, remember we are doing 4x4...
00102                 [ a b c ]       [ j i h ]       [ aj+bk+cn  ai+bl+co  ah+bm+cp ]
00103                 [ d e f ]   x   [ k l m ]   =   [ dj+ek+fn  di+el+fo  dh+em+fp ]
00104                 [ g h i ]       [ n o p ]       [ gj+hk+in  gi+hl+io  gh+hm+ip ]
00105                 */
00106                 const float* c = m.getMatrixPointer();
00107 
00108                 mat[0] = mat[0] * c[0] + mat[1] * c[4] + mat[2] * c[8] + mat[3] * c[12];
00109                 mat[1] = mat[0] * c[1] + mat[1] * c[5] + mat[2] * c[9] + mat[3] * c[13];
00110                 mat[2] = mat[0] * c[2] + mat[1] * c[6] + mat[2] * c[10]+ mat[3] * c[14];
00111                 mat[3] = mat[0] * c[3] + mat[1] * c[7] + mat[2] * c[11]+ mat[3] * c[15];
00112 
00113                 mat[4] = mat[4] * c[0] + mat[5] * c[4] + mat[6] * c[8] + mat[7] * c[12];
00114                 mat[5] = mat[4] * c[1] + mat[5] * c[5] + mat[6] * c[9] + mat[7] * c[13];
00115                 mat[6] = mat[4] * c[2] + mat[5] * c[6] + mat[6] * c[10]+ mat[7] * c[14];
00116                 mat[7] = mat[4] * c[3] + mat[5] * c[7] + mat[6] * c[11]+ mat[7] * c[15];
00117 
00118                 mat[8] = mat[8] * c[0] + mat[9] * c[4] + mat[10]* c[8] + mat[11]* c[12];
00119                 mat[9] = mat[8] * c[1] + mat[9] * c[5] + mat[10]* c[9] + mat[11]* c[13];
00120                 mat[10]= mat[8] * c[2] + mat[9] * c[6] + mat[10]* c[10]+ mat[11]* c[14];
00121                 mat[11]= mat[8] * c[3] + mat[9] * c[7] + mat[10]* c[11]+ mat[11]* c[15];
00122 
00123                 mat[12]= mat[12]* c[0] + mat[13]* c[4] + mat[14]* c[8] + mat[15]* c[12];
00124                 mat[13]= mat[12]* c[1] + mat[13]* c[5] + mat[14]* c[9] + mat[15]* c[13];
00125                 mat[14]= mat[12]* c[2] + mat[13]* c[6] + mat[14]* c[10]+ mat[15]* c[14];
00126                 mat[15]= mat[12]* c[3] + mat[13]* c[7] + mat[14]* c[11]+ mat[15]* c[15];
00127 
00128                 return *this;
00129         }
00130 
00132         IMatrix4x4& operator *= (float s)
00133         {
00134                 int i;
00135                 for(i = 0; i < 16; i++)
00136                         mat[i] *= s;
00137 
00138                 return *this;
00139         }
00140 
00142         IMatrix4x4& operator += (IMatrix4x4& m)
00143         {
00144                 const float* c = m.getMatrixPointer();
00145                 int i;
00146                 for(i = 0; i < 4; i++)
00147                         mat[i] = mat[i] + c[i*4];
00148                 for(i = 4; i < 8; i++)
00149                         mat[i] = mat[i] + c[i*4];
00150                 for(i = 8; i < 12; i++)
00151                         mat[i] = mat[i] + c[i*4];
00152                 for(i = 12; i < 16; i++)
00153                         mat[i] = mat[i] + c[i*4];
00154         }
00155 
00157         IMatrix4x4& operator -= (IMatrix4x4& m)
00158         {
00159                 const float* c = m.getMatrixPointer();
00160                 int i;
00161                 for(i = 0; i < 4; i++)
00162                         mat[i] = mat[i] - c[i*4];
00163                 for(i = 4; i < 8; i++)
00164                         mat[i] = mat[i] - c[i*4];
00165                 for(i = 8; i < 12; i++)
00166                         mat[i] = mat[i] - c[i*4];
00167                 for(i = 12; i < 16; i++)
00168                         mat[i] = mat[i] - c[i*4];
00169         }
00170 
00171 private:
00172         float mat[16];
00173 };
00174 
00175 };
00176 
00177 #endif//AMN_IMATRIX4X4_H
00178 
00179 /* End of File */

Generated on Wed Feb 1 22:23:26 2006 for AmnesiaEngine by  doxygen 1.4.2