1 /* 2 =========================================================================== 3 Copyright (C) 2000 - 2013, Raven Software, Inc. 4 Copyright (C) 2001 - 2013, Activision, Inc. 5 Copyright (C) 2013 - 2015, OpenJK contributors 6 7 This file is part of the OpenJK source code. 8 9 OpenJK is free software; you can redistribute it and/or modify it 10 under the terms of the GNU General Public License version 2 as 11 published by the Free Software Foundation. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, see <http://www.gnu.org/licenses/>. 20 =========================================================================== 21 */ 22 23 //////////////////////////////////////////////////////////////////////////////////////// 24 // RAVEN STANDARD TEMPLATE LIBRARY 25 // (c) 2002 Activision 26 // 27 // 28 // Matrix Library 29 // -------------- 30 // 31 // 32 // 33 // NOTES: 34 // 35 // 36 //////////////////////////////////////////////////////////////////////////////////////// 37 #if !defined(RAVL_MATRIX_INC) 38 #define RAVL_MATRIX_INC 39 40 41 //////////////////////////////////////////////////////////////////////////////////////// 42 // Includes 43 //////////////////////////////////////////////////////////////////////////////////////// 44 #if defined(RA_DEBUG_LINKING) 45 #pragma message("...including CMatrix.h") 46 #endif 47 #if !defined(RAVL_VEC_INC) 48 #include "CVec.h" 49 #endif 50 //namespace ravl 51 //{ 52 53 54 55 56 57 58 //////////////////////////////////////////////////////////////////////////////////////// 59 // The Matrix 60 //////////////////////////////////////////////////////////////////////////////////////// 61 class CMatrix 62 { 63 public: 64 //////////////////////////////////////////////////////////////////////////////////// 65 // Constructors 66 //////////////////////////////////////////////////////////////////////////////////// CMatrix()67 CMatrix() {} CMatrix(const CVec4 & x,const CVec4 & y,const CVec4 & z,const CVec4 & w)68 CMatrix(const CVec4& x,const CVec4& y,const CVec4& z, const CVec4& w) {v[0]=x; v[1]=y; v[2]=z; v[3]=w;} CMatrix(const CMatrix & t)69 CMatrix(const CMatrix& t) {v[0]=t.v[0]; v[1]=t.v[1]; v[2]=t.v[2]; v[3]=t.v[3];} CMatrix(const float t[16])70 CMatrix(const float t[16]) {v[0]=t[0]; v[1]=t[4]; v[2]=t[8]; v[3]=t[12];} 71 72 //////////////////////////////////////////////////////////////////////////////////// 73 // Initializers 74 //////////////////////////////////////////////////////////////////////////////////// Set(const CVec4 & x,const CVec4 & y,const CVec4 & z,const CVec4 & w)75 void Set(const CVec4& x,const CVec4& y,const CVec4& z, const CVec4& w) {v[0]=x; v[1]=y; v[2]=z; v[3]=w;} Set(const CMatrix & t)76 void Set(const CMatrix& t) {v[0]=t.v[0]; v[1]=t.v[1]; v[2]=t.v[2]; v[3]=t.v[3];} Set(const float t[16])77 void Set(const float t[16]) {v[0]=t[0]; v[1]=t[4]; v[2]=t[8]; v[3]=t[12];} 78 Clear()79 void Clear() {v[0].Set(0,0,0,0); v[1].Set(0,0,0,0); v[2].Set(0,0,0,0); v[3].Set(0,0,0,0);} Itentity()80 void Itentity() {v[0].Set(1,0,0,0); v[1].Set(0,1,0,0); v[2].Set(0,0,1,0); v[3].Set(0,0,0,1);} Translate(const float x,const float y,const float z)81 void Translate(const float x, const float y, const float z) {v[0].Set(1,0,0,0); v[1].Set(0,1,0,0); v[2].Set(0,0,1,0); v[3].Set(x,y,z,1);} Scale(const float x,const float y,const float z)82 void Scale(const float x, const float y, const float z) {v[0].Set(x,0,0,0); v[1].Set(0,y,0,0); v[2].Set(0,0,z,0); v[3].Set(0,0,0,1);} Rotate(int axis,const float s,const float c)83 void Rotate(int axis, const float s/*sin(angle)*/, const float c/*cos(angle)*/) 84 { 85 switch(axis) 86 { 87 case 0: 88 v[0].Set( 1, 0, 0, 0); 89 v[1].Set( 0, c,-s, 0); 90 v[2].Set( 0, s, c, 0); 91 break; 92 case 1: 93 v[0].Set( c, 0, s, 0); 94 v[1].Set( 0, 1, 0, 0); 95 v[2].Set(-s, 0, c, 0); 96 break; 97 case 2: 98 v[0].Set( c,-s, 0, 0); 99 v[1].Set( s, c, 0, 0); 100 v[2].Set( 0, 0, 1, 0); 101 break; 102 } 103 v[3].Set( 0, 0, 0, 1); 104 } 105 106 107 //////////////////////////////////////////////////////////////////////////////////// 108 // Member Accessors 109 //////////////////////////////////////////////////////////////////////////////////// 110 const CVec4& operator[](int i) const {return v[i];} 111 CVec4& operator[](int i) {return v[i];} 112 up()113 CVec4& up() {return v[0];} left()114 CVec4& left() {return v[1];} fwd()115 CVec4& fwd() {return v[2];} origin()116 CVec4& origin() {return v[3];} 117 118 //////////////////////////////////////////////////////////////////////////////////// 119 // Equality / Inequality Operators 120 //////////////////////////////////////////////////////////////////////////////////// 121 bool operator== (const CMatrix& t) const {return (v[0]==t.v[0] && v[1]==t.v[1] && v[2]==t.v[2] && v[3]==t.v[3]);} 122 bool operator!= (const CMatrix& t) const {return !(v[0]==t.v[0] && v[1]==t.v[1] && v[2]==t.v[2] && v[3]==t.v[3]);} 123 124 //////////////////////////////////////////////////////////////////////////////////// 125 // Basic Arithimitic Operators 126 //////////////////////////////////////////////////////////////////////////////////// 127 const CMatrix &operator= (const CMatrix& t) {v[0]=t.v[0]; v[1]=t.v[1]; v[2]=t.v[2]; v[3]=t.v[3]; return *this;} 128 const CMatrix &operator+= (const CMatrix& t) {v[0]+=t.v[0]; v[1]+=t.v[1]; v[2]+=t.v[2]; v[3]+=t.v[3];return *this;} 129 const CMatrix &operator-= (const CMatrix& t) {v[0]-=t.v[0]; v[1]-=t.v[1]; v[2]-=t.v[2]; v[3]-=t.v[3];return *this;} 130 131 CMatrix operator+ (const CMatrix &t) const {return CMatrix(v[0]+t.v[0], v[1]+t.v[1], v[2]+t.v[2], v[3]+t.v[3]);} 132 CMatrix operator- (const CMatrix &t) const {return CMatrix(v[0]-t.v[0], v[1]-t.v[1], v[2]-t.v[2], v[3]-t.v[3]);} 133 134 //////////////////////////////////////////////////////////////////////////////////// 135 // Matrix Scale 136 //////////////////////////////////////////////////////////////////////////////////// 137 const CMatrix &operator*= (const float d) {v[0]*=d; v[1]*=d; v[2]*=d; v[3]*=d; return *this;} 138 139 140 //////////////////////////////////////////////////////////////////////////////////// 141 // Matrix To Matrix Multiply 142 //////////////////////////////////////////////////////////////////////////////////// 143 CMatrix operator* (const CMatrix &t) const 144 { 145 // assert(this!=&t); // Don't Multiply With Self 146 147 CMatrix Result; // The Resulting Matrix 148 int i,j,k; // Counters 149 float Accumulator; // Current Value Of The Dot Product 150 for (i=0; i<4; i++) 151 { 152 for (j=0; j<4; j++) 153 { 154 Accumulator = 0.0f; // Reset The Accumulator 155 for(k=0; k<4; k++) 156 { 157 Accumulator += v[i][k]*t[k][j]; // Calculate Dot Product Of The Two Vectors 158 } 159 Result[i][j]=Accumulator; // Place In Result 160 } 161 } 162 163 return Result; 164 } 165 166 //////////////////////////////////////////////////////////////////////////////////// 167 // Vector To Matrix Multiply 168 //////////////////////////////////////////////////////////////////////////////////// 169 CVec4 operator* (const CVec4 &t) const 170 { 171 CVec4 Result; 172 173 Result[0] = v[0][0]*t[0] + v[1][0]*t[1] + v[2][0]*t[2] + v[3][0]; 174 Result[1] = v[0][1]*t[0] + v[1][1]*t[1] + v[2][1]*t[2] + v[3][1]; 175 Result[2] = v[0][2]*t[0] + v[1][2]*t[1] + v[2][2]*t[2] + v[3][2]; 176 177 return Result; 178 } 179 180 public: 181 CVec4 v[4]; 182 }; 183 184 185 186 //} 187 #endif 188