-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmat4.c
executable file
·810 lines (679 loc) · 21.5 KB
/
mat4.c
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*
Copyright (c) 2008, Luke Benstead.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file mat4.c
*/
#include <memory.h>
#include <assert.h>
#include <stdlib.h>
#include "utility.h"
#include "vec3.h"
#include "mat4.h"
#include "mat3.h"
#include "quaternion.h"
#include "plane.h"
/**
* Fills a kmMat4 structure with the values from a 16
* element array of kmScalars
* @Params pOut - A pointer to the destination matrix
* pMat - A 16 element array of kmScalars
* @Return Returns pOut so that the call can be nested
*/
kmMat4* const kmMat4Fill(kmMat4* pOut, const kmScalar* pMat)
{
memcpy(pOut->mat, pMat, sizeof(kmScalar) * 16);
return pOut;
}
/**
* Sets pOut to an identity matrix returns pOut
* @Params pOut - A pointer to the matrix to set to identity
* @Return Returns pOut so that the call can be nested
*/
kmMat4* const kmMat4Identity(kmMat4* pOut)
{
memset(pOut->mat, 0, sizeof(kmScalar) * 16);
pOut->mat[0] = pOut->mat[5] = pOut->mat[10] = pOut->mat[15] = 1.0f;
return pOut;
}
kmScalar get(const kmMat4 * pIn, int row, int col)
{
return pIn->mat[row + 4*col];
}
void set(kmMat4 * pIn, int row, int col, kmScalar value)
{
pIn->mat[row + 4*col] = value;
}
void swap(kmMat4 * pIn, int r1, int c1, int r2, int c2)
{
kmScalar tmp = get(pIn,r1,c1);
set(pIn,r1,c1,get(pIn,r2,c2));
set(pIn,r2,c2, tmp);
}
//Returns an upper and a lower triangular matrix which are L and R in the Gauss algorithm
int gaussj(kmMat4 *a, kmMat4 *b)
{
int i, icol = 0, irow = 0, j, k, l, ll, n = 4, m = 4;
kmScalar big, dum, pivinv;
int indxc[n];
int indxr[n];
int ipiv[n];
for (j = 0; j < n; j++) {
ipiv[j] = 0;
}
for (i = 0; i < n; i++) {
big = 0.0f;
for (j = 0; j < n; j++) {
if (ipiv[j] != 1) {
for (k = 0; k < n; k++) {
if (ipiv[k] == 0) {
if (abs(get(a,j, k)) >= big) {
big = abs(get(a,j, k));
irow = j;
icol = k;
}
}
}
}
}
++(ipiv[icol]);
if (irow != icol) {
for (l = 0; l < n; l++) {
swap(a,irow, l, icol, l);
}
for (l = 0; l < m; l++) {
swap(b,irow, l, icol, l);
}
}
indxr[i] = irow;
indxc[i] = icol;
if (get(a,icol, icol) == 0.0) {
return KM_FALSE;
}
pivinv = 1.0f / get(a,icol, icol);
set(a,icol, icol, 1.0f);
for (l = 0; l < n; l++) {
set(a,icol, l, get(a,icol, l) * pivinv);
}
for (l = 0; l < m; l++) {
set(b,icol, l, get(b,icol, l) * pivinv);
}
for (ll = 0; ll < n; ll++) {
if (ll != icol) {
dum = get(a,ll, icol);
set(a,ll, icol, 0.0f);
for (l = 0; l < n; l++) {
set(a,ll, l, get(a,ll, l) - get(a,icol, l) * dum);
}
for (l = 0; l < m; l++) {
set(b,ll, l, get(a,ll, l) - get(b,icol, l) * dum);
}
}
}
}
// This is the end of the main loop over columns of the reduction. It only remains to unscram-
// ble the solution in view of the column interchanges. We do this by interchanging pairs of
// columns in the reverse order that the permutation was built up.
for (l = n - 1; l >= 0; l--) {
if (indxr[l] != indxc[l]) {
for (k = 0; k < n; k++) {
swap(a,k, indxr[l], k, indxc[l]);
}
}
}
return KM_TRUE;
}
/**
* Calculates the inverse of pM and stores the result in
* pOut.
* @Return Returns NULL if there is no inverse, else pOut
*/
kmMat4* const kmMat4Inverse(kmMat4* pOut, const kmMat4* pM)
{
kmMat4 inv;
kmMat4Assign(&inv, pM);
kmMat4 tmp;
kmMat4Identity(&tmp);
if(gaussj(&inv, &tmp) == KM_FALSE) {
return NULL;
}
kmMat4Assign(pOut, &inv);
return pOut;
}
/**
* Returns KM_TRUE if pIn is an identity matrix
* KM_FALSE otherwise
*/
const int kmMat4IsIdentity(const kmMat4* pIn)
{
static const kmScalar identity [] = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
return (memcmp(identity, pIn->mat, sizeof(kmScalar) * 16) == 0);
}
/**
* Sets pOut to the transpose of pIn, returns pOut
*/
kmMat4* const kmMat4Transpose(kmMat4* pOut, const kmMat4* pIn)
{
int x, z;
for (z = 0; z < 4; ++z) {
for (x = 0; x < 4; ++x) {
pOut->mat[(z * 4) + x] = pIn->mat[(x * 4) + z];
}
}
return pOut;
}
/**
* Multiplies pM1 with pM2, stores the result in pOut, returns pOut
*/
kmMat4* const kmMat4Multiply(kmMat4* pOut, const kmMat4* pM1, const kmMat4* pM2)
{
kmScalar mat[16];
const kmScalar *m1 = pM1->mat, *m2 = pM2->mat;
mat[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
mat[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
mat[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
mat[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
mat[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
mat[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
mat[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
mat[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
mat[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
mat[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
mat[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
mat[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
mat[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
mat[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
mat[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
mat[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
memcpy(pOut->mat, mat, sizeof(kmScalar)*16);
return pOut;
}
/**
* Assigns the value of pIn to pOut
*/
kmMat4* const kmMat4Assign(kmMat4* pOut, const kmMat4* pIn)
{
assert(pOut != pIn && "You have tried to self-assign!!");
memcpy(pOut->mat, pIn->mat, sizeof(kmScalar)*16);
return pOut;
}
kmMat4* const kmMat4AssignMat3(kmMat4* pOut, const kmMat3* pIn) {
kmMat4Identity(pOut);
pOut->mat[0] = pIn->mat[0];
pOut->mat[1] = pIn->mat[1];
pOut->mat[2] = pIn->mat[2];
pOut->mat[3] = 0.0;
pOut->mat[4] = pIn->mat[3];
pOut->mat[5] = pIn->mat[4];
pOut->mat[6] = pIn->mat[5];
pOut->mat[7] = 0.0;
pOut->mat[8] = pIn->mat[6];
pOut->mat[9] = pIn->mat[7];
pOut->mat[10] = pIn->mat[8];
pOut->mat[11] = 0.0;
return pOut;
}
/**
* Returns KM_TRUE if the 2 matrices are equal (approximately)
*/
const int kmMat4AreEqual(const kmMat4* pMat1, const kmMat4* pMat2)
{
int i = 0;
assert(pMat1 != pMat2 && "You are comparing the same thing!");
for (i = 0; i < 16; ++i)
{
if (!(pMat1->mat[i] + kmEpsilon > pMat2->mat[i] &&
pMat1->mat[i] - kmEpsilon < pMat2->mat[i])) {
return KM_FALSE;
}
}
return KM_TRUE;
}
/**
* Build a rotation matrix from an axis and an angle. Result is stored in pOut.
* pOut is returned.
*/
kmMat4* const kmMat4RotationAxisAngle(kmMat4* pOut, const kmVec3* axis, kmScalar radians)
{
kmScalar rcos = cosf(radians);
kmScalar rsin = sinf(radians);
kmVec3 normalizedAxis;
kmVec3Normalize(&normalizedAxis, axis);
pOut->mat[0] = rcos + normalizedAxis.x * normalizedAxis.x * (1 - rcos);
pOut->mat[1] = normalizedAxis.z * rsin + normalizedAxis.y * normalizedAxis.x * (1 - rcos);
pOut->mat[2] = -normalizedAxis.y * rsin + normalizedAxis.z * normalizedAxis.x * (1 - rcos);
pOut->mat[3] = 0.0f;
pOut->mat[4] = -normalizedAxis.z * rsin + normalizedAxis.x * normalizedAxis.y * (1 - rcos);
pOut->mat[5] = rcos + normalizedAxis.y * normalizedAxis.y * (1 - rcos);
pOut->mat[6] = normalizedAxis.x * rsin + normalizedAxis.z * normalizedAxis.y * (1 - rcos);
pOut->mat[7] = 0.0f;
pOut->mat[8] = normalizedAxis.y * rsin + normalizedAxis.x * normalizedAxis.z * (1 - rcos);
pOut->mat[9] = -normalizedAxis.x * rsin + normalizedAxis.y * normalizedAxis.z * (1 - rcos);
pOut->mat[10] = rcos + normalizedAxis.z * normalizedAxis.z * (1 - rcos);
pOut->mat[11] = 0.0f;
pOut->mat[12] = 0.0f;
pOut->mat[13] = 0.0f;
pOut->mat[14] = 0.0f;
pOut->mat[15] = 1.0f;
return pOut;
}
/**
* Builds an X-axis rotation matrix and stores it in pOut, returns pOut
*/
kmMat4* const kmMat4RotationX(kmMat4* pOut, const kmScalar radians)
{
/*
| 1 0 0 0 |
M = | 0 cos(A) -sin(A) 0 |
| 0 sin(A) cos(A) 0 |
| 0 0 0 1 |
*/
pOut->mat[0] = 1.0f;
pOut->mat[1] = 0.0f;
pOut->mat[2] = 0.0f;
pOut->mat[3] = 0.0f;
pOut->mat[4] = 0.0f;
pOut->mat[5] = cosf(radians);
pOut->mat[6] = sinf(radians);
pOut->mat[7] = 0.0f;
pOut->mat[8] = 0.0f;
pOut->mat[9] = -sinf(radians);
pOut->mat[10] = cosf(radians);
pOut->mat[11] = 0.0f;
pOut->mat[12] = 0.0f;
pOut->mat[13] = 0.0f;
pOut->mat[14] = 0.0f;
pOut->mat[15] = 1.0f;
return pOut;
}
/**
* Builds a rotation matrix using the rotation around the Y-axis
* The result is stored in pOut, pOut is returned.
*/
kmMat4* const kmMat4RotationY(kmMat4* pOut, const kmScalar radians)
{
/*
| cos(A) 0 sin(A) 0 |
M = | 0 1 0 0 |
| -sin(A) 0 cos(A) 0 |
| 0 0 0 1 |
*/
pOut->mat[0] = cosf(radians);
pOut->mat[1] = 0.0f;
pOut->mat[2] = -sinf(radians);
pOut->mat[3] = 0.0f;
pOut->mat[4] = 0.0f;
pOut->mat[5] = 1.0f;
pOut->mat[6] = 0.0f;
pOut->mat[7] = 0.0f;
pOut->mat[8] = sinf(radians);
pOut->mat[9] = 0.0f;
pOut->mat[10] = cosf(radians);
pOut->mat[11] = 0.0f;
pOut->mat[12] = 0.0f;
pOut->mat[13] = 0.0f;
pOut->mat[14] = 0.0f;
pOut->mat[15] = 1.0f;
return pOut;
}
/**
* Builds a rotation matrix around the Z-axis. The resulting
* matrix is stored in pOut. pOut is returned.
*/
kmMat4* const kmMat4RotationZ(kmMat4* pOut, const kmScalar radians)
{
/*
| cos(A) -sin(A) 0 0 |
M = | sin(A) cos(A) 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
*/
pOut->mat[0] = cosf(radians);
pOut->mat[1] = sinf(radians);
pOut->mat[2] = 0.0f;
pOut->mat[3] = 0.0f;
pOut->mat[4] = -sinf(radians);;
pOut->mat[5] = cosf(radians);
pOut->mat[6] = 0.0f;
pOut->mat[7] = 0.0f;
pOut->mat[8] = 0.0f;
pOut->mat[9] = 0.0f;
pOut->mat[10] = 1.0f;
pOut->mat[11] = 0.0f;
pOut->mat[12] = 0.0f;
pOut->mat[13] = 0.0f;
pOut->mat[14] = 0.0f;
pOut->mat[15] = 1.0f;
return pOut;
}
/**
* Builds a rotation matrix from pitch, yaw and roll. The resulting
* matrix is stored in pOut and pOut is returned
*/
kmMat4* const kmMat4RotationPitchYawRoll(kmMat4* pOut, const kmScalar pitch, const kmScalar yaw, const kmScalar roll)
{
double cr = cos(pitch);
double sr = sin(pitch);
double cp = cos(yaw);
double sp = sin(yaw);
double cy = cos(roll);
double sy = sin(roll);
double srsp = sr * sp;
double crsp = cr * sp;
pOut->mat[0] = (kmScalar) cp * cy;
pOut->mat[4] = (kmScalar) cp * sy;
pOut->mat[8] = (kmScalar) - sp;
pOut->mat[1] = (kmScalar) srsp * cy - cr * sy;
pOut->mat[5] = (kmScalar) srsp * sy + cr * cy;
pOut->mat[9] = (kmScalar) sr * cp;
pOut->mat[2] = (kmScalar) crsp * cy + sr * sy;
pOut->mat[6] = (kmScalar) crsp * sy - sr * cy;
pOut->mat[10] = (kmScalar) cr * cp;
pOut->mat[3] = pOut->mat[7] = pOut->mat[11] = 0.0;
pOut->mat[15] = 1.0;
return pOut;
}
/** Converts a quaternion to a rotation matrix,
* the result is stored in pOut, returns pOut
*/
kmMat4* const kmMat4RotationQuaternion(kmMat4* pOut, const kmQuaternion* pQ)
{
kmScalar x2 = pQ->x * pQ->x;
kmScalar y2 = pQ->y * pQ->y;
kmScalar z2 = pQ->z * pQ->z;
kmScalar xy = pQ->x * pQ->y;
kmScalar xz = pQ->x * pQ->z;
kmScalar yz = pQ->y * pQ->z;
kmScalar wx = pQ->w * pQ->x;
kmScalar wy = pQ->w * pQ->y;
kmScalar wz = pQ->w * pQ->z;
pOut->mat[0] = 1.0 - 2.0 * (y2 + z2);
pOut->mat[1] = 2.0 * (xy - wz);
pOut->mat[2] = 2.0 * (xz + wy);
pOut->mat[3] = 0.0;
// Second row
pOut->mat[4] = 2.0 * (xy + wz);
pOut->mat[5] = 1.0 - 2.0 * (x2 + z2);
pOut->mat[6] = 2.0 * (yz - wx);
pOut->mat[7] = 0.0;
// Third row
pOut->mat[8] = 2.0 * (xz - wy);
pOut->mat[9] = 2.0 * (yz + wx);
pOut->mat[10] = 1.0 - 2.0 * (x2 + y2);
pOut->mat[11] = 0.0;
// Fourth row
pOut->mat[12] = 0;
pOut->mat[13] = 0;
pOut->mat[14] = 0;
pOut->mat[15] = 1.0;
return pOut;
}
/** Builds a scaling matrix */
kmMat4* const kmMat4Scaling(kmMat4* pOut, const kmScalar x, const kmScalar y,
const kmScalar z)
{
memset(pOut->mat, 0, sizeof(kmScalar) * 16);
pOut->mat[0] = x;
pOut->mat[5] = y;
pOut->mat[10] = z;
pOut->mat[15] = 1.0f;
return pOut;
}
/**
* Builds a translation matrix. All other elements in the matrix
* will be set to zero except for the diagonal which is set to 1.0
*/
kmMat4* const kmMat4Translation(kmMat4* pOut, const kmScalar x,
const kmScalar y, const kmScalar z)
{
//FIXME: Write a test for this
memset(pOut->mat, 0, sizeof(kmScalar) * 16);
pOut->mat[0] = 1.0f;
pOut->mat[5] = 1.0f;
pOut->mat[10] = 1.0f;
pOut->mat[12] = x;
pOut->mat[13] = y;
pOut->mat[14] = z;
pOut->mat[15] = 1.0f;
return pOut;
}
/**
* Get the up vector from a matrix. pIn is the matrix you
* wish to extract the vector from. pOut is a pointer to the
* kmVec3 structure that should hold the resulting vector
*/
kmVec3* const kmMat4GetUpVec3(kmVec3* pOut, const kmMat4* pIn)
{
pOut->x = pIn->mat[4];
pOut->y = pIn->mat[5];
pOut->z = pIn->mat[6];
kmVec3Normalize(pOut, pOut);
return pOut;
}
/** Extract the right vector from a 4x4 matrix. The result is
* stored in pOut. Returns pOut.
*/
kmVec3* const kmMat4GetRightVec3(kmVec3* pOut, const kmMat4* pIn)
{
pOut->x = pIn->mat[0];
pOut->y = pIn->mat[1];
pOut->z = pIn->mat[2];
kmVec3Normalize(pOut, pOut);
return pOut;
}
/**
* Extract the forward vector from a 4x4 matrix. The result is
* stored in pOut. Returns pOut.
*/
kmVec3* const kmMat4GetForwardVec3(kmVec3* pOut, const kmMat4* pIn)
{
pOut->x = pIn->mat[8];
pOut->y = pIn->mat[9];
pOut->z = pIn->mat[10];
kmVec3Normalize(pOut, pOut);
return pOut;
}
/**
* Creates a perspective projection matrix in the
* same way as gluPerspective
*/
kmMat4* const kmMat4PerspectiveProjection(kmMat4* pOut, kmScalar fovY,
kmScalar aspect, kmScalar zNear,
kmScalar zFar)
{
kmScalar r = kmDegreesToRadians(fovY / 2);
kmScalar deltaZ = zFar - zNear;
kmScalar s = sin(r);
kmScalar cotangent = 0;
if (deltaZ == 0 || s == 0 || aspect == 0) {
return NULL;
}
//cos(r) / sin(r) = cot(r)
cotangent = cos(r) / s;
kmMat4Identity(pOut);
pOut->mat[0] = cotangent / aspect;
pOut->mat[5] = cotangent;
pOut->mat[10] = -(zFar + zNear) / deltaZ;
pOut->mat[11] = -1;
pOut->mat[14] = -2 * zNear * zFar / deltaZ;
pOut->mat[15] = 0;
return pOut;
}
/** Creates an orthographic projection matrix like glOrtho */
kmMat4* const kmMat4OrthographicProjection(kmMat4* pOut, kmScalar left,
kmScalar right, kmScalar bottom,
kmScalar top, kmScalar nearVal,
kmScalar farVal)
{
kmScalar tx = -((right + left) / (right - left));
kmScalar ty = -((top + bottom) / (top - bottom));
kmScalar tz = -((farVal + nearVal) / (farVal - nearVal));
kmMat4Identity(pOut);
pOut->mat[0] = 2 / (right - left);
pOut->mat[5] = 2 / (top - bottom);
pOut->mat[10] = -2 / (farVal - nearVal);
pOut->mat[12] = tx;
pOut->mat[13] = ty;
pOut->mat[14] = tz;
return pOut;
}
/**
* Builds a translation matrix in the same way as gluLookAt()
* the resulting matrix is stored in pOut. pOut is returned.
*/
kmMat4* const kmMat4LookAt(kmMat4* pOut, const kmVec3* pEye,
const kmVec3* pCenter, const kmVec3* pUp)
{
kmVec3 f, up, s, u;
kmMat4 translate;
kmVec3Subtract(&f, pCenter, pEye);
kmVec3Normalize(&f, &f);
kmVec3Assign(&up, pUp);
kmVec3Normalize(&up, &up);
kmVec3Cross(&s, &f, &up);
kmVec3Normalize(&s, &s);
kmVec3Cross(&u, &s, &f);
kmVec3Normalize(&s, &s);
kmMat4Identity(pOut);
pOut->mat[0] = s.x;
pOut->mat[4] = s.y;
pOut->mat[8] = s.z;
pOut->mat[1] = u.x;
pOut->mat[5] = u.y;
pOut->mat[9] = u.z;
pOut->mat[2] = -f.x;
pOut->mat[6] = -f.y;
pOut->mat[10] = -f.z;
kmMat4Translation(&translate, -pEye->x, -pEye->y, -pEye->z);
kmMat4Multiply(pOut, pOut, &translate);
return pOut;
}
/**
* Extract a 3x3 rotation matrix from the input 4x4 transformation.
* Stores the result in pOut, returns pOut
*/
kmMat3* const kmMat4ExtractRotation(kmMat3* pOut, const kmMat4* pIn)
{
pOut->mat[0] = pIn->mat[0];
pOut->mat[1] = pIn->mat[1];
pOut->mat[2] = pIn->mat[2];
pOut->mat[3] = pIn->mat[4];
pOut->mat[4] = pIn->mat[5];
pOut->mat[5] = pIn->mat[6];
pOut->mat[6] = pIn->mat[8];
pOut->mat[7] = pIn->mat[9];
pOut->mat[8] = pIn->mat[10];
return pOut;
}
/**
* Take the rotation from a 4x4 transformation matrix, and return it as an axis and an angle (in radians)
* returns the output axis.
*/
kmVec3* const kmMat4RotationToAxisAngle(kmVec3* pAxis, kmScalar* radians, const kmMat4* pIn)
{
/*Surely not this easy?*/
kmQuaternion temp;
kmMat3 rotation;
kmMat4ExtractRotation(&rotation, pIn);
kmQuaternionRotationMatrix(&temp, &rotation);
kmQuaternionToAxisAngle(&temp, pAxis, radians);
return pAxis;
}
/** Build a 4x4 OpenGL transformation matrix using a 3x3 rotation matrix,
* and a 3d vector representing a translation. Assign the result to pOut,
* pOut is also returned.
*/
kmMat4* const kmMat4RotationTranslation(kmMat4* pOut, const kmMat3* rotation, const kmVec3* translation)
{
pOut->mat[0] = rotation->mat[0];
pOut->mat[1] = rotation->mat[1];
pOut->mat[2] = rotation->mat[2];
pOut->mat[3] = 0.0f;
pOut->mat[4] = rotation->mat[3];
pOut->mat[5] = rotation->mat[4];
pOut->mat[6] = rotation->mat[5];
pOut->mat[7] = 0.0f;
pOut->mat[8] = rotation->mat[6];
pOut->mat[9] = rotation->mat[7];
pOut->mat[10] = rotation->mat[8];
pOut->mat[11] = 0.0f;
pOut->mat[12] = translation->x;
pOut->mat[13] = translation->y;
pOut->mat[14] = translation->z;
pOut->mat[15] = 1.0f;
return pOut;
}
kmPlane* const kmMat4ExtractPlane(kmPlane* pOut, const kmMat4* pIn, const kmEnum plane)
{
kmScalar t = 1.0f;
switch(plane) {
case KM_PLANE_RIGHT:
pOut->a = pIn->mat[3] - pIn->mat[0];
pOut->b = pIn->mat[7] - pIn->mat[4];
pOut->c = pIn->mat[11] - pIn->mat[8];
pOut->d = pIn->mat[15] - pIn->mat[12];
break;
case KM_PLANE_LEFT:
pOut->a = pIn->mat[3] + pIn->mat[0];
pOut->b = pIn->mat[7] + pIn->mat[4];
pOut->c = pIn->mat[11] + pIn->mat[8];
pOut->d = pIn->mat[15] + pIn->mat[12];
break;
case KM_PLANE_BOTTOM:
pOut->a = pIn->mat[3] + pIn->mat[1];
pOut->b = pIn->mat[7] + pIn->mat[5];
pOut->c = pIn->mat[11] + pIn->mat[9];
pOut->d = pIn->mat[15] + pIn->mat[13];
break;
case KM_PLANE_TOP:
pOut->a = pIn->mat[3] - pIn->mat[1];
pOut->b = pIn->mat[7] - pIn->mat[5];
pOut->c = pIn->mat[11] - pIn->mat[9];
pOut->d = pIn->mat[15] - pIn->mat[13];
break;
case KM_PLANE_FAR:
pOut->a = pIn->mat[3] - pIn->mat[2];
pOut->b = pIn->mat[7] - pIn->mat[6];
pOut->c = pIn->mat[11] - pIn->mat[10];
pOut->d = pIn->mat[15] - pIn->mat[14];
break;
case KM_PLANE_NEAR:
pOut->a = pIn->mat[3] + pIn->mat[2];
pOut->b = pIn->mat[7] + pIn->mat[6];
pOut->c = pIn->mat[11] + pIn->mat[10];
pOut->d = pIn->mat[15] + pIn->mat[14];
break;
default:
assert(0 && "Invalid plane index");
}
t = sqrtf(pOut->a * pOut->a +
pOut->b * pOut->b +
pOut->c * pOut->c);
pOut->a /= t;
pOut->b /= t;
pOut->c /= t;
pOut->d /= t;
return pOut;
}