-
Notifications
You must be signed in to change notification settings - Fork 1
/
rmath.c3
1961 lines (1550 loc) · 52.5 KB
/
rmath.c3
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
module rl;
import std::math; // Required for: math::sin(), math::cos(), tan(), math::atan2(), math::sqrt(), floor(), math::min(), math::max(), fabs()
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
const EPSILON = 0.000001f;
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
// NOTE: Helper types to be used instead of array return types for *ToFloat functions
def Float3 = float[3];
def Float16 = float[<16>];
def Quaternionf = quaternion::Quaternion(<float>);
//----------------------------------------------------------------------------------
// Module Functions Definition - Utils math
//----------------------------------------------------------------------------------
// Clamp float value
fn float clamp(float value, float min, float max) @inline {
return value.clamp(min, max);
}
// Calculate linear interpolation between two floats
fn float lerp(float start, float end, float amount) {
float result = start + amount*(end - start);
return result;
}
// Normalize input value within input range
fn float normalize(float value, float start, float end) {
float result = (value - start)/(end - start);
return result;
}
// Remap input value within input range to output range
fn float remap(float value, float inputStart, float inputEnd, float outputStart, float outputEnd) {
float result = (value - inputStart)/(inputEnd - inputStart)*(outputEnd - outputStart) + outputStart;
return result;
}
// Wrap input value from min to max
fn float wrap(float value, float min, float max) {
float result = value - (max - min)*math::floor((value - min)/(max - min));
return result;
}
// Check whether two given floats are almost equal
fn bool float_equals(float x, float y) {
bool result = (math::abs(x - y)) <= (EPSILON*math::max(1.0f, math::max(math::abs(x), math::abs(y))));
return result;
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Vector2 math
//----------------------------------------------------------------------------------
// Vector with components value 0.0f
fn Vector2 vector2_zero() @inline {
Vector2 result = { 0.0f, 0.0f };
return result;
}
// Vector with components value 1.0f
fn Vector2 vector2_one() @inline {
Vector2 result = { 1.0f, 1.0f };
return result;
}
// Add two vectors (v1 + v2)
fn Vector2 vector2_add(Vector2 v1, Vector2 v2) {
Vector2 result = { v1.x + v2.x, v1.y + v2.y };
return result;
}
// Add vector and float value
fn Vector2 vector2_add_value(Vector2 v, float add) {
Vector2 result = { v.x + add, v.y + add };
return result;
}
// Subtract two vectors (v1 - v2)
fn Vector2 vector2_subtract(Vector2 v1, Vector2 v2) {
Vector2 result = { v1.x - v2.x, v1.y - v2.y };
return result;
}
// Subtract vector by float value
fn Vector2 vector2_subtract_value(Vector2 v, float sub) {
Vector2 result = { v.x - sub, v.y - sub };
return result;
}
// Calculate vector length
fn float vector2_length(Vector2 v) {
float result = math::sqrt((v.x*v.x) + (v.y*v.y));
return result;
}
// Calculate vector square length
fn float vector2_length_sqr(Vector2 v) {
float result = (v.x*v.x) + (v.y*v.y);
return result;
}
// Calculate two vectors dot product
fn float vector2_dot_product(Vector2 v1, Vector2 v2) {
float result = (v1.x*v2.x + v1.y*v2.y);
return result;
}
// Calculate distance between two vectors
fn float vector2_distance(Vector2 v1, Vector2 v2) {
float result = math::sqrt((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
return result;
}
// Calculate square distance between two vectors
fn float vector2_distance_sqr(Vector2 v1, Vector2 v2) {
float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
return result;
}
// Calculate angle between two vectors
// NOTE: Angle is calculated from origin point (0, 0)
fn float vector2_angle(Vector2 v1, Vector2 v2) {
float result = 0.0f;
float dot = v1.x*v2.x + v1.y*v2.y;
float det = v1.x*v2.y - v1.y*v2.x;
result = math::atan2(det, dot);
return result;
}
// Calculate angle defined by a two vectors line
// NOTE: Parameters need to be normalized
// Current implementation should be aligned with glm::angle
fn float vector2_line_angle(Vector2 start, Vector2 end) {
float result = 0.0f;
// TODO(10/9/2023): Currently angles move clockwise, determine if this is wanted behavior
result = -math::atan2(end.y - start.y, end.x - start.x);
return result;
}
// Scale vector (multiply by value)
fn Vector2 vector2_scale(Vector2 v, float scale) {
Vector2 result = { v.x*scale, v.y*scale };
return result;
}
// Multiply vector by vector
fn Vector2 vector2_multiply(Vector2 v1, Vector2 v2) {
Vector2 result = { v1.x*v2.x, v1.y*v2.y };
return result;
}
// Negate vector
fn Vector2 vector2_negate(Vector2 v) {
Vector2 result = { -v.x, -v.y };
return result;
}
// Divide vector by vector
fn Vector2 vector2_divide(Vector2 v1, Vector2 v2) {
Vector2 result = { v1.x/v2.x, v1.y/v2.y };
return result;
}
// Normalize provided vector
fn Vector2 vector2_normalize(Vector2 v) {
Vector2 result;
float length = math::sqrt((v.x*v.x) + (v.y*v.y));
if (length > 0)
{
float ilength = 1.0f/length;
result.x = v.x*ilength;
result.y = v.y*ilength;
}
return result;
}
// Transforms a Vector2 by a given Matrix
fn Vector2 vector2_transform(Vector2 v, Matrix mat) {
Vector2 result;
float x = v.x;
float y = v.y;
float z = 0;
result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
return result;
}
// Calculate linear interpolation between two vectors
fn Vector2 vector2_lerp(Vector2 v1, Vector2 v2, float amount) {
Vector2 result;
result.x = v1.x + amount*(v2.x - v1.x);
result.y = v1.y + amount*(v2.y - v1.y);
return result;
}
// Calculate reflected vector to normal
fn Vector2 vector2_reflect(Vector2 v, Vector2 normal) {
Vector2 result;
float dotProduct = (v.x*normal.x + v.y*normal.y); // Dot product
result.x = v.x - (2.0f*normal.x)*dotProduct;
result.y = v.y - (2.0f*normal.y)*dotProduct;
return result;
}
// Rotate vector by angle
fn Vector2 vector2_rotate(Vector2 v, float angle) {
Vector2 result;
float cosres = math::cos(angle);
float sinres = math::sin(angle);
result.x = v.x*cosres - v.y*sinres;
result.y = v.x*sinres + v.y*cosres;
return result;
}
// Move Vector towards target
fn Vector2 vector2_move_towards(Vector2 v, Vector2 target, float maxDistance) {
Vector2 result;
float dx = target.x - v.x;
float dy = target.y - v.y;
float value = (dx*dx) + (dy*dy);
if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;
float dist = math::sqrt(value);
result.x = v.x + dx/dist*maxDistance;
result.y = v.y + dy/dist*maxDistance;
return result;
}
// Invert the given vector
fn Vector2 vector2_invert(Vector2 v) {
Vector2 result = { 1.0f/v.x, 1.0f/v.y };
return result;
}
// Clamp the components of the vector between
// min and max values specified by the given vectors
fn Vector2 vector2_clamp(Vector2 v, Vector2 min, Vector2 max) {
Vector2 result;
result.x = math::min(max.x, math::max(min.x, v.x));
result.y = math::min(max.y, math::max(min.y, v.y));
return result;
}
// Clamp the magnitude of the vector between two min and max values
fn Vector2 vector2_clamp_value(Vector2 v, float min, float max) {
Vector2 result = v;
float length = (v.x*v.x) + (v.y*v.y);
if (length > 0.0f)
{
length = math::sqrt(length);
if (length < min)
{
float scale = min/length;
result.x = v.x*scale;
result.y = v.y*scale;
}
else if (length > max)
{
float scale = max/length;
result.x = v.x*scale;
result.y = v.y*scale;
}
}
return result;
}
// Check whether two given vectors are almost equal
fn bool vector2_equals(Vector2 p, Vector2 q) {
bool result = ((math::abs(p.x - q.x)) <= (EPSILON*math::max(1.0f, math::max(math::abs(p.x), math::abs(q.x))))) &&
((math::abs(p.y - q.y)) <= (EPSILON*math::max(1.0f, math::max(math::abs(p.y), math::abs(q.y)))));
return result;
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Vector3 math
//----------------------------------------------------------------------------------
// Vector with components value 0.0f
fn Vector3 vector3_zero() {
Vector3 result = { 0.0f, 0.0f, 0.0f };
return result;
}
// Vector with components value 1.0f
fn Vector3 vector3_one() {
Vector3 result = { 1.0f, 1.0f, 1.0f };
return result;
}
// Add two vectors
fn Vector3 vector3_add(Vector3 v1, Vector3 v2) {
Vector3 result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
return result;
}
// Add vector and float value
fn Vector3 vector3_add_value(Vector3 v, float add) {
Vector3 result = { v.x + add, v.y + add, v.z + add };
return result;
}
// Subtract two vectors
fn Vector3 vector3_subtract(Vector3 v1, Vector3 v2) {
Vector3 result = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
return result;
}
// Subtract vector by float value
fn Vector3 vector3_subtract_value(Vector3 v, float sub) {
Vector3 result = { v.x - sub, v.y - sub, v.z - sub };
return result;
}
// Multiply vector by scalar
fn Vector3 vector3_scale(Vector3 v, float scalar) {
Vector3 result = { v.x*scalar, v.y*scalar, v.z*scalar };
return result;
}
// Multiply vector by vector
fn Vector3 vector3_multiply(Vector3 v1, Vector3 v2) {
Vector3 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z };
return result;
}
// Calculate two vectors cross product
fn Vector3 vector3_cross_product(Vector3 v1, Vector3 v2) {
Vector3 result = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
return result;
}
// Calculate one vector perpendicular vector
fn Vector3 vector3_perpendicular(Vector3 v) {
Vector3 result;
float min = (float) math::abs(v.x);
Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f};
if (math::abs(v.y) < min)
{
min = (float) math::abs(v.y);
Vector3 tmp = {0.0f, 1.0f, 0.0f};
cardinalAxis = tmp;
}
if (math::abs(v.z) < min)
{
Vector3 tmp = {0.0f, 0.0f, 1.0f};
cardinalAxis = tmp;
}
// Cross product between vectors
result.x = v.y*cardinalAxis.z - v.z*cardinalAxis.y;
result.y = v.z*cardinalAxis.x - v.x*cardinalAxis.z;
result.z = v.x*cardinalAxis.y - v.y*cardinalAxis.x;
return result;
}
// Calculate vector length
fn float vector3_length(Vector3 v) {
float result = math::sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
return result;
}
// Calculate vector square length
fn float vector3_length_sqr(Vector3 v) {
float result = v.x*v.x + v.y*v.y + v.z*v.z;
return result;
}
// Calculate two vectors dot product
fn float vector3_dot_product(Vector3 v1, Vector3 v2) {
float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
return result;
}
// Calculate distance between two vectors
fn float vector3_distance(Vector3 v1, Vector3 v2) {
float result = 0.0f;
float dx = v2.x - v1.x;
float dy = v2.y - v1.y;
float dz = v2.z - v1.z;
result = math::sqrt(dx*dx + dy*dy + dz*dz);
return result;
}
// Calculate square distance between two vectors
fn float vector3_distance_sqr(Vector3 v1, Vector3 v2) {
float result = 0.0f;
float dx = v2.x - v1.x;
float dy = v2.y - v1.y;
float dz = v2.z - v1.z;
result = dx*dx + dy*dy + dz*dz;
return result;
}
// Calculate angle between two vectors
fn float vector3_angle(Vector3 v1, Vector3 v2) {
float result = 0.0f;
Vector3 cross = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
float len = math::sqrt(cross.x*cross.x + cross.y*cross.y + cross.z*cross.z);
float dot = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
result = math::atan2(len, dot);
return result;
}
// Negate provided vector (invert direction)
fn Vector3 vector3_negate(Vector3 v) {
Vector3 result = { -v.x, -v.y, -v.z };
return result;
}
// Divide vector by vector
fn Vector3 vector3_divide(Vector3 v1, Vector3 v2) {
Vector3 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z };
return result;
}
// Normalize provided vector
fn Vector3 vector3_normalize(Vector3 v) {
Vector3 result = v;
float length = math::sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
if (length != 0.0f)
{
float ilength = 1.0f/length;
result.x *= ilength;
result.y *= ilength;
result.z *= ilength;
}
return result;
}
//Calculate the projection of the vector v1 on to v2
fn Vector3 vector3_project(Vector3 v1, Vector3 v2) {
Vector3 result;
float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
float mag = v1dv2/v2dv2;
result.x = v2.x*mag;
result.y = v2.y*mag;
result.z = v2.z*mag;
return result;
}
//Calculate the rejection of the vector v1 on to v2
fn Vector3 vector3_reject(Vector3 v1, Vector3 v2) {
Vector3 result;
float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
float mag = v1dv2/v2dv2;
result.x = v1.x - (v2.x*mag);
result.y = v1.y - (v2.y*mag);
result.z = v1.z - (v2.z*mag);
return result;
}
// Orthonormalize provided vectors
// Makes vectors normalized and orthogonal to each other
// Gram-Schmidt function implementation
fn void vector3_ortho_normalize(Vector3 *v1, Vector3 *v2) {
float length = 0.0f;
float ilength = 0.0f;
// vector3_Normalize(*v1);
Vector3 v = *v1;
length = math::sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
if (length == 0.0f) {
length = 1.0f;
}
ilength = 1.0f/length;
v1.x *= ilength;
v1.y *= ilength;
v1.z *= ilength;
// vector3_CrossProduct(*v1, *v2)
Vector3 vn1 = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
// vector3_Normalize(vn1);
v = vn1;
length = math::sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
if (length == 0.0f) length = 1.0f;
ilength = 1.0f/length;
vn1.x *= ilength;
vn1.y *= ilength;
vn1.z *= ilength;
// vector3_CrossProduct(vn1, *v1)
Vector3 vn2 = { vn1.y*v1.z - vn1.z*v1.y, vn1.z*v1.x - vn1.x*v1.z, vn1.x*v1.y - vn1.y*v1.x };
*v2 = vn2;
}
// Transforms a Vector3 by a given Matrix
fn Vector3 vector3_transform(Vector3 v, Matrix mat) {
Vector3 result;
float x = v.x;
float y = v.y;
float z = v.z;
result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14;
return result;
}
// Transform a vector by quaternion rotation
fn Vector3 vector3_rotate_by_quaternion(Vector3 v, Quaternion q) {
Vector3 result;
result.x = v.x*(q.x*q.x + q.w*q.w - q.y*q.y - q.z*q.z) + v.y*(2*q.x*q.y - 2*q.w*q.z) + v.z*(2*q.x*q.z + 2*q.w*q.y);
result.y = v.x*(2*q.w*q.z + 2*q.x*q.y) + v.y*(q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z) + v.z*(-2*q.w*q.x + 2*q.y*q.z);
result.z = v.x*(-2*q.w*q.y + 2*q.x*q.z) + v.y*(2*q.w*q.x + 2*q.y*q.z)+ v.z*(q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z);
return result;
}
// Rotates a vector around an axis
fn Vector3 vector3_rotate_by_axis_angle(Vector3 v, Vector3 axis, float angle) {
// Using Euler-Rodrigues Formula
// Ref.: https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula
Vector3 result = v;
// vector3_Normalize(axis);
float length = math::sqrt(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
if (length == 0.0f) length = 1.0f;
float ilength = 1.0f / length;
axis.x *= ilength;
axis.y *= ilength;
axis.z *= ilength;
angle /= 2.0f;
float a = math::sin(angle);
float b = axis.x*a;
float c = axis.y*a;
float d = axis.z*a;
a = math::cos(angle);
Vector3 w = { b, c, d };
// vector3_CrossProduct(w, v)
Vector3 wv = { w.y*v.z - w.z*v.y, w.z*v.x - w.x*v.z, w.x*v.y - w.y*v.x };
// vector3_CrossProduct(w, wv)
Vector3 wwv = { w.y*wv.z - w.z*wv.y, w.z*wv.x - w.x*wv.z, w.x*wv.y - w.y*wv.x };
// vector3_Scale(wv, 2*a)
a *= 2;
wv.x *= a;
wv.y *= a;
wv.z *= a;
// vector3_Scale(wwv, 2)
wwv.x *= 2;
wwv.y *= 2;
wwv.z *= 2;
result.x += wv.x;
result.y += wv.y;
result.z += wv.z;
result.x += wwv.x;
result.y += wwv.y;
result.z += wwv.z;
return result;
}
// Calculate linear interpolation between two vectors
fn Vector3 vector3_lerp(Vector3 v1, Vector3 v2, float amount) {
Vector3 result;
result.x = v1.x + amount*(v2.x - v1.x);
result.y = v1.y + amount*(v2.y - v1.y);
result.z = v1.z + amount*(v2.z - v1.z);
return result;
}
// Calculate reflected vector to normal
fn Vector3 vector3_reflect(Vector3 v, Vector3 normal) {
Vector3 result;
// I is the original vector
// N is the normal of the incident plane
// R = I - (2*N*(DotProduct[I, N]))
float dotProduct = (v.x*normal.x + v.y*normal.y + v.z*normal.z);
result.x = v.x - (2.0f*normal.x)*dotProduct;
result.y = v.y - (2.0f*normal.y)*dotProduct;
result.z = v.z - (2.0f*normal.z)*dotProduct;
return result;
}
// Get min value for each pair of components
fn Vector3 vector3_min(Vector3 v1, Vector3 v2) {
Vector3 result;
result.x = math::min(v1.x, v2.x);
result.y = math::min(v1.y, v2.y);
result.z = math::min(v1.z, v2.z);
return result;
}
// Get max value for each pair of components
fn Vector3 vector3_max(Vector3 v1, Vector3 v2) {
Vector3 result;
result.x = math::max(v1.x, v2.x);
result.y = math::max(v1.y, v2.y);
result.z = math::max(v1.z, v2.z);
return result;
}
// Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
// NOTE: Assumes P is on the plane of the triangle
fn Vector3 vector3_barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c) {
Vector3 result;
Vector3 v0 = { b.x - a.x, b.y - a.y, b.z - a.z }; // vector3_Subtract(b, a)
Vector3 v1 = { c.x - a.x, c.y - a.y, c.z - a.z }; // vector3_Subtract(c, a)
Vector3 v2 = { p.x - a.x, p.y - a.y, p.z - a.z }; // vector3_Subtract(p, a)
float d00 = (v0.x*v0.x + v0.y*v0.y + v0.z*v0.z); // vector3_DotProduct(v0, v0)
float d01 = (v0.x*v1.x + v0.y*v1.y + v0.z*v1.z); // vector3_DotProduct(v0, v1)
float d11 = (v1.x*v1.x + v1.y*v1.y + v1.z*v1.z); // vector3_DotProduct(v1, v1)
float d20 = (v2.x*v0.x + v2.y*v0.y + v2.z*v0.z); // vector3_DotProduct(v2, v0)
float d21 = (v2.x*v1.x + v2.y*v1.y + v2.z*v1.z); // vector3_DotProduct(v2, v1)
float denom = d00*d11 - d01*d01;
result.y = (d11*d20 - d01*d21)/denom;
result.z = (d00*d21 - d01*d20)/denom;
result.x = 1.0f - (result.z + result.y);
return result;
}
// Projects a Vector3 from screen space into object space
// NOTE: We are avoiding calling other raymath functions despite available
fn Vector3 vector3_unproject(Vector3 source, Matrix projection, Matrix view) {
Vector3 result;
// Calculate unprojected matrix (multiply view matrix by projection matrix) and invert it
Matrix matViewProj = { // MatrixMultiply(view, projection);
view.m0*projection.m0 + view.m1*projection.m4 + view.m2*projection.m8 + view.m3*projection.m12,
view.m0*projection.m1 + view.m1*projection.m5 + view.m2*projection.m9 + view.m3*projection.m13,
view.m0*projection.m2 + view.m1*projection.m6 + view.m2*projection.m10 + view.m3*projection.m14,
view.m0*projection.m3 + view.m1*projection.m7 + view.m2*projection.m11 + view.m3*projection.m15,
view.m4*projection.m0 + view.m5*projection.m4 + view.m6*projection.m8 + view.m7*projection.m12,
view.m4*projection.m1 + view.m5*projection.m5 + view.m6*projection.m9 + view.m7*projection.m13,
view.m4*projection.m2 + view.m5*projection.m6 + view.m6*projection.m10 + view.m7*projection.m14,
view.m4*projection.m3 + view.m5*projection.m7 + view.m6*projection.m11 + view.m7*projection.m15,
view.m8*projection.m0 + view.m9*projection.m4 + view.m10*projection.m8 + view.m11*projection.m12,
view.m8*projection.m1 + view.m9*projection.m5 + view.m10*projection.m9 + view.m11*projection.m13,
view.m8*projection.m2 + view.m9*projection.m6 + view.m10*projection.m10 + view.m11*projection.m14,
view.m8*projection.m3 + view.m9*projection.m7 + view.m10*projection.m11 + view.m11*projection.m15,
view.m12*projection.m0 + view.m13*projection.m4 + view.m14*projection.m8 + view.m15*projection.m12,
view.m12*projection.m1 + view.m13*projection.m5 + view.m14*projection.m9 + view.m15*projection.m13,
view.m12*projection.m2 + view.m13*projection.m6 + view.m14*projection.m10 + view.m15*projection.m14,
view.m12*projection.m3 + view.m13*projection.m7 + view.m14*projection.m11 + view.m15*projection.m15 };
// Calculate inverted matrix . MatrixInvert(matViewProj);
// Cache the matrix values (speed optimization)
float a00 = matViewProj.m0;
float a01 = matViewProj.m1;
float a02 = matViewProj.m2;
float a03 = matViewProj.m3;
float a10 = matViewProj.m4;
float a11 = matViewProj.m5;
float a12 = matViewProj.m6;
float a13 = matViewProj.m7;
float a20 = matViewProj.m8;
float a21 = matViewProj.m9;
float a22 = matViewProj.m10;
float a23 = matViewProj.m11;
float a30 = matViewProj.m12;
float a31 = matViewProj.m13;
float a32 = matViewProj.m14;
float a33 = matViewProj.m15;
float b00 = a00*a11 - a01*a10;
float b01 = a00*a12 - a02*a10;
float b02 = a00*a13 - a03*a10;
float b03 = a01*a12 - a02*a11;
float b04 = a01*a13 - a03*a11;
float b05 = a02*a13 - a03*a12;
float b06 = a20*a31 - a21*a30;
float b07 = a20*a32 - a22*a30;
float b08 = a20*a33 - a23*a30;
float b09 = a21*a32 - a22*a31;
float b10 = a21*a33 - a23*a31;
float b11 = a22*a33 - a23*a32;
// Calculate the invert determinant (inlined to avoid double-caching)
float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
Matrix matViewProjInv = {
(a11*b11 - a12*b10 + a13*b09)*invDet,
(-a01*b11 + a02*b10 - a03*b09)*invDet,
(a31*b05 - a32*b04 + a33*b03)*invDet,
(-a21*b05 + a22*b04 - a23*b03)*invDet,
(-a10*b11 + a12*b08 - a13*b07)*invDet,
(a00*b11 - a02*b08 + a03*b07)*invDet,
(-a30*b05 + a32*b02 - a33*b01)*invDet,
(a20*b05 - a22*b02 + a23*b01)*invDet,
(a10*b10 - a11*b08 + a13*b06)*invDet,
(-a00*b10 + a01*b08 - a03*b06)*invDet,
(a30*b04 - a31*b02 + a33*b00)*invDet,
(-a20*b04 + a21*b02 - a23*b00)*invDet,
(-a10*b09 + a11*b07 - a12*b06)*invDet,
(a00*b09 - a01*b07 + a02*b06)*invDet,
(-a30*b03 + a31*b01 - a32*b00)*invDet,
(a20*b03 - a21*b01 + a22*b00)*invDet };
// Create quaternion from source point
Quaternion quat = { source.x, source.y, source.z, 1.0f };
// Multiply quat point by unprojecte matrix
Quaternion qtransformed = { // QuaternionTransform(quat, matViewProjInv)
matViewProjInv.m0*quat.x + matViewProjInv.m4*quat.y + matViewProjInv.m8*quat.z + matViewProjInv.m12*quat.w,
matViewProjInv.m1*quat.x + matViewProjInv.m5*quat.y + matViewProjInv.m9*quat.z + matViewProjInv.m13*quat.w,
matViewProjInv.m2*quat.x + matViewProjInv.m6*quat.y + matViewProjInv.m10*quat.z + matViewProjInv.m14*quat.w,
matViewProjInv.m3*quat.x + matViewProjInv.m7*quat.y + matViewProjInv.m11*quat.z + matViewProjInv.m15*quat.w };
// Normalized world points in vectors
result.x = qtransformed.x/qtransformed.w;
result.y = qtransformed.y/qtransformed.w;
result.z = qtransformed.z/qtransformed.w;
return result;
}
// Get Vector3 as float array
fn vector::Vec3 vector3_to_vec3(Vector3 v) { // counterpart to Vector3ToFloatV
vector::Vec3 buffer;
buffer.x = v.x;
buffer.y = v.y;
buffer.z = v.z;
return buffer;
}
fn vector::Vec3f vector3_to_vec3f(Vector3 v) { // counterpart to Vector3ToFloatV
vector::Vec3f buffer;
buffer.x = v.x;
buffer.y = v.y;
buffer.z = v.z;
return buffer;
}
// Invert the given vector
fn Vector3 vector3_invert(Vector3 v) {
Vector3 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z };
return result;
}
// Clamp the components of the vector between
// min and max values specified by the given vectors
fn Vector3 vector3_clamp(Vector3 v, Vector3 min, Vector3 max) {
Vector3 result;
result.x = math::min(max.x, math::max(min.x, v.x));
result.y = math::min(max.y, math::max(min.y, v.y));
result.z = math::min(max.z, math::max(min.z, v.z));
return result;
}
// Clamp the magnitude of the vector between two values
fn Vector3 vector3_clamp_value(Vector3 v, float min, float max) {
Vector3 result = v;
float length = (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
if (length > 0.0f)
{
length = math::sqrt(length);
if (length < min)
{
float scale = min/length;
result.x = v.x*scale;
result.y = v.y*scale;
result.z = v.z*scale;
}
else if (length > max)
{
float scale = max/length;
result.x = v.x*scale;
result.y = v.y*scale;
result.z = v.z*scale;
}
}
return result;
}
// Check whether two given vectors are almost equal
fn bool vector3_equals(Vector3 p, Vector3 q) {
bool result = ((math::abs(p.x - q.x)) <= (EPSILON*math::max(1.0f, math::max(math::abs(p.x), math::abs(q.x))))) &&
((math::abs(p.y - q.y)) <= (EPSILON*math::max(1.0f, math::max(math::abs(p.y), math::abs(q.y))))) &&
((math::abs(p.z - q.z)) <= (EPSILON*math::max(1.0f, math::max(math::abs(p.z), math::abs(q.z)))));
return result;
}
// Compute the direction of a refracted ray
// v: normalized direction of the incoming ray
// n: normalized normal vector of the interface of two optical media
// r: ratio of the refractive index of the medium from where the ray comes
// to the refractive index of the medium on the other side of the surface
fn Vector3 vector3_refract(Vector3 v, Vector3 n, float r) {
Vector3 result;
float dot = v.x*n.x + v.y*n.y + v.z*n.z;
float d = 1.0f - r*r*(1.0f - dot*dot);
if (d >= 0.0f)
{
d = math::sqrt(d);
v.x = r*v.x - (r*dot + d)*n.x;
v.y = r*v.y - (r*dot + d)*n.y;
v.z = r*v.z - (r*dot + d)*n.z;
result = v;
}
return result;
}
//----------------------------------------------------------------------------------
// Module Functions Definition - Matrix math
//----------------------------------------------------------------------------------
// Compute matrix determinant
fn float matrix_determinant(Matrix mat) {
float result = 0.0f;
// Cache the matrix values (speed optimization)
float a00 = mat.m0;
float a01 = mat.m1;
float a02 = mat.m2;
float a03 = mat.m3;
float a10 = mat.m4;
float a11 = mat.m5;
float a12 = mat.m6;
float a13 = mat.m7;
float a20 = mat.m8;
float a21 = mat.m9;
float a22 = mat.m10;
float a23 = mat.m11;
float a30 = mat.m12;
float a31 = mat.m13;
float a32 = mat.m14;
float a33 = mat.m15;
result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
return result;
}
// Get the trace of the matrix (sum of the values along the diagonal)
fn float matrix_trace(Matrix mat) {
float result = (mat.m0 + mat.m5 + mat.m10 + mat.m15);
return result;
}
// Transposes provided matrix
fn Matrix matrix_transpose(Matrix mat) {
Matrix result;
result.m0 = mat.m0;
result.m1 = mat.m4;
result.m2 = mat.m8;
result.m3 = mat.m12;
result.m4 = mat.m1;
result.m5 = mat.m5;
result.m6 = mat.m9;
result.m7 = mat.m13;
result.m8 = mat.m2;
result.m9 = mat.m6;
result.m10 = mat.m10;
result.m11 = mat.m14;
result.m12 = mat.m3;
result.m13 = mat.m7;
result.m14 = mat.m11;
result.m15 = mat.m15;
return result;
}
// Invert provided matrix
fn Matrix matrix_invert(Matrix mat) {
Matrix result;
// Cache the matrix values (speed optimization)
float a00 = mat.m0;
float a01 = mat.m1;
float a02 = mat.m2;