Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgreMatrix4.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __Matrix4__
00026 #define __Matrix4__
00027 
00028 // Precompiler options
00029 #include "OgrePrerequisites.h"
00030 
00031 #include "OgreVector3.h"
00032 #include "OgreMatrix3.h"
00033 #include "OgreVector4.h"
00034 #include "OgrePlane.h"
00035 namespace Ogre
00036 {
00069     class _OgreExport Matrix4
00070     {
00071     protected:
00073         union {
00074             Real m[4][4];
00075             Real _m[16];
00076         };
00077     public:
00082         inline Matrix4()
00083         {
00084         }
00085 
00086         inline Matrix4(
00087             Real m00, Real m01, Real m02, Real m03,
00088             Real m10, Real m11, Real m12, Real m13,
00089             Real m20, Real m21, Real m22, Real m23,
00090             Real m30, Real m31, Real m32, Real m33 )
00091         {
00092             m[0][0] = m00;
00093             m[0][1] = m01;
00094             m[0][2] = m02;
00095             m[0][3] = m03;
00096             m[1][0] = m10;
00097             m[1][1] = m11;
00098             m[1][2] = m12;
00099             m[1][3] = m13;
00100             m[2][0] = m20;
00101             m[2][1] = m21;
00102             m[2][2] = m22;
00103             m[2][3] = m23;
00104             m[3][0] = m30;
00105             m[3][1] = m31;
00106             m[3][2] = m32;
00107             m[3][3] = m33;
00108         }
00109 
00113         inline Matrix4(const Matrix3& m3x3)
00114         {
00115           operator=(IDENTITY);
00116           operator=(m3x3);
00117         }
00118 
00122         inline Matrix4(const Quaternion& rot)
00123         {
00124           Matrix3 m3x3;
00125           rot.ToRotationMatrix(m3x3);
00126           operator=(IDENTITY);
00127           operator=(m3x3);
00128         }
00129         
00130 
00131         inline Real* operator [] ( size_t iRow )
00132         {
00133             assert( iRow < 4 );
00134             return m[iRow];
00135         }
00136 
00137         inline const Real *const operator [] ( size_t iRow ) const
00138         {
00139             assert( iRow < 4 );
00140             return m[iRow];
00141         }
00142 
00143         inline Matrix4 concatenate(const Matrix4 &m2) const
00144         {
00145             Matrix4 r;
00146             r.m[0][0] = m[0][0] * m2.m[0][0] + m[0][1] * m2.m[1][0] + m[0][2] * m2.m[2][0] + m[0][3] * m2.m[3][0];
00147             r.m[0][1] = m[0][0] * m2.m[0][1] + m[0][1] * m2.m[1][1] + m[0][2] * m2.m[2][1] + m[0][3] * m2.m[3][1];
00148             r.m[0][2] = m[0][0] * m2.m[0][2] + m[0][1] * m2.m[1][2] + m[0][2] * m2.m[2][2] + m[0][3] * m2.m[3][2];
00149             r.m[0][3] = m[0][0] * m2.m[0][3] + m[0][1] * m2.m[1][3] + m[0][2] * m2.m[2][3] + m[0][3] * m2.m[3][3];
00150 
00151             r.m[1][0] = m[1][0] * m2.m[0][0] + m[1][1] * m2.m[1][0] + m[1][2] * m2.m[2][0] + m[1][3] * m2.m[3][0];
00152             r.m[1][1] = m[1][0] * m2.m[0][1] + m[1][1] * m2.m[1][1] + m[1][2] * m2.m[2][1] + m[1][3] * m2.m[3][1];
00153             r.m[1][2] = m[1][0] * m2.m[0][2] + m[1][1] * m2.m[1][2] + m[1][2] * m2.m[2][2] + m[1][3] * m2.m[3][2];
00154             r.m[1][3] = m[1][0] * m2.m[0][3] + m[1][1] * m2.m[1][3] + m[1][2] * m2.m[2][3] + m[1][3] * m2.m[3][3];
00155 
00156             r.m[2][0] = m[2][0] * m2.m[0][0] + m[2][1] * m2.m[1][0] + m[2][2] * m2.m[2][0] + m[2][3] * m2.m[3][0];
00157             r.m[2][1] = m[2][0] * m2.m[0][1] + m[2][1] * m2.m[1][1] + m[2][2] * m2.m[2][1] + m[2][3] * m2.m[3][1];
00158             r.m[2][2] = m[2][0] * m2.m[0][2] + m[2][1] * m2.m[1][2] + m[2][2] * m2.m[2][2] + m[2][3] * m2.m[3][2];
00159             r.m[2][3] = m[2][0] * m2.m[0][3] + m[2][1] * m2.m[1][3] + m[2][2] * m2.m[2][3] + m[2][3] * m2.m[3][3];
00160 
00161             r.m[3][0] = m[3][0] * m2.m[0][0] + m[3][1] * m2.m[1][0] + m[3][2] * m2.m[2][0] + m[3][3] * m2.m[3][0];
00162             r.m[3][1] = m[3][0] * m2.m[0][1] + m[3][1] * m2.m[1][1] + m[3][2] * m2.m[2][1] + m[3][3] * m2.m[3][1];
00163             r.m[3][2] = m[3][0] * m2.m[0][2] + m[3][1] * m2.m[1][2] + m[3][2] * m2.m[2][2] + m[3][3] * m2.m[3][2];
00164             r.m[3][3] = m[3][0] * m2.m[0][3] + m[3][1] * m2.m[1][3] + m[3][2] * m2.m[2][3] + m[3][3] * m2.m[3][3];
00165 
00166             return r;
00167         }
00168 
00171         inline Matrix4 operator * ( const Matrix4 &m2 ) const
00172         {
00173             return concatenate( m2 );
00174         }
00175 
00185         inline Vector3 operator * ( const Vector3 &v ) const
00186         {
00187             Vector3 r;
00188 
00189             Real fInvW = 1.0 / ( m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] );
00190 
00191             r.x = ( m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] ) * fInvW;
00192             r.y = ( m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] ) * fInvW;
00193             r.z = ( m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] ) * fInvW;
00194 
00195             return r;
00196         }
00197         inline Vector4 operator * (const Vector4& v) const
00198         {
00199             return Vector4(
00200                 m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w, 
00201                 m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
00202                 m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
00203                 m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w
00204                 );
00205         }
00206         inline Plane operator * (const Plane& p) const
00207         {
00208             Plane ret;
00209             Matrix4 invTrans = inverse().transpose();
00210             ret.normal.x =
00211                 invTrans[0][0] * p.normal.x + invTrans[0][1] * p.normal.y + invTrans[0][2] * p.normal.z;
00212             ret.normal.y = 
00213                 invTrans[1][0] * p.normal.x + invTrans[1][1] * p.normal.y + invTrans[1][2] * p.normal.z;
00214             ret.normal.z = 
00215                 invTrans[2][0] * p.normal.x + invTrans[2][1] * p.normal.y + invTrans[2][2] * p.normal.z;
00216             Vector3 pt = p.normal * -p.d;
00217             pt = *this * pt;
00218             ret.d = - pt.dotProduct(ret.normal);
00219             return ret;
00220         }
00221 
00222 
00225         inline Matrix4 operator + ( const Matrix4 &m2 ) const
00226         {
00227             Matrix4 r;
00228 
00229             r.m[0][0] = m[0][0] + m2.m[0][0];
00230             r.m[0][1] = m[0][1] + m2.m[0][1];
00231             r.m[0][2] = m[0][2] + m2.m[0][2];
00232             r.m[0][3] = m[0][3] + m2.m[0][3];
00233 
00234             r.m[1][0] = m[1][0] + m2.m[1][0];
00235             r.m[1][1] = m[1][1] + m2.m[1][1];
00236             r.m[1][2] = m[1][2] + m2.m[1][2];
00237             r.m[1][3] = m[1][3] + m2.m[1][3];
00238 
00239             r.m[2][0] = m[2][0] + m2.m[2][0];
00240             r.m[2][1] = m[2][1] + m2.m[2][1];
00241             r.m[2][2] = m[2][2] + m2.m[2][2];
00242             r.m[2][3] = m[2][3] + m2.m[2][3];
00243 
00244             r.m[3][0] = m[3][0] + m2.m[3][0];
00245             r.m[3][1] = m[3][1] + m2.m[3][1];
00246             r.m[3][2] = m[3][2] + m2.m[3][2];
00247             r.m[3][3] = m[3][3] + m2.m[3][3];
00248 
00249             return r;
00250         }
00251 
00254         inline Matrix4 operator - ( const Matrix4 &m2 ) const
00255         {
00256             Matrix4 r;
00257             r.m[0][0] = m[0][0] - m2.m[0][0];
00258             r.m[0][1] = m[0][1] - m2.m[0][1];
00259             r.m[0][2] = m[0][2] - m2.m[0][2];
00260             r.m[0][3] = m[0][3] - m2.m[0][3];
00261 
00262             r.m[1][0] = m[1][0] - m2.m[1][0];
00263             r.m[1][1] = m[1][1] - m2.m[1][1];
00264             r.m[1][2] = m[1][2] - m2.m[1][2];
00265             r.m[1][3] = m[1][3] - m2.m[1][3];
00266 
00267             r.m[2][0] = m[2][0] - m2.m[2][0];
00268             r.m[2][1] = m[2][1] - m2.m[2][1];
00269             r.m[2][2] = m[2][2] - m2.m[2][2];
00270             r.m[2][3] = m[2][3] - m2.m[2][3];
00271 
00272             r.m[3][0] = m[3][0] - m2.m[3][0];
00273             r.m[3][1] = m[3][1] - m2.m[3][1];
00274             r.m[3][2] = m[3][2] - m2.m[3][2];
00275             r.m[3][3] = m[3][3] - m2.m[3][3];
00276 
00277             return r;
00278         }
00279 
00282         inline bool operator == ( const Matrix4& m2 ) const
00283         {
00284             if( 
00285                 m[0][0] != m2.m[0][0] || m[0][1] != m2.m[0][1] || m[0][2] != m2.m[0][2] || m[0][3] != m2.m[0][3] ||
00286                 m[1][0] != m2.m[1][0] || m[1][1] != m2.m[1][1] || m[1][2] != m2.m[1][2] || m[1][3] != m2.m[1][3] ||
00287                 m[2][0] != m2.m[2][0] || m[2][1] != m2.m[2][1] || m[2][2] != m2.m[2][2] || m[2][3] != m2.m[2][3] ||
00288                 m[3][0] != m2.m[3][0] || m[3][1] != m2.m[3][1] || m[3][2] != m2.m[3][2] || m[3][3] != m2.m[3][3] )
00289                 return false;
00290             return true;
00291         }
00292 
00295         inline bool operator != ( const Matrix4& m2 ) const
00296         {
00297             if( 
00298                 m[0][0] != m2.m[0][0] || m[0][1] != m2.m[0][1] || m[0][2] != m2.m[0][2] || m[0][3] != m2.m[0][3] ||
00299                 m[1][0] != m2.m[1][0] || m[1][1] != m2.m[1][1] || m[1][2] != m2.m[1][2] || m[1][3] != m2.m[1][3] ||
00300                 m[2][0] != m2.m[2][0] || m[2][1] != m2.m[2][1] || m[2][2] != m2.m[2][2] || m[2][3] != m2.m[2][3] ||
00301                 m[3][0] != m2.m[3][0] || m[3][1] != m2.m[3][1] || m[3][2] != m2.m[3][2] || m[3][3] != m2.m[3][3] )
00302                 return true;
00303             return false;
00304         }
00305 
00308         inline void operator = ( const Matrix3& mat3 )
00309         {
00310             m[0][0] = mat3.m[0][0]; m[0][1] = mat3.m[0][1]; m[0][2] = mat3.m[0][2];
00311             m[1][0] = mat3.m[1][0]; m[1][1] = mat3.m[1][1]; m[1][2] = mat3.m[1][2];
00312             m[2][0] = mat3.m[2][0]; m[2][1] = mat3.m[2][1]; m[2][2] = mat3.m[2][2];
00313         }
00314 
00315         inline Matrix4 transpose(void) const
00316         {
00317             return Matrix4(m[0][0], m[1][0], m[2][0], m[3][0],
00318                            m[0][1], m[1][1], m[2][1], m[3][1],
00319                            m[0][2], m[1][2], m[2][2], m[3][2],
00320                            m[0][3], m[1][3], m[2][3], m[3][3]);
00321         }
00322 
00323         /*
00324         -----------------------------------------------------------------------
00325         Translation Transformation
00326         -----------------------------------------------------------------------
00327         */
00330         inline void setTrans( const Vector3& v )
00331         {
00332             m[0][3] = v.x;
00333             m[1][3] = v.y;
00334             m[2][3] = v.z;
00335         }
00336 
00339         inline Vector3 getTrans() const
00340         {
00341           return Vector3(m[0][3], m[1][3], m[2][3]);
00342         }
00343         
00344 
00347         inline void makeTrans( const Vector3& v )
00348         {
00349             m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = 0.0; m[0][3] = v.x;
00350             m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = 0.0; m[1][3] = v.y;
00351             m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0; m[2][3] = v.z;
00352             m[3][0] = 0.0; m[3][1] = 0.0; m[3][2] = 0.0; m[3][3] = 1.0;
00353         }
00354 
00355         inline void makeTrans( Real tx, Real ty, Real tz )
00356         {
00357             m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = 0.0; m[0][3] = tx;
00358             m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = 0.0; m[1][3] = ty;
00359             m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0; m[2][3] = tz;
00360             m[3][0] = 0.0; m[3][1] = 0.0; m[3][2] = 0.0; m[3][3] = 1.0;
00361         }
00362 
00365         inline static Matrix4 getTrans( const Vector3& v )
00366         {
00367             Matrix4 r;
00368 
00369             r.m[0][0] = 1.0; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = v.x;
00370             r.m[1][0] = 0.0; r.m[1][1] = 1.0; r.m[1][2] = 0.0; r.m[1][3] = v.y;
00371             r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = 1.0; r.m[2][3] = v.z;
00372             r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
00373 
00374             return r;
00375         }
00376 
00379         inline static Matrix4 getTrans( Real t_x, Real t_y, Real t_z )
00380         {
00381             Matrix4 r;
00382 
00383             r.m[0][0] = 1.0; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = t_x;
00384             r.m[1][0] = 0.0; r.m[1][1] = 1.0; r.m[1][2] = 0.0; r.m[1][3] = t_y;
00385             r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = 1.0; r.m[2][3] = t_z;
00386             r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
00387 
00388             return r;
00389         }
00390 
00391         /*
00392         -----------------------------------------------------------------------
00393         Scale Transformation
00394         -----------------------------------------------------------------------
00395         */
00398         inline void setScale( const Vector3& v )
00399         {
00400             m[0][0] = v.x;
00401             m[1][1] = v.y;
00402             m[2][2] = v.z;
00403         }
00404 
00407         inline static Matrix4 getScale( const Vector3& v )
00408         {
00409             Matrix4 r;
00410             r.m[0][0] = v.x; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = 0.0;
00411             r.m[1][0] = 0.0; r.m[1][1] = v.y; r.m[1][2] = 0.0; r.m[1][3] = 0.0;
00412             r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = v.z; r.m[2][3] = 0.0;
00413             r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
00414 
00415             return r;
00416         }
00417 
00420         inline static Matrix4 getScale( Real s_x, Real s_y, Real s_z )
00421         {
00422             Matrix4 r;
00423             r.m[0][0] = s_x; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = 0.0;
00424             r.m[1][0] = 0.0; r.m[1][1] = s_y; r.m[1][2] = 0.0; r.m[1][3] = 0.0;
00425             r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = s_z; r.m[2][3] = 0.0;
00426             r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
00427 
00428             return r;
00429         }
00430 
00434         inline void extract3x3Matrix(Matrix3& m3x3) const
00435         {
00436             m3x3.m[0][0] = m[0][0];
00437             m3x3.m[0][1] = m[0][1];
00438             m3x3.m[0][2] = m[0][2];
00439             m3x3.m[1][0] = m[1][0];
00440             m3x3.m[1][1] = m[1][1];
00441             m3x3.m[1][2] = m[1][2];
00442             m3x3.m[2][0] = m[2][0];
00443             m3x3.m[2][1] = m[2][1];
00444             m3x3.m[2][2] = m[2][2];
00445 
00446         }
00447 
00450         inline Quaternion extractQuaternion() const
00451         {
00452           Matrix3 m3x3;
00453           extract3x3Matrix(m3x3);
00454           return Quaternion(m3x3);
00455         }
00456 
00457         static const Matrix4 ZERO;
00458         static const Matrix4 IDENTITY;
00461         static const Matrix4 CLIPSPACE2DTOIMAGESPACE;
00462 
00463         inline Matrix4 operator*(Real scalar)
00464         {
00465             return Matrix4(
00466                 scalar*m[0][0], scalar*m[0][1], scalar*m[0][2], scalar*m[0][3],
00467                 scalar*m[1][0], scalar*m[1][1], scalar*m[1][2], scalar*m[1][3],
00468                 scalar*m[2][0], scalar*m[2][1], scalar*m[2][2], scalar*m[2][3],
00469                 scalar*m[3][0], scalar*m[3][1], scalar*m[3][2], scalar*m[3][3]);
00470         }
00471 
00474         inline _OgreExport friend std::ostream& operator <<
00475             ( std::ostream& o, const Matrix4& m )
00476         {
00477             o << "Matrix4(";
00478             for (size_t i = 0; i < 4; ++i)
00479             {
00480                 o << " row" << (unsigned)i << "{";
00481                 for(size_t j = 0; j < 4; ++j)
00482                 {
00483                     o << m[i][j] << " ";
00484                 }
00485                 o << "}";
00486             }
00487             o << ")";
00488             return o;
00489         }
00490         
00491         Matrix4 adjoint() const;
00492         Real determinant() const;
00493         Matrix4 inverse() const;
00494 
00501         void makeTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation);
00502 
00508         void makeInverseTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation);
00509     };
00510 
00511     /* Removed from Vector4 and made a non-member here because otherwise
00512        OgreMatrix4.h and OgreVector4.h have to try to include and inline each 
00513        other, which frankly doesn't work ;)
00514    */
00515     inline Vector4 operator * (const Vector4& v, const Matrix4& mat)
00516     {
00517         return Vector4(
00518             v.x*mat[0][0] + v.y*mat[1][0] + v.z*mat[2][0] + v.w*mat[3][0],
00519             v.x*mat[0][1] + v.y*mat[1][1] + v.z*mat[2][1] + v.w*mat[3][1],
00520             v.x*mat[0][2] + v.y*mat[1][2] + v.z*mat[2][2] + v.w*mat[3][2],
00521             v.x*mat[0][3] + v.y*mat[1][3] + v.z*mat[2][3] + v.w*mat[3][3]
00522             );
00523     }
00524 
00525 }
00526 #endif

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 12 14:37:44 2006