forked from n64decomp/sm64
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgd_math.c
973 lines (833 loc) · 27 KB
/
gd_math.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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
#include <PR/ultratypes.h>
#include "debug_utils.h"
#include "gd_macros.h"
#include "gd_main.h"
#include "gd_math.h"
#include "gd_types.h"
#include "macros.h"
#include "renderer.h"
/**
* Finds the square root of a float by treating
* it as a double and finding the square root from there.
*/
f32 gd_sqrt_f(f32 val) {
return (f32) gd_sqrt_d(val);
}
/**
* Set mtx to a look-at matrix for the camera. The resulting transformation
* transforms the world as if there exists a camera at position 'from' pointed
* at the position 'to'.
* An effective goddard copy of mtxf_lookat.
*/
void gd_mat4f_lookat(Mat4f *mtx, f32 xFrom, f32 yFrom, f32 zFrom, f32 xTo, f32 yTo, f32 zTo,
f32 zColY, f32 yColY, f32 xColY) {
f32 invLength;
struct GdVec3f d;
struct GdVec3f colX;
struct GdVec3f norm;
// No reason to do this? mtx is set lower.
gd_set_identity_mat4(mtx);
d.z = xTo - xFrom;
d.y = yTo - yFrom;
d.x = zTo - zFrom;
invLength = ABS(d.z) + ABS(d.y) + ABS(d.x);
// Scales 'd' if smaller than 10 or larger than 10,000 to be
// of a magnitude of 10,000.
if (invLength > 10000.0f || invLength < 10.0f) {
norm.x = d.z;
norm.y = d.y;
norm.z = d.x;
gd_normalize_vec3f(&norm);
norm.x *= 10000.0f;
norm.y *= 10000.0f;
norm.z *= 10000.0f;
d.z = norm.x;
d.y = norm.y;
d.x = norm.z;
}
invLength = -1.0 / gd_sqrt_f(SQ(d.z) + SQ(d.y) + SQ(d.x));
d.z *= invLength;
d.y *= invLength;
d.x *= invLength;
colX.z = yColY * d.x - xColY * d.y;
colX.y = xColY * d.z - zColY * d.x;
colX.x = zColY * d.y - yColY * d.z;
invLength = 1.0 / gd_sqrt_f(SQ(colX.z) + SQ(colX.y) + SQ(colX.x));
colX.z *= invLength;
colX.y *= invLength;
colX.x *= invLength;
zColY = d.y * colX.x - d.x * colX.y;
yColY = d.x * colX.z - d.z * colX.x;
xColY = d.z * colX.y - d.y * colX.z;
invLength = 1.0 / gd_sqrt_f(SQ(zColY) + SQ(yColY) + SQ(xColY));
zColY *= invLength;
yColY *= invLength;
xColY *= invLength;
(*mtx)[0][0] = colX.z;
(*mtx)[1][0] = colX.y;
(*mtx)[2][0] = colX.x;
(*mtx)[3][0] = -(xFrom * colX.z + yFrom * colX.y + zFrom * colX.x);
(*mtx)[0][1] = zColY;
(*mtx)[1][1] = yColY;
(*mtx)[2][1] = xColY;
(*mtx)[3][1] = -(xFrom * zColY + yFrom * yColY + zFrom * xColY);
(*mtx)[0][2] = d.z;
(*mtx)[1][2] = d.y;
(*mtx)[2][2] = d.x;
(*mtx)[3][2] = -(xFrom * d.z + yFrom * d.y + zFrom * d.x);
(*mtx)[0][3] = 0.0f;
(*mtx)[1][3] = 0.0f;
(*mtx)[2][3] = 0.0f;
(*mtx)[3][3] = 1.0f;
}
/**
* Scales a mat4f in each dimension by a vector.
*/
void gd_scale_mat4f_by_vec3f(Mat4f *mtx, struct GdVec3f *vec) {
(*mtx)[0][0] *= vec->x;
(*mtx)[0][1] *= vec->x;
(*mtx)[0][2] *= vec->x;
(*mtx)[1][0] *= vec->y;
(*mtx)[1][1] *= vec->y;
(*mtx)[1][2] *= vec->y;
(*mtx)[2][0] *= vec->z;
(*mtx)[2][1] *= vec->z;
(*mtx)[2][2] *= vec->z;
}
/**
* Rotates the matrix 'mtx' about the vector given.
*/
void gd_rot_mat_about_vec(Mat4f *mtx, struct GdVec3f *vec) {
if (vec->x != 0.0f) {
gd_absrot_mat4(mtx, GD_X_AXIS, vec->x);
}
if (vec->y != 0.0f) {
gd_absrot_mat4(mtx, GD_Y_AXIS, vec->y);
}
if (vec->z != 0.0f) {
gd_absrot_mat4(mtx, GD_Z_AXIS, vec->z);
}
}
/**
* Adds each component of a vector to the
* translation column of a mat4f matrix.
*/
void gd_add_vec3f_to_mat4f_offset(Mat4f *mtx, struct GdVec3f *vec) {
UNUSED Mat4f temp;
f32 z, y, x;
x = vec->x;
y = vec->y;
z = vec->z;
(*mtx)[3][0] += x;
(*mtx)[3][1] += y;
(*mtx)[3][2] += z;
}
/**
* Creates a lookat matrix, but specifically from the perspective of the origin.
* Rolls is only ever 0 in practice, and this is really only ever used once.
*
* Matrix has form- | -(cz+sxy)/h sh (cx-syz)/h 0 |
* | (sz-cxy)/h ch -(sx+cyz)/h 0 |
* | -x -y -z 0 |
* | 0 0 0 1 |
*/
void gd_create_origin_lookat(Mat4f *mtx, struct GdVec3f *vec, f32 roll) {
f32 invertedHMag;
f32 hMag;
f32 c;
f32 s;
f32 radPerDeg = RAD_PER_DEG;
struct GdVec3f unit;
unit.x = vec->x;
unit.y = vec->y;
unit.z = vec->z;
gd_normalize_vec3f(&unit);
hMag = gd_sqrt_f(SQ(unit.x) + SQ(unit.z));
roll *= radPerDeg; // convert roll from degrees to radians
s = gd_sin_d(roll);
c = gd_cos_d(roll);
gd_set_identity_mat4(mtx);
if (hMag != 0.0f) {
invertedHMag = 1.0f / hMag;
(*mtx)[0][0] = ((-unit.z * c) - (s * unit.y * unit.x)) * invertedHMag;
(*mtx)[1][0] = ((unit.z * s) - (c * unit.y * unit.x)) * invertedHMag;
(*mtx)[2][0] = -unit.x;
(*mtx)[3][0] = 0.0f;
(*mtx)[0][1] = s * hMag;
(*mtx)[1][1] = c * hMag;
(*mtx)[2][1] = -unit.y;
(*mtx)[3][1] = 0.0f;
(*mtx)[0][2] = ((c * unit.x) - (s * unit.y * unit.z)) * invertedHMag;
(*mtx)[1][2] = ((-s * unit.x) - (c * unit.y * unit.z)) * invertedHMag;
(*mtx)[2][2] = -unit.z;
(*mtx)[3][2] = 0.0f;
(*mtx)[0][3] = 0.0f;
(*mtx)[1][3] = 0.0f;
(*mtx)[2][3] = 0.0f;
(*mtx)[3][3] = 1.0f;
} else {
(*mtx)[0][0] = 0.0f;
(*mtx)[1][0] = 1.0f;
(*mtx)[2][0] = 0.0f;
(*mtx)[3][0] = 0.0f;
(*mtx)[0][1] = 0.0f;
(*mtx)[1][1] = 0.0f;
(*mtx)[2][1] = 1.0f;
(*mtx)[3][1] = 0.0f;
(*mtx)[0][2] = 1.0f;
(*mtx)[1][2] = 0.0f;
(*mtx)[2][2] = 0.0f;
(*mtx)[3][2] = 0.0f;
(*mtx)[0][3] = 0.0f;
(*mtx)[1][3] = 0.0f;
(*mtx)[2][3] = 0.0f;
(*mtx)[3][3] = 1.0f;
}
}
/**
* Clamps a float within a set range about zero.
*/
f32 gd_clamp_f32(f32 a, f32 b) {
if (b < a) {
a = b;
} else if (a < -b) {
a = -b;
}
return a;
}
/**
* Clamps a vector within a set range about zero.
*/
void gd_clamp_vec3f(struct GdVec3f *vec, f32 limit) {
if (vec->x > limit) {
vec->x = limit;
} else if (vec->x < -limit) {
vec->x = -limit;
}
if (vec->y > limit) {
vec->y = limit;
} else if (vec->y < -limit) {
vec->y = -limit;
}
if (vec->z > limit) {
vec->z = limit;
} else if (vec->z < -limit) {
vec->z = -limit;
}
}
/**
* Rotates a 2D vector by some angle in degrees.
*/
void gd_rot_2d_vec(f32 deg, f32 *x, f32 *y) {
f32 xP;
f32 yP;
f32 rad;
rad = deg / DEG_PER_RAD;
xP = (*x * gd_cos_d(rad)) - (*y * gd_sin_d(rad));
yP = (*x * gd_sin_d(rad)) + (*y * gd_cos_d(rad));
*x = xP;
*y = yP;
}
/**
* Rotates a matrix about one of its rows.
*/
void UNUSED gd_rot_mat_about_row(Mat4f *mat, s32 row, f32 ang) {
Mat4f rot;
struct GdVec3f vec;
vec.x = (*mat)[row][0];
vec.y = (*mat)[row][1];
vec.z = (*mat)[row][2];
gd_create_rot_mat_angular(&rot, &vec, ang / 2.0);
gd_mult_mat4f(mat, &rot, mat);
}
/**
* Rotates a mat4f matrix about a given axis
* by a set angle in degrees.
*/
void gd_absrot_mat4(Mat4f *mtx, s32 axisnum, f32 ang) {
Mat4f rMat;
struct GdVec3f rot;
switch (axisnum) {
case GD_X_AXIS:
rot.x = 1.0f;
rot.y = 0.0f;
rot.z = 0.0f;
break;
case GD_Y_AXIS:
rot.x = 0.0f;
rot.y = 1.0f;
rot.z = 0.0f;
break;
case GD_Z_AXIS:
rot.x = 0.0f;
rot.y = 0.0f;
rot.z = 1.0f;
break;
default:
fatal_printf("absrot_matrix4(): Bad axis num");
}
gd_create_rot_mat_angular(&rMat, &rot, ang / 2.0); //? 2.0f
gd_mult_mat4f(mtx, &rMat, mtx);
}
f32 gd_vec3f_magnitude(struct GdVec3f *vec) {
return gd_sqrt_f(SQ(vec->x) + SQ(vec->y) + SQ(vec->z));
}
/**
* Normalizes a vec3f to have a length of 1.
*/
s32 gd_normalize_vec3f(struct GdVec3f *vec) {
f32 mag;
if ((mag = SQ(vec->x) + SQ(vec->y) + SQ(vec->z)) == 0.0f) {
return FALSE;
}
mag = gd_sqrt_f(mag);
// gd_sqrt_f rounds near 0 numbers to 0, so verify again.
if (mag == 0.0f) {
vec->x = 0.0f;
vec->y = 0.0f;
vec->z = 0.0f;
return FALSE;
}
vec->x /= mag;
vec->y /= mag;
vec->z /= mag;
return TRUE;
}
/**
* Stores the cross product of 'a' x 'b' in 'dst'.
*/
void gd_cross_vec3f(struct GdVec3f *a, struct GdVec3f *b, struct GdVec3f *dst) {
struct GdVec3f result;
result.x = (a->y * b->z) - (a->z * b->y);
result.y = (a->z * b->x) - (a->x * b->z);
result.z = (a->x * b->y) - (a->y * b->x);
dst->x = result.x;
dst->y = result.y;
dst->z = result.z;
}
/**
* Returns the dot product of 'a' and 'b'.
*/
f32 gd_dot_vec3f(struct GdVec3f *a, struct GdVec3f *b) {
return (a->x * b->x) + (a->y * b->y) + (a->z * b->z);
}
/**
* Inverts each element of src into dst.
*/
void UNUSED gd_invert_elements_mat4f(Mat4f *src, Mat4f *dst) {
s32 i;
s32 j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
(*dst)[i][j] = 1.0f / (*src)[i][j];
}
}
}
/**
* Inverts a matrix from src and stores it into dst.
* Reaches a fatal_print if the determinant is 0.
*/
void gd_inverse_mat4f(Mat4f *src, Mat4f *dst) {
s32 i;
s32 j;
f32 determinant;
gd_adjunct_mat4f(src, dst);
determinant = gd_mat4f_det(dst);
if (ABS(determinant) < 1e-5) { //? 1e-5f
fatal_print("Non-singular matrix, no inverse!\n");
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
(*dst)[i][j] /= determinant;
}
}
}
/**
* Takes a matrix from src and converts it into its adjunct in dst.
*/
void gd_adjunct_mat4f(Mat4f *src, Mat4f *dst) {
struct InvMat4 inv;
inv.r3.c3 = (*src)[0][0];
inv.r2.c3 = (*src)[0][1];
inv.r1.c3 = (*src)[0][2];
inv.r0.c3 = (*src)[0][3];
inv.r3.c2 = (*src)[1][0];
inv.r2.c2 = (*src)[1][1];
inv.r1.c2 = (*src)[1][2];
inv.r0.c2 = (*src)[1][3];
inv.r3.c1 = (*src)[2][0];
inv.r2.c1 = (*src)[2][1];
inv.r1.c1 = (*src)[2][2];
inv.r0.c1 = (*src)[2][3];
inv.r3.c0 = (*src)[3][0];
inv.r2.c0 = (*src)[3][1];
inv.r1.c0 = (*src)[3][2];
inv.r0.c0 = (*src)[3][3];
(*dst)[0][0] = gd_3x3_det(inv.r2.c2, inv.r2.c1, inv.r2.c0, inv.r1.c2, inv.r1.c1, inv.r1.c0,
inv.r0.c2, inv.r0.c1, inv.r0.c0);
(*dst)[1][0] = -gd_3x3_det(inv.r3.c2, inv.r3.c1, inv.r3.c0, inv.r1.c2, inv.r1.c1, inv.r1.c0,
inv.r0.c2, inv.r0.c1, inv.r0.c0);
(*dst)[2][0] = gd_3x3_det(inv.r3.c2, inv.r3.c1, inv.r3.c0, inv.r2.c2, inv.r2.c1, inv.r2.c0,
inv.r0.c2, inv.r0.c1, inv.r0.c0);
(*dst)[3][0] = -gd_3x3_det(inv.r3.c2, inv.r3.c1, inv.r3.c0, inv.r2.c2, inv.r2.c1, inv.r2.c0,
inv.r1.c2, inv.r1.c1, inv.r1.c0);
(*dst)[0][1] = -gd_3x3_det(inv.r2.c3, inv.r2.c1, inv.r2.c0, inv.r1.c3, inv.r1.c1, inv.r1.c0,
inv.r0.c3, inv.r0.c1, inv.r0.c0);
(*dst)[1][1] = gd_3x3_det(inv.r3.c3, inv.r3.c1, inv.r3.c0, inv.r1.c3, inv.r1.c1, inv.r1.c0,
inv.r0.c3, inv.r0.c1, inv.r0.c0);
(*dst)[2][1] = -gd_3x3_det(inv.r3.c3, inv.r3.c1, inv.r3.c0, inv.r2.c3, inv.r2.c1, inv.r2.c0,
inv.r0.c3, inv.r0.c1, inv.r0.c0);
(*dst)[3][1] = gd_3x3_det(inv.r3.c3, inv.r3.c1, inv.r3.c0, inv.r2.c3, inv.r2.c1, inv.r2.c0,
inv.r1.c3, inv.r1.c1, inv.r1.c0);
(*dst)[0][2] = gd_3x3_det(inv.r2.c3, inv.r2.c2, inv.r2.c0, inv.r1.c3, inv.r1.c2, inv.r1.c0,
inv.r0.c3, inv.r0.c2, inv.r0.c0);
(*dst)[1][2] = -gd_3x3_det(inv.r3.c3, inv.r3.c2, inv.r3.c0, inv.r1.c3, inv.r1.c2, inv.r1.c0,
inv.r0.c3, inv.r0.c2, inv.r0.c0);
(*dst)[2][2] = gd_3x3_det(inv.r3.c3, inv.r3.c2, inv.r3.c0, inv.r2.c3, inv.r2.c2, inv.r2.c0,
inv.r0.c3, inv.r0.c2, inv.r0.c0);
(*dst)[3][2] = -gd_3x3_det(inv.r3.c3, inv.r3.c2, inv.r3.c0, inv.r2.c3, inv.r2.c2, inv.r2.c0,
inv.r1.c3, inv.r1.c2, inv.r1.c0);
(*dst)[0][3] = -gd_3x3_det(inv.r2.c3, inv.r2.c2, inv.r2.c1, inv.r1.c3, inv.r1.c2, inv.r1.c1,
inv.r0.c3, inv.r0.c2, inv.r0.c1);
(*dst)[1][3] = gd_3x3_det(inv.r3.c3, inv.r3.c2, inv.r3.c1, inv.r1.c3, inv.r1.c2, inv.r1.c1,
inv.r0.c3, inv.r0.c2, inv.r0.c1);
(*dst)[2][3] = -gd_3x3_det(inv.r3.c3, inv.r3.c2, inv.r3.c1, inv.r2.c3, inv.r2.c2, inv.r2.c1,
inv.r0.c3, inv.r0.c2, inv.r0.c1);
(*dst)[3][3] = gd_3x3_det(inv.r3.c3, inv.r3.c2, inv.r3.c1, inv.r2.c3, inv.r2.c2, inv.r2.c1,
inv.r1.c3, inv.r1.c2, inv.r1.c1);
}
/**
* Returns the determinant of a mat4f matrix.
*/
f32 gd_mat4f_det(Mat4f *mtx) {
f32 det;
struct InvMat4 inv;
inv.r3.c3 = (*mtx)[0][0];
inv.r2.c3 = (*mtx)[0][1];
inv.r1.c3 = (*mtx)[0][2];
inv.r0.c3 = (*mtx)[0][3];
inv.r3.c2 = (*mtx)[1][0];
inv.r2.c2 = (*mtx)[1][1];
inv.r1.c2 = (*mtx)[1][2];
inv.r0.c2 = (*mtx)[1][3];
inv.r3.c1 = (*mtx)[2][0];
inv.r2.c1 = (*mtx)[2][1];
inv.r1.c1 = (*mtx)[2][2];
inv.r0.c1 = (*mtx)[2][3];
inv.r3.c0 = (*mtx)[3][0];
inv.r2.c0 = (*mtx)[3][1];
inv.r1.c0 = (*mtx)[3][2];
inv.r0.c0 = (*mtx)[3][3];
det = (inv.r3.c3
* gd_3x3_det(inv.r2.c2, inv.r2.c1, inv.r2.c0,
inv.r1.c2, inv.r1.c1, inv.r1.c0,
inv.r0.c2, inv.r0.c1, inv.r0.c0)
- inv.r2.c3
* gd_3x3_det(inv.r3.c2, inv.r3.c1, inv.r3.c0,
inv.r1.c2, inv.r1.c1, inv.r1.c0,
inv.r0.c2, inv.r0.c1, inv.r0.c0))
+ inv.r1.c3
* gd_3x3_det(inv.r3.c2, inv.r3.c1, inv.r3.c0,
inv.r2.c2, inv.r2.c1, inv.r2.c0,
inv.r0.c2, inv.r0.c1, inv.r0.c0)
- inv.r0.c3
* gd_3x3_det(inv.r3.c2, inv.r3.c1, inv.r3.c0,
inv.r2.c2, inv.r2.c1, inv.r2.c0,
inv.r1.c2, inv.r1.c1, inv.r1.c0);
return det;
}
/**
* Takes the individual values of a 3 by 3 matrix and
* returns the determinant.
*/
f32 gd_3x3_det(f32 r0c0, f32 r0c1, f32 r0c2,
f32 r1c0, f32 r1c1, f32 r1c2,
f32 r2c0, f32 r2c1, f32 r2c2) {
f32 det;
det = r0c0 * gd_2x2_det(r1c1, r1c2, r2c1, r2c2) - r1c0 * gd_2x2_det(r0c1, r0c2, r2c1, r2c2)
+ r2c0 * gd_2x2_det(r0c1, r0c2, r1c1, r1c2);
return det;
}
/**
* Takes the individual values of a 2 by 2 matrix and
* returns the determinant.
*/
f32 gd_2x2_det(f32 a, f32 b, f32 c, f32 d) {
f32 det = a * d - b * c;
return det;
}
/**
* Creates a vector negative to what was passed in. Also sets the first row of a mat4f
* to 1 0 0 0. Perhaps meant to be used at the end of gd_create_quat_rot_mat? Not
* sure of the purpose of the vector portion, though.
*/
void UNUSED gd_create_neg_vec_zero_first_mat_row(Mat4f *mtx, struct GdVec3f *vec, f32 x, f32 y, f32 z) {
s32 i;
vec->x = -x;
vec->y = -y;
vec->z = -z;
(*mtx)[0][0] = 1.0f;
for (i = 1; i < 4; i++) {
(*mtx)[0][i] = 0.0f;
}
}
/**
* This function quite literally does nothing.
* Seems to have been meant to create a vector from a quaternion?
*/
void UNUSED gd_broken_quat_to_vec3f(f32 quat[4], struct GdVec3f *vec, f32 zHalf, s32 i, s32 run) {
s32 j;
s32 k;
UNUSED f32 jVal;
UNUSED f32 kVal;
UNUSED struct GdVec3f uVec;
struct GdVec3f tVec;
tVec.x = vec->x;
tVec.y = vec->y;
tVec.z = vec->z;
if (run < 0) {
goto end;
}
if ((j = i + 1) >= 4) {
j = 1;
}
if ((k = j + 1) >= 4) {
k = 1;
}
jVal = quat[j];
kVal = quat[k];
uVec.x = quat[0];
uVec.y = quat[i];
uVec.z = zHalf + zHalf;
end:
vec->x = tVec.x;
vec->y = tVec.y;
vec->z = tVec.z;
}
/**
* This function is a pitch rotation of a quaternion, with the sign allowing both regular
* and inverse multiplication.
*/
void UNUSED gd_quat_rotation(f32 quat[4], UNUSED s32 unused, f32 c, f32 s, s32 i, s32 sign) {
s32 j;
s32 k;
f32 quatVal;
UNUSED u8 filler[8];
if ((j = i + 1) >= 4) {
j = 1;
}
if ((k = j + 1) >= 4) {
k = 1;
}
quatVal = quat[i];
quat[i] = sign * s * quat[0] + quatVal * c;
quat[0] = quat[0] * c - sign * s * quatVal;
quatVal = quat[j];
quat[j] = quat[k] * s + quatVal * c;
quat[k] = quat[k] * c - s * quatVal;
}
/**
* Shifts a matrix up by one row, putting the top row on bottom.
*/
void gd_shift_mat_up(Mat4f *mtx) {
s32 i;
s32 j;
f32 temp[3];
for (i = 0; i < 3; i++) {
temp[i] = (*mtx)[0][i + 1];
}
for (i = 1; i < 4; i++) {
for (j = 1; j < 4; j++) {
(*mtx)[i - 1][j - 1] = (*mtx)[i][j];
}
}
(*mtx)[0][3] = 0.0f;
(*mtx)[1][3] = 0.0f;
(*mtx)[2][3] = 0.0f;
(*mtx)[3][3] = 1.0f;
for (i = 0; i < 3; i++) {
(*mtx)[3][i] = temp[i];
}
}
/**
* Creates a rotation matrix from a quaternion.
*
* Has form-
* | 1 - - - |
* | 0 w^2+i^2-j^2-k^2 2ij+2wk 2ik+2wj |
* | 0 2ij-2wk w^2+j^2-i^2-k^2 2jk+2wi |
* | 0 2ik+2wj 2jk-2wi w^2+k^2-i^2-j^2 |
*
* Potentially broken if 'mtx' is not an identity matrix/zero'ed.
*/
void UNUSED gd_create_quat_rot_mat(f32 quat[4], UNUSED s32 unused, Mat4f *mtx) {
f32 twoIJ;
f32 two0K;
f32 sqQuat[4];
s32 i;
s32 j;
s32 k;
for (i = 0; i < 4; i++) {
sqQuat[i] = SQ(quat[i]);
}
for (i = 1; i < 4; i++) {
if ((j = i + 1) >= 4) {
j = 1;
}
if ((k = j + 1) >= 4) {
k = 1;
}
twoIJ = 2.0 * quat[i] * quat[j];
two0K = 2.0 * quat[k] * quat[0];
(*mtx)[j][i] = twoIJ - two0K;
(*mtx)[i][j] = twoIJ + two0K;
(*mtx)[i][i] = sqQuat[i] + sqQuat[0] - sqQuat[j] - sqQuat[k];
(*mtx)[i][0] = 0.0f;
}
//! The first row only ever has the first value set to 1, but the
//! latter portions remain what they were originally. Perhaps this was meant
//! to call gd_create_neg_vec_zero_first_mat_row?
(*mtx)[0][0] = 1.0f;
gd_shift_mat_up(mtx);
}
/**
* Creates a rotation matrix to multiply the primary matrix by.
* s/c are sin(angle)/cos(angle). That angular rotation is about vector
* 'vec'.
*
* Matrix has form-
*
* | (1-c)z^2+c (1-c)zy-sx (1-c)xz-sy 0 |
* | (1-c)zy-sx (1-c)y^2+c (1-c)xy-sz 0 |
* | (1-c)xz-sy (1-c)xy-sz (1-c)x^2+c 0 |
* | 0 0 0 1 |
*/
void gd_create_rot_matrix(Mat4f *mtx, struct GdVec3f *vec, f32 s, f32 c) {
f32 oneMinusCos;
struct GdVec3f rev;
rev.z = vec->x;
rev.y = vec->y;
rev.x = vec->z;
oneMinusCos = 1.0 - c;
(*mtx)[0][0] = oneMinusCos * rev.z * rev.z + c;
(*mtx)[0][1] = oneMinusCos * rev.z * rev.y + s * rev.x;
(*mtx)[0][2] = oneMinusCos * rev.z * rev.x - s * rev.y;
(*mtx)[0][3] = 0.0f;
(*mtx)[1][0] = oneMinusCos * rev.z * rev.y - s * rev.x;
(*mtx)[1][1] = oneMinusCos * rev.y * rev.y + c;
(*mtx)[1][2] = oneMinusCos * rev.y * rev.x + s * rev.z;
(*mtx)[1][3] = 0.0f;
(*mtx)[2][0] = oneMinusCos * rev.z * rev.x + s * rev.y;
(*mtx)[2][1] = oneMinusCos * rev.y * rev.x - s * rev.z;
(*mtx)[2][2] = oneMinusCos * rev.x * rev.x + c;
(*mtx)[2][3] = 0.0f;
(*mtx)[3][0] = 0.0f;
(*mtx)[3][1] = 0.0f;
(*mtx)[3][2] = 0.0f;
(*mtx)[3][3] = 1.0f;
}
/**
* Creates a rotation matrix about vector 'vec' with ang in degrees.
*/
void gd_create_rot_mat_angular(Mat4f *mtx, struct GdVec3f *vec, f32 ang) {
f32 s;
f32 c;
s = gd_sin_d(ang / (DEG_PER_RAD / 2.0));
c = gd_cos_d(ang / (DEG_PER_RAD / 2.0));
gd_create_rot_matrix(mtx, vec, s, c);
}
/**
* Sets a mat4f matrix to an identity matrix.
*/
void gd_set_identity_mat4(Mat4f *mtx) {
(*mtx)[0][0] = 1.0f;
(*mtx)[0][1] = 0.0f;
(*mtx)[0][2] = 0.0f;
(*mtx)[0][3] = 0.0f;
(*mtx)[1][0] = 0.0f;
(*mtx)[1][1] = 1.0f;
(*mtx)[1][2] = 0.0f;
(*mtx)[1][3] = 0.0f;
(*mtx)[2][0] = 0.0f;
(*mtx)[2][1] = 0.0f;
(*mtx)[2][2] = 1.0f;
(*mtx)[2][3] = 0.0f;
(*mtx)[3][0] = 0.0f;
(*mtx)[3][1] = 0.0f;
(*mtx)[3][2] = 0.0f;
(*mtx)[3][3] = 1.0f;
}
/**
* Copies a mat4f from src to dst.
*/
void gd_copy_mat4f(const Mat4f *src, Mat4f *dst) {
(*dst)[0][0] = (*src)[0][0];
(*dst)[0][1] = (*src)[0][1];
(*dst)[0][2] = (*src)[0][2];
(*dst)[0][3] = (*src)[0][3];
(*dst)[1][0] = (*src)[1][0];
(*dst)[1][1] = (*src)[1][1];
(*dst)[1][2] = (*src)[1][2];
(*dst)[1][3] = (*src)[1][3];
(*dst)[2][0] = (*src)[2][0];
(*dst)[2][1] = (*src)[2][1];
(*dst)[2][2] = (*src)[2][2];
(*dst)[2][3] = (*src)[2][3];
(*dst)[3][0] = (*src)[3][0];
(*dst)[3][1] = (*src)[3][1];
(*dst)[3][2] = (*src)[3][2];
(*dst)[3][3] = (*src)[3][3];
}
/**
* Transforms a vec3f, rotating with the main 3x3 portion of the mat4f
* and translating with the 4th column.
*/
void gd_rotate_and_translate_vec3f(struct GdVec3f *vec, const Mat4f *mtx) {
struct GdVec3f out;
out.x = (*mtx)[0][0] * vec->x + (*mtx)[1][0] * vec->y + (*mtx)[2][0] * vec->z;
out.y = (*mtx)[0][1] * vec->x + (*mtx)[1][1] * vec->y + (*mtx)[2][1] * vec->z;
out.z = (*mtx)[0][2] * vec->x + (*mtx)[1][2] * vec->y + (*mtx)[2][2] * vec->z;
out.x += (*mtx)[3][0];
out.y += (*mtx)[3][1];
out.z += (*mtx)[3][2];
vec->x = out.x;
vec->y = out.y;
vec->z = out.z;
}
/**
* Multiples a vec3f by the main 3x3 portion of a mat4f matrix.
*/
void gd_mat4f_mult_vec3f(struct GdVec3f *vec, const Mat4f *mtx) {
struct GdVec3f out;
out.x = (*mtx)[0][0] * vec->x + (*mtx)[1][0] * vec->y + (*mtx)[2][0] * vec->z;
out.y = (*mtx)[0][1] * vec->x + (*mtx)[1][1] * vec->y + (*mtx)[2][1] * vec->z;
out.z = (*mtx)[0][2] * vec->x + (*mtx)[1][2] * vec->y + (*mtx)[2][2] * vec->z;
vec->x = out.x;
vec->y = out.y;
vec->z = out.z;
}
#define MAT4_DOT_PROD(A, B, R, row, col) \
{ \
(R)[(row)][(col)] = (A)[(row)][0] * (B)[0][(col)]; \
(R)[(row)][(col)] += (A)[(row)][1] * (B)[1][(col)]; \
(R)[(row)][(col)] += (A)[(row)][2] * (B)[2][(col)]; \
(R)[(row)][(col)] += (A)[(row)][3] * (B)[3][(col)]; \
}
#define MAT4_MULTIPLY(A, B, R) \
{ \
MAT4_DOT_PROD((A), (B), (R), 0, 0); \
MAT4_DOT_PROD((A), (B), (R), 0, 1); \
MAT4_DOT_PROD((A), (B), (R), 0, 2); \
MAT4_DOT_PROD((A), (B), (R), 0, 3); \
MAT4_DOT_PROD((A), (B), (R), 1, 0); \
MAT4_DOT_PROD((A), (B), (R), 1, 1); \
MAT4_DOT_PROD((A), (B), (R), 1, 2); \
MAT4_DOT_PROD((A), (B), (R), 1, 3); \
MAT4_DOT_PROD((A), (B), (R), 2, 0); \
MAT4_DOT_PROD((A), (B), (R), 2, 1); \
MAT4_DOT_PROD((A), (B), (R), 2, 2); \
MAT4_DOT_PROD((A), (B), (R), 2, 3); \
MAT4_DOT_PROD((A), (B), (R), 3, 0); \
MAT4_DOT_PROD((A), (B), (R), 3, 1); \
MAT4_DOT_PROD((A), (B), (R), 3, 2); \
MAT4_DOT_PROD((A), (B), (R), 3, 3); \
}
/**
* Multiplies two Mat4f matrices and puts it in dst.
*/
void gd_mult_mat4f(const Mat4f *mA, const Mat4f *mB, Mat4f *dst) {
Mat4f res;
MAT4_MULTIPLY((*mA), (*mB), res);
gd_copy_mat4f(&res, dst);
}
#undef MAT4_MULTIPLY
#undef MAT4_DOT_PROD
/**
* Prints a vec3f vector.
*
* Printed the prefix at some point, as shown by how the function is used.
*/
void gd_print_vec(UNUSED const char *prefix, const struct GdVec3f *vec) {
UNUSED u8 filler[8];
printf("%f,%f,%f\n", vec->x, vec->y, vec->z);
printf("\n");
}
/**
* Prints a plane's boundaries.
*
* Printed a prefix at some point, as shone by how the function is used.
*/
void gd_print_bounding_box(UNUSED const char *prefix, UNUSED const struct GdBoundingBox *p) {
UNUSED u8 filler[8];
printf("Min X = %f, Max X = %f \n", p->minX, p->maxX);
printf("Min Y = %f, Max Y = %f \n", p->minY, p->maxY);
printf("Min Z = %f, Max Z = %f \n", p->minZ, p->maxZ);
printf("\n");
}
/**
* Prints a Mat4f.
*
* Although the prefix input is unused, the one usage of this function
* does have a "Matrix:" prefix, so it was definitely used at one point.
*/
void gd_print_mtx(UNUSED const char *prefix, const Mat4f *mtx) {
s32 i;
s32 j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
gd_printf("%f ", (*mtx)[i][j]);
}
gd_printf("\n");
}
}
/**
* Prints a quaternion along with a prefix.
*/
void UNUSED gd_print_quat(const char *prefix, const f32 f[4]) {
s32 i;
gd_printf(prefix);
for (i = 0; i < 4; i++) {
gd_printf("%f ", f[i]);
}
gd_printf("\n");
}
/**
* Rotates a matrix or creates a rotation matrix about a vector made from an offset
* of 100 and the passed in x, y, and z values.
*/
void UNUSED gd_rot_mat_offset(Mat4f *dst, f32 x, f32 y, f32 z, s32 copy) {
f32 adj = 100.0f;
Mat4f rot;
f32 c;
f32 s;
f32 opp;
f32 mag;
struct GdVec3f vec;
opp = gd_sqrt_f(SQ(x) + SQ(y) + SQ(z));
if (opp == 0.0f) {
if (copy) {
gd_set_identity_mat4(dst);
}
return;
}
mag = gd_sqrt_f(SQ(adj) + SQ(opp));
#ifdef TARGET_NDS
c = swiDivide(adj, mag);
s = swiDivide(opp, mag);
vec.x = swiDivide(-y, opp);
vec.y = swiDivide(-x, opp);
vec.z = swiDivide(-z, opp);
#else
c = adj / mag;
s = opp / mag;
vec.x = -y / opp;
vec.y = -x / opp;
vec.z = -z / opp;
#endif
gd_create_rot_matrix(&rot, &vec, s, c);
if (!copy) {
gd_mult_mat4f(dst, &rot, dst);
} else {
gd_copy_mat4f(&rot, dst);
}
}