/* --------------------------------------------------------------------- This file is part of Sandra Engine (real-time 3D engine) Copyright (C) 2004 Jesus Gumbau Portales This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt. --------------------------------------------------------------------- */ // 21-02-2001 // Jesus Gumbau Portales #ifndef __TMATRIX_H__ #define __TMATRIX_H__ #include "Vector.h" #define MATRIX_WORLD_SPACE 0 #define MATRIX_OBJECT_SPACE 1 class RotMatrix { private: Vector col[3]; float tempmat[16]; public: RotMatrix(void){ col[0].x=1.0f; col[1].x=0.0f; col[2].x=0.0f; col[0].y=0.0f; col[1].y=1.0f; col[2].y=0.0f; col[0].z=0.0f; col[1].z=0.0f; col[2].z=1.0f; } RotMatrix(const RotMatrix &t){ col[0]=t.col[0]; col[1]=t.col[1]; col[2]=t.col[2]; } void SetBaseX(float v[3]){ col[0].x=v[0]; col[0].y=v[1]; col[0].z=v[2]; } void SetBaseY(float v[3]){ col[1].x=v[0]; col[1].y=v[1]; col[1].z=v[2]; } void SetBaseZ(float v[3]){ col[2].x=v[0]; col[2].y=v[1]; col[2].z=v[2]; } void SetBaseX(float x, float y, float z){ col[0].x=x; col[0].y=y; col[0].z=z; } void SetBaseY(float x, float y, float z){ col[1].x=x; col[1].y=y; col[1].z=z; } void SetBaseZ(float x, float y, float z){ col[2].x=x; col[2].y=y; col[2].z=z; } void SetBaseX(const Vector &v){ col[0].x=v.x; col[0].y=v.y; col[0].z=v.z; } void SetBaseY(const Vector &v){ col[1].x=v.x; col[1].y=v.y; col[1].z=v.z; } void SetBaseZ(const Vector &v){ col[2].x=v.x; col[2].y=v.y; col[2].z=v.z; } Vector GetBaseX(void) const { return col[0]; } Vector GetBaseY(void) const { return col[1]; } Vector GetBaseZ(void) const { return col[2]; } // RotMatrix x Vector Vector operator*(const Vector &v) const { return Vector(col[0].x*v.x + col[1].x*v.y + col[2].x*v.z, col[0].y*v.x + col[1].y*v.y + col[2].y*v.z, col[0].z*v.x + col[1].z*v.y + col[2].z*v.z); } Vector &operator[](unsigned int column){ return ((Vector&)(col[column])); } const Vector &operator[](unsigned int column) const { return ((Vector&)(col[column])); } // RotMatrix - RotMatrix RotMatrix operator-(const RotMatrix &tm) const { RotMatrix res; for (unsigned int c=0; c<3; c++) for (unsigned int f=0; f<3; f++) res[c][f]=col[c][f]-tm[c][f]; return res; } // RotMatrix * RotMatrix RotMatrix operator*(const RotMatrix &tm) const { RotMatrix res; for (unsigned int c=0; c<3; c++) for (unsigned int f=0; f<3; f++) { float ff=0.0f; ff+=float(col[0][f]*tm[c][0]); ff+=float(col[1][f]*tm[c][1]); ff+=float(col[2][f]*tm[c][2]); res[c][f]=ff; } return res; } void PostMultiply(const RotMatrix &tm){ RotMatrix res; for (unsigned int f=0; f<3; f++) for (unsigned int c=0; c<3; c++) { float ff=0.0f; ff+=float(col[0][c]*tm[f][0]); ff+=float(col[1][c]*tm[f][1]); ff+=float(col[2][c]*tm[f][2]); res[c][f]=ff; } (*this)=res; } const RotMatrix & operator=(const RotMatrix &tm){ col[0]=tm.col[0]; col[1]=tm.col[1]; col[2]=tm.col[2]; return tm; } bool operator!=(const RotMatrix &tm) const { if (col[0]!=tm[0]) return false; if (col[1]!=tm[1]) return false; if (col[2]!=tm[2]) return false; return true; } void LoadIdentity(void){ col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f; col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f; col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f; } // returns a mat[16] in opengl conform format // WARNING: returns a dir to a TEMP variable float *GetOpenGLConform(void){ tempmat[0]=float(col[0][0]); tempmat[1]=float(col[0][1]); tempmat[2]=float(col[0][2]); tempmat[3]=0.0f; tempmat[4]=float(col[1][0]); tempmat[5]=float(col[1][1]); tempmat[6]=float(col[1][2]); tempmat[7]=0.0f; tempmat[8]=float(col[2][0]); tempmat[9]=float(col[2][1]); tempmat[10]=float(col[2][2]); tempmat[11]=0.0f; tempmat[12]=0.0f; tempmat[13]=0.0f; tempmat[14]=0.0f; tempmat[15]=1.0f; return tempmat; } // sets the matrix to a rotation matrix void LoadRotationXMatrix(float angle){ float cosa=cosf(GradToRad(angle)); float sina=sinf(GradToRad(angle)); col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f; col[0][1]=0.0f; col[1][1]=cosa; col[2][1]=-sina; col[0][2]=0.0f; col[1][2]=sina; col[2][2]=cosa; } void LoadRotationYMatrix(float angle){ float cosa=cosf(GradToRad(angle)); float sina=sinf(GradToRad(angle)); col[0][0]=cosa; col[1][0]=0.0f; col[2][0]=sina; col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f; col[0][2]=-sina;col[1][2]=0.0f; col[2][2]=cosa; } void LoadRotationZMatrix(float angle){ float cosa=cosf(GradToRad(angle)); float sina=sinf(GradToRad(angle)); col[0][0]=cosa; col[1][0]=-sina;col[2][0]=0.0f; col[0][1]=sina; col[1][1]=cosa; col[2][1]=0.0f; col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f; } // Rotate the object around X axis void RotateX(float angle, int mode=MATRIX_OBJECT_SPACE){ RotMatrix tr; tr.LoadRotationXMatrix(angle); // FIXME!!!!! -angle?!?! if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*tr ); else operator=( tr*(*this) ); } void RotateY(float angle, int mode=MATRIX_OBJECT_SPACE){ RotMatrix tr; tr.LoadRotationYMatrix(angle); if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*tr ); else operator=( tr*(*this) ); } void RotateZ(float angle, int mode=MATRIX_OBJECT_SPACE){ RotMatrix tr; tr.LoadRotationZMatrix(angle); if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*tr ); else operator=( tr*(*this) ); } void RotateAxis(float angle, Vector axis, int mode=MATRIX_OBJECT_SPACE){ RotMatrix Rx,Ry,Rz,iRx,iRy; float d = (float)sqrt(axis.y*axis.y + axis.z*axis.z); Rx.SetBaseX(1.0f, 0.0f, 0.0f); Rx.SetBaseY(0.0f, float(axis.z/d), float(axis.y/d)); Rx.SetBaseZ(0.0f, float(-axis.y/d), float(axis.z/d)); Ry.SetBaseX(d, 0.0f, float(axis.x)); Ry.SetBaseY(0.0f, 1.0f, 0.0f); Ry.SetBaseZ(float(-axis.x), 0.0f, d); Rz.LoadRotationZMatrix(angle); iRx = Rx; iRx.Traspose(); iRy = Ry; iRy.Traspose(); RotMatrix accum; accum.LoadIdentity(); if (d!=0) accum = accum * iRx; accum = accum * iRy; accum = accum * Rz; accum = accum * Ry; if (d!=0) accum = accum * Rx; if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*accum ); else operator=( accum*(*this) ); } void Traspose(void){ tempmat[0]=float(col[0][0]); tempmat[1]=float(col[0][1]); tempmat[2]=float(col[0][2]); tempmat[3]=float(col[1][0]); tempmat[4]=float(col[1][1]); tempmat[5]=float(col[1][2]); tempmat[6]=float(col[2][0]); tempmat[7]=float(col[2][1]); tempmat[8]=float(col[2][2]); col[0].x=tempmat[0]; col[1].x=tempmat[1]; col[2].x=tempmat[2]; col[0].y=tempmat[4]; col[1].y=tempmat[4]; col[2].y=tempmat[5]; col[0].z=tempmat[6]; col[1].z=tempmat[7]; col[2].z=tempmat[8]; } // This function inverts (only) a valid ROTRANSFORM Matrix (with unitary vector lengths). This should not work on arbitrary matrices void Invert(void){ Traspose(); } }; class TMatrix { private: Vector col[4]; float tempmat[16]; public: TMatrix(void){ col[0].x=1.0f; col[1].x=0.0f; col[2].x=0.0f; col[3].x=0.0f; col[0].y=0.0f; col[1].y=1.0f; col[2].y=0.0f; col[3].y=0.0f; col[0].z=0.0f; col[1].z=0.0f; col[2].z=1.0f; col[3].z=0.0f; col[0].w=0.0f; col[1].w=0.0f; col[2].w=0.0f; col[3].w=1.0f; } TMatrix(const TMatrix &t){ col[0]=t.col[0]; col[1]=t.col[1]; col[2]=t.col[2]; col[3]=t.col[3]; } TMatrix(const Vector &tr, const RotMatrix &rot, const Vector &scale){ col[0] = rot.GetBaseX()*scale.x; col[1] = rot.GetBaseY()*scale.y; col[2] = rot.GetBaseZ()*scale.z; col[3] = tr; col[0].w=0.0f; col[1].w=0.0f; col[2].w=0.0f; col[3].w=1.0f; } void SetBaseX(float v[3]){ col[0].x=v[0]; col[0].y=v[1]; col[0].z=v[2]; } void SetBaseY(float v[3]){ col[1].x=v[0]; col[1].y=v[1]; col[1].z=v[2]; } void SetBaseZ(float v[3]){ col[2].x=v[0]; col[2].y=v[1]; col[2].z=v[2]; } void SetPoint(float v[3]){ col[3].x=v[0]; col[3].y=v[1]; col[3].z=v[2]; } void SetBaseX(float x, float y, float z){ col[0].x=x; col[0].y=y; col[0].z=z; } void SetBaseY(float x, float y, float z){ col[1].x=x; col[1].y=y; col[1].z=z; } void SetBaseZ(float x, float y, float z){ col[2].x=x; col[2].y=y; col[2].z=z; } void SetPoint(float x, float y, float z){ col[3].x=x; col[3].y=y; col[3].z=z; } void SetBaseX(const Vector &v){ col[0].x=v.x; col[0].y=v.y; col[0].z=v.z; } void SetBaseY(const Vector &v){ col[1].x=v.x; col[1].y=v.y; col[1].z=v.z; } void SetBaseZ(const Vector &v){ col[2].x=v.x; col[2].y=v.y; col[2].z=v.z; } void SetPoint(const Vector &v){ col[3].x=v.x; col[3].y=v.y; col[3].z=v.z; } Vector GetBaseX(void) const { return col[0]; } Vector GetBaseY(void) const { return col[1]; } Vector GetBaseZ(void) const { return col[2]; } // TMatrix x Vector Vector operator*(const Vector &v) const { return Vector(col[0].x*v.x + col[1].x*v.y + col[2].x*v.z + col[3].x*v.w, col[0].y*v.x + col[1].y*v.y + col[2].y*v.z + col[3].y*v.w, col[0].z*v.x + col[1].z*v.y + col[2].z*v.z + col[3].z*v.w, col[0].w*v.x + col[1].w*v.y + col[2].w*v.z + col[3].w*v.w); } Vector &operator[](unsigned int column){ return ((Vector&)(col[column])); } const Vector &operator[](unsigned int column) const { return ((Vector&)(col[column])); } // TMatrix - TMatrix TMatrix operator-(const TMatrix &tm) const { TMatrix res; for (unsigned int c=0; c<4; c++) for (unsigned int f=0; f<4; f++) res[c][f]=col[c][f]-tm[c][f]; return res; } // TMatrix * TMatrix TMatrix operator*(const TMatrix &tm) const { TMatrix res; for (unsigned int c=0; c<4; c++) for (unsigned int f=0; f<4; f++) { float ff=0.0f; ff+=float(col[0][f]*tm[c][0]); ff+=float(col[1][f]*tm[c][1]); ff+=float(col[2][f]*tm[c][2]); ff+=float(col[3][f]*tm[c][3]); res[c][f]=ff; } return res; } void PostMultiply(const TMatrix &tm){ TMatrix res; for (unsigned int f=0; f<4; f++) for (unsigned int c=0; c<4; c++) { float ff=0.0f; ff+=float(col[0][c]*tm[f][0]); ff+=float(col[1][c]*tm[f][1]); ff+=float(col[2][c]*tm[f][2]); ff+=float(col[3][c]*tm[f][3]); res[c][f]=ff; } (*this)=res; } const TMatrix & operator=(const TMatrix &tm){ col[0]=tm.col[0]; col[1]=tm.col[1]; col[2]=tm.col[2]; col[3]=tm.col[3]; return tm; } bool operator!=(const TMatrix &tm) const { if (col[0]!=tm[0]) return false; if (col[1]!=tm[1]) return false; if (col[2]!=tm[2]) return false; if (col[3]!=tm[3]) return false; return true; } void LoadIdentity(void){ col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f; col[3][0]=0.0f; col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f; col[3][1]=0.0f; col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f; col[3][2]=0.0f; col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f; } // returns a mat[16] in opengl conform format // WARNING: returns a dir to a TEMP variable float *GetOpenGLConform(void){ tempmat[0]=float(col[0][0]); tempmat[1]=float(col[0][1]); tempmat[2]=float(col[0][2]); tempmat[3]=float(col[0][3]); tempmat[4]=float(col[1][0]); tempmat[5]=float(col[1][1]); tempmat[6]=float(col[1][2]); tempmat[7]=float(col[1][3]); tempmat[8]=float(col[2][0]); tempmat[9]=float(col[2][1]); tempmat[10]=float(col[2][2]); tempmat[11]=float(col[2][3]); tempmat[12]=float(col[3][0]); tempmat[13]=float(col[3][1]); tempmat[14]=float(col[3][2]); tempmat[15]=float(col[3][3]); return tempmat; } void ImportOpenGLMatrix(float *oglmatrix){ col[0][0]=oglmatrix[0]; col[0][1]=oglmatrix[1]; col[0][2]=oglmatrix[2]; col[0][3]=oglmatrix[3]; col[1][0]=oglmatrix[4]; col[1][1]=oglmatrix[5]; col[1][2]=oglmatrix[6]; col[1][3]=oglmatrix[7]; col[2][0]=oglmatrix[8]; col[2][1]=oglmatrix[9]; col[2][2]=oglmatrix[10]; col[2][3]=oglmatrix[11]; col[3][0]=oglmatrix[12]; col[3][1]=oglmatrix[13]; col[3][2]=oglmatrix[14]; col[3][3]=oglmatrix[15]; } /* // REVISAR ESTO Vector GetAbsolutePos(void) const { return (operator*(col[3])); } */ Vector GetTranslation(void) const { return col[3]; } // sets the matrix to a rotation matrix void LoadRotationXMatrix(float angle){ float cosa=cosf(GradToRad(angle)); float sina=sinf(GradToRad(angle)); col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f; col[3][0]=0.0f; col[0][1]=0.0f; col[1][1]=cosa; col[2][1]=-sina;col[3][1]=0.0f; col[0][2]=0.0f; col[1][2]=sina; col[2][2]=cosa; col[3][2]=0.0f; col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f; } void LoadRotationYMatrix(float angle){ float cosa=cosf(GradToRad(angle)); float sina=sinf(GradToRad(angle)); col[0][0]=cosa; col[1][0]=0.0f; col[2][0]=sina; col[3][0]=0.0f; col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f; col[3][1]=0.0f; col[0][2]=-sina;col[1][2]=0.0f; col[2][2]=cosa; col[3][2]=0.0f; col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f; } void LoadRotationZMatrix(float angle){ float cosa=cosf(GradToRad(angle)); float sina=sinf(GradToRad(angle)); col[0][0]=cosa; col[1][0]=-sina;col[2][0]=0.0f; col[3][0]=0.0f; col[0][1]=sina; col[1][1]=cosa; col[2][1]=0.0f; col[3][1]=0.0f; col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f; col[3][2]=0.0f; col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f; } // Rotate the object around X axis void RotateX(float angle, int mode=MATRIX_OBJECT_SPACE){ TMatrix tr; tr.LoadRotationXMatrix(angle); // FIXME!!!!! -angle?!?! if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*tr ); else operator=( tr*(*this) ); } void RotateY(float angle, int mode=MATRIX_OBJECT_SPACE){ TMatrix tr; tr.LoadRotationYMatrix(angle); if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*tr ); else operator=( tr*(*this) ); } void RotateZ(float angle, int mode=MATRIX_OBJECT_SPACE){ TMatrix tr; tr.LoadRotationZMatrix(angle); if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*tr ); else operator=( tr*(*this) ); } void RotateAxis(float angle, Vector axis, int mode=MATRIX_OBJECT_SPACE){ TMatrix Rx,Ry,Rz,iRx,iRy; float d = (float)sqrt(axis.y*axis.y + axis.z*axis.z); Rx.SetBaseX(1.0f, 0.0f, 0.0f); Rx.SetBaseY(0.0f, float(axis.z/d), float(axis.y/d)); Rx.SetBaseZ(0.0f, float(-axis.y/d), float(axis.z/d)); Ry.SetBaseX(d, 0.0f, float(axis.x)); Ry.SetBaseY(0.0f, 1.0f, 0.0f); Ry.SetBaseZ(float(-axis.x), 0.0f, d); Rz.LoadRotationZMatrix(angle); iRx = Rx; iRx.Traspose(); iRy = Ry; iRy.Traspose(); TMatrix accum; accum.LoadIdentity(); if (d!=0) accum = accum * iRx; accum = accum * iRy; accum = accum * Rz; accum = accum * Ry; if (d!=0) accum = accum * Rx; if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*accum ); else operator=( accum*(*this) ); } void Translate(Vector tv, int mode=MATRIX_OBJECT_SPACE){ TMatrix tr; tr.col[3]=tv; if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*tr ); else operator=( tr*(*this) ); } void Scale(Vector sv, int mode=MATRIX_OBJECT_SPACE){ if (mode==MATRIX_OBJECT_SPACE) operator=( (*this)*ScaleMatrix(sv) ); else operator=( ScaleMatrix(sv)*(*this) ); } static TMatrix ScaleMatrix(const Vector &v){ TMatrix mat; mat[0].x = v.x; mat[1].y = v.y; mat[2].z = v.z; return mat; } static TMatrix TranslationMatrix(const Vector &v){ TMatrix mat; mat[3] = v; mat[3].w = 1.0f; return mat; } void Traspose(void){ tempmat[0]=float(col[0][0]); tempmat[1]=float(col[0][1]); tempmat[2]=float(col[0][2]); tempmat[3]=float(col[0][3]); tempmat[4]=float(col[1][0]); tempmat[5]=float(col[1][1]); tempmat[6]=float(col[1][2]); tempmat[7]=float(col[1][3]); tempmat[8]=float(col[2][0]); tempmat[9]=float(col[2][1]); tempmat[10]=float(col[2][2]); tempmat[11]=float(col[2][3]); tempmat[12]=float(col[3][0]); tempmat[13]=float(col[3][1]); tempmat[14]=float(col[3][2]); tempmat[15]=float(col[3][3]); col[0].x=tempmat[0]; col[1].x=tempmat[1]; col[2].x=tempmat[2]; col[3].x=tempmat[3]; col[0].y=tempmat[4]; col[1].y=tempmat[5]; col[2].y=tempmat[6]; col[3].y=tempmat[7]; col[0].z=tempmat[8]; col[1].z=tempmat[9]; col[2].z=tempmat[10]; col[3].z=tempmat[11]; col[0].w=tempmat[12]; col[1].w=tempmat[13]; col[2].w=tempmat[14]; col[3].w=tempmat[15]; } // This function inverts a (only) TRANSFORM Matrix. This should not work on arbitrary matrices void Invert(void){ tempmat[0]=float(col[0][0]); tempmat[1]=float(col[0][1]); tempmat[2]=float(col[0][2]); tempmat[3]=0.0f; tempmat[4]=float(col[1][0]); tempmat[5]=float(col[1][1]); tempmat[6]=float(col[1][2]); tempmat[7]=0.0f; tempmat[8]=float(col[2][0]); tempmat[9]=float(col[2][1]); tempmat[10]=float(col[2][2]); tempmat[11]=0.0f; tempmat[12]=float(-(col[3][0]*col[0][0])-(col[3][1]*col[0][1])-(col[3][2]*col[0][2])); tempmat[13]=float(-(col[3][0]*col[1][0])-(col[3][1]*col[1][1])-(col[3][2]*col[1][2])); tempmat[14]=float(-(col[3][0]*col[2][0])-(col[3][1]*col[2][1])-(col[3][2]*col[2][2])); tempmat[15]=1.0f; col[0].x=tempmat[0]; col[1].x=tempmat[1]; col[2].x=tempmat[2]; col[3].x=tempmat[12]; col[0].y=tempmat[4]; col[1].y=tempmat[5]; col[2].y=tempmat[6]; col[3].y=tempmat[13]; col[0].z=tempmat[8]; col[1].z=tempmat[9]; col[2].z=tempmat[10]; col[3].z=tempmat[14]; col[0].w=0.0f; col[1].w=0.0f; col[2].w=0.0f; col[3].w=tempmat[15]; } }; #endif