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