1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of LEXIExporter |
---|
4 | |
---|
5 | Copyright 2006 NDS Limited |
---|
6 | |
---|
7 | Author(s): |
---|
8 | Bo Krohn |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | ----------------------------------------------------------------------------- |
---|
24 | */ |
---|
25 | |
---|
26 | ///////////////////////////////////////////////////// |
---|
27 | // |
---|
28 | // Matrix 4x4 class |
---|
29 | // |
---|
30 | ///////////////////////////////////////////////////// |
---|
31 | |
---|
32 | #ifndef __Matrix_4x4_Class__ |
---|
33 | #define __Matrix_4x4_Class__ |
---|
34 | |
---|
35 | // |
---|
36 | |
---|
37 | class CMatrix { |
---|
38 | |
---|
39 | public: |
---|
40 | |
---|
41 | static const CMatrix _identity; |
---|
42 | static const CMatrix _zero; |
---|
43 | |
---|
44 | // Constructors/Destructor |
---|
45 | inline CMatrix() |
---|
46 | { |
---|
47 | } |
---|
48 | inline CMatrix(const CMatrix& m) |
---|
49 | { |
---|
50 | memcpy(mat, m.mat, 4 * 4 * sizeof(float)); |
---|
51 | } |
---|
52 | inline CMatrix(const float* m) |
---|
53 | { |
---|
54 | memcpy(mat, m, 4 * 4 * sizeof(float)); |
---|
55 | } |
---|
56 | |
---|
57 | // Direct access to matrix |
---|
58 | float mat[4][4]; |
---|
59 | |
---|
60 | // Assignment |
---|
61 | inline CMatrix& operator = (const CMatrix& m) |
---|
62 | { |
---|
63 | memcpy(mat, m.mat, 4*4*sizeof(float)); |
---|
64 | |
---|
65 | return *this; |
---|
66 | } |
---|
67 | |
---|
68 | // Comparing |
---|
69 | inline bool operator == (const CMatrix& m) const |
---|
70 | { |
---|
71 | return memcmp(mat, m.mat, 4*4*sizeof(float))==0 ? true : false; |
---|
72 | } |
---|
73 | |
---|
74 | inline bool operator != (const CMatrix& m) const |
---|
75 | { |
---|
76 | return memcmp(mat, m.mat, 4*4*sizeof(float))!=0 ? true : false; |
---|
77 | } |
---|
78 | |
---|
79 | inline bool IsIdentity() const |
---|
80 | { |
---|
81 | return memcmp(mat, _identity.mat, 4 * 4 * sizeof(float)) ? false : true; |
---|
82 | } |
---|
83 | |
---|
84 | // Operators |
---|
85 | inline CMatrix operator + (const CMatrix& m) const |
---|
86 | { |
---|
87 | CMatrix ma; |
---|
88 | |
---|
89 | for(unsigned int r=0; r<4; r++) |
---|
90 | { |
---|
91 | for(unsigned int c=0; c<4; c++) |
---|
92 | { |
---|
93 | ma.mat[r][c] = mat[r][c] + m.mat[r][c]; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | return ma; |
---|
98 | } |
---|
99 | |
---|
100 | inline CMatrix& operator += (const CMatrix& m) |
---|
101 | { |
---|
102 | for(unsigned int r=0; r<4; r++) |
---|
103 | { |
---|
104 | for(unsigned int c=0; c<4; c++) |
---|
105 | { |
---|
106 | mat[r][c] += m.mat[r][c]; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | return *this; |
---|
111 | } |
---|
112 | |
---|
113 | inline CMatrix operator - (const CMatrix& m) const |
---|
114 | { |
---|
115 | CMatrix ma; |
---|
116 | |
---|
117 | for(unsigned int r=0; r<4; r++) |
---|
118 | { |
---|
119 | for(unsigned int c=0; c<4; c++) |
---|
120 | { |
---|
121 | ma.mat[r][c] = mat[r][c] - m.mat[r][c]; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | return ma; |
---|
126 | } |
---|
127 | |
---|
128 | inline CMatrix& operator -= (const CMatrix& m) |
---|
129 | { |
---|
130 | for(unsigned int r=0; r<4; r++) |
---|
131 | { |
---|
132 | for(unsigned int c=0; c<4; c++) |
---|
133 | { |
---|
134 | mat[r][c] -= m.mat[r][c]; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | return *this; |
---|
139 | } |
---|
140 | |
---|
141 | inline CMatrix operator * (float f) const |
---|
142 | { |
---|
143 | CMatrix ma; |
---|
144 | |
---|
145 | for(unsigned int r=0; r<4; r++) |
---|
146 | { |
---|
147 | for(unsigned int c=0; c<4; c++) |
---|
148 | { |
---|
149 | ma.mat[r][c] = mat[r][c] * f; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | return ma; |
---|
154 | } |
---|
155 | |
---|
156 | inline CMatrix& operator *= (float f) |
---|
157 | { |
---|
158 | for(unsigned int r=0; r<4; r++) |
---|
159 | { |
---|
160 | for(unsigned int c=0; c<4; c++) |
---|
161 | { |
---|
162 | mat[r][c] *= f; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | return *this; |
---|
167 | } |
---|
168 | |
---|
169 | inline CMatrix operator / (float f) const |
---|
170 | { |
---|
171 | CMatrix ma; |
---|
172 | |
---|
173 | for(unsigned int r=0; r<4; r++) |
---|
174 | { |
---|
175 | for(unsigned int c=0; c<4; c++) |
---|
176 | { |
---|
177 | ma.mat[r][c] = mat[r][c] / f; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | return ma; |
---|
182 | } |
---|
183 | |
---|
184 | inline CMatrix& operator /= (float f) |
---|
185 | { |
---|
186 | for(unsigned int r=0; r<4; r++) |
---|
187 | { |
---|
188 | for(unsigned int c=0; c<4; c++) |
---|
189 | { |
---|
190 | mat[r][c] /= f; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | return *this; |
---|
195 | } |
---|
196 | |
---|
197 | // Functions |
---|
198 | inline void makeIdentity() |
---|
199 | { |
---|
200 | mat[0][0] = 1.0f; mat[0][1] = 0.0f; mat[0][2] = 0.0f; mat[0][3] = 0.0f; |
---|
201 | mat[1][0] = 0.0f; mat[1][1] = 1.0f; mat[1][2] = 0.0f; mat[1][3] = 0.0f; |
---|
202 | mat[2][0] = 0.0f; mat[2][1] = 0.0f; mat[2][2] = 1.0f; mat[2][3] = 0.0f; |
---|
203 | mat[3][0] = 0.0f; mat[3][1] = 0.0f; mat[3][2] = 0.0f; mat[3][3] = 1.0f; |
---|
204 | } |
---|
205 | |
---|
206 | void makeLookAt(const CVec3& eye, const CVec3& point, const CVec3& up); |
---|
207 | |
---|
208 | void makeLookAtDirection(const CVec3& eye, const CVec3& dir, const CVec3& up); |
---|
209 | |
---|
210 | void makePerspective(float left, float right, float bottom, float top, float znear, float zfar); |
---|
211 | |
---|
212 | void makePerspectiveFOV(float hfov, float vfov, float aspect, float znear, float zfar); |
---|
213 | |
---|
214 | void makeOrthogonalPerspective(float left, float right, float bottom, float top, float znear, float zfar); |
---|
215 | |
---|
216 | inline void makeRotation(float angle, const CVec3& axis) |
---|
217 | { |
---|
218 | makeRotationRadians(UtilDegToRad(angle), axis); |
---|
219 | } |
---|
220 | |
---|
221 | inline void makeRotationRadians(float angle, const CVec3& axis) |
---|
222 | { |
---|
223 | setRotationRadians(angle, axis); |
---|
224 | setRow(3, CVec4::_w); |
---|
225 | setColumn(3, CVec3::_zero); |
---|
226 | } |
---|
227 | |
---|
228 | inline void makeRotation(float anglex, float angley, float anglez) |
---|
229 | { |
---|
230 | makeRotationRadians(UtilDegToRad(anglex), UtilDegToRad(angley), UtilDegToRad(anglez)); |
---|
231 | } |
---|
232 | |
---|
233 | inline void makeRotationRadians(float anglex, float angley, float anglez) |
---|
234 | { |
---|
235 | setRotationRadians(anglex, angley, anglez); |
---|
236 | setRow(3, CVec4::_w); |
---|
237 | setColumn(3, CVec3::_zero); |
---|
238 | } |
---|
239 | |
---|
240 | inline void setRotation(float angle, const CVec3& axis) |
---|
241 | { |
---|
242 | setRotationRadians(UtilDegToRad(angle), axis); |
---|
243 | } |
---|
244 | |
---|
245 | void setRotationRadians(float angle, const CVec3& axis); |
---|
246 | |
---|
247 | inline void setRotation(float anglex, float angley, float anglez) |
---|
248 | { |
---|
249 | setRotationRadians(UtilDegToRad(anglex), UtilDegToRad(angley), UtilDegToRad(anglez)); |
---|
250 | } |
---|
251 | |
---|
252 | void setRotationRadians(float anglex, float angley, float anglez); |
---|
253 | |
---|
254 | inline void getRotation(float& anglex, float& angley, float& anglez) |
---|
255 | { |
---|
256 | getRotationRadians(anglex, angley, anglez); |
---|
257 | |
---|
258 | anglex = UtilRadToDeg(anglex); |
---|
259 | angley = UtilRadToDeg(angley); |
---|
260 | anglez = UtilRadToDeg(anglez); |
---|
261 | } |
---|
262 | |
---|
263 | void getRotationRadians(float& anglex, float& angley, float& anglez); |
---|
264 | |
---|
265 | inline void makeScale(const CVec3& scale) |
---|
266 | { |
---|
267 | makeIdentity(); |
---|
268 | mat[0][0] = scale.x; |
---|
269 | mat[1][1] = scale.y; |
---|
270 | mat[2][2] = scale.z; |
---|
271 | } |
---|
272 | |
---|
273 | inline void makeScale(float scalar) |
---|
274 | { |
---|
275 | makeIdentity(); |
---|
276 | mat[0][0] = scalar; |
---|
277 | mat[1][1] = scalar; |
---|
278 | mat[2][2] = scalar; |
---|
279 | } |
---|
280 | |
---|
281 | inline void setScale(const CVec3& scale) |
---|
282 | { |
---|
283 | mat[0][0] = scale.x; |
---|
284 | mat[1][1] = scale.y; |
---|
285 | mat[2][2] = scale.z; |
---|
286 | } |
---|
287 | |
---|
288 | inline void preScale(const CVec3& vector) |
---|
289 | { |
---|
290 | mat[0][0] *= vector.x; |
---|
291 | mat[0][1] *= vector.x; |
---|
292 | mat[0][2] *= vector.x; |
---|
293 | mat[0][3] *= vector.x; |
---|
294 | |
---|
295 | mat[1][0] *= vector.y; |
---|
296 | mat[1][1] *= vector.y; |
---|
297 | mat[1][2] *= vector.y; |
---|
298 | mat[1][3] *= vector.y; |
---|
299 | |
---|
300 | mat[2][0] *= vector.z; |
---|
301 | mat[2][1] *= vector.z; |
---|
302 | mat[2][2] *= vector.z; |
---|
303 | mat[2][3] *= vector.z; |
---|
304 | } |
---|
305 | |
---|
306 | inline void postScale(const CVec3& vector) |
---|
307 | { |
---|
308 | mat[0][0] *= vector.x; |
---|
309 | mat[0][1] *= vector.y; |
---|
310 | mat[0][2] *= vector.z; |
---|
311 | |
---|
312 | mat[1][0] *= vector.x; |
---|
313 | mat[1][1] *= vector.y; |
---|
314 | mat[1][2] *= vector.z; |
---|
315 | |
---|
316 | mat[2][0] *= vector.x; |
---|
317 | mat[2][1] *= vector.y; |
---|
318 | mat[2][2] *= vector.z; |
---|
319 | |
---|
320 | mat[3][0] *= vector.x; |
---|
321 | mat[3][1] *= vector.y; |
---|
322 | mat[3][2] *= vector.z; |
---|
323 | } |
---|
324 | |
---|
325 | inline void makeTranslation(const CVec3& translation) |
---|
326 | { |
---|
327 | mat[0][0] = 1.0f; mat[0][1] = 0.0f; mat[0][2] = 0.0f; mat[0][3] = 0.0f; |
---|
328 | mat[1][0] = 0.0f; mat[1][1] = 1.0f; mat[1][2] = 0.0f; mat[1][3] = 0.0f; |
---|
329 | mat[2][0] = 0.0f; mat[2][1] = 0.0f; mat[2][2] = 1.0f; mat[2][3] = 0.0f; |
---|
330 | mat[3][0] = translation.x; |
---|
331 | mat[3][1] = translation.y; |
---|
332 | mat[3][2] = translation.z; |
---|
333 | mat[3][3] = 1.0f; |
---|
334 | } |
---|
335 | |
---|
336 | void multiply(const CMatrix& m); |
---|
337 | |
---|
338 | void multiply(const CMatrix& m1, const CMatrix& m2); |
---|
339 | |
---|
340 | inline void preTranslation(const CVec3& translation) |
---|
341 | { |
---|
342 | mat[3][0] += translation.x * mat[0][0] |
---|
343 | + translation.y * mat[1][0] |
---|
344 | + translation.z * mat[2][0]; |
---|
345 | |
---|
346 | mat[3][1] += translation.x * mat[0][1] |
---|
347 | + translation.y * mat[1][1] |
---|
348 | + translation.z * mat[2][1]; |
---|
349 | |
---|
350 | mat[3][2] += translation.x * mat[0][2] |
---|
351 | + translation.y * mat[1][2] |
---|
352 | + translation.z * mat[2][2]; |
---|
353 | |
---|
354 | mat[3][3] += translation.x * mat[0][3] |
---|
355 | + translation.y * mat[1][3] |
---|
356 | + translation.z * mat[2][3]; |
---|
357 | } |
---|
358 | |
---|
359 | inline void postTranslation(const CVec3& translation) |
---|
360 | { |
---|
361 | mat[0][0] += mat[0][3] * translation.x; |
---|
362 | mat[0][1] += mat[0][3] * translation.y; |
---|
363 | mat[0][2] += mat[0][3] * translation.z; |
---|
364 | |
---|
365 | mat[1][0] += mat[1][3] * translation.x; |
---|
366 | mat[1][1] += mat[1][3] * translation.y; |
---|
367 | mat[1][2] += mat[1][3] * translation.z; |
---|
368 | |
---|
369 | mat[2][0] += mat[2][3] * translation.x; |
---|
370 | mat[2][1] += mat[2][3] * translation.y; |
---|
371 | mat[2][2] += mat[2][3] * translation.z; |
---|
372 | |
---|
373 | mat[3][0] += mat[3][3] * translation.x; |
---|
374 | mat[3][1] += mat[3][3] * translation.y; |
---|
375 | mat[3][2] += mat[3][3] * translation.z; |
---|
376 | } |
---|
377 | |
---|
378 | inline void preMultiply(const CMatrix& m) |
---|
379 | { |
---|
380 | multiply(m); |
---|
381 | } |
---|
382 | |
---|
383 | inline void postMultiply(const CMatrix& m) |
---|
384 | { |
---|
385 | multiply(*this, m); |
---|
386 | } |
---|
387 | |
---|
388 | inline void preRotation(float angle, const CVec3& axis) |
---|
389 | { |
---|
390 | preRotationRadians(UtilDegToRad(angle), axis); |
---|
391 | } |
---|
392 | |
---|
393 | inline void preRotationRadians(float angle, const CVec3& axis) |
---|
394 | { |
---|
395 | CMatrix m; |
---|
396 | m.makeRotationRadians(angle, axis); |
---|
397 | preMultiply(m); |
---|
398 | } |
---|
399 | |
---|
400 | inline void postRotation(float angle, const CVec3& axis) |
---|
401 | { |
---|
402 | postRotationRadians(UtilDegToRad(angle), axis); |
---|
403 | } |
---|
404 | |
---|
405 | inline void postRotationRadians(float angle, const CVec3& axis) |
---|
406 | { |
---|
407 | CMatrix m; |
---|
408 | m.makeRotationRadians(angle, axis); |
---|
409 | postMultiply(m); |
---|
410 | } |
---|
411 | |
---|
412 | inline void getTranslation(CVec3& translation) const |
---|
413 | { |
---|
414 | translation.x=mat[3][0]; |
---|
415 | translation.y=mat[3][1]; |
---|
416 | translation.z=mat[3][2]; |
---|
417 | } |
---|
418 | |
---|
419 | inline void setTranslation(const CVec3& translation) |
---|
420 | { |
---|
421 | mat[3][0] = translation.x; |
---|
422 | mat[3][1] = translation.y; |
---|
423 | mat[3][2] = translation.z; |
---|
424 | } |
---|
425 | |
---|
426 | inline void getTranslation(CMatrix& translation) const |
---|
427 | { |
---|
428 | translation.makeTranslation(CVec3(mat[3][0], mat[3][1], mat[3][2])); |
---|
429 | } |
---|
430 | |
---|
431 | inline void transformPoint(CVec3* point) const |
---|
432 | { |
---|
433 | transformPoints(point, point, 1); |
---|
434 | } |
---|
435 | |
---|
436 | inline void transformPoints(CVec3* points, unsigned int iCount) const |
---|
437 | { |
---|
438 | transformPoints(points, points, iCount); |
---|
439 | } |
---|
440 | |
---|
441 | inline void transformPoint(CVec4* point) const |
---|
442 | { |
---|
443 | transformPoints(point, point, 1); |
---|
444 | } |
---|
445 | |
---|
446 | inline void transformPoints(CVec4* points, unsigned int iCount) const |
---|
447 | { |
---|
448 | transformPoints(points, points, iCount); |
---|
449 | } |
---|
450 | |
---|
451 | void transformPoints(const CVec3* from, CVec3* to, unsigned int iCount) const; |
---|
452 | void transformPoints(const CVec4* from, CVec4* to, unsigned int iCount) const; |
---|
453 | |
---|
454 | inline void transformVector(CVec3* vec) const |
---|
455 | { |
---|
456 | transformVectors(vec, vec, 1); |
---|
457 | } |
---|
458 | |
---|
459 | inline void transformVectors(CVec3* vecs, unsigned int iCount) const |
---|
460 | { |
---|
461 | transformVectors(vecs, vecs, iCount); |
---|
462 | } |
---|
463 | |
---|
464 | inline void transformVector(CVec4* vec) const |
---|
465 | { |
---|
466 | transformVectors(vec, vec, 1); |
---|
467 | } |
---|
468 | |
---|
469 | inline void transformVectors(CVec4* vecs, unsigned int iCount) const |
---|
470 | { |
---|
471 | transformVectors(vecs, vecs, iCount); |
---|
472 | } |
---|
473 | |
---|
474 | void transformVectors(const CVec3* from, CVec3* to, unsigned int iCount) const; |
---|
475 | void transformVectors(const CVec4* from, CVec4* to, unsigned int iCount) const; |
---|
476 | |
---|
477 | void invert(); |
---|
478 | |
---|
479 | void transpose(); |
---|
480 | |
---|
481 | float determinant() const; |
---|
482 | |
---|
483 | // Columns |
---|
484 | inline void getColumn(unsigned int iCol, CVec3& v) const |
---|
485 | { |
---|
486 | v.x=mat[0][iCol]; |
---|
487 | v.y=mat[1][iCol]; |
---|
488 | v.z=mat[2][iCol]; |
---|
489 | } |
---|
490 | |
---|
491 | inline void getColumn(unsigned int iCol, CVec4& v) const |
---|
492 | { |
---|
493 | v.x=mat[0][iCol]; |
---|
494 | v.y=mat[1][iCol]; |
---|
495 | v.z=mat[2][iCol]; |
---|
496 | v.w=mat[3][iCol]; |
---|
497 | } |
---|
498 | |
---|
499 | inline void setColumn(unsigned int iCol, const CVec3& v) |
---|
500 | { |
---|
501 | mat[0][iCol]=v.x; |
---|
502 | mat[1][iCol]=v.y; |
---|
503 | mat[2][iCol]=v.z; |
---|
504 | } |
---|
505 | |
---|
506 | inline void setColumn(unsigned int iCol, const CVec4& v) |
---|
507 | { |
---|
508 | mat[0][iCol]=v.x; |
---|
509 | mat[1][iCol]=v.y; |
---|
510 | mat[2][iCol]=v.z; |
---|
511 | mat[3][iCol]=v.w; |
---|
512 | } |
---|
513 | |
---|
514 | // Rows |
---|
515 | inline void getRow(unsigned int iRow, CVec3& v) const |
---|
516 | { |
---|
517 | v.x=mat[iRow][0]; |
---|
518 | v.y=mat[iRow][1]; |
---|
519 | v.z=mat[iRow][2]; |
---|
520 | } |
---|
521 | |
---|
522 | inline void getRow(unsigned int iRow, CVec4& v) const |
---|
523 | { |
---|
524 | v.x=mat[iRow][0]; |
---|
525 | v.y=mat[iRow][1]; |
---|
526 | v.z=mat[iRow][2]; |
---|
527 | v.w=mat[iRow][3]; |
---|
528 | } |
---|
529 | |
---|
530 | inline void setRow(unsigned int iRow, const CVec3& v) |
---|
531 | { |
---|
532 | mat[iRow][0]=v.x; |
---|
533 | mat[iRow][1]=v.y; |
---|
534 | mat[iRow][2]=v.z; |
---|
535 | } |
---|
536 | |
---|
537 | inline void setRow(unsigned int iRow, const CVec4& v) |
---|
538 | { |
---|
539 | mat[iRow][0]=v.x; |
---|
540 | mat[iRow][1]=v.y; |
---|
541 | mat[iRow][2]=v.z; |
---|
542 | mat[iRow][3]=v.w; |
---|
543 | } |
---|
544 | |
---|
545 | void adjoint(const CMatrix& in, CMatrix& out); |
---|
546 | |
---|
547 | }; |
---|
548 | |
---|
549 | // |
---|
550 | |
---|
551 | #endif // __Matrix_4x4_Class__ |
---|