Matrix16.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/Matrix16.h>
00022 
00023 static float IDENTITY_MATRIX[16] = 
00024 {
00025    1.0, 0.0, 0.0, 0.0,
00026    0.0, 1.0, 0.0, 0.0,
00027    0.0, 0.0, 1.0, 0.0,
00028    0.0, 0.0, 0.0, 1.0
00029 };
00030 
00031 Matrix16::Matrix16(float *init)
00032 {
00033         if (!init) init = IDENTITY_MATRIX;
00034         for (int i=0; i<16; i++) M[i] = init[i];
00035 }
00036 
00037 Matrix16::~Matrix16()
00038 {
00039 }
00040 
00041 void Matrix16::identity()
00042 {
00043         for (int i=0; i<16; i++) M[i] = IDENTITY_MATRIX[i];
00044 }
00045 
00046 #define A(row,col)  a[(col<<2)+row]
00047 #define B(row,col)  b[(col<<2)+row]
00048 #define P(row,col)  product[(col<<2)+row]
00049 
00050 void Matrix16::multiply(float *b)
00051 {
00052         static float product[16];
00053         float *a = M;
00054 
00055         int i;
00056         for (i = 0; i < 4; i++) 
00057         {
00058                 const float ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
00059                 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
00060                 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
00061                 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
00062                 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
00063         }
00064 
00065         for (int i=0; i<16; i++) M[i] = product[i];
00066 }
00067 
00068 void Matrix16::scale(float x, float y, float z)
00069 {
00070         float *m = M;
00071         m[0] *= x;   m[4] *= y;   m[8]  *= z;
00072         m[1] *= x;   m[5] *= y;   m[9]  *= z;
00073         m[2] *= x;   m[6] *= y;   m[10] *= z;
00074         m[3] *= x;   m[7] *= y;   m[11] *= z;
00075 }
00076 
00077 void Matrix16::translate(float x, float y, float z)
00078 {
00079         float *m = M;
00080         m[12] = m[0] * x + m[4] * y + m[8]  * z + m[12];
00081         m[13] = m[1] * x + m[5] * y + m[9]  * z + m[13];
00082         m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
00083         m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
00084 }

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3