-
Notifications
You must be signed in to change notification settings - Fork 57
/
vec_math.h
1336 lines (1219 loc) · 46.9 KB
/
vec_math.h
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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file vec_math.h
* @brief 3D Linear math library
* @copyright Copyright (c) 2013 Kyle Weicht. All rights reserved.
*/
#ifndef __vec_math_h__
#define __vec_math_h__
#include <math.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4204) /* Nonstandard extension: non-constant \
aggregate initializer */
/* MSVC doens't define fminf and fmaxf */
#if _MSC_VER < 1800
static float fminf(float a, float b) {
return (a < b ? a : b);
}
static float fmaxf(float a, float b) {
return (a > b ? a : b);
}
#endif
#elif defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wc99-extensions"
#endif
#ifdef __cplusplus
namespace Vec_Math {
#endif
/**
* Types
*/
typedef struct Vec2 { float x, y; } Vec2;
typedef struct Vec3 { float x, y, z; } Vec3;
typedef struct Vec4 { float x, y, z, w; } Vec4;
typedef struct Mat3 { Vec3 r0, r1, r2; } Mat3;
typedef struct Mat4 { Vec4 r0, r1, r2, r3; } Mat4;
typedef Vec4 Quaternion;
typedef struct Transform {
Quaternion orientation;
Vec3 position;
float scale;
} Transform;
typedef Vec4 Plane;
typedef struct Sphere {
Vec3 center;
float radius;
} Sphere;
/**
* Constants
*/
static const float kEpsilon = 0.00001f;
static const float kPi = 3.141592653589793238462643383280f;
static const float k2Pi = 6.283185307179586476925286766559f;
static const float kPiDiv2 = 1.570796326794896619231321691640f;
static const float kInvPi = 0.318309886183790671537767526745f;
static const float kDegToRad = 0.017453292519943295769236907685f;
static const float kRadToDeg = 57.29577951308232087679815481410f;
#ifdef __cplusplus
extern "C" { // C linkage
typedef const Vec2& VEC2_INPUT;
typedef const Vec3& VEC3_INPUT;
typedef const Vec4& VEC4_INPUT;
typedef const Mat3& MAT3_INPUT;
typedef const Mat4& MAT4_INPUT;
typedef const Quaternion& QUAT_INPUT;
typedef const Plane& PLANE_INPUT;
typedef const Sphere& SPHERE_INPUT;
typedef const Transform& TRANSFORM_INPUT;
#define INLINE inline
#else
typedef Vec2 VEC2_INPUT;
typedef Vec3 VEC3_INPUT;
typedef Vec4 VEC4_INPUT;
typedef Mat3 MAT3_INPUT;
typedef Mat4 MAT4_INPUT;
typedef Quaternion QUAT_INPUT;
typedef Plane PLANE_INPUT;
typedef Sphere SPHERE_INPUT;
typedef Transform TRANSFORM_INPUT;
#define INLINE static __inline
#endif
INLINE float rad_to_deg(float r) {
return kRadToDeg * r;
}
INLINE float deg_to_rad(float d) {
return kDegToRad * d;
}
INLINE void swapf(float& a, float& b) {
float t = a;
a = b;
b = t;
}
INLINE float lerp(float a, float b, float t) {
float d = b - a;
float tt = d * t;
return a + tt;
}
INLINE float saturate(float f) {
if (f < 0)
return 0.0f;
if (f > 1.0f)
return 1.0f;
return f;
}
/******************************************************************************\
* Vec2 *
\******************************************************************************/
static const Vec2 vec2_zero = {0.0f, 0.0f};
INLINE Vec2 vec2_create(float x, float y) {
Vec2 r;
r.x = x;
r.y = y;
return r;
}
/* Basic aritmatic */
INLINE Vec2 vec2_add(VEC2_INPUT a, VEC2_INPUT b) {
Vec2 res;
res.x = a.x + b.x;
res.y = a.y + b.y;
return res;
}
INLINE Vec2 vec2_sub(VEC2_INPUT a, VEC2_INPUT b) {
Vec2 res;
res.x = a.x - b.x;
res.y = a.y - b.y;
return res;
}
INLINE Vec2 vec2_mul(VEC2_INPUT a, VEC2_INPUT b) {
Vec2 res;
res.x = a.x * b.x;
res.y = a.y * b.y;
return res;
}
INLINE Vec2 vec2_div(VEC2_INPUT a, VEC2_INPUT b) {
Vec2 res;
res.x = a.x / b.x;
res.y = a.y / b.y;
return res;
}
/* Scalar math */
INLINE Vec2 vec2_add_scalar(VEC2_INPUT v, float f) {
Vec2 res;
res.x = v.x + f;
res.y = v.y + f;
return res;
}
INLINE Vec2 vec2_sub_scalar(VEC2_INPUT v, float f) {
Vec2 res;
res.x = v.x - f;
res.y = v.y - f;
return res;
}
INLINE Vec2 vec2_mul_scalar(VEC2_INPUT v, float f) {
Vec2 res;
res.x = v.x * f;
res.y = v.y * f;
return res;
}
INLINE Vec2 vec2_div_scalar(VEC2_INPUT v, float f) {
Vec2 res;
res.x = v.x / f;
res.y = v.y / f;
return res;
}
/* Misc */
INLINE float vec2_hadd(VEC2_INPUT v) {
return v.x + v.y;
}
INLINE int vec2_equal(VEC2_INPUT a, VEC2_INPUT b) {
return fabsf(a.x - b.x) < kEpsilon && fabsf(a.y - b.y) < kEpsilon;
}
INLINE int vec2_equal_scalar(VEC2_INPUT v, float f) {
return fabsf(v.x - f) < kEpsilon && fabsf(v.y - f) < kEpsilon;
}
INLINE float vec2_length_sq(VEC2_INPUT v) {
return v.x * v.x + v.y * v.y;
}
INLINE float vec2_length(VEC2_INPUT v) {
return sqrtf(vec2_length_sq(v));
}
INLINE float vec2_distance_sq(VEC2_INPUT a, VEC2_INPUT b) {
return vec2_length_sq(vec2_sub(a, b));
}
INLINE float vec2_distance(VEC2_INPUT a, VEC2_INPUT b) {
return sqrtf(vec2_distance_sq(a, b));
}
INLINE Vec2 vec2_normalize(VEC2_INPUT v) {
return vec2_div_scalar(v, vec2_length(v));
}
INLINE Vec2 vec2_min(VEC2_INPUT a, VEC2_INPUT b) {
Vec2 v;
v.x = fminf(a.x, b.x);
v.y = fminf(a.y, b.y);
return v;
}
INLINE Vec2 vec2_max(VEC2_INPUT a, VEC2_INPUT b) {
Vec2 v;
v.x = fmaxf(a.x, b.x);
v.y = fmaxf(a.y, b.y);
return v;
}
INLINE Vec2 vec2_lerp(VEC2_INPUT a, VEC2_INPUT b, float t) {
Vec2 d = vec2_sub(b, a);
Vec2 tt = vec2_mul_scalar(d, t);
return vec2_add(a, tt);
}
INLINE Vec2 vec2_negate(VEC2_INPUT v) {
Vec2 r;
r.x = -v.x;
r.y = -v.y;
return r;
}
/******************************************************************************\
* Vec3 *
\******************************************************************************/
static const Vec3 vec3_zero = {0.0f, 0.0f, 0.0f};
INLINE Vec3 vec3_from_vec4(VEC4_INPUT v) {
Vec3 r;
r.x = v.x;
r.y = v.y;
r.z = v.z;
return r;
}
INLINE Vec3 vec3_create(float x, float y, float z) {
Vec3 r;
r.x = x;
r.y = y;
r.z = z;
return r;
}
/* Basic aritmatic */
INLINE Vec3 vec3_add(VEC3_INPUT a, VEC3_INPUT b) {
Vec3 res;
res.x = a.x + b.x;
res.y = a.y + b.y;
res.z = a.z + b.z;
return res;
}
INLINE Vec3 vec3_sub(VEC3_INPUT a, VEC3_INPUT b) {
Vec3 res;
res.x = a.x - b.x;
res.y = a.y - b.y;
res.z = a.z - b.z;
return res;
}
INLINE Vec3 vec3_mul(VEC3_INPUT a, VEC3_INPUT b) {
Vec3 res;
res.x = a.x * b.x;
res.y = a.y * b.y;
res.z = a.z * b.z;
return res;
}
INLINE Vec3 vec3_div(VEC3_INPUT a, VEC3_INPUT b) {
Vec3 res;
res.x = a.x / b.x;
res.y = a.y / b.y;
res.z = a.z / b.z;
return res;
}
/* Scalar math */
INLINE Vec3 vec3_add_scalar(VEC3_INPUT v, float f) {
Vec3 res;
res.x = v.x + f;
res.y = v.y + f;
res.z = v.z + f;
return res;
}
INLINE Vec3 vec3_sub_scalar(VEC3_INPUT v, float f) {
Vec3 res;
res.x = v.x - f;
res.y = v.y - f;
res.z = v.z - f;
return res;
}
INLINE Vec3 vec3_mul_scalar(VEC3_INPUT v, float f) {
Vec3 res;
res.x = v.x * f;
res.y = v.y * f;
res.z = v.z * f;
return res;
}
INLINE Vec3 vec3_div_scalar(VEC3_INPUT v, float f) {
Vec3 res;
res.x = v.x / f;
res.y = v.y / f;
res.z = v.z / f;
return res;
}
/* Misc */
INLINE float vec3_hadd(VEC3_INPUT v) {
return v.x + v.y + v.z;
}
INLINE int vec3_equal(VEC3_INPUT a, VEC3_INPUT b) {
return fabsf(a.x - b.x) < kEpsilon && fabsf(a.y - b.y) < kEpsilon &&
fabsf(a.z - b.z) < kEpsilon;
}
INLINE int vec3_equal_scalar(VEC3_INPUT v, float f) {
return fabsf(v.x - f) < kEpsilon && fabsf(v.y - f) < kEpsilon &&
fabsf(v.z - f) < kEpsilon;
}
INLINE float vec3_length_sq(VEC3_INPUT v) {
return v.x * v.x + v.y * v.y + v.z * v.z;
}
INLINE float vec3_length(VEC3_INPUT v) {
return sqrtf(vec3_length_sq(v));
}
INLINE float vec3_distance_sq(VEC3_INPUT a, VEC3_INPUT b) {
return vec3_length_sq(vec3_sub(a, b));
}
INLINE float vec3_distance(VEC3_INPUT a, VEC3_INPUT b) {
return sqrtf(vec3_distance_sq(a, b));
}
INLINE Vec3 vec3_normalize(VEC3_INPUT v) {
return vec3_div_scalar(v, vec3_length(v));
}
INLINE Vec3 vec3_min(VEC3_INPUT a, VEC3_INPUT b) {
Vec3 v;
v.x = fminf(a.x, b.x);
v.y = fminf(a.y, b.y);
v.z = fminf(a.z, b.z);
return v;
}
INLINE Vec3 vec3_max(VEC3_INPUT a, VEC3_INPUT b) {
Vec3 v;
v.x = fmaxf(a.x, b.x);
v.y = fmaxf(a.y, b.y);
v.z = fmaxf(a.z, b.z);
return v;
}
INLINE Vec3 vec3_lerp(VEC3_INPUT a, VEC3_INPUT b, float t) {
Vec3 d = vec3_sub(b, a);
Vec3 tt = vec3_mul_scalar(d, t);
return vec3_add(a, tt);
}
INLINE Vec3 vec3_negate(VEC3_INPUT v) {
Vec3 r;
r.x = -v.x;
r.y = -v.y;
r.z = -v.z;
return r;
}
INLINE float vec3_dot(VEC3_INPUT a, VEC3_INPUT b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
INLINE Vec3 vec3_cross(VEC3_INPUT a, VEC3_INPUT b) {
Vec3 r;
r.x = a.y * b.z - a.z * b.y;
r.y = a.z * b.x - a.x * b.z;
r.z = a.x * b.y - a.y * b.x;
return r;
}
/******************************************************************************\
* Vec4 *
\******************************************************************************/
static const Vec4 vec4_zero = {0.0f, 0.0f, 0.0f, 0.0f};
INLINE Vec4 vec4_from_vec3(VEC3_INPUT v, float w) {
Vec4 r;
r.x = v.x;
r.y = v.y;
r.z = v.z;
r.w = w;
return r;
}
INLINE Vec4 vec4_create(float x, float y, float z, float w) {
Vec4 r;
r.x = x;
r.y = y;
r.z = z;
r.w = w;
return r;
}
/* Basic aritmatic */
INLINE Vec4 vec4_add(VEC4_INPUT a, VEC4_INPUT b) {
Vec4 res;
res.x = a.x + b.x;
res.y = a.y + b.y;
res.z = a.z + b.z;
res.w = a.w + b.w;
return res;
}
INLINE Vec4 vec4_sub(VEC4_INPUT a, VEC4_INPUT b) {
Vec4 res;
res.x = a.x - b.x;
res.y = a.y - b.y;
res.z = a.z - b.z;
res.w = a.w - b.w;
return res;
}
INLINE Vec4 vec4_mul(VEC4_INPUT a, VEC4_INPUT b) {
Vec4 res;
res.x = a.x * b.x;
res.y = a.y * b.y;
res.z = a.z * b.z;
res.w = a.w * b.w;
return res;
}
INLINE Vec4 vec4_div(VEC4_INPUT a, VEC4_INPUT b) {
Vec4 res;
res.x = a.x / b.x;
res.y = a.y / b.y;
res.z = a.z / b.z;
res.w = a.w / b.w;
return res;
}
/* Scalar math */
INLINE Vec4 vec4_add_scalar(VEC4_INPUT v, float f) {
Vec4 res;
res.x = v.x + f;
res.y = v.y + f;
res.z = v.z + f;
res.w = v.w + f;
return res;
}
INLINE Vec4 vec4_sub_scalar(VEC4_INPUT v, float f) {
Vec4 res;
res.x = v.x - f;
res.y = v.y - f;
res.z = v.z - f;
res.w = v.w - f;
return res;
}
INLINE Vec4 vec4_mul_scalar(VEC4_INPUT v, float f) {
Vec4 res;
res.x = v.x * f;
res.y = v.y * f;
res.z = v.z * f;
res.w = v.w * f;
return res;
}
INLINE Vec4 vec4_div_scalar(VEC4_INPUT v, float f) {
Vec4 res;
res.x = v.x / f;
res.y = v.y / f;
res.z = v.z / f;
res.w = v.w / f;
return res;
}
/* Misc */
INLINE float vec4_hadd(VEC4_INPUT v) {
return v.x + v.y + v.z + v.w;
}
INLINE int vec4_equal(VEC4_INPUT a, VEC4_INPUT b) {
return fabsf(a.x - b.x) < kEpsilon && fabsf(a.y - b.y) < kEpsilon &&
fabsf(a.z - b.z) < kEpsilon && fabsf(a.w - b.w) < kEpsilon;
}
INLINE int vec4_equal_scalar(VEC4_INPUT v, float f) {
return fabsf(v.x - f) < kEpsilon && fabsf(v.y - f) < kEpsilon &&
fabsf(v.z - f) < kEpsilon && fabsf(v.w - f) < kEpsilon;
}
INLINE float vec4_length_sq(VEC4_INPUT v) {
return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
}
INLINE float vec4_length(VEC4_INPUT v) {
return sqrtf(vec4_length_sq(v));
}
INLINE float vec4_distance_sq(VEC4_INPUT a, VEC4_INPUT b) {
return vec4_length_sq(vec4_sub(a, b));
}
INLINE float vec4_distance(VEC4_INPUT a, VEC4_INPUT b) {
return sqrtf(vec4_distance_sq(a, b));
}
INLINE Vec4 vec4_normalize(VEC4_INPUT v) {
return vec4_div_scalar(v, vec4_length(v));
}
INLINE Vec4 vec4_min(VEC4_INPUT a, VEC4_INPUT b) {
Vec4 v;
v.x = fminf(a.x, b.x);
v.y = fminf(a.y, b.y);
v.z = fminf(a.z, b.z);
v.w = fminf(a.w, b.w);
return v;
}
INLINE Vec4 vec4_max(VEC4_INPUT a, VEC4_INPUT b) {
Vec4 v;
v.x = fmaxf(a.x, b.x);
v.y = fmaxf(a.y, b.y);
v.z = fmaxf(a.z, b.z);
v.w = fmaxf(a.w, b.w);
return v;
}
INLINE Vec4 vec4_lerp(VEC4_INPUT a, VEC4_INPUT b, float t) {
Vec4 d = vec4_sub(b, a);
Vec4 tt = vec4_mul_scalar(d, t);
return vec4_add(a, tt);
}
INLINE Vec4 vec4_negate(VEC4_INPUT v) {
Vec4 r;
r.x = -v.x;
r.y = -v.y;
r.z = -v.z;
r.w = -v.w;
return r;
}
/******************************************************************************\
* Mat3 *
\******************************************************************************/
static const Mat3 mat3_identity = {
{1.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 1.0f},
};
INLINE Mat4 mat4_from_mat3(MAT3_INPUT m) {
Mat4 r = {
{m.r0.x, m.r0.y, m.r0.z, 0.0f},
{m.r1.x, m.r1.y, m.r1.z, 0.0f},
{m.r2.x, m.r2.y, m.r2.z, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f},
};
return r;
}
INLINE Mat3 mat3_create(float f00,
float f01,
float f02,
float f10,
float f11,
float f12,
float f20,
float f21,
float f22) {
Mat3 m = {
{f00, f01, f02}, {f10, f11, f12}, {f20, f21, f22},
};
return m;
}
INLINE Mat3 mat3_scalef(float x, float y, float z) {
Mat3 r = mat3_identity;
r.r0.x = x;
r.r1.y = y;
r.r2.z = z;
return r;
}
INLINE Mat3 mat3_scale(VEC3_INPUT v) {
return mat3_scalef(v.x, v.y, v.z);
}
INLINE Mat3 mat3_rotation_x(float rad) {
float c = cosf(rad);
float s = sinf(rad);
Mat3 r = mat3_identity;
r.r1.y = c;
r.r1.z = s;
r.r2.y = -s;
r.r2.z = c;
return r;
}
INLINE Mat3 mat3_rotation_y(float rad) {
float c = cosf(rad);
float s = sinf(rad);
Mat3 r = mat3_identity;
r.r0.x = c;
r.r0.z = -s;
r.r2.x = s;
r.r2.z = c;
return r;
}
INLINE Mat3 mat3_rotation_z(float rad) {
float c = cosf(rad);
float s = sinf(rad);
Mat3 r = mat3_identity;
r.r0.x = c;
r.r0.y = s;
r.r1.x = -s;
r.r1.y = c;
return r;
}
INLINE Mat3 mat3_rotation_axis(VEC3_INPUT axis, float rad) {
Vec3 normAxis = vec3_normalize(axis);
float c = cosf(rad);
float s = sinf(rad);
float t = 1 - c;
float x = normAxis.x;
float y = normAxis.y;
float z = normAxis.z;
Mat3 m;
m.r0.x = (t * x * x) + c;
m.r0.y = (t * x * y) + s * z;
m.r0.z = (t * x * z) - s * y;
m.r1.x = (t * x * y) - (s * z);
m.r1.y = (t * y * y) + c;
m.r1.z = (t * y * z) + (s * x);
m.r2.x = (t * x * z) + (s * y);
m.r2.y = (t * y * z) - (s * x);
m.r2.z = (t * z * z) + c;
return m;
}
INLINE Mat3 mat3_mul_scalar(MAT3_INPUT m, float f) {
Mat3 result;
result.r0 = vec3_mul_scalar(m.r0, f);
result.r1 = vec3_mul_scalar(m.r1, f);
result.r2 = vec3_mul_scalar(m.r2, f);
return result;
}
#define MTX3_INDEX(f, r, c) ((f)[(r * 3) + c])
INLINE Mat3 mat3_multiply(MAT3_INPUT a, MAT3_INPUT b) {
Mat3 m = mat3_identity;
const float* left = &a.r0.x;
const float* right = &b.r0.x;
float* result = (float*)&m;
int ii, jj, kk;
for (ii = 0; ii < 3; ++ii) /* row */
{
for (jj = 0; jj < 3; ++jj) /* column */
{
float sum = MTX3_INDEX(left, ii, 0) * MTX3_INDEX(right, 0, jj);
for (kk = 1; kk < 3; ++kk) {
sum += (MTX3_INDEX(left, ii, kk) * MTX3_INDEX(right, kk, jj));
}
MTX3_INDEX(result, ii, jj) = sum;
}
}
return m;
}
#undef MTX3_INDEX
INLINE float mat3_determinant(MAT3_INPUT m) {
float f0 = m.r0.x * (m.r1.y * m.r2.z - m.r2.y * m.r1.z);
float f1 = m.r0.y * -(m.r1.x * m.r2.z - m.r2.x * m.r1.z);
float f2 = m.r0.z * (m.r1.x * m.r2.y - m.r2.x * m.r1.y);
return f0 + f1 + f2;
}
INLINE Mat3 mat3_transpose(MAT3_INPUT m) {
Mat3 result = m;
swapf(result.r0.y, result.r1.x);
swapf(result.r0.z, result.r2.x);
swapf(result.r1.z, result.r2.y);
return result;
}
INLINE Mat3 mat3_from_axis(VEC3_INPUT vx, VEC3_INPUT vy, VEC3_INPUT vz) {
Mat3 m = mat3_identity;
// YZ plane
if (vx.x == 0 && vx.y == 0 && vx.z == 0) {
// use y axis
m.r1 = vec3_normalize(vy);
m.r0 = vec3_normalize(vec3_cross(vy, vz));
m.r2 = vec3_normalize(vec3_cross(m.r0, m.r1));
}
// XZ plane
if (vy.x == 0 && vy.y == 0 && vy.z == 0) {
// use x axis
m.r0 = vec3_normalize(vx);
m.r1 = vec3_normalize(vec3_cross(vz, vx));
m.r2 = vec3_normalize(vec3_cross(m.r0, m.r1));
}
// XY plane
if (vz.x == 0 && vz.y == 0 && vz.z == 0) {
// use y axis
m.r1 = vec3_normalize(vy);
m.r2 = vec3_normalize(vec3_cross(vec3_normalize(vx), m.r1));
m.r0 = vec3_normalize(vec3_cross(m.r1, m.r2));
}
return m;
}
INLINE Mat3 mat3_inverse(MAT3_INPUT m) {
float det = mat3_determinant(m);
Mat3 inv;
inv.r0.x = (m.r1.y * m.r2.z) - (m.r1.z * m.r2.y);
inv.r0.y = -((m.r1.x * m.r2.z) - (m.r1.z * m.r2.x));
inv.r0.z = (m.r1.x * m.r2.y) - (m.r1.y * m.r2.x);
inv.r1.x = -((m.r0.y * m.r2.z) - (m.r0.z * m.r2.y));
inv.r1.y = (m.r0.x * m.r2.z) - (m.r0.z * m.r2.x);
inv.r1.z = -((m.r0.x * m.r2.y) - (m.r0.y * m.r2.x));
inv.r2.x = (m.r0.y * m.r1.z) - (m.r0.z * m.r1.y);
inv.r2.y = -((m.r0.x * m.r1.z) - (m.r0.z * m.r1.x));
inv.r2.z = (m.r0.x * m.r1.y) - (m.r0.y * m.r1.x);
inv = mat3_transpose(inv);
inv = mat3_mul_scalar(inv, 1.0f / det);
return inv;
}
INLINE Vec3 mat3_mul_vector(VEC3_INPUT v, MAT3_INPUT m) {
Mat3 transpose = mat3_transpose(m);
Vec3 res, t;
t = vec3_mul(transpose.r0, v);
res.x = vec3_hadd(t);
t = vec3_mul(transpose.r1, v);
res.y = vec3_hadd(t);
t = vec3_mul(transpose.r2, v);
res.z = vec3_hadd(t);
return res;
}
/******************************************************************************\
* Mat4 *
\******************************************************************************/
static const Mat4 mat4_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},
};
INLINE Mat3 mat3_from_mat4(MAT4_INPUT m) {
Mat3 r = {
{m.r0.x, m.r0.y, m.r0.z},
{m.r1.x, m.r1.y, m.r1.z},
{m.r2.x, m.r2.y, m.r2.z},
};
return r;
}
INLINE Mat4 mat4_scalef(float x, float y, float z) {
Mat4 r = mat4_identity;
r.r0.x = x;
r.r1.y = y;
r.r2.z = z;
return r;
}
INLINE Mat4 mat4_scale(VEC3_INPUT v) {
return mat4_scalef(v.x, v.y, v.z);
}
INLINE Mat4 mat4_translatef(float x, float y, float z) {
Mat4 r = mat4_identity;
r.r3.x = x;
r.r3.y = y;
r.r3.z = z;
return r;
}
INLINE Mat4 mat4_translate(VEC3_INPUT v) {
return mat4_translatef(v.x, v.y, v.z);
}
INLINE Mat4 mat4_rotation_x(float rad) {
float c = cosf(rad);
float s = sinf(rad);
Mat4 r = mat4_identity;
r.r1.y = c;
r.r1.z = s;
r.r2.y = -s;
r.r2.z = c;
return r;
}
INLINE Mat4 mat4_rotation_y(float rad) {
float c = cosf(rad);
float s = sinf(rad);
Mat4 r = mat4_identity;
r.r0.x = c;
r.r0.z = -s;
r.r2.x = s;
r.r2.z = c;
return r;
}
INLINE Mat4 mat4_rotation_z(float rad) {
float c = cosf(rad);
float s = sinf(rad);
Mat4 r = mat4_identity;
r.r0.x = c;
r.r0.y = s;
r.r1.x = -s;
r.r1.y = c;
return r;
}
INLINE Mat4 mat4_rotation_axis(VEC3_INPUT axis, float rad) {
Vec3 normAxis = vec3_normalize(axis);
float c = cosf(rad);
float s = sinf(rad);
float t = 1 - c;
float x = normAxis.x;
float y = normAxis.y;
float z = normAxis.z;
Mat4 m = mat4_identity;
m.r0.x = (t * x * x) + c;
m.r0.y = (t * x * y) + s * z;
m.r0.z = (t * x * z) - s * y;
m.r1.x = (t * x * y) - (s * z);
m.r1.y = (t * y * y) + c;
m.r1.z = (t * y * z) + (s * x);
m.r2.x = (t * x * z) + (s * y);
m.r2.y = (t * y * z) - (s * x);
m.r2.z = (t * z * z) + c;
return m;
}
INLINE Mat4 mat4_mul_scalar(MAT4_INPUT m, float f) {
Mat4 result;
result.r0 = vec4_mul_scalar(m.r0, f);
result.r1 = vec4_mul_scalar(m.r1, f);
result.r2 = vec4_mul_scalar(m.r2, f);
result.r3 = vec4_mul_scalar(m.r3, f);
return result;
}
#define MTX4_INDEX(f, r, c) ((f)[(r * 4) + c])
INLINE Mat4 mat4_multiply(MAT4_INPUT a, MAT4_INPUT b) {
Mat4 m = mat4_identity;
const float* left = &a.r0.x;
const float* right = &b.r0.x;
float* result = (float*)&m;
int ii, jj, kk;
for (ii = 0; ii < 4; ++ii) /* row */
{
for (jj = 0; jj < 4; ++jj) /* column */
{
float sum = MTX4_INDEX(left, ii, 0) * MTX4_INDEX(right, 0, jj);
for (kk = 1; kk < 4; ++kk) {
sum += (MTX4_INDEX(left, ii, kk) * MTX4_INDEX(right, kk, jj));
}
MTX4_INDEX(result, ii, jj) = sum;
}
}
return m;
}
#undef MTX4_INDEX
INLINE float mat4_determinant(MAT4_INPUT m) {
float det = 0.0f;
Mat3 a = {{m.r1.y, m.r1.z, m.r1.w},
{m.r2.y, m.r2.z, m.r2.w},
{m.r3.y, m.r3.z, m.r3.w}};
Mat3 b = {{m.r1.x, m.r1.z, m.r1.w},
{m.r2.x, m.r2.z, m.r2.w},
{m.r3.x, m.r3.z, m.r3.w}};
Mat3 c = {{m.r1.x, m.r1.y, m.r1.w},
{m.r2.x, m.r2.y, m.r2.w},
{m.r3.x, m.r3.y, m.r3.w}};
Mat3 d = {{m.r1.x, m.r1.y, m.r1.z},
{m.r2.x, m.r2.y, m.r2.z},
{m.r3.x, m.r3.y, m.r3.z}};
det += m.r0.x * mat3_determinant(a);
det -= m.r0.y * mat3_determinant(b);
det += m.r0.z * mat3_determinant(c);
det -= m.r0.w * mat3_determinant(d);
return det;
}
INLINE Mat4 mat4_transpose(MAT4_INPUT m) {
Mat4 result = m;
swapf(result.r0.y, result.r1.x);
swapf(result.r0.z, result.r2.x);
swapf(result.r0.w, result.r3.x);
swapf(result.r1.z, result.r2.y);
swapf(result.r1.w, result.r3.y);
swapf(result.r2.w, result.r3.z);
return result;
}
INLINE Mat4 mat4_inverse(MAT4_INPUT mat) {
Mat4 ret;
float recip;
/* temp matrices */
/* row 1 */
Mat3 a = mat3_create(mat.r1.y, mat.r1.z, mat.r1.w, mat.r2.y, mat.r2.z,
mat.r2.w, mat.r3.y, mat.r3.z, mat.r3.w);
Mat3 b = mat3_create(mat.r1.x, mat.r1.z, mat.r1.w, mat.r2.x, mat.r2.z,
mat.r2.w, mat.r3.x, mat.r3.z, mat.r3.w);
Mat3 c = mat3_create(mat.r1.x, mat.r1.y, mat.r1.w, mat.r2.x, mat.r2.y,
mat.r2.w, mat.r3.x, mat.r3.y, mat.r3.w);
Mat3 d = mat3_create(mat.r1.x, mat.r1.y, mat.r1.z, mat.r2.x, mat.r2.y,
mat.r2.z, mat.r3.x, mat.r3.y, mat.r3.z);
/* row 2 */
Mat3 e = mat3_create(mat.r0.y, mat.r0.z, mat.r0.w, mat.r2.y, mat.r2.z,
mat.r2.w, mat.r3.y, mat.r3.z, mat.r3.w);
Mat3 f = mat3_create(mat.r0.x, mat.r0.z, mat.r0.w, mat.r2.x, mat.r2.z,
mat.r2.w, mat.r3.x, mat.r3.z, mat.r3.w);
Mat3 g = mat3_create(mat.r0.x, mat.r0.y, mat.r0.w, mat.r2.x, mat.r2.y,
mat.r2.w, mat.r3.x, mat.r3.y, mat.r3.w);
Mat3 h = mat3_create(mat.r0.x, mat.r0.y, mat.r0.z, mat.r2.x, mat.r2.y,
mat.r2.z, mat.r3.x, mat.r3.y, mat.r3.z);
/* row 3 */
Mat3 i = mat3_create(mat.r0.y, mat.r0.z, mat.r0.w, mat.r1.y, mat.r1.z,
mat.r1.w, mat.r3.y, mat.r3.z, mat.r3.w);
Mat3 j = mat3_create(mat.r0.x, mat.r0.z, mat.r0.w, mat.r1.x, mat.r1.z,
mat.r1.w, mat.r3.x, mat.r3.z, mat.r3.w);
Mat3 k = mat3_create(mat.r0.x, mat.r0.y, mat.r0.w, mat.r1.x, mat.r1.y,
mat.r1.w, mat.r3.x, mat.r3.y, mat.r3.w);
Mat3 l = mat3_create(mat.r0.x, mat.r0.y, mat.r0.z, mat.r1.x, mat.r1.y,
mat.r1.z, mat.r3.x, mat.r3.y, mat.r3.z);
/* row 4 */
Mat3 m = mat3_create(mat.r0.y, mat.r0.z, mat.r0.w, mat.r1.y, mat.r1.z,
mat.r1.w, mat.r2.y, mat.r2.z, mat.r2.w);
Mat3 n = mat3_create(mat.r0.x, mat.r0.z, mat.r0.w, mat.r1.x, mat.r1.z,
mat.r1.w, mat.r2.x, mat.r2.z, mat.r2.w);
Mat3 o = mat3_create(mat.r0.x, mat.r0.y, mat.r0.w, mat.r1.x, mat.r1.y,
mat.r1.w, mat.r2.x, mat.r2.y, mat.r2.w);
Mat3 p = mat3_create(mat.r0.x, mat.r0.y, mat.r0.z, mat.r1.x, mat.r1.y,
mat.r1.z, mat.r2.x, mat.r2.y, mat.r2.z);
/* row 1 */
ret.r0.x = mat3_determinant(a);
ret.r0.y = -mat3_determinant(b);
ret.r0.z = mat3_determinant(c);
ret.r0.w = -mat3_determinant(d);
/* row 2 */
ret.r1.x = -mat3_determinant(e);
ret.r1.y = mat3_determinant(f);
ret.r1.z = -mat3_determinant(g);
ret.r1.w = mat3_determinant(h);
/* row 3 */
ret.r2.x = mat3_determinant(i);
ret.r2.y = -mat3_determinant(j);
ret.r2.z = mat3_determinant(k);
ret.r2.w = -mat3_determinant(l);
/* row 4 */
ret.r3.x = -mat3_determinant(m);
ret.r3.y = mat3_determinant(n);
ret.r3.z = -mat3_determinant(o);
ret.r3.w = mat3_determinant(p);
ret = mat4_transpose(ret);
recip = 1.0f / mat4_determinant(mat);
ret = mat4_mul_scalar(ret, recip);
return ret;
}
INLINE Vec4 mat4_mul_vector(VEC4_INPUT v, MAT4_INPUT m) {
Mat4 transpose = mat4_transpose(m);
Vec4 res, t;
t = vec4_mul(transpose.r0, v);
res.x = vec4_hadd(t);
t = vec4_mul(transpose.r1, v);
res.y = vec4_hadd(t);
t = vec4_mul(transpose.r2, v);
res.z = vec4_hadd(t);
t = vec4_mul(transpose.r3, v);
res.w = vec4_hadd(t);
return res;
}
INLINE Mat4 mat4_ortho_off_center(float left,
float right,
float bottom,
float top,
float nearPlane,
float farPlane) {
Mat4 m = mat4_identity;
float diff = farPlane - nearPlane;
m.r0.x = 2.0f / (right - left);