d07c171f |
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | * Mupen64plus - VectorMath.cpp * |
3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
4 | * Copyright (C) 2002 Rice1964 * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the * |
18 | * Free Software Foundation, Inc., * |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
20 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
21 | |
22 | #include <string.h> |
23 | |
24 | #include "VectorMath.h" |
25 | |
26 | //---------- XMATRIX |
27 | |
28 | XMATRIX::XMATRIX() |
29 | { |
30 | } |
31 | |
32 | XMATRIX::XMATRIX( const float *pIn ) |
33 | { |
34 | memcpy(m, pIn, 16*4); |
35 | } |
36 | |
37 | XMATRIX::XMATRIX( const MATRIX &pIn ) |
38 | { |
39 | memcpy(m, pIn.m, 16*4); |
40 | } |
41 | |
42 | XMATRIX::XMATRIX( float _11, float _12, float _13, float _14, |
43 | float _21, float _22, float _23, float _24, |
44 | float _31, float _32, float _33, float _34, |
45 | float _41, float _42, float _43, float _44 ) |
46 | { |
47 | this->_11 = _11; |
48 | this->_12 = _12; |
49 | this->_13 = _13; |
50 | this->_14 = _14; |
51 | this->_21 = _21; |
52 | this->_22 = _22; |
53 | this->_23 = _23; |
54 | this->_24 = _24; |
55 | this->_31 = _31; |
56 | this->_32 = _32; |
57 | this->_33 = _33; |
58 | this->_34 = _34; |
59 | this->_41 = _41; |
60 | this->_42 = _42; |
61 | this->_43 = _43; |
62 | this->_44 = _44; |
63 | } |
64 | |
65 | float& XMATRIX::operator () ( unsigned int Row, unsigned int Col ) |
66 | { |
67 | return m[Row][Col]; |
68 | } |
69 | |
70 | float XMATRIX::operator () ( unsigned int Row, unsigned int Col ) const |
71 | { |
72 | return m[Row][Col]; |
73 | } |
74 | |
75 | XMATRIX::operator float* () |
76 | { |
77 | return (float*)m; |
78 | } |
79 | |
80 | XMATRIX::operator const float* () const |
81 | { |
82 | return (float*)m; |
83 | } |
84 | |
85 | XMATRIX& XMATRIX::operator *= ( const XMATRIX &pIn ) |
86 | { |
87 | XMATRIX mTemp(*this); |
88 | *this = mTemp*pIn; |
89 | return *this; |
90 | } |
91 | |
92 | XMATRIX& XMATRIX::operator += ( const XMATRIX &pIn ) |
93 | { |
94 | XMATRIX mTemp(*this); |
95 | *this = mTemp+pIn; |
96 | return *this; |
97 | } |
98 | |
99 | XMATRIX& XMATRIX::operator -= ( const XMATRIX &pIn ) |
100 | { |
101 | XMATRIX mTemp(*this); |
102 | *this = mTemp-pIn; |
103 | return *this; |
104 | } |
105 | |
106 | XMATRIX& XMATRIX::operator *= ( float f) |
107 | { |
108 | for (int i=0; i<16; i++) |
109 | { |
110 | ((float*)m)[i] *= f; |
111 | } |
112 | |
113 | return *this; |
114 | } |
115 | |
116 | XMATRIX& XMATRIX::operator /= ( float f) |
117 | { |
118 | for (int i=0; i<16; i++) |
119 | { |
120 | ((float*)m)[i] /= f; |
121 | } |
122 | |
123 | return *this; |
124 | } |
125 | |
126 | XMATRIX XMATRIX::operator + () const |
127 | { |
128 | return *this; |
129 | } |
130 | |
131 | XMATRIX XMATRIX::operator - () const |
132 | { |
133 | XMATRIX mTemp; |
134 | |
135 | for (int i=0; i<16; i++) |
136 | { |
137 | ((float*)mTemp.m)[i] = -((float*)m)[i]; |
138 | } |
139 | |
140 | return mTemp; |
141 | } |
142 | |
143 | XMATRIX XMATRIX::operator * ( const XMATRIX &pIn ) const |
144 | { |
145 | XMATRIX mTemp; |
146 | |
147 | for (int i=0; i<4; i++) |
148 | { |
149 | for (int j=0; j<4; j++) |
150 | { |
151 | mTemp.m[i][j] = m[i][0]*pIn.m[0][j] + |
152 | m[i][1]*pIn.m[1][j] + |
153 | m[i][2]*pIn.m[2][j] + |
154 | m[i][3]*pIn.m[3][j]; |
155 | } |
156 | } |
157 | |
158 | return mTemp; |
159 | } |
160 | |
161 | XMATRIX XMATRIX::operator + ( const XMATRIX &pIn ) const |
162 | { |
163 | XMATRIX mTemp; |
164 | |
165 | for (int i=0; i<16; i++) |
166 | { |
167 | ((float*)mTemp.m)[i] = ((float*)m)[i] + ((float*)pIn.m)[i]; |
168 | } |
169 | |
170 | return mTemp; |
171 | } |
172 | |
173 | XMATRIX XMATRIX::operator - ( const XMATRIX &pIn ) const |
174 | { |
175 | XMATRIX mTemp; |
176 | |
177 | for (int i=0; i<16; i++) |
178 | { |
179 | ((float*)mTemp.m)[i] = ((float*)m)[i] - ((float*)pIn.m)[i]; |
180 | } |
181 | |
182 | return mTemp; |
183 | } |
184 | |
185 | /* |
186 | XMATRIX operator * ( float ) const; |
187 | XMATRIX operator / ( float ) const; |
188 | friend XMATRIX operator * ( float, const XMATRIX & ); |
189 | bool operator == ( const XMATRIX & ) const; |
190 | bool operator != ( const XMATRIX & ) const; |
191 | */ |
192 | |
193 | //---------- VECTOR3 |
194 | |
195 | XVECTOR3::XVECTOR3() |
196 | { |
197 | } |
198 | |
199 | XVECTOR3::XVECTOR3( const float *f ) |
200 | { |
201 | x = f[0]; |
202 | y = f[1]; |
203 | z = f[2]; |
204 | } |
205 | |
206 | XVECTOR3::XVECTOR3( const VECTOR3 &v ) |
207 | { |
208 | x = v.x; |
209 | y = v.y; |
210 | z = v.z; |
211 | } |
212 | |
213 | XVECTOR3::XVECTOR3( float _x, float _y, float _z ) |
214 | { |
215 | x = _x; |
216 | y = _y; |
217 | z = _z; |
218 | } |
219 | |
220 | /* |
221 | // casting |
222 | inline operator float* (); |
223 | inline operator const float* () const; |
224 | |
225 | // assignment operators |
226 | inline XVECTOR3& operator += ( const XVECTOR3 &op ); |
227 | inline XVECTOR3& operator -= ( const XVECTOR3 &op ); |
228 | inline XVECTOR3& operator *= ( float op ); |
229 | inline XVECTOR3& operator /= ( float op ); |
230 | |
231 | // unary operators |
232 | inline XVECTOR3 operator + () const; |
233 | inline XVECTOR3 operator - () const; |
234 | |
235 | // binary operators |
236 | inline XVECTOR3 operator + ( const XVECTOR3 &op ) const; |
237 | inline XVECTOR3 operator - ( const XVECTOR3 &op ) const; |
238 | inline XVECTOR3 operator * ( float op ) const; |
239 | inline XVECTOR3 operator / ( float op ) const; |
240 | |
241 | |
242 | friend XVECTOR3 operator * ( float, const XVECTOR3& ); |
243 | |
244 | inline bool operator == ( const XVECTOR3 &op ) const; |
245 | inline bool operator != ( const XVECTOR3 &op ) const; |
246 | */ |
247 | |
248 | //---------- XVECTOR4 |
249 | |
250 | XVECTOR4::XVECTOR4() |
251 | { |
252 | } |
253 | |
254 | /* |
255 | XVECTOR4( const float *f ); |
256 | XVECTOR4( const VECTOR4 &v ); |
257 | XVECTOR4( float _x, float _y, float _z, float _w ); |
258 | |
259 | // casting |
260 | inline operator float* (); |
261 | inline operator const float* () const; |
262 | |
263 | // assignment operators |
264 | inline XVECTOR4& operator += ( const XVECTOR4 &op ); |
265 | inline XVECTOR4& operator -= ( const XVECTOR4 &op ); |
266 | inline XVECTOR4& operator *= ( float op ); |
267 | inline XVECTOR4& operator /= ( float op ); |
268 | |
269 | // unary operators |
270 | inline XVECTOR4 operator + () const; |
271 | inline XVECTOR4 operator - () const; |
272 | |
273 | // binary operators |
274 | inline XVECTOR4 operator + ( const XVECTOR4 &op ) const; |
275 | inline XVECTOR4 operator - ( const XVECTOR4 &op ) const; |
276 | inline XVECTOR4 operator * ( float op ) const; |
277 | inline XVECTOR4 operator / ( float op ) const; |
278 | |
279 | friend XVECTOR4 operator * ( float, const XVECTOR4& ); |
280 | |
281 | inline bool operator == ( const XVECTOR4 &op ) const; |
282 | inline bool operator != ( const XVECTOR4 &op ) const; |
283 | */ |
284 | |
285 | //---------- OTHER |
286 | |
287 | XMATRIX* MatrixTranspose( XMATRIX* pOut, const XMATRIX* pM ) |
288 | { |
289 | pOut->_11 = pM->_11; |
290 | pOut->_12 = pM->_21; |
291 | pOut->_13 = pM->_31; |
292 | pOut->_14 = pM->_41; |
293 | pOut->_21 = pM->_12; |
294 | pOut->_22 = pM->_22; |
295 | pOut->_23 = pM->_32; |
296 | pOut->_24 = pM->_42; |
297 | pOut->_31 = pM->_13; |
298 | pOut->_32 = pM->_23; |
299 | pOut->_33 = pM->_33; |
300 | pOut->_34 = pM->_43; |
301 | pOut->_41 = pM->_14; |
302 | pOut->_42 = pM->_24; |
303 | pOut->_43 = pM->_34; |
304 | pOut->_44 = pM->_44; |
305 | return pOut; |
306 | } |
307 | |
308 | XVECTOR4 Vec3Transform( XVECTOR4 *pOut, const XVECTOR3 *pV, const XMATRIX *pM ) |
309 | { |
310 | pOut->x = pV->x*pM->_11 + pV->y*pM->_21 + pV->z*pM->_31 + pM->_41; |
311 | pOut->y = pV->x*pM->_12 + pV->y*pM->_22 + pV->z*pM->_32 + pM->_42; |
312 | pOut->z = pV->x*pM->_13 + pV->y*pM->_23 + pV->z*pM->_33 + pM->_43; |
313 | pOut->w = pV->x*pM->_14 + pV->y*pM->_24 + pV->z*pM->_34 + pM->_44; |
314 | return *pOut; |
315 | } |
316 | |