-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathglmatrix.cpp
470 lines (379 loc) · 10.6 KB
/
glmatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Copyright (c) 2000-2007 Lee Thomason (www.grinninglizard.com)
Grinning Lizard Utilities.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "glmatrix.h"
#include "glgeometry.h"
#include "glrectangle.h"
using namespace grinliz;
void grinliz::SinCosDegree( float degreeTheta, float* sinTheta, float* cosTheta )
{
degreeTheta = NormalizeAngleDegrees( degreeTheta );
if ( degreeTheta == 0.0f ) {
*sinTheta = 0.0f;
*cosTheta = 1.0f;
}
else if ( degreeTheta == 90.0f ) {
*sinTheta = 1.0f;
*cosTheta = 0.0f;
}
else if ( degreeTheta == 180.0f ) {
*sinTheta = 0.0f;
*cosTheta = -1.0f;
}
else if ( degreeTheta == 270.0f ) {
*sinTheta = -1.0f;
*cosTheta = 0.0f;
}
else {
float radian = ToRadian( degreeTheta );
*sinTheta = sinf( radian );
*cosTheta = cosf( radian );
}
}
void Matrix4::ConcatRotation( float thetaDegree, int axis )
{
Matrix4 r;
switch ( axis ) {
case 0: r.SetXRotation( thetaDegree ); break;
case 1: r.SetYRotation( thetaDegree ); break;
case 2: r.SetZRotation( thetaDegree ); break;
default:
GLASSERT( 0 );
}
*this = (*this) * r;
}
void Matrix4::SetXRotation( float theta )
{
float cosTheta, sinTheta;
SinCosDegree( theta, &sinTheta, &cosTheta );
// COLUMN 1
x[0] = 1.0f;
x[1] = 0.0f;
x[2] = 0.0f;
// COLUMN 2
x[4] = 0.0f;
x[5] = cosTheta;
x[6] = sinTheta;
// COLUMN 3
x[8] = 0.0f;
x[9] = -sinTheta;
x[10] = cosTheta;
// COLUMN 4
x[12] = 0.0f;
x[13] = 0.0f;
x[14] = 0.0f;
}
void Matrix4::SetYRotation( float theta )
{
float cosTheta, sinTheta;
SinCosDegree( theta, &sinTheta, &cosTheta );
// COLUMN 1
x[0] = cosTheta;
x[1] = 0.0f;
x[2] = -sinTheta;
// COLUMN 2
x[4] = 0.0f;
x[5] = 1.0f;
x[6] = 0.0f;
// COLUMN 3
x[8] = sinTheta;
x[9] = 0;
x[10] = cosTheta;
// COLUMN 4
x[12] = 0.0f;
x[13] = 0.0f;
x[14] = 0.0f;
}
void Matrix4::SetZRotation( float theta )
{
float cosTheta, sinTheta;
SinCosDegree( theta, &sinTheta, &cosTheta );
// COLUMN 1
x[0] = cosTheta;
x[1] = sinTheta;
x[2] = 0.0f;
// COLUMN 2
x[4] = -sinTheta;
x[5] = cosTheta;
x[6] = 0.0f;
// COLUMN 3
x[8] = 0.0f;
x[9] = 0.0f;
x[10] = 1.0f;
// COLUMN 4
x[12] = 0.0f;
x[13] = 0.0f;
x[14] = 0.0f;
}
float Matrix4::CalcRotationAroundAxis( int axis ) const
{
float theta = 0.0f;
switch ( axis ) {
case 0: theta = atan2f( x[6], x[10] ); break;
case 1: theta = atan2f( x[8], x[0] ); break;
case 2: theta = atan2f( x[1], x[0] ); break;
default:
GLASSERT( 0 );
}
theta *= RAD_TO_DEG;
if ( theta < 0.0f ) theta += 360.0f;
return theta ;
}
void Matrix4::SetAxisAngle( const Vector3F& axis, float angle )
{
angle *= DEG_TO_RAD;
float c = (float) cos( angle );
float s = (float) sin( angle );
float t = 1.0f - c;
x[0] = t*axis.x*axis.x + c;
x[1] = t*axis.x*axis.y - s*axis.z;
x[2] = t*axis.x*axis.z + s*axis.y;
x[3] = 0.0f;
x[4] = t*axis.x*axis.y + s*axis.z;
x[5] = t*axis.y*axis.y + c;
x[6] = t*axis.y*axis.z - s*axis.x;
x[7] = 0.0f;
x[8] = t*axis.x*axis.z - s*axis.y;
x[9] = t*axis.y*axis.z + s*axis.x;
x[10] = t*axis.z*axis.z + c;
x[11] = 0.0f;
x[12] = x[13] = x[14] = 0.0f;
x[15] = 1.0f;
}
void Matrix4::SetFrustum( float left, float right, float bottom, float top, float near, float far )
{
SetIdentity();
if ( near <= 0.0 || far <= 0.0 || near == far || left == right || top == bottom)
{
GLASSERT( 0 );
}
else {
float x = (2.0f*near) / (right-left);
float y = (2.0f*near) / (top-bottom);
float a = (right+left) / (right-left);
float b = (top+bottom) / (top-bottom);
float c = -(far+near) / ( far-near);
float d = -(2.0f*far*near) / (far-near);
Set( 0, 0, x ); Set( 0, 1, 0 ); Set( 0, 2, a ); Set( 0, 3, 0 );
Set( 1, 0, 0 ); Set( 1, 1, y ); Set( 1, 2, b ); Set( 1, 3, 0 );
Set( 2, 0, 0 ); Set( 2, 1, 0 ); Set( 2, 2, c ); Set( 2, 3, d );
Set( 3, 0, 0 ); Set( 3, 1, 0 ); Set( 3, 2, -1.0f ); Set( 3, 3, 0 );
}
}
void Matrix4::SetOrtho( float left, float right, float bottom, float top, float zNear, float zFar )
{
SetIdentity();
if ( ( right != left ) && ( zFar != zNear ) && ( top != bottom ) ) {
Set( 0, 0, 2.0f / (right-left ) );
Set( 0, 3, -(right+left) / (right-left) );
Set( 1, 1, 2.0f / (top-bottom) );
Set( 1, 3, -(top+bottom) / (top-bottom) );
Set( 2, 2, -2.0f / (zFar-zNear) );
Set( 2, 3, -(zFar+zNear) / (zFar-zNear) );
}
else {
GLASSERT( 0 );
}
}
bool Matrix4::IsRotation() const
{
// Check the rows and columns:
for( int i=0; i<4; ++i )
{
float row = 0.0f;
float col = 0.0f;
for( int j=0; j<4; ++j )
{
row += x[i+j*4]*x[i+j*4];
col += x[i*4+j]*x[i*4+j];
}
if ( !Equal( row, 1.0f, 0.0001f ) )
return false;
if ( !Equal( row, 1.0f, 0.0001f ) )
return false;
}
// Should also really check orthogonality.
return true;
}
void Matrix4::Transpose( Matrix4* transpose ) const
{
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
transpose->x[INDEX(c,r)] = x[INDEX(r,c)];
}
}
}
float Matrix4::Determinant() const
{
// 11 12 13 14
// 21 22 23 24
// 31 32 33 34
// 41 42 43 44
// Old school. Derived from Graphics Gems 1
return m11 * Determinant3x3( m22, m23, m24,
m32, m33, m34,
m42, m43, m44 ) -
m12 * Determinant3x3( m21, m23, m24,
m31, m33, m34,
m41, m43, m44 ) +
m13 * Determinant3x3( m21, m22, m24,
m31, m32, m34,
m41, m42, m44 ) -
m14 * Determinant3x3( m21, m22, m23,
m31, m32, m33,
m41, m42, m43 );
/*
// The determinant is the dot product of:
// the first row and the first row of cofactors
// which is the first col of the adjoint matrix
Matrix4 cofactor;
Cofactor( &cofactor );
Vector3F rowOfCofactor, rowOfThis;
Row( 0, &rowOfThis );
float det = 0.0f;
for( int c=0; c<4; ++c ) {
cofactor.Row( c, &rowOfCofactor );
det += DotProduct( rowOfCofactor, rowOfThis );
}
return det;
*/
}
void Matrix4::Adjoint( Matrix4* adjoint ) const
{
Matrix4 cofactor;
Cofactor( &cofactor );
cofactor.Transpose( adjoint );
}
void Matrix4::Invert( Matrix4* inverse ) const
{
// The inverse is the adjoint / determinant of adjoint
Adjoint( inverse );
float d = Determinant();
inverse->ApplyScalar( 1.0f / d );
#ifdef DEBUG
Matrix4 result;
Matrix4 identity;
MultMatrix4( *this, *inverse, &result );
GLASSERT( Equal( identity, result, 0.001f ) );
#endif
}
void Matrix4::InvertRotationMat( Matrix4* inverse ) const
{
// http://graphics.stanford.edu/courses/cs248-98-fall/Final/q4.html
*inverse = *this;
// Transpose the rotation terms.
Swap( &inverse->m12, &inverse->m21 );
Swap( &inverse->m13, &inverse->m31 );
Swap( &inverse->m23, &inverse->m32 );
const Vector3F* u = (const Vector3F*)(&x[0]);
const Vector3F* v = (const Vector3F*)(&x[4]);
const Vector3F* w = (const Vector3F*)(&x[8]);
const Vector3F* t = (const Vector3F*)(&x[12]);
inverse->m14 = -DotProduct( *u, *t );
inverse->m24 = -DotProduct( *v, *t );
inverse->m34 = -DotProduct( *w, *t );
#ifdef DEBUG
Matrix4 result;
Matrix4 identity;
MultMatrix4( *this, *inverse, &result );
GLASSERT( Equal( identity, result, 0.001f ) );
#endif
}
void Matrix4::Cofactor( Matrix4* cofactor ) const
{
int i = 1;
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
float det = SubDeterminant(r, c);
cofactor->x[INDEX(r,c)] = i * det; // i flip flops between 1 and -1
i = -i;
}
i = -i;
}
}
float Matrix4::SubDeterminant(int excludeRow, int excludeCol) const
{
// Compute non-excluded row and column indices
int row[3];
int col[3];
for (int i = 0; i < 3; ++i) {
row[i] = i;
col[i] = i;
if (i >= excludeRow) {
++row[i];
}
if (i >= excludeCol) {
++col[i];
}
}
// Compute the first row of cofactors
float cofactor00 =
x[INDEX(row[1],col[1])] * x[INDEX(row[2],col[2])] -
x[INDEX(row[1],col[2])] * x[INDEX(row[2],col[1])];
float cofactor10 =
x[INDEX(row[1],col[2])] * x[INDEX(row[2],col[0])] -
x[INDEX(row[1],col[0])] * x[INDEX(row[2],col[2])];
float cofactor20 =
x[INDEX(row[1],col[0])] * x[INDEX(row[2],col[1])] -
x[INDEX(row[1],col[1])] * x[INDEX(row[2],col[0])];
// Product of the first row and the cofactors along the first row
return x[INDEX(row[0],col[0])] * cofactor00 +
x[INDEX(row[0],col[1])] * cofactor10 +
x[INDEX(row[0],col[2])] * cofactor20;
}
bool Matrix4::SetLookAt( const Vector3F& eye, const Vector3F& center, const Vector3F& up )
{
Vector3F forward = center - eye;
forward.Normalize();
/* Side = forward x up */
Vector3F side;
CrossProduct(forward, up, &side);
side.Normalize();
/* Recompute up as: up = side x forward */
Vector3F up2;
CrossProduct(side, forward, &up2);
SetIdentity();
x[ INDEX(0,0) ] = side.x;
x[ INDEX(0,1) ] = side.y;
x[ INDEX(0,2) ] = side.z;
x[ INDEX(1,0)] = up2.x;
x[ INDEX(1,1)] = up2.y;
x[ INDEX(1,2)] = up2.z;
x[ INDEX(2,0)] = -forward.x;
x[ INDEX(2,1)] = -forward.y;
x[ INDEX(2,2)] = -forward.z;
Matrix4 m, n;
m.SetTranslation( -eye.x, -eye.y, -eye.z );
MultMatrix4( *this, m, &n );
*this = n;
return true;
}
void grinliz::MultMatrix4( const Matrix4& m, const Rectangle3F& in, Rectangle3F* out )
{
Vector3F q;
Vector3F p = m * in.min;
out->min = p;
out->max = p;
for( int i=1; i<8; ++i ) {
q.Set( (i&1) ? in.max.x : in.min.x,
(i&2) ? in.max.y : in.min.y,
(i&4) ? in.max.z : in.min.z );
p = m * q;
out->DoUnion( p );
}
}