Blender V2.61 - r43446

MT_CmMatrix4x4.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "MT_CmMatrix4x4.h"
00034 #include "MT_Vector3.h"
00035 #include "MT_Point3.h"
00036 
00037 
00038 MT_CmMatrix4x4::MT_CmMatrix4x4()
00039 {
00040     Identity();
00041 }
00042 
00043 
00044 
00045 MT_CmMatrix4x4::MT_CmMatrix4x4(const float value[4][4])
00046 {
00047     for (int i=0;i<4;i++)
00048     {
00049         for (int j=0;j<4;j++)
00050             m_V[i][j] = value[i][j];
00051     }
00052 }
00053 
00054 
00055 
00056 MT_CmMatrix4x4::MT_CmMatrix4x4(const double value[16])
00057 {
00058     for (int i=0;i<16;i++)
00059         m_Vflat[i] = value[i];
00060 }
00061 
00062 
00063 
00064 MT_CmMatrix4x4::MT_CmMatrix4x4(const MT_CmMatrix4x4& other)
00065 {
00066     SetMatrix(other);
00067 }
00068 
00069 
00070 
00071 MT_CmMatrix4x4::MT_CmMatrix4x4(const MT_Point3& orig,
00072                              const MT_Vector3& dir,
00073                              const MT_Vector3 up)
00074 {
00075     MT_Vector3 z = -(dir.normalized());
00076     MT_Vector3 x = (up.cross(z)).normalized();
00077     MT_Vector3 y = (z.cross(x));
00078     
00079     m_V[0][0] = x.x();
00080     m_V[0][1] = y.x();
00081     m_V[0][2] = z.x();
00082     m_V[0][3] = 0.0f;
00083     
00084     m_V[1][0] = x.y();
00085     m_V[1][1] = y.y();
00086     m_V[1][2] = z.y();
00087     m_V[1][3] = 0.0f;
00088     
00089     m_V[2][0] = x.z();
00090     m_V[2][1] = y.z();
00091     m_V[2][2] = z.z();
00092     m_V[2][3] = 0.0f;
00093     
00094     m_V[3][0] = orig.x();//0.0f;
00095     m_V[3][1] = orig.y();//0.0f;
00096     m_V[3][2] = orig.z();//0.0f;
00097     m_V[3][3] = 1.0f;
00098     
00099     //Translate(-orig);
00100 }
00101 
00102 
00103 
00104 MT_Vector3 MT_CmMatrix4x4::GetRight() const
00105 {
00106     return MT_Vector3(m_V[0][0], m_V[0][1], m_V[0][2]);
00107 }
00108 
00109 
00110 
00111 MT_Vector3 MT_CmMatrix4x4::GetUp() const
00112 {
00113     return MT_Vector3(m_V[1][0], m_V[1][1], m_V[1][2]);
00114 }
00115 
00116 
00117 
00118 MT_Vector3 MT_CmMatrix4x4::GetDir() const
00119 {
00120     return MT_Vector3(m_V[2][0], m_V[2][1], m_V[2][2]);
00121 }
00122 
00123 
00124 
00125 MT_Point3 MT_CmMatrix4x4::GetPos() const
00126 {
00127     return MT_Point3(m_V[3][0], m_V[3][1], m_V[3][2]);
00128 }
00129 
00130 
00131 
00132 void MT_CmMatrix4x4::Identity()
00133 {
00134     for (int i=0; i<4; i++)
00135     {
00136         for (int j=0; j<4; j++)
00137             m_V[i][j] = (i==j?1.0f:0.0f);
00138     } 
00139 }
00140 
00141 
00142 
00143 void MT_CmMatrix4x4::SetMatrix(const MT_CmMatrix4x4& other)
00144 {
00145     for (int i=0; i<16; i++)
00146         m_Vflat[i] = other.m_Vflat[i];
00147 }
00148 
00149 
00150 
00151 double* MT_CmMatrix4x4::getPointer()
00152 {
00153     return &m_V[0][0];
00154 }
00155 
00156 
00157 
00158 const double* MT_CmMatrix4x4::getPointer() const
00159 {
00160     return &m_V[0][0];
00161 }   
00162 
00163 
00164 
00165 void MT_CmMatrix4x4::setElem(int pos,double newvalue)
00166 {
00167     m_Vflat[pos] = newvalue;
00168 }   
00169 
00170 MT_CmMatrix4x4 MT_CmMatrix4x4::Perspective(
00171     MT_Scalar inLeft,
00172     MT_Scalar inRight,
00173     MT_Scalar inBottom,
00174     MT_Scalar inTop,
00175     MT_Scalar inNear,
00176     MT_Scalar inFar
00177 ){
00178 
00179     MT_CmMatrix4x4 mat;
00180     
00181     // Column 0
00182     mat(0, 0) = -(2.0*inNear)           / (inRight-inLeft);
00183     mat(1, 0) = 0;
00184     mat(2, 0) = 0;
00185     mat(3, 0) = 0;
00186 
00187     // Column 1
00188     mat(0, 1) = 0;
00189     mat(1, 1) = (2.0*inNear)            / (inTop-inBottom);
00190     mat(2, 1) = 0;
00191     mat(3, 1) = 0;
00192 
00193     // Column 2
00194     mat(0, 2) =  (inRight+inLeft)       / (inRight-inLeft);
00195     mat(1, 2) =  (inTop+inBottom)       / (inTop-inBottom);
00196     mat(2, 2) = -(inFar+inNear)         / (inFar-inNear);
00197     mat(3, 2) = -1;
00198 
00199     // Column 3
00200     mat(0, 3) = 0;
00201     mat(1, 3) = 0;
00202     mat(2, 3) = -(2.0*inFar*inNear)     / (inFar-inNear);
00203     mat(3, 3) = 0;
00204 
00205     return mat;
00206 }