ps2matrix4x4.cpp

00001 /*
00002 * "PS2" Application Framework
00003 *
00004 * University of Abertay Dundee
00005 * May be used for educational purposed only
00006 *
00007 * Author - Dr Henry S Fortuna
00008 *
00009 * $Revision: 1.2 $
00010 * $Date: 2007/08/19 12:45:11 $
00011 *
00012 */#include <memory.h>
00013 #include "ps2maths.h"
00014 #include "ps2matrix4x4.h"
00015 
00016 const Matrix4x4 Matrix4x4::IDENTITY(1.0f,0.0f,0.0f,0.0f,
00017                                                                         0.0f,1.0f,0.0f,0.0f,
00018                                                                         0.0f,0.0f,1.0f,0.0f,
00019                                                                         0.0f,0.0f,0.0f,1.0f);
00020 
00021 const Matrix4x4 Matrix4x4::NULLMATRIX(0.0f,0.0f,0.0f,0.0f,
00022                                                                           0.0f,0.0f,0.0f,0.0f,
00023                                                                           0.0f,0.0f,0.0f,0.0f,
00024                                                                           0.0f,0.0f,0.0f,0.0f);
00025 
00026 Matrix4x4::Matrix4x4(void)
00027 {
00028 }
00029 
00030 Matrix4x4::~Matrix4x4(void)
00031 {
00032 }
00033 
00034 Matrix4x4::Matrix4x4(const Matrix4x4 & rhs)
00035 {
00036         memcpy(elem, rhs.elem, sizeof(float) * 16);
00037 }
00038 
00039 Matrix4x4::Matrix4x4(float _11, float _12, float _13, float _14,
00040                                          float _21, float _22, float _23, float _24,
00041                                          float _31, float _32, float _33, float _34,
00042                                          float _41, float _42, float _43, float _44)
00043 {
00044         elem[0][0] = _11;       elem[0][1] = _12;       elem[0][2] = _13;       elem[0][3] = _14;
00045         elem[1][0] = _21;       elem[1][1] = _22;       elem[1][2] = _23;       elem[1][3] = _24;
00046         elem[2][0] = _31;       elem[2][1] = _32;       elem[2][2] = _33;       elem[2][3] = _34;
00047         elem[3][0] = _41;       elem[3][1] = _42;       elem[3][2] = _43;       elem[3][3] = _44;
00048 }
00049 
00050 Matrix4x4::Matrix4x4(float _elem[][4])
00051 {
00052         memcpy(elem, _elem, sizeof(float) * 16);
00053 }
00054 
00055 Matrix4x4 operator*( const Matrix4x4 &M1,
00056                      const Matrix4x4 &M2)
00057 {
00058     Matrix4x4 ret;
00059 
00060     for(int i = 0; i < 4; i++)
00061     {
00062         for(int j = 0; j < 4; j++)
00063         {
00064             float Value = 0;
00065           
00066             for(int k = 0; k < 4; k++)
00067             {
00068                 Value += M1(i,k) * M2(k,j);
00069             }
00070 
00071             ret(i,j) = Value;
00072         }
00073     }
00074 
00075     return ret;
00076 }
00077 
00078 Vector4 operator * ( const Matrix4x4 &M,
00079                                          const Vector4 &V )
00080 {
00081     Vector4 ret;
00082 
00083         ret.x = M(0,0) * V.x + M(1,0) * V.y + M(2,0) * V.z + M(3,0) * V.w;
00084         ret.y = M(0,1) * V.x + M(1,1) * V.y + M(2,1) * V.z + M(3,1) * V.w;
00085         ret.z = M(0,2) * V.x + M(1,2) * V.y + M(2,2) * V.z + M(3,2) * V.w;
00086         ret.w = M(0,3) * V.x + M(1,3) * V.y + M(2,3) * V.z + M(3,3) * V.w;
00087 
00088     return ret;
00089 }
00090 
00091 void Matrix4x4::Translation(float X, float Y, float Z)
00092 {
00093         memcpy(elem, IDENTITY.elem, sizeof(float) * 16);
00094 
00095         elem[3][0] = X;
00096         elem[3][1] = Y;
00097         elem[3][2] = Z;
00098 }
00099 
00100 void Matrix4x4::Translate(float X, float Y, float Z)
00101 {
00102         elem[3][0] += X;
00103         elem[3][1] += Y;
00104         elem[3][2] += Z;
00105 }
00106 
00107 
00108 void Matrix4x4::Scaling(float S)
00109 {
00110         memcpy(elem, IDENTITY.elem, sizeof(float) * 16);
00111 
00112         elem[0][0] = S;
00113         elem[1][1] = S;
00114         elem[2][2] = S;
00115         
00116 }
00117 
00118 void Matrix4x4::RotationX(float fAngle)
00119 {
00120         memcpy(elem, IDENTITY.elem, sizeof(float) * 16);
00121         float c = Cos(fAngle);
00122         float s = Sin(fAngle);
00123         elem[1][1] = c;
00124         elem[1][2] = s;
00125         elem[2][1] = -s;
00126         elem[2][2] = c;
00127 }
00128 
00129 void Matrix4x4::RotationY(float fAngle)
00130 {
00131         memcpy(elem, IDENTITY.elem, sizeof(float) * 16);
00132         float c = Cos(fAngle);
00133         float s = Sin(fAngle);
00134         elem[0][0] = c;
00135         elem[2][0] = s;
00136         elem[0][2] = -s;
00137         elem[2][2] = c;
00138 }
00139 
00140 void Matrix4x4::RotationZ(float fAngle)
00141 {
00142         memcpy(elem, IDENTITY.elem, sizeof(float) * 16);
00143         float c = Cos(fAngle);
00144         float s = Sin(fAngle);
00145         elem[0][0] = c;
00146         elem[0][1] = s;
00147         elem[1][0] = -s;
00148         elem[1][1] = c;
00149 }
00150 
00151 void Matrix4x4::LookAt(const Vector4 & vFrom, const Vector4 & vTo, const Vector4 & vUp)
00152 {
00153         Vector4 vZ = Normalise(vFrom - vTo);
00154         //vZ.DumpVector4("vZ");
00155         Vector4 vX = Normalise(vUp.Cross(vZ));
00156         //vX.DumpVector4("vX");
00157         Vector4 vY = vZ.Cross(vX);
00158         //vY.DumpVector4("vY");
00159 
00160     elem[0][0] = vX.x;  elem[0][1] = vY.x;      elem[0][2] = vZ.x;      elem[0][3] = 0;
00161         elem[1][0] = vX.y;      elem[1][1] = vY.y;      elem[1][2] = vZ.y;      elem[1][3] = 0;
00162         elem[2][0] = vX.z;      elem[2][1] = vY.z;      elem[2][2] = vZ.z;      elem[2][3] = 0;
00163 
00164         elem[3][0] = -vX.Dot3(vFrom);
00165         elem[3][1] = -vY.Dot3(vFrom);
00166         elem[3][2] = -vZ.Dot3(vFrom);
00167         elem[3][3] = 1;
00168 }
00169 
00170 
00171 void Matrix4x4::DumpMatrix4x4(char * s)
00172 {
00173         if(s != NULL)printf("\n%s\n");
00174         else printf("\n");
00175         printf("%f %f %f %f\n",   elem[0][0], elem[0][1], elem[0][2], elem[0][3]);
00176         printf("%f %f %f %f\n",   elem[1][0], elem[1][1], elem[1][2], elem[1][3]);
00177         printf("%f %f %f %f\n",   elem[2][0], elem[2][1], elem[2][2], elem[2][3]);
00178         printf("%f %f %f %f\n\n", elem[3][0], elem[3][1], elem[3][2], elem[3][3]);
00179 }

Generated on Sun May 18 21:45:08 2008 for PS2X by  doxygen 1.5.4