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

Matrix44.cpp

Go to the documentation of this file.
00001 
00007 #include <iomanip>
00008 #include <cmath>
00009 
00010 #include"PS2Math.h"
00011 
00012 using namespace PS2Math;
00013 
00024 Matrix44::Matrix44(bool IdentityMatrix) {
00025 
00026     if (IdentityMatrix) {
00027     
00028         memset(elem, 0, sizeof(elem));
00029         elem[0][0] = 1.0f;
00030         elem[1][1] = 1.0f;
00031         elem[2][2] = 1.0f;
00032         elem[3][3] = 1.0f;
00033     }
00034 }
00035 
00043 Matrix44::Matrix44(float Angle, MainAxis Axis) {
00044 
00045     switch (Axis) {
00046     
00047         case X_AXIS:
00048             SetRotationX(Angle);
00049         break;
00050         case Y_AXIS:
00051             SetRotationY(Angle);
00052         break;
00053         case Z_AXIS:
00054             SetRotationZ(Angle);
00055         break;
00056         default: 
00057         break;
00058     }
00059 }
00060 
00070 Matrix44 &Matrix44::SetRotationX(float Angle) {
00071 
00072     memset(elem, 0, sizeof(elem));
00073     elem[0][0] = elem[3][3] = 1;
00074     elem[2][2] = elem[1][1] = cos(Angle);
00075     elem[2][1] = sin(Angle);
00076     elem[1][2] = -elem[2][1]; 
00077     return *this;
00078 }
00079 
00089 Matrix44 &Matrix44::SetRotationY(float Angle) {
00090 
00091     memset(elem, 0, sizeof(elem));
00092     elem[1][1] = elem[3][3] = 1;
00093     elem[0][0] = elem[2][2] = cos(Angle);
00094     elem[0][2] = sin(Angle);
00095     elem[2][0] = -elem[0][2]; 
00096     return *this;
00097 }
00098 
00108 Matrix44 &Matrix44::SetRotationZ(float Angle) {
00109 
00110     memset(elem, 0, sizeof(elem));
00111     elem[2][2] = elem[3][3] = 1;
00112     elem[0][0] = elem[1][1] = cos(Angle);
00113     elem[1][0] = sin(Angle);
00114     elem[0][1] = -elem[1][0]; 
00115     return *this;
00116 }
00117 
00128 Matrix44 &Matrix44::AddRotationX(float Angle) {
00129 
00130     Matrix44 Rot(Angle, X_AXIS);
00131     Matrix44MultiplyR(Rot, *this);
00132     return *this;
00133 }
00134 
00145 Matrix44 &Matrix44::AddRotationY(float Angle) {
00146 
00147     Matrix44 Rot(Angle, Y_AXIS);
00148     Matrix44MultiplyR(Rot, *this);
00149     return *this;
00150 }
00151 
00162 Matrix44 &Matrix44::AddRotationZ(float Angle) {
00163 
00164     Matrix44 Rot(Angle, Z_AXIS);
00165     Matrix44MultiplyR(Rot, *this);
00166     return *this;
00167 }
00168 
00180 Matrix44 &Matrix44::AddRotation(float Angle, MainAxis Axis) {
00181 
00182     switch (Axis) {
00183     
00184         case X_AXIS:
00185             AddRotationX(Angle);
00186         break;
00187         case Y_AXIS:
00188             AddRotationY(Angle);
00189         break;
00190         case Z_AXIS:
00191             AddRotationZ(Angle);
00192         break;
00193         default: 
00194         break;
00195     }
00196     return *this;
00197 }
00198 
00207 Matrix44 &Matrix44::ViewLH(const Vector4 &Eye, const Vector4 &At, const Vector4 &Up) {
00208 
00209     //First of all the vector N is the direction of view, 
00210     Vector4 N = At - Eye;
00211     N.Normalize();
00212 
00213     //Up vector is calculated with a simple cross product between the 
00214     //direction vector and general Up direction
00215     Vector4 U;
00216     U = N * Up;
00217     U.Normalize();
00218 
00219     //Once direction and U vector are known V (Exact Up direction) is 
00220     //obtained by a simple cross product, note that since the product is
00221     //U * N the three vector form a Left Handed coordinate system
00222     Vector4 V = U * N;
00223 
00224     //Now simply build the matrix that perform change of coordinate from
00225     //World Coordinate System to View Coordinate System
00226     elem[0][0] = U.x;
00227     elem[0][1] = U.y;
00228     elem[0][2] = U.z;
00229     elem[0][3] = -Dot(U, Eye);
00230     elem[1][0] = V.x;
00231     elem[1][1] = V.y;
00232     elem[1][2] = V.z;
00233     elem[1][3] = -Dot(V, Eye);
00234     elem[2][0] = N.x;
00235     elem[2][1] = N.y;
00236     elem[2][2] = N.z;
00237     elem[2][3] = -Dot(N, Eye);
00238     elem[3][0] = 0;
00239     elem[3][1] = 0;
00240     elem[3][2] = 0;
00241     elem[3][3] = 1;
00242 
00243     //Return a reference to current object
00244     return *this;
00245 }
00246 
00256 Matrix44 &Matrix44::PerspectiveLH(float Width, float Height, float zNear, float zFar) {
00257 
00258     memset(elem, 0, sizeof(elem));
00259     elem[0][0] = 2 * zNear / Width;
00260     elem[1][1] = 2 * zNear / Height;
00261     elem[2][2] = (zFar + zNear) / (zFar - zNear);
00262     elem[3][2] = 1;
00263     elem[2][3] = (2 * zFar * zNear) / (zNear - zFar);
00264 
00265     return *this;
00266 }
00267 
00280 Matrix44 &Matrix44::PerspectiveRH(float Width, float Height, float zNear, float zFar) {
00281 
00282     //Reset all element of the matrix
00283     memset(elem, 0, sizeof(elem));
00284 
00285     //Simply build the matrix, these factors are calculated and explanated
00286     //into Appendix A of the tutorial.
00287     elem[0][0] = 2 * zNear / Width;
00288     elem[1][1] = 2 * zNear / Height;
00289     elem[2][2] = (zFar + zNear) / (zNear - zFar);
00290     elem[3][2] = -1;
00291     elem[2][3] = (2 * zFar * zNear) / (zNear - zFar);
00292 
00293     //Simply return a referente to current object.
00294     return *this;
00295 }
00296 
00302 Matrix44 &Matrix44::MapToViewPort(float xMin, float xMax, 
00303                                              float yMin, float yMax,
00304                                              float zMin, float zMax) {
00305         
00306    //first of all calculate scaling factor 
00307     float cx = (xMax - xMin) / 2;
00308     float cy = (yMax - yMin) / 2;
00309     float cz = (zMax - zMin) / 2;
00310 
00311     //Then declare a matrix that will represent a Mapping Transform
00312     Matrix44 MapMatrix;
00313     MapMatrix.Reset();
00314 
00315     //set the scaling factor in MapMatrix
00316     MapMatrix.elem[0][0] = cx;
00317     MapMatrix.elem[1][1] = cy;
00318     MapMatrix.elem[2][2] = cz;
00319 
00320     //set the translation element
00321     MapMatrix.elem[0][3] = cx + xMin;
00322     MapMatrix.elem[1][3] = cy + yMin;
00323     MapMatrix.elem[2][3] = cz + zMin;
00324 
00325     //Last row is [0, 0, 0, 1]
00326     MapMatrix.elem[3][3] = 1;
00327 
00328     //simply multiply MapMatrix for current matrix and store back
00329     //the result.
00330     *this = MapMatrix * *this;
00331     return *this;
00332 }
00333 
00342 const Matrix44 PS2Math::operator*(const Matrix44 &M1, 
00343                                              const Matrix44 &M2) {
00344                                  
00345     Matrix44 Result;
00346     Matrix44Multiply(M1, M2, Result);
00347     return Result;
00348 }
00349 
00360 Matrix44 &PS2Math::operator*=(Matrix44 &M1, 
00361                                         const Matrix44 &M2) {
00362                             
00363     return Matrix44MultiplyL(M1, M2);                   
00364 }
00365 
00372 std::ostream &PS2Math::operator<<(std::ostream &outStream, const Matrix44 &Mat) {
00373 
00374     outStream << std::setprecision(3);
00375     outStream << "[" << Mat.elem[0][0] << "\t" << Mat.elem[0][1] << "\t" << 
00376                      Mat.elem[0][2] << "\t" << Mat.elem[0][3] << "]\n";
00377     outStream << "[" << Mat.elem[1][0] << "\t" << Mat.elem[1][1] << "\t" << 
00378                     Mat.elem[1][2] << "\t" << Mat.elem[1][3] << "]\n";
00379     outStream << "[" << Mat.elem[2][0] << "\t" << Mat.elem[2][1] << "\t" << 
00380                     Mat.elem[2][2] << "\t" << Mat.elem[2][3] << "]\n";
00381     outStream << "[" << Mat.elem[3][0] << "\t" << Mat.elem[3][1] << "\t" << 
00382                     Mat.elem[3][2] << "\t" << Mat.elem[3][3] << "]\n";
00383     return outStream;
00384 }
00385 
00386 
00387 

Generated on Wed Jan 7 19:11:56 2004 for PS2 Tutorial by doxygen 1.3.4