MercuryMatrix.cpp

Go to the documentation of this file.
00001 #include "MercuryMatrix.h"
00002 #include "MercuryMath.h"
00003 #include "MercuryTypes.h"
00004 #include "MercuryUtil.h"
00005 
00006 MercuryMatrix::MercuryMatrix()
00007 {
00008     Identity();
00009 }
00010 
00011 const MercuryMatrix& MercuryMatrix::operator=(const MercuryMatrix& m)
00012 {
00013     Copy16f(m_matrix, m.m_matrix);
00014     return *this;
00015 }
00016 
00017 const MercuryMatrix& MercuryMatrix::operator=(const float* m)
00018 {
00019     Copy16f(m_matrix, m);
00020     return *this;
00021 }
00022 
00023 void MercuryMatrix::Zero()
00024 {
00025     m_matrix[0][0] = 0;
00026     m_matrix[0][1] = 0;
00027     m_matrix[0][2] = 0;
00028     m_matrix[0][3] = 0;
00029     
00030     m_matrix[1][0] = 0;
00031     m_matrix[1][1] = 0;
00032     m_matrix[1][2] = 0;
00033     m_matrix[1][3] = 0;
00034     
00035     m_matrix[2][0] = 0;
00036     m_matrix[2][1] = 0;
00037     m_matrix[2][2] = 0;
00038     m_matrix[2][3] = 0;
00039     
00040     m_matrix[3][0] = 0;
00041     m_matrix[3][1] = 0;
00042     m_matrix[3][2] = 0;
00043     m_matrix[3][3] = 0;
00044 }
00045 
00046 void MercuryMatrix::Identity()
00047 {
00048     m_matrix[0][0] = 1;
00049     m_matrix[0][1] = 0;
00050     m_matrix[0][2] = 0;
00051     m_matrix[0][3] = 0;
00052     
00053     m_matrix[1][0] = 0;
00054     m_matrix[1][1] = 1;
00055     m_matrix[1][2] = 0;
00056     m_matrix[1][3] = 0;
00057     
00058     m_matrix[2][0] = 0;
00059     m_matrix[2][1] = 0;
00060     m_matrix[2][2] = 1;
00061     m_matrix[2][3] = 0;
00062     
00063     m_matrix[3][0] = 0;
00064     m_matrix[3][1] = 0;
00065     m_matrix[3][2] = 0;
00066     m_matrix[3][3] = 1;
00067 }
00068 
00069 void MercuryMatrix::Translate(float x, float y, float z)
00070 {
00071     MercuryMatrix m;
00072     m.m_matrix[0][3] = x;
00073     m.m_matrix[1][3] = y;
00074     m.m_matrix[2][3] = z;
00075     *this *= m;
00076 }
00077 
00078 void MercuryMatrix::RotateXYZ(float x, float y, float z)
00079 {
00080     //x,y,z must be negated for some reason
00081     float X = -x*2*Q_PI/360; //Reduced calulation for speed
00082     float Y = -y*2*Q_PI/360;
00083     float Z = -z*2*Q_PI/360;
00084     float cx = COS(X);
00085     float sx = SIN(X);
00086     float cy = COS(Y);
00087     float sy = SIN(Y);
00088     float cz = COS(Z);
00089     float sz = SIN(Z);
00090 
00091     MercuryMatrix matrix;
00092 
00093     //Row major
00094     //manually transposed
00095     matrix.m_matrix[0][0] = cy*cz;
00096     matrix.m_matrix[1][0] = (sx*sy*cz)-(cx*sz);
00097     matrix.m_matrix[2][0] = (cx*sy*cz)+(sx*sz);
00098     matrix.m_matrix[3][0] = 0;
00099 
00100     matrix.m_matrix[0][1] = cy*sz;
00101     matrix.m_matrix[1][1] = (sx*sy*sz)+(cx*cz);
00102     matrix.m_matrix[2][1] = (cx*sy*sz)-(sx*cz);
00103     matrix.m_matrix[3][1] = 0;
00104 
00105     matrix.m_matrix[0][2] = -sy;
00106     matrix.m_matrix[1][2] = sx*cy;
00107     matrix.m_matrix[2][2] = cx*cy;
00108     matrix.m_matrix[3][2] = 0;
00109 
00110     matrix.m_matrix[0][3] = 0;
00111     matrix.m_matrix[1][3] = 0;
00112     matrix.m_matrix[2][3] = 0;
00113     matrix.m_matrix[3][3] = 1;
00114 
00115     *this *= matrix;    
00116 }
00117 
00118 void MercuryMatrix::RotateAngAxis( float fAngle, float ix, float iy, float iz )
00119 {
00120     float c = COS( fAngle*Q_PI/180 );
00121     float s = SIN( fAngle*Q_PI/180 );
00122     float absin = SQRT( ix*ix + iy*iy + iz*iz );
00123     float x = ix/absin;
00124     float y = iy/absin;
00125     float z = iz/absin;
00126 
00127     m_matrix[0][0] = x*x*(1-c)+c;
00128     m_matrix[0][1] = x*y*(1-c)-z*s;
00129     m_matrix[0][2] = x*z*(1-c)+y*s;
00130     m_matrix[0][3] = 0;
00131 
00132     m_matrix[1][0] = y*x*(1-c)+z*s;
00133     m_matrix[1][1] = y*y*(1-c)+c;
00134     m_matrix[1][2] = y*z*(1-c)-x*s;
00135     m_matrix[1][3] = 0;
00136 
00137     m_matrix[2][0] = x*z*(1-c)-y*s;
00138     m_matrix[2][1] = y*z*(1-c)+x*s;
00139     m_matrix[2][2] = z*z*(1-c)+c;
00140     m_matrix[2][3] = 0;
00141 
00142     m_matrix[3][0] = 0;
00143     m_matrix[3][1] = 0;
00144     m_matrix[3][2] = 0;
00145     m_matrix[3][3] = 1;
00146 }
00147 
00148 void MercuryMatrix::Transotale( float tX, float tY, float tZ, float rX, float rY, float rZ, float sX, float sY, float sZ )
00149 {
00150     //x,y,z must be negated for some reason
00151     float X = -rX*DEGRAD; //Reduced calulation for speed
00152     float Y = -rY*DEGRAD;
00153     float Z = -rZ*DEGRAD;
00154     float cx = COS(X);
00155     float sx = SIN(X);
00156     float cy = COS(Y);
00157     float sy = SIN(Y);
00158     float cz = COS(Z);
00159     float sz = SIN(Z);
00160 
00161     MercuryMatrix matrix;
00162 
00163     //Row major
00164     //manually transposed
00165     matrix.m_matrix[0][0] = sX*cy*cz;
00166     matrix.m_matrix[1][0] = sX*((sx*sy*cz)-(cx*sz));
00167     matrix.m_matrix[2][0] = sX*((cx*sy*cz)+(sx*sz));
00168     matrix.m_matrix[3][0] = 0;
00169 
00170     matrix.m_matrix[0][1] = sY*cy*sz;
00171     matrix.m_matrix[1][1] = sY*((sx*sy*sz)+(cx*cz));
00172     matrix.m_matrix[2][1] = sY*((cx*sy*sz)-(sx*cz));
00173     matrix.m_matrix[3][1] = 0;
00174 
00175     matrix.m_matrix[0][2] = sZ*(-sy);
00176     matrix.m_matrix[1][2] = sZ*sx*cy;
00177     matrix.m_matrix[2][2] = sZ*cx*cy;
00178     matrix.m_matrix[3][2] = 0;
00179 
00180     matrix.m_matrix[0][3] = tX;
00181     matrix.m_matrix[1][3] = tY;
00182     matrix.m_matrix[2][3] = tZ;
00183     matrix.m_matrix[3][3] = 1;
00184 
00185     *this *= matrix;    
00186 }
00187 
00188 
00189 void MercuryMatrix::Scale(float x, float y, float z)
00190 {
00191     MercuryMatrix m;
00192 
00193     m.m_matrix[0][0] = x;
00194     m.m_matrix[1][1] = y;
00195     m.m_matrix[2][2] = z;
00196 
00197     *this *= m;
00198 }
00199 
00200 MercuryMatrix MercuryMatrix::operator*(const MercuryMatrix& m) const
00201 {
00202     MercuryMatrix r(*this);
00203     R_ConcatTransforms4 ( (float*)&m_matrix, (float*)&m.m_matrix, (float*)&r.m_matrix);
00204     return r;
00205 }
00206 
00207 MercuryMatrix& MercuryMatrix::operator*=(const MercuryMatrix& m) 
00208 {
00209     MercuryMatrix r(*this);
00210     R_ConcatTransforms4 ( (float*)&r.m_matrix, (float*)&m.m_matrix, (float*)&m_matrix);
00211     return *this;
00212 }
00213 
00214 void TransposeMatrix( const MercuryMatrix &in, MercuryMatrix &out )
00215 {
00216     float tmp;
00217     const float* _in = (const float*)in.Ptr();
00218     float* _out = out.Ptr();
00219 
00220     //unchanging
00221     _out[0] = _in[0];
00222     _out[5] = _in[5];
00223     _out[10] = _in[10];
00224     _out[15] = _in[15];
00225 
00226     tmp = _in[1];
00227     _out[1] = _in[4];
00228     _out[4] = tmp;
00229     tmp = _in[2];
00230     _out[2] = _in[8];
00231     _out[8] = tmp;
00232     tmp = _in[3];
00233     _out[3] = _in[12];
00234     _out[12] = tmp;
00235 
00236     tmp = _in[6];
00237     _out[6] = _in[9];
00238     _out[9] = tmp;
00239     tmp = _in[7];
00240     _out[7] = _in[13];
00241     _out[13] = tmp;
00242 
00243     tmp = _in[11];
00244     _out[11] = _in[14];
00245     _out[14] = tmp;
00246 }
00247 
00248 /* 
00249  * Copyright (c) 2006 Joshua Allen
00250  * All rights reserved.
00251  *
00252  * Redistribution and use in source and binary forms, with or
00253  * without modification, are permitted provided that the following
00254  * conditions are met:
00255  *  -   Redistributions of source code must retain the above
00256  *      copyright notice, this list of conditions and the following disclaimer.
00257  *  -   Redistributions in binary form must reproduce the above copyright
00258  *      notice, this list of conditions and the following disclaimer in
00259  *      the documentation and/or other materials provided with the distribution.
00260  *  -   Neither the name of the Mercury Engine nor the names of its
00261  *      contributors may be used to endorse or promote products derived from
00262  *      this software without specific prior written permission.
00263  *
00264  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00265  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00266  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00267  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00268  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00269  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00270  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00271  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00272  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00273  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00274  */
00275 

Hosted by SourceForge.net Logo