1 | /*
|
---|
2 | ---------------------------------------------------------------------
|
---|
3 | This file is part of Sandra Engine (real-time 3D engine)
|
---|
4 | Copyright (C) 2004 Jesus Gumbau Portales
|
---|
5 |
|
---|
6 | This library is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Lesser General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2.1 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | This library 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 GNU
|
---|
14 | Lesser General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Lesser General Public
|
---|
17 | License along with this library; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
|
---|
19 | USA, or go to http://www.gnu.org/copyleft/lesser.txt.
|
---|
20 | ---------------------------------------------------------------------
|
---|
21 | */
|
---|
22 |
|
---|
23 | // 21-02-2001
|
---|
24 | // Jesus Gumbau Portales
|
---|
25 |
|
---|
26 | #ifndef __TMATRIX_H__
|
---|
27 | #define __TMATRIX_H__
|
---|
28 |
|
---|
29 | #include "Vector.h"
|
---|
30 |
|
---|
31 | #define MATRIX_WORLD_SPACE 0
|
---|
32 | #define MATRIX_OBJECT_SPACE 1
|
---|
33 |
|
---|
34 | class RotMatrix
|
---|
35 | {
|
---|
36 | private:
|
---|
37 | Vector col[3];
|
---|
38 | float tempmat[16];
|
---|
39 | public:
|
---|
40 | RotMatrix(void){
|
---|
41 | col[0].x=1.0f; col[1].x=0.0f; col[2].x=0.0f;
|
---|
42 | col[0].y=0.0f; col[1].y=1.0f; col[2].y=0.0f;
|
---|
43 | col[0].z=0.0f; col[1].z=0.0f; col[2].z=1.0f;
|
---|
44 | }
|
---|
45 | RotMatrix(const RotMatrix &t){
|
---|
46 | col[0]=t.col[0];
|
---|
47 | col[1]=t.col[1];
|
---|
48 | col[2]=t.col[2];
|
---|
49 | }
|
---|
50 |
|
---|
51 | void SetBaseX(float v[3]){ col[0].x=v[0]; col[0].y=v[1]; col[0].z=v[2]; }
|
---|
52 | void SetBaseY(float v[3]){ col[1].x=v[0]; col[1].y=v[1]; col[1].z=v[2]; }
|
---|
53 | void SetBaseZ(float v[3]){ col[2].x=v[0]; col[2].y=v[1]; col[2].z=v[2]; }
|
---|
54 |
|
---|
55 | void SetBaseX(float x, float y, float z){ col[0].x=x; col[0].y=y; col[0].z=z; }
|
---|
56 | void SetBaseY(float x, float y, float z){ col[1].x=x; col[1].y=y; col[1].z=z; }
|
---|
57 | void SetBaseZ(float x, float y, float z){ col[2].x=x; col[2].y=y; col[2].z=z; }
|
---|
58 |
|
---|
59 | void SetBaseX(const Vector &v){ col[0].x=v.x; col[0].y=v.y; col[0].z=v.z; }
|
---|
60 | void SetBaseY(const Vector &v){ col[1].x=v.x; col[1].y=v.y; col[1].z=v.z; }
|
---|
61 | void SetBaseZ(const Vector &v){ col[2].x=v.x; col[2].y=v.y; col[2].z=v.z; }
|
---|
62 |
|
---|
63 | Vector GetBaseX(void) const { return col[0]; }
|
---|
64 | Vector GetBaseY(void) const { return col[1]; }
|
---|
65 | Vector GetBaseZ(void) const { return col[2]; }
|
---|
66 |
|
---|
67 | // RotMatrix x Vector
|
---|
68 | Vector operator*(const Vector &v) const {
|
---|
69 | return Vector(col[0].x*v.x + col[1].x*v.y + col[2].x*v.z,
|
---|
70 | col[0].y*v.x + col[1].y*v.y + col[2].y*v.z,
|
---|
71 | col[0].z*v.x + col[1].z*v.y + col[2].z*v.z);
|
---|
72 | }
|
---|
73 |
|
---|
74 | Vector &operator[](unsigned int column){
|
---|
75 | return ((Vector&)(col[column]));
|
---|
76 | }
|
---|
77 | const Vector &operator[](unsigned int column) const {
|
---|
78 | return ((Vector&)(col[column]));
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | // RotMatrix - RotMatrix
|
---|
83 | RotMatrix operator-(const RotMatrix &tm) const {
|
---|
84 | RotMatrix res;
|
---|
85 |
|
---|
86 | for (unsigned int c=0; c<3; c++)
|
---|
87 | for (unsigned int f=0; f<3; f++)
|
---|
88 | res[c][f]=col[c][f]-tm[c][f];
|
---|
89 |
|
---|
90 | return res;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | // RotMatrix * RotMatrix
|
---|
95 | RotMatrix operator*(const RotMatrix &tm) const {
|
---|
96 | RotMatrix res;
|
---|
97 | for (unsigned int c=0; c<3; c++)
|
---|
98 | for (unsigned int f=0; f<3; f++)
|
---|
99 | {
|
---|
100 | float ff=0.0f;
|
---|
101 | ff+=float(col[0][f]*tm[c][0]);
|
---|
102 | ff+=float(col[1][f]*tm[c][1]);
|
---|
103 | ff+=float(col[2][f]*tm[c][2]);
|
---|
104 | res[c][f]=ff;
|
---|
105 | }
|
---|
106 | return res;
|
---|
107 | }
|
---|
108 |
|
---|
109 | void PostMultiply(const RotMatrix &tm){
|
---|
110 | RotMatrix res;
|
---|
111 | for (unsigned int f=0; f<3; f++)
|
---|
112 | for (unsigned int c=0; c<3; c++)
|
---|
113 | {
|
---|
114 | float ff=0.0f;
|
---|
115 | ff+=float(col[0][c]*tm[f][0]);
|
---|
116 | ff+=float(col[1][c]*tm[f][1]);
|
---|
117 | ff+=float(col[2][c]*tm[f][2]);
|
---|
118 | res[c][f]=ff;
|
---|
119 | }
|
---|
120 | (*this)=res;
|
---|
121 | }
|
---|
122 |
|
---|
123 | const RotMatrix & operator=(const RotMatrix &tm){
|
---|
124 | col[0]=tm.col[0];
|
---|
125 | col[1]=tm.col[1];
|
---|
126 | col[2]=tm.col[2];
|
---|
127 | return tm;
|
---|
128 | }
|
---|
129 |
|
---|
130 | bool operator!=(const RotMatrix &tm) const {
|
---|
131 | if (col[0]!=tm[0]) return false;
|
---|
132 | if (col[1]!=tm[1]) return false;
|
---|
133 | if (col[2]!=tm[2]) return false;
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 |
|
---|
137 | void LoadIdentity(void){
|
---|
138 | col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f;
|
---|
139 | col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f;
|
---|
140 | col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f;
|
---|
141 | }
|
---|
142 |
|
---|
143 | // returns a mat[16] in opengl conform format
|
---|
144 | // WARNING: returns a dir to a TEMP variable
|
---|
145 | float *GetOpenGLConform(void){
|
---|
146 | tempmat[0]=float(col[0][0]);
|
---|
147 | tempmat[1]=float(col[0][1]);
|
---|
148 | tempmat[2]=float(col[0][2]);
|
---|
149 | tempmat[3]=0.0f;
|
---|
150 | tempmat[4]=float(col[1][0]);
|
---|
151 | tempmat[5]=float(col[1][1]);
|
---|
152 | tempmat[6]=float(col[1][2]);
|
---|
153 | tempmat[7]=0.0f;
|
---|
154 | tempmat[8]=float(col[2][0]);
|
---|
155 | tempmat[9]=float(col[2][1]);
|
---|
156 | tempmat[10]=float(col[2][2]);
|
---|
157 | tempmat[11]=0.0f;
|
---|
158 | tempmat[12]=0.0f;
|
---|
159 | tempmat[13]=0.0f;
|
---|
160 | tempmat[14]=0.0f;
|
---|
161 | tempmat[15]=1.0f;
|
---|
162 | return tempmat;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | // sets the matrix to a rotation matrix
|
---|
167 | void LoadRotationXMatrix(float angle){
|
---|
168 | float cosa=cosf(GradToRad(angle));
|
---|
169 | float sina=sinf(GradToRad(angle));
|
---|
170 | col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f;
|
---|
171 | col[0][1]=0.0f; col[1][1]=cosa; col[2][1]=-sina;
|
---|
172 | col[0][2]=0.0f; col[1][2]=sina; col[2][2]=cosa;
|
---|
173 | }
|
---|
174 |
|
---|
175 | void LoadRotationYMatrix(float angle){
|
---|
176 | float cosa=cosf(GradToRad(angle));
|
---|
177 | float sina=sinf(GradToRad(angle));
|
---|
178 | col[0][0]=cosa; col[1][0]=0.0f; col[2][0]=sina;
|
---|
179 | col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f;
|
---|
180 | col[0][2]=-sina;col[1][2]=0.0f; col[2][2]=cosa;
|
---|
181 | }
|
---|
182 |
|
---|
183 | void LoadRotationZMatrix(float angle){
|
---|
184 | float cosa=cosf(GradToRad(angle));
|
---|
185 | float sina=sinf(GradToRad(angle));
|
---|
186 | col[0][0]=cosa; col[1][0]=-sina;col[2][0]=0.0f;
|
---|
187 | col[0][1]=sina; col[1][1]=cosa; col[2][1]=0.0f;
|
---|
188 | col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f;
|
---|
189 | }
|
---|
190 |
|
---|
191 | // Rotate the object around X axis
|
---|
192 | void RotateX(float angle, int mode=MATRIX_OBJECT_SPACE){
|
---|
193 | RotMatrix tr;
|
---|
194 | tr.LoadRotationXMatrix(angle); // FIXME!!!!! -angle?!?!
|
---|
195 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
196 | operator=( (*this)*tr );
|
---|
197 | else
|
---|
198 | operator=( tr*(*this) );
|
---|
199 | }
|
---|
200 |
|
---|
201 | void RotateY(float angle, int mode=MATRIX_OBJECT_SPACE){
|
---|
202 | RotMatrix tr;
|
---|
203 | tr.LoadRotationYMatrix(angle);
|
---|
204 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
205 | operator=( (*this)*tr );
|
---|
206 | else
|
---|
207 | operator=( tr*(*this) );
|
---|
208 | }
|
---|
209 |
|
---|
210 | void RotateZ(float angle, int mode=MATRIX_OBJECT_SPACE){
|
---|
211 | RotMatrix tr;
|
---|
212 | tr.LoadRotationZMatrix(angle);
|
---|
213 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
214 | operator=( (*this)*tr );
|
---|
215 | else
|
---|
216 | operator=( tr*(*this) );
|
---|
217 | }
|
---|
218 |
|
---|
219 | void RotateAxis(float angle, Vector axis, int mode=MATRIX_OBJECT_SPACE){
|
---|
220 | RotMatrix Rx,Ry,Rz,iRx,iRy;
|
---|
221 | float d = (float)sqrt(axis.y*axis.y + axis.z*axis.z);
|
---|
222 |
|
---|
223 | Rx.SetBaseX(1.0f, 0.0f, 0.0f);
|
---|
224 | Rx.SetBaseY(0.0f, float(axis.z/d), float(axis.y/d));
|
---|
225 | Rx.SetBaseZ(0.0f, float(-axis.y/d), float(axis.z/d));
|
---|
226 |
|
---|
227 | Ry.SetBaseX(d, 0.0f, float(axis.x));
|
---|
228 | Ry.SetBaseY(0.0f, 1.0f, 0.0f);
|
---|
229 | Ry.SetBaseZ(float(-axis.x), 0.0f, d);
|
---|
230 |
|
---|
231 | Rz.LoadRotationZMatrix(angle);
|
---|
232 |
|
---|
233 | iRx = Rx; iRx.Traspose();
|
---|
234 | iRy = Ry; iRy.Traspose();
|
---|
235 |
|
---|
236 | RotMatrix accum;
|
---|
237 | accum.LoadIdentity();
|
---|
238 | if (d!=0)
|
---|
239 | accum = accum * iRx;
|
---|
240 | accum = accum * iRy;
|
---|
241 | accum = accum * Rz;
|
---|
242 | accum = accum * Ry;
|
---|
243 | if (d!=0)
|
---|
244 | accum = accum * Rx;
|
---|
245 |
|
---|
246 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
247 | operator=( (*this)*accum );
|
---|
248 | else
|
---|
249 | operator=( accum*(*this) );
|
---|
250 | }
|
---|
251 |
|
---|
252 | void Traspose(void){
|
---|
253 | tempmat[0]=float(col[0][0]);
|
---|
254 | tempmat[1]=float(col[0][1]);
|
---|
255 | tempmat[2]=float(col[0][2]);
|
---|
256 |
|
---|
257 | tempmat[3]=float(col[1][0]);
|
---|
258 | tempmat[4]=float(col[1][1]);
|
---|
259 | tempmat[5]=float(col[1][2]);
|
---|
260 |
|
---|
261 | tempmat[6]=float(col[2][0]);
|
---|
262 | tempmat[7]=float(col[2][1]);
|
---|
263 | tempmat[8]=float(col[2][2]);
|
---|
264 |
|
---|
265 | col[0].x=tempmat[0]; col[1].x=tempmat[1]; col[2].x=tempmat[2];
|
---|
266 | col[0].y=tempmat[4]; col[1].y=tempmat[4]; col[2].y=tempmat[5];
|
---|
267 | col[0].z=tempmat[6]; col[1].z=tempmat[7]; col[2].z=tempmat[8];
|
---|
268 | }
|
---|
269 |
|
---|
270 | // This function inverts (only) a valid ROTRANSFORM Matrix (with unitary vector lengths). This should not work on arbitrary matrices
|
---|
271 | void Invert(void){
|
---|
272 | Traspose();
|
---|
273 | }
|
---|
274 | };
|
---|
275 |
|
---|
276 |
|
---|
277 | class TMatrix
|
---|
278 | {
|
---|
279 | private:
|
---|
280 | Vector col[4];
|
---|
281 | float tempmat[16];
|
---|
282 | public:
|
---|
283 | TMatrix(void){
|
---|
284 | col[0].x=1.0f; col[1].x=0.0f; col[2].x=0.0f; col[3].x=0.0f;
|
---|
285 | col[0].y=0.0f; col[1].y=1.0f; col[2].y=0.0f; col[3].y=0.0f;
|
---|
286 | col[0].z=0.0f; col[1].z=0.0f; col[2].z=1.0f; col[3].z=0.0f;
|
---|
287 | col[0].w=0.0f; col[1].w=0.0f; col[2].w=0.0f; col[3].w=1.0f;
|
---|
288 | }
|
---|
289 | TMatrix(const TMatrix &t){
|
---|
290 | col[0]=t.col[0];
|
---|
291 | col[1]=t.col[1];
|
---|
292 | col[2]=t.col[2];
|
---|
293 | col[3]=t.col[3];
|
---|
294 | }
|
---|
295 |
|
---|
296 | TMatrix(const Vector &tr, const RotMatrix &rot, const Vector &scale){
|
---|
297 | col[0] = rot.GetBaseX()*scale.x;
|
---|
298 | col[1] = rot.GetBaseY()*scale.y;
|
---|
299 | col[2] = rot.GetBaseZ()*scale.z;
|
---|
300 | col[3] = tr;
|
---|
301 | col[0].w=0.0f; col[1].w=0.0f; col[2].w=0.0f; col[3].w=1.0f;
|
---|
302 | }
|
---|
303 |
|
---|
304 | void SetBaseX(float v[3]){ col[0].x=v[0]; col[0].y=v[1]; col[0].z=v[2]; }
|
---|
305 | void SetBaseY(float v[3]){ col[1].x=v[0]; col[1].y=v[1]; col[1].z=v[2]; }
|
---|
306 | void SetBaseZ(float v[3]){ col[2].x=v[0]; col[2].y=v[1]; col[2].z=v[2]; }
|
---|
307 | void SetPoint(float v[3]){ col[3].x=v[0]; col[3].y=v[1]; col[3].z=v[2]; }
|
---|
308 |
|
---|
309 | void SetBaseX(float x, float y, float z){ col[0].x=x; col[0].y=y; col[0].z=z; }
|
---|
310 | void SetBaseY(float x, float y, float z){ col[1].x=x; col[1].y=y; col[1].z=z; }
|
---|
311 | void SetBaseZ(float x, float y, float z){ col[2].x=x; col[2].y=y; col[2].z=z; }
|
---|
312 | void SetPoint(float x, float y, float z){ col[3].x=x; col[3].y=y; col[3].z=z; }
|
---|
313 |
|
---|
314 | void SetBaseX(const Vector &v){ col[0].x=v.x; col[0].y=v.y; col[0].z=v.z; }
|
---|
315 | void SetBaseY(const Vector &v){ col[1].x=v.x; col[1].y=v.y; col[1].z=v.z; }
|
---|
316 | void SetBaseZ(const Vector &v){ col[2].x=v.x; col[2].y=v.y; col[2].z=v.z; }
|
---|
317 | void SetPoint(const Vector &v){ col[3].x=v.x; col[3].y=v.y; col[3].z=v.z; }
|
---|
318 |
|
---|
319 | Vector GetBaseX(void) const { return col[0]; }
|
---|
320 | Vector GetBaseY(void) const { return col[1]; }
|
---|
321 | Vector GetBaseZ(void) const { return col[2]; }
|
---|
322 |
|
---|
323 | // TMatrix x Vector
|
---|
324 | Vector operator*(const Vector &v) const {
|
---|
325 | return Vector(col[0].x*v.x + col[1].x*v.y + col[2].x*v.z + col[3].x*v.w,
|
---|
326 | col[0].y*v.x + col[1].y*v.y + col[2].y*v.z + col[3].y*v.w,
|
---|
327 | col[0].z*v.x + col[1].z*v.y + col[2].z*v.z + col[3].z*v.w,
|
---|
328 | col[0].w*v.x + col[1].w*v.y + col[2].w*v.z + col[3].w*v.w);
|
---|
329 | }
|
---|
330 |
|
---|
331 | Vector &operator[](unsigned int column){
|
---|
332 | return ((Vector&)(col[column]));
|
---|
333 | }
|
---|
334 | const Vector &operator[](unsigned int column) const {
|
---|
335 | return ((Vector&)(col[column]));
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | // TMatrix - TMatrix
|
---|
340 | TMatrix operator-(const TMatrix &tm) const {
|
---|
341 | TMatrix res;
|
---|
342 |
|
---|
343 | for (unsigned int c=0; c<4; c++)
|
---|
344 | for (unsigned int f=0; f<4; f++)
|
---|
345 | res[c][f]=col[c][f]-tm[c][f];
|
---|
346 |
|
---|
347 | return res;
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | // TMatrix * TMatrix
|
---|
352 | TMatrix operator*(const TMatrix &tm) const {
|
---|
353 | TMatrix res;
|
---|
354 | for (unsigned int c=0; c<4; c++)
|
---|
355 | for (unsigned int f=0; f<4; f++)
|
---|
356 | {
|
---|
357 | float ff=0.0f;
|
---|
358 | ff+=float(col[0][f]*tm[c][0]);
|
---|
359 | ff+=float(col[1][f]*tm[c][1]);
|
---|
360 | ff+=float(col[2][f]*tm[c][2]);
|
---|
361 | ff+=float(col[3][f]*tm[c][3]);
|
---|
362 | res[c][f]=ff;
|
---|
363 | }
|
---|
364 | return res;
|
---|
365 | }
|
---|
366 |
|
---|
367 | void PostMultiply(const TMatrix &tm){
|
---|
368 | TMatrix res;
|
---|
369 | for (unsigned int f=0; f<4; f++)
|
---|
370 | for (unsigned int c=0; c<4; c++)
|
---|
371 | {
|
---|
372 | float ff=0.0f;
|
---|
373 | ff+=float(col[0][c]*tm[f][0]);
|
---|
374 | ff+=float(col[1][c]*tm[f][1]);
|
---|
375 | ff+=float(col[2][c]*tm[f][2]);
|
---|
376 | ff+=float(col[3][c]*tm[f][3]);
|
---|
377 | res[c][f]=ff;
|
---|
378 | }
|
---|
379 | (*this)=res;
|
---|
380 | }
|
---|
381 |
|
---|
382 | const TMatrix & operator=(const TMatrix &tm){
|
---|
383 | col[0]=tm.col[0];
|
---|
384 | col[1]=tm.col[1];
|
---|
385 | col[2]=tm.col[2];
|
---|
386 | col[3]=tm.col[3];
|
---|
387 | return tm;
|
---|
388 | }
|
---|
389 |
|
---|
390 | bool operator!=(const TMatrix &tm) const {
|
---|
391 | if (col[0]!=tm[0]) return false;
|
---|
392 | if (col[1]!=tm[1]) return false;
|
---|
393 | if (col[2]!=tm[2]) return false;
|
---|
394 | if (col[3]!=tm[3]) return false;
|
---|
395 | return true;
|
---|
396 | }
|
---|
397 |
|
---|
398 | void LoadIdentity(void){
|
---|
399 | col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f; col[3][0]=0.0f;
|
---|
400 | col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f; col[3][1]=0.0f;
|
---|
401 | col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f; col[3][2]=0.0f;
|
---|
402 | col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f;
|
---|
403 | }
|
---|
404 |
|
---|
405 | // returns a mat[16] in opengl conform format
|
---|
406 | // WARNING: returns a dir to a TEMP variable
|
---|
407 | float *GetOpenGLConform(void){
|
---|
408 | tempmat[0]=float(col[0][0]);
|
---|
409 | tempmat[1]=float(col[0][1]);
|
---|
410 | tempmat[2]=float(col[0][2]);
|
---|
411 | tempmat[3]=float(col[0][3]);
|
---|
412 | tempmat[4]=float(col[1][0]);
|
---|
413 | tempmat[5]=float(col[1][1]);
|
---|
414 | tempmat[6]=float(col[1][2]);
|
---|
415 | tempmat[7]=float(col[1][3]);
|
---|
416 | tempmat[8]=float(col[2][0]);
|
---|
417 | tempmat[9]=float(col[2][1]);
|
---|
418 | tempmat[10]=float(col[2][2]);
|
---|
419 | tempmat[11]=float(col[2][3]);
|
---|
420 | tempmat[12]=float(col[3][0]);
|
---|
421 | tempmat[13]=float(col[3][1]);
|
---|
422 | tempmat[14]=float(col[3][2]);
|
---|
423 | tempmat[15]=float(col[3][3]);
|
---|
424 | return tempmat;
|
---|
425 | }
|
---|
426 |
|
---|
427 | void ImportOpenGLMatrix(float *oglmatrix){
|
---|
428 | col[0][0]=oglmatrix[0];
|
---|
429 | col[0][1]=oglmatrix[1];
|
---|
430 | col[0][2]=oglmatrix[2];
|
---|
431 | col[0][3]=oglmatrix[3];
|
---|
432 | col[1][0]=oglmatrix[4];
|
---|
433 | col[1][1]=oglmatrix[5];
|
---|
434 | col[1][2]=oglmatrix[6];
|
---|
435 | col[1][3]=oglmatrix[7];
|
---|
436 | col[2][0]=oglmatrix[8];
|
---|
437 | col[2][1]=oglmatrix[9];
|
---|
438 | col[2][2]=oglmatrix[10];
|
---|
439 | col[2][3]=oglmatrix[11];
|
---|
440 | col[3][0]=oglmatrix[12];
|
---|
441 | col[3][1]=oglmatrix[13];
|
---|
442 | col[3][2]=oglmatrix[14];
|
---|
443 | col[3][3]=oglmatrix[15];
|
---|
444 | }
|
---|
445 |
|
---|
446 | /* // REVISAR ESTO
|
---|
447 | Vector GetAbsolutePos(void) const {
|
---|
448 | return (operator*(col[3]));
|
---|
449 | }
|
---|
450 | */
|
---|
451 | Vector GetTranslation(void) const {
|
---|
452 | return col[3];
|
---|
453 | }
|
---|
454 |
|
---|
455 | // sets the matrix to a rotation matrix
|
---|
456 | void LoadRotationXMatrix(float angle){
|
---|
457 | float cosa=cosf(GradToRad(angle));
|
---|
458 | float sina=sinf(GradToRad(angle));
|
---|
459 | col[0][0]=1.0f; col[1][0]=0.0f; col[2][0]=0.0f; col[3][0]=0.0f;
|
---|
460 | col[0][1]=0.0f; col[1][1]=cosa; col[2][1]=-sina;col[3][1]=0.0f;
|
---|
461 | col[0][2]=0.0f; col[1][2]=sina; col[2][2]=cosa; col[3][2]=0.0f;
|
---|
462 | col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f;
|
---|
463 | }
|
---|
464 |
|
---|
465 | void LoadRotationYMatrix(float angle){
|
---|
466 | float cosa=cosf(GradToRad(angle));
|
---|
467 | float sina=sinf(GradToRad(angle));
|
---|
468 | col[0][0]=cosa; col[1][0]=0.0f; col[2][0]=sina; col[3][0]=0.0f;
|
---|
469 | col[0][1]=0.0f; col[1][1]=1.0f; col[2][1]=0.0f; col[3][1]=0.0f;
|
---|
470 | col[0][2]=-sina;col[1][2]=0.0f; col[2][2]=cosa; col[3][2]=0.0f;
|
---|
471 | col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f;
|
---|
472 | }
|
---|
473 |
|
---|
474 | void LoadRotationZMatrix(float angle){
|
---|
475 | float cosa=cosf(GradToRad(angle));
|
---|
476 | float sina=sinf(GradToRad(angle));
|
---|
477 | col[0][0]=cosa; col[1][0]=-sina;col[2][0]=0.0f; col[3][0]=0.0f;
|
---|
478 | col[0][1]=sina; col[1][1]=cosa; col[2][1]=0.0f; col[3][1]=0.0f;
|
---|
479 | col[0][2]=0.0f; col[1][2]=0.0f; col[2][2]=1.0f; col[3][2]=0.0f;
|
---|
480 | col[0][3]=0.0f; col[1][3]=0.0f; col[2][3]=0.0f; col[3][3]=1.0f;
|
---|
481 | }
|
---|
482 |
|
---|
483 | // Rotate the object around X axis
|
---|
484 | void RotateX(float angle, int mode=MATRIX_OBJECT_SPACE){
|
---|
485 | TMatrix tr;
|
---|
486 | tr.LoadRotationXMatrix(angle); // FIXME!!!!! -angle?!?!
|
---|
487 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
488 | operator=( (*this)*tr );
|
---|
489 | else
|
---|
490 | operator=( tr*(*this) );
|
---|
491 | }
|
---|
492 |
|
---|
493 | void RotateY(float angle, int mode=MATRIX_OBJECT_SPACE){
|
---|
494 | TMatrix tr;
|
---|
495 | tr.LoadRotationYMatrix(angle);
|
---|
496 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
497 | operator=( (*this)*tr );
|
---|
498 | else
|
---|
499 | operator=( tr*(*this) );
|
---|
500 | }
|
---|
501 |
|
---|
502 | void RotateZ(float angle, int mode=MATRIX_OBJECT_SPACE){
|
---|
503 | TMatrix tr;
|
---|
504 | tr.LoadRotationZMatrix(angle);
|
---|
505 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
506 | operator=( (*this)*tr );
|
---|
507 | else
|
---|
508 | operator=( tr*(*this) );
|
---|
509 | }
|
---|
510 |
|
---|
511 | void RotateAxis(float angle, Vector axis, int mode=MATRIX_OBJECT_SPACE){
|
---|
512 | TMatrix Rx,Ry,Rz,iRx,iRy;
|
---|
513 | float d = (float)sqrt(axis.y*axis.y + axis.z*axis.z);
|
---|
514 |
|
---|
515 | Rx.SetBaseX(1.0f, 0.0f, 0.0f);
|
---|
516 | Rx.SetBaseY(0.0f, float(axis.z/d), float(axis.y/d));
|
---|
517 | Rx.SetBaseZ(0.0f, float(-axis.y/d), float(axis.z/d));
|
---|
518 |
|
---|
519 | Ry.SetBaseX(d, 0.0f, float(axis.x));
|
---|
520 | Ry.SetBaseY(0.0f, 1.0f, 0.0f);
|
---|
521 | Ry.SetBaseZ(float(-axis.x), 0.0f, d);
|
---|
522 |
|
---|
523 | Rz.LoadRotationZMatrix(angle);
|
---|
524 |
|
---|
525 | iRx = Rx; iRx.Traspose();
|
---|
526 | iRy = Ry; iRy.Traspose();
|
---|
527 |
|
---|
528 | TMatrix accum;
|
---|
529 | accum.LoadIdentity();
|
---|
530 | if (d!=0)
|
---|
531 | accum = accum * iRx;
|
---|
532 | accum = accum * iRy;
|
---|
533 | accum = accum * Rz;
|
---|
534 | accum = accum * Ry;
|
---|
535 | if (d!=0)
|
---|
536 | accum = accum * Rx;
|
---|
537 |
|
---|
538 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
539 | operator=( (*this)*accum );
|
---|
540 | else
|
---|
541 | operator=( accum*(*this) );
|
---|
542 | }
|
---|
543 |
|
---|
544 | void Translate(Vector tv, int mode=MATRIX_OBJECT_SPACE){
|
---|
545 | TMatrix tr;
|
---|
546 | tr.col[3]=tv;
|
---|
547 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
548 | operator=( (*this)*tr );
|
---|
549 | else
|
---|
550 | operator=( tr*(*this) );
|
---|
551 | }
|
---|
552 |
|
---|
553 | void Scale(Vector sv, int mode=MATRIX_OBJECT_SPACE){
|
---|
554 | if (mode==MATRIX_OBJECT_SPACE)
|
---|
555 | operator=( (*this)*ScaleMatrix(sv) );
|
---|
556 | else
|
---|
557 | operator=( ScaleMatrix(sv)*(*this) );
|
---|
558 | }
|
---|
559 |
|
---|
560 | static TMatrix ScaleMatrix(const Vector &v){
|
---|
561 | TMatrix mat;
|
---|
562 | mat[0].x = v.x;
|
---|
563 | mat[1].y = v.y;
|
---|
564 | mat[2].z = v.z;
|
---|
565 | return mat;
|
---|
566 | }
|
---|
567 |
|
---|
568 | static TMatrix TranslationMatrix(const Vector &v){
|
---|
569 | TMatrix mat;
|
---|
570 | mat[3] = v;
|
---|
571 | mat[3].w = 1.0f;
|
---|
572 | return mat;
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | void Traspose(void){
|
---|
577 | tempmat[0]=float(col[0][0]);
|
---|
578 | tempmat[1]=float(col[0][1]);
|
---|
579 | tempmat[2]=float(col[0][2]);
|
---|
580 | tempmat[3]=float(col[0][3]);
|
---|
581 |
|
---|
582 | tempmat[4]=float(col[1][0]);
|
---|
583 | tempmat[5]=float(col[1][1]);
|
---|
584 | tempmat[6]=float(col[1][2]);
|
---|
585 | tempmat[7]=float(col[1][3]);
|
---|
586 |
|
---|
587 | tempmat[8]=float(col[2][0]);
|
---|
588 | tempmat[9]=float(col[2][1]);
|
---|
589 | tempmat[10]=float(col[2][2]);
|
---|
590 | tempmat[11]=float(col[2][3]);
|
---|
591 |
|
---|
592 | tempmat[12]=float(col[3][0]);
|
---|
593 | tempmat[13]=float(col[3][1]);
|
---|
594 | tempmat[14]=float(col[3][2]);
|
---|
595 | tempmat[15]=float(col[3][3]);
|
---|
596 |
|
---|
597 | col[0].x=tempmat[0]; col[1].x=tempmat[1]; col[2].x=tempmat[2]; col[3].x=tempmat[3];
|
---|
598 | col[0].y=tempmat[4]; col[1].y=tempmat[5]; col[2].y=tempmat[6]; col[3].y=tempmat[7];
|
---|
599 | col[0].z=tempmat[8]; col[1].z=tempmat[9]; col[2].z=tempmat[10]; col[3].z=tempmat[11];
|
---|
600 | col[0].w=tempmat[12]; col[1].w=tempmat[13]; col[2].w=tempmat[14]; col[3].w=tempmat[15];
|
---|
601 | }
|
---|
602 |
|
---|
603 | // This function inverts a (only) TRANSFORM Matrix. This should not work on arbitrary matrices
|
---|
604 | void Invert(void){
|
---|
605 | tempmat[0]=float(col[0][0]);
|
---|
606 | tempmat[1]=float(col[0][1]);
|
---|
607 | tempmat[2]=float(col[0][2]);
|
---|
608 | tempmat[3]=0.0f;
|
---|
609 |
|
---|
610 | tempmat[4]=float(col[1][0]);
|
---|
611 | tempmat[5]=float(col[1][1]);
|
---|
612 | tempmat[6]=float(col[1][2]);
|
---|
613 | tempmat[7]=0.0f;
|
---|
614 |
|
---|
615 | tempmat[8]=float(col[2][0]);
|
---|
616 | tempmat[9]=float(col[2][1]);
|
---|
617 | tempmat[10]=float(col[2][2]);
|
---|
618 | tempmat[11]=0.0f;
|
---|
619 |
|
---|
620 | tempmat[12]=float(-(col[3][0]*col[0][0])-(col[3][1]*col[0][1])-(col[3][2]*col[0][2]));
|
---|
621 | tempmat[13]=float(-(col[3][0]*col[1][0])-(col[3][1]*col[1][1])-(col[3][2]*col[1][2]));
|
---|
622 | tempmat[14]=float(-(col[3][0]*col[2][0])-(col[3][1]*col[2][1])-(col[3][2]*col[2][2]));
|
---|
623 | tempmat[15]=1.0f;
|
---|
624 |
|
---|
625 | col[0].x=tempmat[0]; col[1].x=tempmat[1]; col[2].x=tempmat[2]; col[3].x=tempmat[12];
|
---|
626 | col[0].y=tempmat[4]; col[1].y=tempmat[5]; col[2].y=tempmat[6]; col[3].y=tempmat[13];
|
---|
627 | col[0].z=tempmat[8]; col[1].z=tempmat[9]; col[2].z=tempmat[10]; col[3].z=tempmat[14];
|
---|
628 | col[0].w=0.0f; col[1].w=0.0f; col[2].w=0.0f; col[3].w=tempmat[15];
|
---|
629 | }
|
---|
630 |
|
---|
631 | };
|
---|
632 |
|
---|
633 |
|
---|
634 |
|
---|
635 |
|
---|
636 |
|
---|
637 |
|
---|
638 | #endif
|
---|
639 |
|
---|