-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathraymath.bi
1534 lines (1401 loc) · 63.9 KB
/
raymath.bi
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
#pragma once
#include once "crt/math.bi"
extern "C"
#define RAYMATH_H
const PI = 3.14159265358979323846
const EPSILON = 0.000001f
const DEG2RAD = PI / 180.0f
const RAD2DEG = 180.0f / PI
#define MatrixToFloat(mat) MatrixToFloatV(mat).v
#define Vector3ToFloat(vec) Vector3ToFloatV(vec).v
type Vector2
x as single
y as single
declare constructor()
declare constructor(x as single, y as single)
end type
constructor Vector2(x as single, y as single)
this.x = x
this.y = y
end constructor
constructor Vector2()
end constructor
#define RL_VECTOR2_TYPE
type Vector3
x as single
y as single
z as single
declare constructor()
declare constructor(x as single, y as single, z as single)
end type
constructor Vector3()
end constructor
constructor Vector3(x as single, y as single, z as single)
this.x = x
this.y = y
this.z = z
end constructor
#define RL_VECTOR3_TYPE
type Vector4
x as single
y as single
z as single
w as single
declare constructor()
declare constructor(x as single, y as single, z as single, w as single)
end type
constructor Vector4()
end constructor
constructor Vector4(x as single, y as single, z as single, w as single)
this.x = x
this.y = y
this.z = z
this.w = w
end constructor
#define RL_VECTOR4_TYPE
type Quaternion as Vector4
#define RL_QUATERNION_TYPE
type Matrix
m0 as single
m4 as single
m8 as single
m12 as single
m1 as single
m5 as single
m9 as single
m13 as single
m2 as single
m6 as single
m10 as single
m14 as single
m3 as single
m7 as single
m11 as single
m15 as single
end type
#define RL_MATRIX_TYPE
type float3
v(0 to 2) as single
end type
type float16
v(0 to 15) as single
end type
#if (not defined(RAYMATH_HEADER_ONLY)) and defined(RAYLIB_H)
declare function Clamp(byval value as single, byval min as single, byval max as single) as single
declare function Lerp(byval start as single, byval end_ as single, byval amount as single) as single
declare function Normalize(byval value as single, byval start as single, byval end_ as single) as single
declare function Remap(byval value as single, byval inputStart as single, byval inputEnd as single, byval outputStart as single, byval outputEnd as single) as single
declare function Wrap(byval value as single, byval min as single, byval max as single) as single
declare function FloatEquals(byval x as single, byval y as single) as long
declare function Vector2Zero() as Vector2
declare function Vector2One() as Vector2
declare function Vector2Add(byval v1 as Vector2, byval v2 as Vector2) as Vector2
declare function Vector2AddValue(byval v as Vector2, byval add as single) as Vector2
declare function Vector2Subtract(byval v1 as Vector2, byval v2 as Vector2) as Vector2
declare function Vector2SubtractValue(byval v as Vector2, byval sub_ as single) as Vector2
declare function Vector2Length(byval v as Vector2) as single
declare function Vector2LengthSqr(byval v as Vector2) as single
declare function Vector2DotProduct(byval v1 as Vector2, byval v2 as Vector2) as single
declare function Vector2Distance(byval v1 as Vector2, byval v2 as Vector2) as single
declare function Vector2DistanceSqr(byval v1 as Vector2, byval v2 as Vector2) as single
declare function Vector2Angle(byval v1 as Vector2, byval v2 as Vector2) as single
declare function Vector2Scale(byval v as Vector2, byval scale as single) as Vector2
declare function Vector2Multiply(byval v1 as Vector2, byval v2 as Vector2) as Vector2
declare function Vector2Negate(byval v as Vector2) as Vector2
declare function Vector2Divide(byval v1 as Vector2, byval v2 as Vector2) as Vector2
declare function Vector2Normalize(byval v as Vector2) as Vector2
declare function Vector2Transform(byval v as Vector2, byval mat as Matrix) as Vector2
declare function Vector2Lerp(byval v1 as Vector2, byval v2 as Vector2, byval amount as single) as Vector2
declare function Vector2Reflect(byval v as Vector2, byval normal as Vector2) as Vector2
declare function Vector2Rotate(byval v as Vector2, byval angle as single) as Vector2
declare function Vector2MoveTowards(byval v as Vector2, byval target as Vector2, byval maxDistance as single) as Vector2
declare function Vector2Invert(byval v as Vector2) as Vector2
declare function Vector2Clamp(byval v as Vector2, byval min as Vector2, byval max as Vector2) as Vector2
declare function Vector2ClampValue(byval v as Vector2, byval min as single, byval max as single) as Vector2
declare function Vector2Equals(byval p as Vector2, byval q as Vector2) as long
declare function Vector3Zero() as Vector3
declare function Vector3One() as Vector3
declare function Vector3Add(byval v1 as Vector3, byval v2 as Vector3) as Vector3
declare function Vector3AddValue(byval v as Vector3, byval add as single) as Vector3
declare function Vector3Subtract(byval v1 as Vector3, byval v2 as Vector3) as Vector3
declare function Vector3SubtractValue(byval v as Vector3, byval sub_ as single) as Vector3
declare function Vector3Scale(byval v as Vector3, byval scalar as single) as Vector3
declare function Vector3Multiply(byval v1 as Vector3, byval v2 as Vector3) as Vector3
declare function Vector3CrossProduct(byval v1 as Vector3, byval v2 as Vector3) as Vector3
declare function Vector3Perpendicular(byval v as Vector3) as Vector3
declare function Vector3Length(byval v as const Vector3) as single
declare function Vector3LengthSqr(byval v as const Vector3) as single
declare function Vector3DotProduct(byval v1 as Vector3, byval v2 as Vector3) as single
declare function Vector3Distance(byval v1 as Vector3, byval v2 as Vector3) as single
declare function Vector3DistanceSqr(byval v1 as Vector3, byval v2 as Vector3) as single
declare function Vector3Angle(byval v1 as Vector3, byval v2 as Vector3) as single
declare function Vector3Negate(byval v as Vector3) as Vector3
declare function Vector3Divide(byval v1 as Vector3, byval v2 as Vector3) as Vector3
declare function Vector3Normalize(byval v as Vector3) as Vector3
declare sub Vector3OrthoNormalize(byval v1 as Vector3 ptr, byval v2 as Vector3 ptr)
declare function Vector3Transform(byval v as Vector3, byval mat as Matrix) as Vector3
declare function Vector3RotateByQuaternion(byval v as Vector3, byval q as Quaternion) as Vector3
declare function Vector3RotateByAxisAngle(byval v as Vector3, byval axis as Vector3, byval angle as single) as Vector3
declare function Vector3Lerp(byval v1 as Vector3, byval v2 as Vector3, byval amount as single) as Vector3
declare function Vector3Reflect(byval v as Vector3, byval normal as Vector3) as Vector3
declare function Vector3Min(byval v1 as Vector3, byval v2 as Vector3) as Vector3
declare function Vector3Max(byval v1 as Vector3, byval v2 as Vector3) as Vector3
declare function Vector3Barycenter(byval p as Vector3, byval a as Vector3, byval b as Vector3, byval c as Vector3) as Vector3
declare function Vector3Unproject(byval source as Vector3, byval projection as Matrix, byval view_ as Matrix) as Vector3
declare function Vector3ToFloatV(byval v as Vector3) as float3
declare function Vector3Invert(byval v as Vector3) as Vector3
declare function Vector3Clamp(byval v as Vector3, byval min as Vector3, byval max as Vector3) as Vector3
declare function Vector3ClampValue(byval v as Vector3, byval min as single, byval max as single) as Vector3
declare function Vector3Equals(byval p as Vector3, byval q as Vector3) as long
declare function Vector3Refract(byval v as Vector3, byval n as Vector3, byval r as single) as Vector3
declare function MatrixDeterminant(byval mat as Matrix) as single
declare function MatrixTrace(byval mat as Matrix) as single
declare function MatrixTranspose(byval mat as Matrix) as Matrix
declare function MatrixInvert(byval mat as Matrix) as Matrix
declare function MatrixIdentity() as Matrix
declare function MatrixAdd(byval left_ as Matrix, byval right_ as Matrix) as Matrix
declare function MatrixSubtract(byval left_ as Matrix, byval right_ as Matrix) as Matrix
declare function MatrixMultiply(byval left_ as Matrix, byval right_ as Matrix) as Matrix
declare function MatrixTranslate(byval x as single, byval y as single, byval z as single) as Matrix
declare function MatrixRotate(byval axis as Vector3, byval angle as single) as Matrix
declare function MatrixRotateX(byval angle as single) as Matrix
declare function MatrixRotateY(byval angle as single) as Matrix
declare function MatrixRotateZ(byval angle as single) as Matrix
declare function MatrixRotateXYZ(byval angle as Vector3) as Matrix
declare function MatrixRotateZYX(byval angle as Vector3) as Matrix
declare function MatrixScale(byval x as single, byval y as single, byval z as single) as Matrix
declare function MatrixFrustum(byval left_ as double, byval right_ as double, byval bottom as double, byval top as double, byval near as double, byval far as double) as Matrix
declare function MatrixPerspective(byval fovy as double, byval aspect as double, byval near as double, byval far as double) as Matrix
declare function MatrixOrtho(byval left_ as double, byval right_ as double, byval bottom as double, byval top as double, byval near as double, byval far as double) as Matrix
declare function MatrixLookAt(byval eye as Vector3, byval target as Vector3, byval up as Vector3) as Matrix
declare function MatrixToFloatV(byval mat as Matrix) as float16
declare function QuaternionAdd(byval q1 as Quaternion, byval q2 as Quaternion) as Quaternion
declare function QuaternionAddValue(byval q as Quaternion, byval add as single) as Quaternion
declare function QuaternionSubtract(byval q1 as Quaternion, byval q2 as Quaternion) as Quaternion
declare function QuaternionSubtractValue(byval q as Quaternion, byval sub_ as single) as Quaternion
declare function QuaternionIdentity() as Quaternion
declare function QuaternionLength(byval q as Quaternion) as single
declare function QuaternionNormalize(byval q as Quaternion) as Quaternion
declare function QuaternionInvert(byval q as Quaternion) as Quaternion
declare function QuaternionMultiply(byval q1 as Quaternion, byval q2 as Quaternion) as Quaternion
declare function QuaternionScale(byval q as Quaternion, byval mul as single) as Quaternion
declare function QuaternionDivide(byval q1 as Quaternion, byval q2 as Quaternion) as Quaternion
declare function QuaternionLerp(byval q1 as Quaternion, byval q2 as Quaternion, byval amount as single) as Quaternion
declare function QuaternionNlerp(byval q1 as Quaternion, byval q2 as Quaternion, byval amount as single) as Quaternion
declare function QuaternionSlerp(byval q1 as Quaternion, byval q2 as Quaternion, byval amount as single) as Quaternion
declare function QuaternionFromVector3ToVector3(byval from as Vector3, byval to_ as Vector3) as Quaternion
declare function QuaternionFromMatrix(byval mat as Matrix) as Quaternion
declare function QuaternionToMatrix(byval q as Quaternion) as Matrix
declare function QuaternionFromAxisAngle(byval axis as Vector3, byval angle as single) as Quaternion
declare sub QuaternionToAxisAngle(byval q as Quaternion, byval outAxis as Vector3 ptr, byval outAngle as single ptr)
declare function QuaternionFromEuler(byval pitch as single, byval yaw as single, byval roll as single) as Quaternion
declare function QuaternionToEuler(byval q as Quaternion) as Vector3
declare function QuaternionTransform(byval q as Quaternion, byval mat as Matrix) as Quaternion
declare function QuaternionEquals(byval p as Quaternion, byval q as Quaternion) as long
#else
private function Clamp(byval value as single, byval min as single, byval max as single) as single
dim result as single = iif(value < min, min, value)
if result > max then
result = max
end if
return result
end function
private function Lerp(byval start as single, byval end_ as single, byval amount as single) as single
dim result as single = start + (amount * (end_ - start))
return result
end function
private function Normalize(byval value as single, byval start as single, byval end_ as single) as single
dim result as single = (value - start) / (end_ - start)
return result
end function
private function Remap(byval value as single, byval inputStart as single, byval inputEnd as single, byval outputStart as single, byval outputEnd as single) as single
dim result as single = (((value - inputStart) / (inputEnd - inputStart)) * (outputEnd - outputStart)) + outputStart
return result
end function
private function Wrap(byval value as single, byval min as single, byval max as single) as single
dim result as single = value - ((max - min) * floorf((value - min) / (max - min)))
return result
end function
private function FloatEquals(byval x as single, byval y as single) as long
dim result as long = -(fabsf(x - y) <= (0.000001f * fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y)))))
return result
end function
private function Vector2Zero() as Vector2
dim result as Vector2 = Vector2(0.0f, 0.0f)
return result
end function
private function Vector2One() as Vector2
dim result as Vector2 = Vector2(1.0f, 1.0f)
return result
end function
private function Vector2Add(byval v1 as Vector2, byval v2 as Vector2) as Vector2
dim result as Vector2 = Vector2(v1.x + v2.x, v1.y + v2.y)
return result
end function
private function Vector2AddValue(byval v as Vector2, byval add as single) as Vector2
dim result as Vector2 = Vector2(v.x + add, v.y + add)
return result
end function
private function Vector2Subtract(byval v1 as Vector2, byval v2 as Vector2) as Vector2
dim result as Vector2 = Vector2(v1.x - v2.x, v1.y - v2.y)
return result
end function
private function Vector2SubtractValue(byval v as Vector2, byval sub_ as single) as Vector2
dim result as Vector2 = Vector2(v.x - sub_, v.y - sub_)
return result
end function
private function Vector2Length(byval v as Vector2) as single
dim result as single = sqrtf((v.x * v.x) + (v.y * v.y))
return result
end function
private function Vector2LengthSqr(byval v as Vector2) as single
dim result as single = (v.x * v.x) + (v.y * v.y)
return result
end function
private function Vector2DotProduct(byval v1 as Vector2, byval v2 as Vector2) as single
dim result as single = (v1.x * v2.x) + (v1.y * v2.y)
return result
end function
private function Vector2Distance(byval v1 as Vector2, byval v2 as Vector2) as single
dim result as single = sqrtf(((v1.x - v2.x) * (v1.x - v2.x)) + ((v1.y - v2.y) * (v1.y - v2.y)))
return result
end function
private function Vector2DistanceSqr(byval v1 as Vector2, byval v2 as Vector2) as single
dim result as single = ((v1.x - v2.x) * (v1.x - v2.x)) + ((v1.y - v2.y) * (v1.y - v2.y))
return result
end function
private function Vector2Angle(byval v1 as Vector2, byval v2 as Vector2) as single
dim result as single = atan2f(v2.y, v2.x) - atan2f(v1.y, v1.x)
return result
end function
private function Vector2Scale(byval v as Vector2, byval scale as single) as Vector2
dim result as Vector2 = Vector2(v.x * scale, v.y * scale)
return result
end function
private function Vector2Multiply(byval v1 as Vector2, byval v2 as Vector2) as Vector2
dim result as Vector2 = Vector2(v1.x * v2.x, v1.y * v2.y)
return result
end function
private function Vector2Negate(byval v as Vector2) as Vector2
dim result as Vector2 = Vector2(-v.x, -v.y)
return result
end function
private function Vector2Divide(byval v1 as Vector2, byval v2 as Vector2) as Vector2
dim result as Vector2 = Vector2(v1.x / v2.x, v1.y / v2.y)
return result
end function
private function Vector2Normalize(byval v as Vector2) as Vector2
dim result as Vector2
dim length as single = sqrtf((v.x * v.x) + (v.y * v.y))
if length > 0 then
dim ilength as single = 1.0f / length
result.x = v.x * ilength
result.y = v.y * ilength
end if
return result
end function
private function Vector2Transform(byval v as Vector2, byval mat as Matrix) as Vector2
dim result as Vector2
dim x as single = v.x
dim y as single = v.y
dim z as single = 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
end function
private function Vector2Lerp(byval v1 as Vector2, byval v2 as Vector2, byval amount as single) as Vector2
dim result as Vector2
result.x = v1.x + (amount * (v2.x - v1.x))
result.y = v1.y + (amount * (v2.y - v1.y))
return result
end function
private function Vector2Reflect(byval v as Vector2, byval normal as Vector2) as Vector2
dim result as Vector2
dim dotProduct as single = (v.x * normal.x) + (v.y * normal.y)
result.x = v.x - ((2.0f * normal.x) * dotProduct)
result.y = v.y - ((2.0f * normal.y) * dotProduct)
return result
end function
private function Vector2Rotate(byval v as Vector2, byval angle as single) as Vector2
dim result as Vector2
dim cosres as single = cosf(angle)
dim sinres as single = sinf(angle)
result.x = (v.x * cosres) - (v.y * sinres)
result.y = (v.x * sinres) + (v.y * cosres)
return result
end function
private function Vector2MoveTowards(byval v as Vector2, byval target as Vector2, byval maxDistance as single) as Vector2
dim result as Vector2
dim dx as single = target.x - v.x
dim dy as single = target.y - v.y
dim value as single = (dx * dx) + (dy * dy)
if (value = 0) orelse ((maxDistance >= 0) andalso (value <= (maxDistance * maxDistance))) then
return target
end if
dim dist as single = sqrtf(value)
result.x = v.x + ((dx / dist) * maxDistance)
result.y = v.y + ((dy / dist) * maxDistance)
return result
end function
private function Vector2Invert(byval v as Vector2) as Vector2
dim result as Vector2 = Vector2(1.0f / v.x, 1.0f / v.y)
return result
end function
private function Vector2Clamp(byval v as Vector2, byval min as Vector2, byval max as Vector2) as Vector2
dim result as Vector2
result.x = fminf(max.x, fmaxf(min.x, v.x))
result.y = fminf(max.y, fmaxf(min.y, v.y))
return result
end function
private function Vector2ClampValue(byval v as Vector2, byval min as single, byval max as single) as Vector2
dim result as Vector2 = v
dim length as single = (v.x * v.x) + (v.y * v.y)
if length > 0.0f then
length = sqrtf(length)
if length < min then
dim scale as single = min / length
result.x = v.x * scale
result.y = v.y * scale
elseif length > max then
dim scale as single = max / length
result.x = v.x * scale
result.y = v.y * scale
end if
end if
return result
end function
private function Vector2Equals(byval p as Vector2, byval q as Vector2) as long
dim result as long = -((fabsf(p.x - q.x) <= (0.000001f * fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) andalso (fabsf(p.y - q.y) <= (0.000001f * fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))))
return result
end function
private function Vector3Zero() as Vector3
dim result as Vector3 = Vector3(0.0f, 0.0f, 0.0f)
return result
end function
private function Vector3One() as Vector3
dim result as Vector3 = Vector3(1.0f, 1.0f, 1.0f)
return result
end function
private function Vector3Add(byval v1 as Vector3, byval v2 as Vector3) as Vector3
dim result as Vector3 = Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
return result
end function
private function Vector3AddValue(byval v as Vector3, byval add as single) as Vector3
dim result as Vector3 = Vector3(v.x + add, v.y + add, v.z + add)
return result
end function
private function Vector3Subtract(byval v1 as Vector3, byval v2 as Vector3) as Vector3
dim result as Vector3 = Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
return result
end function
private function Vector3SubtractValue(byval v as Vector3, byval sub_ as single) as Vector3
dim result as Vector3 = Vector3(v.x - sub_, v.y - sub_, v.z - sub_)
return result
end function
private function Vector3Scale(byval v as Vector3, byval scalar as single) as Vector3
dim result as Vector3 = Vector3(v.x * scalar, v.y * scalar, v.z * scalar)
return result
end function
private function Vector3Multiply(byval v1 as Vector3, byval v2 as Vector3) as Vector3
dim result as Vector3 = Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z)
return result
end function
private function Vector3CrossProduct(byval v1 as Vector3, byval v2 as Vector3) as Vector3
dim result as Vector3 = Vector3((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
end function
private function Vector3Perpendicular(byval v as Vector3) as Vector3
dim result as Vector3
dim min as single = csng(fabs(v.x))
dim cardinalAxis as Vector3 = Vector3(1.0f, 0.0f, 0.0f)
if fabsf(v.y) < min then
min = csng(fabs(v.y))
dim tmp as Vector3 = Vector3(0.0f, 1.0f, 0.0f)
cardinalAxis = tmp
end if
if fabsf(v.z) < min then
dim tmp as Vector3 = Vector3(0.0f, 0.0f, 1.0f)
cardinalAxis = tmp
end if
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
end function
private function Vector3Length(byval v as const Vector3) as single
dim result as single = sqrtf(((v.x * v.x) + (v.y * v.y)) + (v.z * v.z))
return result
end function
private function Vector3LengthSqr(byval v as const Vector3) as single
dim result as single = ((v.x * v.x) + (v.y * v.y)) + (v.z * v.z)
return result
end function
private function Vector3DotProduct(byval v1 as Vector3, byval v2 as Vector3) as single
dim result as single = ((v1.x * v2.x) + (v1.y * v2.y)) + (v1.z * v2.z)
return result
end function
private function Vector3Distance(byval v1 as Vector3, byval v2 as Vector3) as single
dim result as single = 0.0f
dim dx as single = v2.x - v1.x
dim dy as single = v2.y - v1.y
dim dz as single = v2.z - v1.z
result = sqrtf(((dx * dx) + (dy * dy)) + (dz * dz))
return result
end function
private function Vector3DistanceSqr(byval v1 as Vector3, byval v2 as Vector3) as single
dim result as single = 0.0f
dim dx as single = v2.x - v1.x
dim dy as single = v2.y - v1.y
dim dz as single = v2.z - v1.z
result = ((dx * dx) + (dy * dy)) + (dz * dz)
return result
end function
private function Vector3Angle(byval v1 as Vector3, byval v2 as Vector3) as single
dim result as single = 0.0f
dim cross as Vector3 = Vector3((v1.y * v2.z) - (v1.z * v2.y), (v1.z * v2.x) - (v1.x * v2.z), (v1.x * v2.y) - (v1.y * v2.x))
dim len_ as single = sqrtf(((cross.x * cross.x) + (cross.y * cross.y)) + (cross.z * cross.z))
dim dot as single = ((v1.x * v2.x) + (v1.y * v2.y)) + (v1.z * v2.z)
result = atan2f(len_, dot)
return result
end function
private function Vector3Negate(byval v as Vector3) as Vector3
dim result as Vector3 = Vector3(-v.x, -v.y, -v.z)
return result
end function
private function Vector3Divide(byval v1 as Vector3, byval v2 as Vector3) as Vector3
dim result as Vector3 = Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z)
return result
end function
private function Vector3Normalize(byval v as Vector3) as Vector3
dim result as Vector3 = v
dim length as single = sqrtf(((v.x * v.x) + (v.y * v.y)) + (v.z * v.z))
if length = 0.0f then
length = 1.0f
end if
dim ilength as single = 1.0f / length
result.x *= ilength
result.y *= ilength
result.z *= ilength
return result
end function
private sub Vector3OrthoNormalize(byval v1 as Vector3 ptr, byval v2 as Vector3 ptr)
dim length as single = 0.0f
dim ilength as single = 0.0f
dim v as Vector3 = *v1
length = sqrtf(((v.x * v.x) + (v.y * v.y)) + (v.z * v.z))
if length = 0.0f then
length = 1.0f
end if
ilength = 1.0f / length
v1->x *= ilength
v1->y *= ilength
v1->z *= ilength
dim vn1 as Vector3 = Vector3((v1->y * v2->z) - (v1->z * v2->y), (v1->z * v2->x) - (v1->x * v2->z), (v1->x * v2->y) - (v1->y * v2->x))
v = vn1
length = sqrtf(((v.x * v.x) + (v.y * v.y)) + (v.z * v.z))
if length = 0.0f then
length = 1.0f
end if
ilength = 1.0f / length
vn1.x *= ilength
vn1.y *= ilength
vn1.z *= ilength
dim vn2 as Vector3 = Vector3((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
end sub
private function Vector3Transform(byval v as Vector3, byval mat as Matrix) as Vector3
dim result as Vector3
dim x as single = v.x
dim y as single = v.y
dim z as single = 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
end function
private function Vector3RotateByQuaternion(byval v as Vector3, byval q as Quaternion) as Vector3
dim result as Vector3
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
end function
private function Vector3RotateByAxisAngle(byval v as Vector3, byval axis as Vector3, byval angle as single) as Vector3
dim result as Vector3 = v
dim length as single = sqrtf(((axis.x * axis.x) + (axis.y * axis.y)) + (axis.z * axis.z))
if length = 0.0f then
length = 1.0f
end if
dim ilength as single = 1.0f / length
axis.x *= ilength
axis.y *= ilength
axis.z *= ilength
angle /= 2.0f
dim a as single = sinf(angle)
dim b as single = axis.x * a
dim c as single = axis.y * a
dim d as single = axis.z * a
a = cosf(angle)
dim w as Vector3 = Vector3(b, c, d)
dim wv as Vector3 = Vector3((w.y * v.z) - (w.z * v.y), (w.z * v.x) - (w.x * v.z), (w.x * v.y) - (w.y * v.x))
dim wwv as Vector3 = Vector3((w.y * wv.z) - (w.z * wv.y), (w.z * wv.x) - (w.x * wv.z), (w.x * wv.y) - (w.y * wv.x))
a *= 2
wv.x *= a
wv.y *= a
wv.z *= a
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
end function
private function Vector3Lerp(byval v1 as Vector3, byval v2 as Vector3, byval amount as single) as Vector3
dim result as Vector3
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
end function
private function Vector3Reflect(byval v as Vector3, byval normal as Vector3) as Vector3
dim result as Vector3
dim dotProduct as single = ((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
end function
private function Vector3Min(byval v1 as Vector3, byval v2 as Vector3) as Vector3
dim result as Vector3
result.x = fminf(v1.x, v2.x)
result.y = fminf(v1.y, v2.y)
result.z = fminf(v1.z, v2.z)
return result
end function
private function Vector3Max(byval v1 as Vector3, byval v2 as Vector3) as Vector3
dim result as Vector3
result.x = fmaxf(v1.x, v2.x)
result.y = fmaxf(v1.y, v2.y)
result.z = fmaxf(v1.z, v2.z)
return result
end function
private function Vector3Barycenter(byval p as Vector3, byval a as Vector3, byval b as Vector3, byval c as Vector3) as Vector3
dim result as Vector3
dim v0 as Vector3 = Vector3(b.x - a.x, b.y - a.y, b.z - a.z)
dim v1 as Vector3 = Vector3(c.x - a.x, c.y - a.y, c.z - a.z)
dim v2 as Vector3 = Vector3(p.x - a.x, p.y - a.y, p.z - a.z)
dim d00 as single = ((v0.x * v0.x) + (v0.y * v0.y)) + (v0.z * v0.z)
dim d01 as single = ((v0.x * v1.x) + (v0.y * v1.y)) + (v0.z * v1.z)
dim d11 as single = ((v1.x * v1.x) + (v1.y * v1.y)) + (v1.z * v1.z)
dim d20 as single = ((v2.x * v0.x) + (v2.y * v0.y)) + (v2.z * v0.z)
dim d21 as single = ((v2.x * v1.x) + (v2.y * v1.y)) + (v2.z * v1.z)
dim denom as single = (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
end function
private function Vector3Unproject(byval source as Vector3, byval projection as Matrix, byval view_ as Matrix) as Vector3
dim result as Vector3
dim matViewProj as Matrix = ((((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))
dim a00 as single = matViewProj.m0
dim a01 as single = matViewProj.m1
dim a02 as single = matViewProj.m2
dim a03 as single = matViewProj.m3
dim a10 as single = matViewProj.m4
dim a11 as single = matViewProj.m5
dim a12 as single = matViewProj.m6
dim a13 as single = matViewProj.m7
dim a20 as single = matViewProj.m8
dim a21 as single = matViewProj.m9
dim a22 as single = matViewProj.m10
dim a23 as single = matViewProj.m11
dim a30 as single = matViewProj.m12
dim a31 as single = matViewProj.m13
dim a32 as single = matViewProj.m14
dim a33 as single = matViewProj.m15
dim b00 as single = (a00 * a11) - (a01 * a10)
dim b01 as single = (a00 * a12) - (a02 * a10)
dim b02 as single = (a00 * a13) - (a03 * a10)
dim b03 as single = (a01 * a12) - (a02 * a11)
dim b04 as single = (a01 * a13) - (a03 * a11)
dim b05 as single = (a02 * a13) - (a03 * a12)
dim b06 as single = (a20 * a31) - (a21 * a30)
dim b07 as single = (a20 * a32) - (a22 * a30)
dim b08 as single = (a20 * a33) - (a23 * a30)
dim b09 as single = (a21 * a32) - (a22 * a31)
dim b10 as single = (a21 * a33) - (a23 * a31)
dim b11 as single = (a22 * a33) - (a23 * a32)
dim invDet as single = 1.0f / ((((((b00 * b11) - (b01 * b10)) + (b02 * b09)) + (b03 * b08)) - (b04 * b07)) + (b05 * b06))
dim matViewProjInv as Matrix = ((((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)
dim quat as Quaternion = Quaternion(source.x, source.y, source.z, 1.0f)
dim qtransformed as Quaternion = Quaternion((((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))
result.x = qtransformed.x / qtransformed.w
result.y = qtransformed.y / qtransformed.w
result.z = qtransformed.z / qtransformed.w
return result
end function
private function Vector3ToFloatV(byval v as Vector3) as float3
dim buffer as float3
buffer.v(0) = v.x
buffer.v(1) = v.y
buffer.v(2) = v.z
return buffer
end function
private function Vector3Invert(byval v as Vector3) as Vector3
dim result as Vector3 = Vector3(1.0f / v.x, 1.0f / v.y, 1.0f / v.z)
return result
end function
private function Vector3Clamp(byval v as Vector3, byval min as Vector3, byval max as Vector3) as Vector3
dim result as Vector3
result.x = fminf(max.x, fmaxf(min.x, v.x))
result.y = fminf(max.y, fmaxf(min.y, v.y))
result.z = fminf(max.z, fmaxf(min.z, v.z))
return result
end function
private function Vector3ClampValue(byval v as Vector3, byval min as single, byval max as single) as Vector3
dim result as Vector3 = v
dim length as single = ((v.x * v.x) + (v.y * v.y)) + (v.z * v.z)
if length > 0.0f then
length = sqrtf(length)
if length < min then
dim scale as single = min / length
result.x = v.x * scale
result.y = v.y * scale
result.z = v.z * scale
elseif length > max then
dim scale as single = max / length
result.x = v.x * scale
result.y = v.y * scale
result.z = v.z * scale
end if
end if
return result
end function
private function Vector3Equals(byval p as Vector3, byval q as Vector3) as long
dim result as long = -(((fabsf(p.x - q.x) <= (0.000001f * fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) andalso (fabsf(p.y - q.y) <= (0.000001f * fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y)))))) andalso (fabsf(p.z - q.z) <= (0.000001f * fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))))
return result
end function
private function Vector3Refract(byval v as Vector3, byval n as Vector3, byval r as single) as Vector3
dim result as Vector3
dim dot as single = ((v.x * n.x) + (v.y * n.y)) + (v.z * n.z)
dim d as single = 1.0f - ((r * r) * (1.0f - (dot * dot)))
if d >= 0.0f then
d = sqrtf(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
end if
return result
end function
private function MatrixDeterminant(byval mat as Matrix) as single
dim result as single = 0.0f
dim a00 as single = mat.m0
dim a01 as single = mat.m1
dim a02 as single = mat.m2
dim a03 as single = mat.m3
dim a10 as single = mat.m4
dim a11 as single = mat.m5
dim a12 as single = mat.m6
dim a13 as single = mat.m7
dim a20 as single = mat.m8
dim a21 as single = mat.m9
dim a22 as single = mat.m10
dim a23 as single = mat.m11
dim a30 as single = mat.m12
dim a31 as single = mat.m13
dim a32 as single = mat.m14
dim a33 as single = 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
end function
private function MatrixTrace(byval mat as Matrix) as single
dim result as single = ((mat.m0 + mat.m5) + mat.m10) + mat.m15
return result
end function
private function MatrixTranspose(byval mat as Matrix) as Matrix
dim result as Matrix = (0)
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
end function
private function MatrixInvert(byval mat as Matrix) as Matrix
dim result as Matrix = (0)
dim a00 as single = mat.m0
dim a01 as single = mat.m1
dim a02 as single = mat.m2
dim a03 as single = mat.m3
dim a10 as single = mat.m4
dim a11 as single = mat.m5
dim a12 as single = mat.m6
dim a13 as single = mat.m7
dim a20 as single = mat.m8
dim a21 as single = mat.m9
dim a22 as single = mat.m10
dim a23 as single = mat.m11
dim a30 as single = mat.m12
dim a31 as single = mat.m13
dim a32 as single = mat.m14
dim a33 as single = mat.m15
dim b00 as single = (a00 * a11) - (a01 * a10)
dim b01 as single = (a00 * a12) - (a02 * a10)
dim b02 as single = (a00 * a13) - (a03 * a10)
dim b03 as single = (a01 * a12) - (a02 * a11)
dim b04 as single = (a01 * a13) - (a03 * a11)
dim b05 as single = (a02 * a13) - (a03 * a12)
dim b06 as single = (a20 * a31) - (a21 * a30)
dim b07 as single = (a20 * a32) - (a22 * a30)
dim b08 as single = (a20 * a33) - (a23 * a30)
dim b09 as single = (a21 * a32) - (a22 * a31)
dim b10 as single = (a21 * a33) - (a23 * a31)
dim b11 as single = (a22 * a33) - (a23 * a32)
dim invDet as single = 1.0f / ((((((b00 * b11) - (b01 * b10)) + (b02 * b09)) + (b03 * b08)) - (b04 * b07)) + (b05 * b06))
result.m0 = (((a11 * b11) - (a12 * b10)) + (a13 * b09)) * invDet
result.m1 = ((((-a01) * b11) + (a02 * b10)) - (a03 * b09)) * invDet
result.m2 = (((a31 * b05) - (a32 * b04)) + (a33 * b03)) * invDet
result.m3 = ((((-a21) * b05) + (a22 * b04)) - (a23 * b03)) * invDet
result.m4 = ((((-a10) * b11) + (a12 * b08)) - (a13 * b07)) * invDet
result.m5 = (((a00 * b11) - (a02 * b08)) + (a03 * b07)) * invDet
result.m6 = ((((-a30) * b05) + (a32 * b02)) - (a33 * b01)) * invDet
result.m7 = (((a20 * b05) - (a22 * b02)) + (a23 * b01)) * invDet
result.m8 = (((a10 * b10) - (a11 * b08)) + (a13 * b06)) * invDet
result.m9 = ((((-a00) * b10) + (a01 * b08)) - (a03 * b06)) * invDet
result.m10 = (((a30 * b04) - (a31 * b02)) + (a33 * b00)) * invDet
result.m11 = ((((-a20) * b04) + (a21 * b02)) - (a23 * b00)) * invDet
result.m12 = ((((-a10) * b09) + (a11 * b07)) - (a12 * b06)) * invDet
result.m13 = (((a00 * b09) - (a01 * b07)) + (a02 * b06)) * invDet
result.m14 = ((((-a30) * b03) + (a31 * b01)) - (a32 * b00)) * invDet
result.m15 = (((a20 * b03) - (a21 * b01)) + (a22 * b00)) * invDet
return result
end function
private function MatrixIdentity() as Matrix
dim result as Matrix = (1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
return result
end function
private function MatrixAdd(byval left_ as Matrix, byval right_ as Matrix) as Matrix
dim result as Matrix = (0)
result.m0 = left_.m0 + right_.m0
result.m1 = left_.m1 + right_.m1
result.m2 = left_.m2 + right_.m2
result.m3 = left_.m3 + right_.m3
result.m4 = left_.m4 + right_.m4
result.m5 = left_.m5 + right_.m5
result.m6 = left_.m6 + right_.m6
result.m7 = left_.m7 + right_.m7
result.m8 = left_.m8 + right_.m8
result.m9 = left_.m9 + right_.m9
result.m10 = left_.m10 + right_.m10
result.m11 = left_.m11 + right_.m11
result.m12 = left_.m12 + right_.m12
result.m13 = left_.m13 + right_.m13
result.m14 = left_.m14 + right_.m14
result.m15 = left_.m15 + right_.m15
return result
end function
private function MatrixSubtract(byval left_ as Matrix, byval right_ as Matrix) as Matrix
dim result as Matrix = (0)
result.m0 = left_.m0 - right_.m0
result.m1 = left_.m1 - right_.m1
result.m2 = left_.m2 - right_.m2
result.m3 = left_.m3 - right_.m3
result.m4 = left_.m4 - right_.m4
result.m5 = left_.m5 - right_.m5
result.m6 = left_.m6 - right_.m6
result.m7 = left_.m7 - right_.m7
result.m8 = left_.m8 - right_.m8
result.m9 = left_.m9 - right_.m9
result.m10 = left_.m10 - right_.m10
result.m11 = left_.m11 - right_.m11
result.m12 = left_.m12 - right_.m12
result.m13 = left_.m13 - right_.m13
result.m14 = left_.m14 - right_.m14
result.m15 = left_.m15 - right_.m15
return result
end function
private function MatrixMultiply(byval left_ as Matrix, byval right_ as Matrix) as Matrix
dim result as Matrix = (0)
result.m0 = (((left_.m0 * right_.m0) + (left_.m1 * right_.m4)) + (left_.m2 * right_.m8)) + (left_.m3 * right_.m12)
result.m1 = (((left_.m0 * right_.m1) + (left_.m1 * right_.m5)) + (left_.m2 * right_.m9)) + (left_.m3 * right_.m13)
result.m2 = (((left_.m0 * right_.m2) + (left_.m1 * right_.m6)) + (left_.m2 * right_.m10)) + (left_.m3 * right_.m14)
result.m3 = (((left_.m0 * right_.m3) + (left_.m1 * right_.m7)) + (left_.m2 * right_.m11)) + (left_.m3 * right_.m15)
result.m4 = (((left_.m4 * right_.m0) + (left_.m5 * right_.m4)) + (left_.m6 * right_.m8)) + (left_.m7 * right_.m12)
result.m5 = (((left_.m4 * right_.m1) + (left_.m5 * right_.m5)) + (left_.m6 * right_.m9)) + (left_.m7 * right_.m13)
result.m6 = (((left_.m4 * right_.m2) + (left_.m5 * right_.m6)) + (left_.m6 * right_.m10)) + (left_.m7 * right_.m14)
result.m7 = (((left_.m4 * right_.m3) + (left_.m5 * right_.m7)) + (left_.m6 * right_.m11)) + (left_.m7 * right_.m15)
result.m8 = (((left_.m8 * right_.m0) + (left_.m9 * right_.m4)) + (left_.m10 * right_.m8)) + (left_.m11 * right_.m12)
result.m9 = (((left_.m8 * right_.m1) + (left_.m9 * right_.m5)) + (left_.m10 * right_.m9)) + (left_.m11 * right_.m13)
result.m10 = (((left_.m8 * right_.m2) + (left_.m9 * right_.m6)) + (left_.m10 * right_.m10)) + (left_.m11 * right_.m14)
result.m11 = (((left_.m8 * right_.m3) + (left_.m9 * right_.m7)) + (left_.m10 * right_.m11)) + (left_.m11 * right_.m15)
result.m12 = (((left_.m12 * right_.m0) + (left_.m13 * right_.m4)) + (left_.m14 * right_.m8)) + (left_.m15 * right_.m12)
result.m13 = (((left_.m12 * right_.m1) + (left_.m13 * right_.m5)) + (left_.m14 * right_.m9)) + (left_.m15 * right_.m13)
result.m14 = (((left_.m12 * right_.m2) + (left_.m13 * right_.m6)) + (left_.m14 * right_.m10)) + (left_.m15 * right_.m14)
result.m15 = (((left_.m12 * right_.m3) + (left_.m13 * right_.m7)) + (left_.m14 * right_.m11)) + (left_.m15 * right_.m15)
return result
end function
private function MatrixTranslate(byval x as single, byval y as single, byval z as single) as Matrix
dim result as Matrix = (1.0f, 0.0f, 0.0f, x, 0.0f, 1.0f, 0.0f, y, 0.0f, 0.0f, 1.0f, z, 0.0f, 0.0f, 0.0f, 1.0f)
return result
end function
private function MatrixRotate(byval axis as Vector3, byval angle as single) as Matrix
dim result as Matrix = (0)
dim x as single = axis.x
dim y as single = axis.y
dim z as single = axis.z
dim lengthSquared as single = ((x * x) + (y * y)) + (z * z)
if (lengthSquared <> 1.0f) andalso (lengthSquared <> 0.0f) then
dim ilength as single = 1.0f / sqrtf(lengthSquared)
x *= ilength
y *= ilength
z *= ilength
end if
dim sinres as single = sinf(angle)
dim cosres as single = cosf(angle)
dim t as single = 1.0f - cosres
result.m0 = ((x * x) * t) + cosres
result.m1 = ((y * x) * t) + (z * sinres)
result.m2 = ((z * x) * t) - (y * sinres)
result.m3 = 0.0f
result.m4 = ((x * y) * t) - (z * sinres)
result.m5 = ((y * y) * t) + cosres
result.m6 = ((z * y) * t) + (x * sinres)
result.m7 = 0.0f
result.m8 = ((x * z) * t) + (y * sinres)
result.m9 = ((y * z) * t) - (x * sinres)
result.m10 = ((z * z) * t) + cosres
result.m11 = 0.0f
result.m12 = 0.0f
result.m13 = 0.0f
result.m14 = 0.0f
result.m15 = 1.0f
return result
end function
private function MatrixRotateX(byval angle as single) as Matrix
dim result as Matrix = (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)
dim cosres as single = cosf(angle)
dim sinres as single = sinf(angle)
result.m5 = cosres
result.m6 = sinres
result.m9 = -sinres
result.m10 = cosres
return result
end function