-
Notifications
You must be signed in to change notification settings - Fork 0
/
socrates.h
1610 lines (1259 loc) · 45.7 KB
/
socrates.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
// Socrates v1.0 - A single-file easy to use math library made for game development
//
// To include the library in your project, copy this file (socrates.h) into the directory where you usually
// keep your libraries. And then... that's it. No compilation, no setting up for shared or static libraries, and
// no messing around with CMake or other build systems. Everything you'll need to know is in this one file you're
// currently viewing.
//
// If you have any inquires or potential bugs, please contact me: https://mohamedag2002.github.io/
#pragma once
#include <cmath>
namespace soc { // Start of soc
// Platform defines
///////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// Windows inline keyword
#define SOC_INLINE __forceinline
#define SOC_NOINLINE __declspec(noinline)
#else
// Non-Windows inline keyword
#define SOC_INLINE inline
#define SOC_NOINLINE
#endif
///////////////////////////////////////////////////////////////
// Defines
///////////////////////////////////////////////////////////////
// The value of PI
#define SOC_PI 3.14159265359
// Radians to degrees multiplier
#define SOC_RAD2DEG (180.0f / SOC_PI)
// Degrees to radians multiplier
#define SOC_DEG2RAD (SOC_PI / 180.0f)
// The epsilon which is the lowest possible decimal point value
#define SOC_EPSILON 1.192092896e-07f
#define SOC_FLOAT_MIN -3.40282e+38F
#define SOC_FLOAT_MAX 3.40282e+38F
///////////////////////////////////////////////////////////////
// Typedefs
///////////////////////////////////////////////////////////////
// char
typedef char int8;
// short
typedef short int16;
// int
typedef int int32;
// long
typedef long int64;
// unsigned char
typedef unsigned char uint8;
// unsigned short
typedef unsigned short uint16;
// unsigned int
typedef unsigned int uint32;
// unsigned long
typedef unsigned long uint64;
// float
typedef float float32;
// double
typedef double float64;
///////////////////////////////////////////////////////////////
// Socrates types
///////////////////////////////////////////////////////////////
// Two component vector
union Vector2 {
float32 components[2];
struct {
float32 x, y;
};
struct {
float32 u, v;
};
struct {
float32 r, g;
};
// Default CTOR
Vector2() {
x = 0.0f;
y = 0.0f;
}
// Takes the `x` and `y` components of the vector
Vector2(float32 x, float32 y)
:x(x), y(y)
{}
// Fils all of the components with the given value
Vector2(float32 s)
:x(s), y(s)
{}
// Index operator overload into the components
// NOTE: This indexing operator overload and all other overloads of this type
// will NOT check for out of bounds indices. Please keep this in mind
float32 operator[](const uint32 index) {
return components[index];
}
float32 operator[](const uint32 index) const {
if(index < 0 || index > 2) {
return 0.0f;
}
return components[index];
}
};
// Three component vector
union Vector3 {
float32 components[3];
struct {
float32 x, y, z;
};
struct {
float32 u, v, w;
};
struct {
float32 r, g, b;
};
// Default CTOR
Vector3() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
// Takes the `x`, `y`, and `z` components of the vector
Vector3(float32 x, float32 y, float32 z)
:x(x), y(y), z(z)
{}
// Takes a `Vector2` to fill the `x` and `y` components and also a `z` component
Vector3(const Vector2& v, float32 z)
:x(v.x), y(v.y), z(z)
{}
// Fils all of the components with the given value
Vector3(float32 s)
:x(s), y(s), z(s)
{}
// Index operator overload into the components
// NOTE: This indexing operator overload and all other overloads of this type
// will NOT check for out of bounds indices. Please keep this in mind
float32 operator[](const uint32 index) {
if(index < 0 || index > 3) {
// @TODO: Should assert here
return 0.0f;
}
return components[index];
}
float32 operator[](const uint32 index) const {
if(index < 0 || index > 3) {
// @TODO: Should assert here
return 0.0f;
}
return components[index];
}
};
// Four component vector
union Vector4 {
float32 components[4];
struct {
float32 x, y, z, w;
};
struct {
float32 r, g, b, a;
};
// Default CTOR
Vector4() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
w = 0.0f;
}
// Takes the `x`, `y`, `z`, and `w` components of the vector
Vector4(float32 x, float32 y, float32 z, float32 w)
:x(x), y(y), z(z), w(w)
{}
// Takes a `Vector3` to fill the `x`, `y`, `w` components and a scalar to fill the `w` component
Vector4(const Vector3& v, float32 w)
:x(v.x), y(v.y), z(v.z), w(w)
{}
// Fils all of the components with the given value
Vector4(float32 s)
:x(s), y(s), z(s), w(s)
{}
// Index operator overload into the components
// NOTE: This indexing operator overload and all other overloads of this type
// will NOT check for out of bounds indices. Please keep this in mind
float32 operator[](const uint32 index) {
if(index < 0 || index > 4) {
// @TODO: Should assert here
return 0.0f;
}
return components[index];
}
float32 operator[](const uint32 index) const {
if(index < 0 || index > 4) {
// @TODO: Should assert here
return 0.0f;
}
return components[index];
}
};
// A 3x3 matrix
struct Matrix3 {
// Entries
float32 data[9];
// Default CTOR where the matrix gets initialized as an identity matrix
Matrix3() {
data[0] = 1; data[1] = 0; data[2] = 0;
data[3] = 0; data[4] = 1; data[5] = 0;
data[6] = 0; data[7] = 0; data[8] = 1;
}
// Takes in every single entry for the matrix
Matrix3(float32 m0, float32 m1, float32 m2,
float32 m3, float32 m4, float32 m5,
float32 m6, float32 m7, float32 m8) {
data[0] = m0; data[1] = m1; data[2] = m2;
data[3] = m3; data[4] = m4; data[5] = m5;
data[6] = m6; data[7] = m7; data[8] = m8;
}
// Takes in an array of floats and fills the entries of the matrix
// NOTE: The constructor does not check if the array of values given
// is valid or has the same size as the matrix.
Matrix3(float32* values) {
for(uint8 i = 0; i < 9; i++) {
data[i] = values[i];
}
}
// Sets all entries of the matrix to this scalar
Matrix3(float32 scalar) {
data[0] = scalar; data[1] = scalar; data[2] = scalar;
data[3] = scalar; data[4] = scalar; data[5] = scalar;
data[6] = scalar; data[7] = scalar; data[8] = scalar;
}
// Fills the columns of the matrix with the given vectors
Matrix3(const Vector3& col1, const Vector3& col2, const Vector3& col3) {
data[0] = col1.x; data[1] = col1.y; data[2] = col1.z;
data[3] = col2.x; data[4] = col2.y; data[5] = col2.z;
data[6] = col3.x; data[7] = col3.y; data[8] = col3.z;
}
// Index operator overload into the components
// NOTE: This indexing operator overload and all other overloads of this type
// will NOT check for out of bounds indices. Please keep this in mind
float32 operator[](const uint32 index) {
if(index > 9 || index < 0) {
return 0.0f;
}
return data[index];
}
float32 operator[](const uint32 index) const {
if(index > 9 || index < 0) {
return 0.0f;
}
return data[index];
}
};
// A 4x4 matrix
union Matrix4 {
// Entries
float32 data[16];
// Default CTOR where the matrix gets initialized as an identity matrix
Matrix4() {
data[0] = 1; data[1] = 0; data[2] = 0; data[3] = 0;
data[4] = 0; data[5] = 1; data[6] = 0; data[7] = 0;
data[8] = 0; data[9] = 0; data[10] = 1; data[11] = 0;
data[12] = 0; data[13] = 0; data[14] = 0; data[15] = 1;
}
// Takes in every single entry for the matrix
Matrix4(float32 m0, float32 m1, float32 m2, float32 m3,
float32 m4, float32 m5, float32 m6, float32 m7,
float32 m8, float32 m9, float32 m10, float32 m11,
float32 m12, float32 m13, float32 m14, float32 m15) {
data[0] = m0; data[1] = m1; data[2] = m2; data[3] = m3;
data[4] = m4; data[5] = m5; data[6] = m6; data[7] = m7;
data[8] = m8; data[9] = m9; data[10] = m10; data[11] = m11;
data[12] = m12; data[13] = m13; data[14] = m14; data[15] = m15;
}
// Takes in an array of floats and fills the entries of the matrix
// NOTE: The constructor does not check if the array of values given
// is valid or has the same size as the matrix.
Matrix4(float32* values) {
for(uint8 i = 0; i < 16; i++) {
data[i] = values[i];
}
}
// Sets all entries to this scalar
Matrix4(float32 scalar) {
data[0] = scalar; data[1] = scalar; data[2] = scalar; data[3] = scalar;
data[4] = scalar; data[5] = scalar; data[6] = scalar; data[7] = scalar;
data[8] = scalar; data[9] = scalar; data[10] = scalar; data[11] = scalar;
data[12] = scalar; data[13] = scalar; data[14] = scalar; data[15] = scalar;
}
// Fills the columns of the matrix with the given vectors
Matrix4(const Vector4& col1, const Vector4& col2, const Vector4& col3, const Vector4& col4) {
data[0] = col1.x; data[1] = col1.y; data[2] = col1.z; data[3] = col1.w;
data[4] = col2.x; data[5] = col2.y; data[6] = col2.z; data[7] = col2.w;
data[8] = col3.x; data[9] = col3.y; data[10] = col3.z; data[11] = col3.w;
data[12] = col4.x; data[13] = col4.y; data[14] = col4.z; data[15] = col4.w;
}
// Index operator overload into the components
// NOTE: This indexing operator overload and all other overloads of this type
// will NOT check for out of bounds indices. Please keep this in mind
float32 operator[](const uint32 index) {
if(index > 16 || index < 0) {
return 0.0f;
}
return data[index];
}
float32 operator[](const uint32 index) const {
if(index > 16 || index < 0) {
return 0.0f;
}
return data[index];
}
};
// A quaternion
union Quaternion {
struct {
float32 x, y, z, w;
};
// Default CTOR where the Quaternion gets initialized as: `(0.0f, 0.0f, 0.0f, 1.0f)`
Quaternion()
:x(0.0f), y(0.0f), z(0.0f), w(1.0f)
{}
// Taking in the 4 components of the Quaternion
Quaternion(const float32 x, const float32 y, const float32 z, const float32 w)
:x(x), y(y), z(z), w(w)
{}
// Fill the `x`, `y`, and `z` components with the given vector `vec` and
// fill the `w` component with given scalar `w`
Quaternion(const Vector3& vec, const float32 w)
:x(vec.x), y(vec.y), z(vec.z), w(w)
{}
// Takes a `Vector4` to fill the components of the Quaternion
Quaternion(const Vector4& vec)
:x(vec.x), y(vec.y), z(vec.z), w(vec.w)
{}
};
///////////////////////////////////////////////////////////////
// Vector2 operator overloading
///////////////////////////////////////////////////////////////
SOC_INLINE Vector2 operator+(const Vector2& v1, const Vector2& v2) {
return Vector2(v1.x + v2.x, v1.y + v2.y);
}
SOC_INLINE Vector2 operator+(const Vector2& v, const float32& s) {
return Vector2(v.x + s, v.y + s);
}
SOC_INLINE Vector2 operator-(const Vector2& v1, const Vector2& v2) {
return Vector2(v1.x - v2.x, v1.y - v2.y);
}
SOC_INLINE Vector2 operator-(const Vector2& v, const float32& s) {
return Vector2(v.x - s, v.y - s);
}
SOC_INLINE Vector2 operator*(const Vector2& v1, const Vector2& v2) {
return Vector2(v1.x * v2.x, v1.y * v2.y);
}
SOC_INLINE Vector2 operator*(const Vector2& v, const float32& s) {
return Vector2(v.x * s, v.y * s);
}
SOC_INLINE Vector2 operator/(const Vector2& v1, const Vector2& v2) {
return Vector2(v1.x / v2.x, v1.y / v2.y);
}
SOC_INLINE Vector2 operator/(const Vector2& v, const float32& s) {
return Vector2(v.x / s, v.y / s);
}
SOC_INLINE void operator+=(Vector2& v1, const Vector2& v2) {
v1 = v1 + v2;
}
SOC_INLINE void operator+=(Vector2& v, const float32& s) {
v = v + s;
}
SOC_INLINE void operator-=(Vector2& v1, const Vector2& v2) {
v1 = v1 - v2;
}
SOC_INLINE void operator-=(Vector2& v, const float32& s) {
v = v - s;
}
SOC_INLINE void operator*=(Vector2& v1, const Vector2& v2) {
v1 = v1 * v2;
}
SOC_INLINE void operator*=(Vector2& v, const float32& s) {
v = v * s;
}
SOC_INLINE void operator/=(Vector2& v1, const Vector2& v2) {
v1 = v1 / v2;
}
SOC_INLINE void operator/=(Vector2& v, const float32& s) {
v = v / s;
}
SOC_INLINE Vector2 operator-(const Vector2& v) {
return Vector2(-v.x, -v.y);
}
///////////////////////////////////////////////////////////////
// Vector3 operator overloading
///////////////////////////////////////////////////////////////
SOC_INLINE Vector3 operator+(const Vector3& v1, const Vector3& v2) {
return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
SOC_INLINE Vector3 operator+(const Vector3& v, const float32 s) {
return Vector3(v.x + s, v.y + s, v.z + s);
}
SOC_INLINE Vector3 operator-(const Vector3& v1, const Vector3& v2) {
return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
SOC_INLINE Vector3 operator-(const Vector3& v, const float32 s) {
return Vector3(v.x - s, v.y - s, v.z - s);
}
SOC_INLINE Vector3 operator*(const Vector3& v1, const Vector3& v2) {
return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
}
SOC_INLINE Vector3 operator*(const Vector3& v, const float32 s) {
return Vector3(v.x * s, v.y * s, v.z * s);
}
SOC_INLINE Vector3 operator*(const Vector3& v, const Matrix3& m) {
return Vector3((v.x * m[0]) + (v.y * m[1]) + (v.z * m[2]),
(v.x * m[3]) + (v.y * m[4]) + (v.z * m[5]),
(v.x * m[6]) + (v.y * m[7]) + (v.z * m[8]));
}
SOC_INLINE Vector3 operator/(const Vector3& v1, const Vector3& v2) {
return Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
}
SOC_INLINE Vector3 operator/(const Vector3& v, const float32 s) {
return Vector3(v.x / s, v.y / s, v.z / s);
}
SOC_INLINE void operator+=(Vector3& v1, const Vector3& v2) {
v1 = v1 + v2;
}
SOC_INLINE void operator+=(Vector3& v, const float32& s) {
v = v + s;
}
SOC_INLINE void operator-=(Vector3& v1, const Vector3& v2) {
v1 = v1 - v2;
}
SOC_INLINE void operator-=(Vector3& v, const float32& s) {
v = v - s;
}
SOC_INLINE void operator*=(Vector3& v1, const Vector3& v2) {
v1 = v1 * v2;
}
SOC_INLINE void operator*=(Vector3& v, const float32& s) {
v = v * s;
}
SOC_INLINE void operator/=(Vector3& v1, const Vector3& v2) {
v1 = v1 / v2;
}
SOC_INLINE void operator/=(Vector3& v, const float32& s) {
v = v / s;
}
SOC_INLINE Vector3 operator-(const Vector3& v) {
return Vector3(-v.x, -v.y, -v.z);
}
///////////////////////////////////////////////////////////////
// Vector4 operator overloading
///////////////////////////////////////////////////////////////
SOC_INLINE Vector4 operator+(const Vector4& v1, const Vector4& v2) {
return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
}
SOC_INLINE Vector4 operator+(const Vector4& v, const float32 s) {
return Vector4(v.x + s, v.y + s, v.z + s, v.w + s);
}
SOC_INLINE Vector4 operator-(const Vector4& v1, const Vector4& v2) {
return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
}
SOC_INLINE Vector4 operator-(const Vector4& v, const float32 s) {
return Vector4(v.x - s, v.y - s, v.z - s, v.w - s);
}
SOC_INLINE Vector4 operator*(const Vector4& v1, const Vector4& v2) {
return Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
}
SOC_INLINE Vector4 operator*(const Vector4& v, const float32 s) {
return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
}
SOC_INLINE Vector4 operator*(const Vector4& v, const Matrix4& m) {
return Vector4((v.x * m[0]) + (v.y * m[1]) + (v.z * m[2]) + (v.w * m[3]),
(v.x * m[4]) + (v.y * m[5]) + (v.z * m[6]) + (v.w * m[7]),
(v.x * m[8]) + (v.y * m[9]) + (v.z * m[10]) + (v.w * m[11]),
(v.x * m[12]) + (v.y * m[13]) + (v.z * m[14]) + (v.w * m[15]));
}
SOC_INLINE Vector4 operator/(const Vector4& v1, const Vector4& v2) {
return Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w);
}
SOC_INLINE Vector4 operator/(const Vector4& v, const float32 s) {
return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
}
SOC_INLINE void operator+=(Vector4& v1, const Vector4& v2) {
v1 = v1 + v2;
}
SOC_INLINE void operator+=(Vector4& v, const float32& s) {
v = v + s;
}
SOC_INLINE void operator-=(Vector4& v1, const Vector4& v2) {
v1 = v1 - v2;
}
SOC_INLINE void operator-=(Vector4& v, const float32& s) {
v = v - s;
}
SOC_INLINE void operator*=(Vector4& v1, const Vector4& v2) {
v1 = v1 * v2;
}
SOC_INLINE void operator*=(Vector4& v, const float32& s) {
v = v * s;
}
SOC_INLINE void operator/=(Vector4& v1, const Vector4& v2) {
v1 = v1 / v2;
}
SOC_INLINE void operator/=(Vector4& v, const float32& s) {
v = v / s;
}
SOC_INLINE Vector4 operator-(const Vector4& v) {
return Vector4(-v.x, -v.y, -v.z, -v.w);
}
///////////////////////////////////////////////////////////////
// Matrix3 operator overloading
///////////////////////////////////////////////////////////////
SOC_INLINE Matrix3 operator+(const Matrix3& m1, const Matrix3& m2) {
Matrix3 result(0.0f);
for(uint32 i = 0; i < 9; i++) {
result.data[i] = m1[i] + m2[i];
}
return result;
}
SOC_INLINE Matrix3 operator+(const Matrix3& m, const float32 s) {
Matrix3 result(0.0f);
for(uint32 i = 0; i < 9; i++) {
result.data[i] = m[i] + s;
}
return result;
}
SOC_INLINE Matrix3 operator-(const Matrix3& m1, const Matrix3& m2) {
Matrix3 result(0.0f);
for(uint32 i = 0; i < 9; i++) {
result.data[i] = m1[i] - m2[i];
}
return result;
}
SOC_INLINE Matrix3 operator-(const Matrix3& m, const float32 s) {
Matrix3 result(0.0f);
for(uint32 i = 0; i < 9; i++) {
result.data[i] = m[i] - s;
}
return result;
}
SOC_INLINE Matrix3 operator*(const Matrix3& m1, const Matrix3& m2) {
return Matrix3(
(m1[0] * m2[0]) + (m1[1] * m2[3]) + (m1[2] * m2[6]), // m0
(m1[0] * m2[1]) + (m1[1] * m2[4]) + (m1[2] * m2[7]), // m1
(m1[0] * m2[2]) + (m1[1] * m2[5]) + (m1[2] * m2[8]), // m2
(m1[3] * m2[0]) + (m1[4] * m2[3]) + (m1[5] * m2[6]), // m3
(m1[3] * m2[1]) + (m1[4] * m2[4]) + (m1[5] * m2[7]), // m4
(m1[3] * m2[2]) + (m1[4] * m2[5]) + (m1[5] * m2[8]), // m5
(m1[6] * m2[0]) + (m1[7] * m2[3]) + (m1[8] * m2[6]), // m6
(m1[6] * m2[1]) + (m1[7] * m2[4]) + (m1[8] * m2[7]), // m7
(m1[6] * m2[2]) + (m1[7] * m2[5]) + (m1[8] * m2[8]) // m8
);
}
SOC_INLINE Matrix3 operator*(const Matrix3& m, const float32 s) {
Matrix3 result(0.0f);
for(uint32 i = 0; i < 9; i++) {
result.data[i] = m[i] * s;
}
return result;
}
SOC_INLINE Matrix3 operator-(const Matrix3& m) {
Matrix3 result(0.0f);
for(uint32 i = 0; i < 9; i++) {
result.data[i] = -m[i];
}
return result;
}
SOC_INLINE void operator+=(Matrix3& m1, const Matrix3& m2) {
m1 = m1 + m2;
}
SOC_INLINE void operator+=(Matrix3& m, const float32 s) {
m = m + s;
}
SOC_INLINE void operator-=(Matrix3& m1, const Matrix3& m2) {
m1 = m1 - m2;
}
SOC_INLINE void operator-=(Matrix3& m, const float32 s) {
m = m - s;
}
SOC_INLINE void operator*=(Matrix3& m1, const Matrix3& m2) {
m1 = m1 * m2;
}
SOC_INLINE void operator*=(Matrix3& m, const float32 s) {
m = m * s;
}
//////////////////////////////////////////////////////////////
// Matrix4 operator overloading
///////////////////////////////////////////////////////////////
SOC_INLINE Matrix4 operator+(const Matrix4& m1, const Matrix4& m2) {
Matrix4 result(0.0f);
for(uint32 i = 0; i < 16; i++) {
result.data[i] = m1[i] + m2[i];
}
return result;
}
SOC_INLINE Matrix4 operator+(const Matrix4& m, const float32 s) {
Matrix4 result(0.0f);
for(uint32 i = 0; i < 16; i++) {
result.data[i] = m[i] + s;
}
return result;
}
SOC_INLINE Matrix4 operator-(const Matrix4& m1, const Matrix4& m2) {
Matrix4 result(0.0f);
for(uint32 i = 0; i < 16; i++) {
result.data[i] = m1[i] - m2[i];
}
return result;
}
SOC_INLINE Matrix4 operator-(const Matrix4& m, const float32 s) {
Matrix4 result(0.0f);
for(uint32 i = 0; i < 16; i++) {
result.data[i] = m[i] - s;
}
return result;
}
SOC_INLINE Matrix4 operator*(const Matrix4& m1, const Matrix4& m2) {
return Matrix4((m2[0] * m1[0]) + (m2[1] * m1[4]) + (m2[2] * m1[8]) + (m2[3] * m1[12]), // m0
(m2[0] * m1[1]) + (m2[1] * m1[5]) + (m2[2] * m1[9]) + (m2[3] * m1[13]), // m1
(m2[0] * m1[2]) + (m2[1] * m1[6]) + (m2[2] * m1[10]) + (m2[3] * m1[14]), // m2
(m2[0] * m1[3]) + (m2[1] * m1[7]) + (m2[2] * m1[11]) + (m2[3] * m1[15]), // m3
(m2[4] * m1[0]) + (m2[5] * m1[4]) + (m2[6] * m1[8]) + (m2[7] * m1[12]), // m4
(m2[4] * m1[1]) + (m2[5] * m1[5]) + (m2[6] * m1[9]) + (m2[7] * m1[13]), // m5
(m2[4] * m1[2]) + (m2[5] * m1[6]) + (m2[6] * m1[10]) + (m2[7] * m1[14]), // m6
(m2[4] * m1[3]) + (m2[5] * m1[7]) + (m2[6] * m1[11]) + (m2[7] * m1[15]), // m7
(m2[8] * m1[0]) + (m2[9] * m1[4]) + (m2[10] * m1[8]) + (m2[11] * m1[12]), // m8
(m2[8] * m1[1]) + (m2[9] * m1[5]) + (m2[10] * m1[9]) + (m2[11] * m1[13]), // m9
(m2[8] * m1[2]) + (m2[9] * m1[6]) + (m2[10] * m1[10]) + (m2[11] * m1[14]), // m10
(m2[8] * m1[3]) + (m2[9] * m1[7]) + (m2[10] * m1[11]) + (m2[11] * m1[15]), // m11
(m2[12] * m1[0]) + (m2[13] * m1[4]) + (m2[14] * m1[8]) + (m2[15] * m1[12]), // m12
(m2[12] * m1[1]) + (m2[13] * m1[5]) + (m2[14] * m1[9]) + (m2[15] * m1[13]), // m13
(m2[12] * m1[2]) + (m2[13] * m1[6]) + (m2[14] * m1[10]) + (m2[15] * m1[14]), // m14
(m2[12] * m1[3]) + (m2[13] * m1[7]) + (m2[14] * m1[11]) + (m2[15] * m1[15]) // m15
);
}
SOC_INLINE Matrix4 operator*(const Matrix4& m, const float32 s) {
Matrix4 result(0.0f);
for(uint32 i = 0; i < 16; i++) {
result.data[i] = m[i] * s;
}
return result;
}
SOC_INLINE Matrix4 operator-(const Matrix4& m) {
Matrix4 result(0.0f);
for(uint32 i = 0; i < 16; i++) {
result.data[i] = -m[i];
}
return result;
}
SOC_INLINE void operator+=(Matrix4& m1, const Matrix4& m2) {
m1 = m1 + m2;
}
SOC_INLINE void operator+=(Matrix4& m, const float32 s) {
m = m + s;
}
SOC_INLINE void operator-=(Matrix4& m1, const Matrix4& m2) {
m1 = m1 - m2;
}
SOC_INLINE void operator-=(Matrix4& m, const float32 s) {
m = m - s;
}
SOC_INLINE void operator*=(Matrix4& m1, const Matrix4& m2) {
m1 = m1 * m2;
}
SOC_INLINE void operator*=(Matrix4& m, const float32 s) {
m = m * s;
}
///////////////////////////////////////////////////////////////
// Quaternion operator overloading
///////////////////////////////////////////////////////////////
SOC_INLINE const Quaternion operator+(const Quaternion& q1, const Quaternion& q2) {
return Quaternion(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w);
}
SOC_INLINE const void operator+=(Quaternion& q1, const Quaternion& q2) {
q1 = q1 + q2;
}
SOC_INLINE const Quaternion operator-(const Quaternion& q1, const Quaternion& q2) {
return Quaternion(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w);
}
SOC_INLINE const void operator-=(Quaternion& q1, const Quaternion& q2) {
q1 = q1 - q2;
}
SOC_INLINE const Quaternion operator*(const Quaternion& q1, const Quaternion& q2) {
return Quaternion(q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x,
q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w,
q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z);
}
SOC_INLINE const void operator*=(Quaternion& q1, const Quaternion& q2) {
q1 = q1 * q2;
}
SOC_INLINE const Quaternion operator*(const Quaternion& q, const float32 s) {
return Quaternion(q.x * s, q.y * s, q.z * s, q.w * s);
}
SOC_INLINE const void operator*=(Quaternion& q, const float32 s) {
q = q * s;
}
///////////////////////////////////////////////////////////////
// Misc. functions
///////////////////////////////////////////////////////////////
// Returns a float scalar between `min` and `max`
SOC_INLINE float32 float_clamp(const float32 value, const float32 min, const float32 max) {
if(value > max) {
return max;
}
else if(value < min) {
return value;
}
return value;
}
// Returns a int scalar between `min` and `max`
SOC_INLINE int32 int_clamp(const int32 value, const int32 min, const int32 max) {
if(value > max) {
return max;
}
else if(value < min) {
return value;
}
return value;
}
// Sin of `x`
SOC_INLINE float64 sin(const float64 x) {
return std::sin(x);
}
// Cos of `x`
SOC_INLINE float64 cos(const float64 x) {
return std::cos(x);
}
// Tan of `x`
SOC_INLINE float64 tan(const float64 x) {
return std::tan(x);
}
// Atan of `x`
SOC_INLINE float64 atan(const float64 x) {
return std::atan(x);
}
// Atan2 of `y` and `x`
SOC_INLINE float64 atan2(const float64 y, const float64 x) {
return std::atan2(y, x);
}
// Floor of `x`
SOC_INLINE float64 floor(const float64 x) {
return std::floor(x);
}
// Square root of `x`
SOC_INLINE float64 sqrt(const float64 x) {
return std::sqrt(x);
}
// Returns the smallest number between `x` and `y`
SOC_INLINE float64 min(const float64 x, const float64 y) {
return std::min(x, y);
}
// Returns the biggest number between `x` and `y`
SOC_INLINE float64 max(const float64 x, const float64 y) {
return std::max(x, y);
}
// Returns the linear interpolation with the given `start`, `end`, and `amount`
SOC_INLINE float32 lerp(const float32 start, const float32 end, const float32 amount) {
return start + amount * (end - start);
}
// Remaps the given `value` from the old range to a new range
SOC_INLINE float32 remap(const float32 value, const float32 old_min, const float32 old_max, const float32 new_min, const float32 new_max) {
return (value - old_min) / (old_max - old_min) * (new_max - new_min) + new_max;
}
///////////////////////////////////////////////////////////////
// Vector2 functions
///////////////////////////////////////////////////////////////
// Returns the dot product of `v1` and `v2`
SOC_INLINE const float32 vec2_dot(const Vector2& v1, const Vector2& v2) {
return (v1.x * v2.x) + (v1.y * v2.y);
}
// Returns the length/magnitude of `v`
SOC_INLINE const float32 vec2_length(const Vector2& v) {
return sqrt((v.x * v.x) + (v.y * v.y));
}
// Returns the normalized version of the vector `v`