forked from GokuMK/TSRE5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLMatrix.cpp
1579 lines (1491 loc) · 41.9 KB
/
GLMatrix.cpp
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
/* This file is part of TSRE5.
*
* TSRE5 - train sim game engine and MSTS/OR Editors.
* Copyright (C) 2016 Piotr Gadecki <[email protected]>
*
* This file is c++ conversion of glMatrix:
* Javascript Matrix and Vector library for High Performance WebGL apps.
* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
*
* Licensed under GNU General Public License 3.0 or later.
*
* See LICENSE.md or https://www.gnu.org/licenses/gpl.html
*/
#include "GLMatrix.h"
#include <math.h>
#include <iostream>
/**
* Creates a new, empty vec2
* @returns {vec2} a new 2D vector
*/
float* Vec2::create() {
float * out = new float[2];
out[0] = 0;
out[1] = 0;
return out;
};
/**
* Creates a new vec2 initialized with values from an existing vector
* @param {vec2} a vector to clone
* @returns {vec2} a new 2D vector
*/
float* Vec2::clone(float* a) {
float * out = new float[2];
out[0] = a[0];
out[1] = a[1];
return out;
};
/**
* Creates a new vec2 initialized with the given values
* @param {Number} x X component
* @param {Number} y Y component
* @returns {vec2} a new 2D vector
*/
float* Vec2::fromValues(float x, float y) {
float* out = new float[2];
out[0] = x;
out[1] = y;
return out;
};
/**
* Copy the values from one vec2 to another
* @param {vec2} out the receiving vector
* @param {vec2} a the source vector
* @returns {vec2} out
*/
float* Vec2::copy(float* out, float* a) {
out[0] = a[0];
out[1] = a[1];
return out;
};
/**
* Set the components of a vec2 to the given values
* @param {vec2} out the receiving vector
* @param {Number} x X component
* @param {Number} y Y component
* @returns {vec2} out
*/
float* Vec2::set(float* out, float x, float y) {
out[0] = x;
out[1] = y;
return out;
};
/**
* Adds two vec2's
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec2} out
*/
float* Vec2::add(float *out, float *a, float *b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
return out;
}
/**
* Subtracts vector b from vector a
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec2} out
*/
float* Vec2::subtract(float *out, float *a, float *b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
return out;
}
/**
* Alias for {@link vec2.subtract}
* @function
*/
float* Vec2::sub(float *out, float *a, float *b) {
return subtract(out, a, b);
}
/**
* Multiplies two vec2's
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec2} out
*/
float* Vec2::multiply(float *out, float *a, float *b) {
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
return out;
}
/**
* Alias for {@link vec2.multiply}
* @function
*/
float* Vec2::mul(float *out, float *a, float *b) {
return multiply(out, a, b);
}
/**
* Divides two vec2's
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec2} out
*/
float* Vec2::divide(float *out, float *a, float *b) {
out[0] = a[0] / b[0];
out[1] = a[1] / b[1];
return out;
}
/**
* Alias for {@link vec2.divide}
* @function
*/
float* Vec2::div(float *out, float *a, float *b) {
return divide(out, a, b);
}
/**
* Returns the minimum of two vec2's
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec2} out
*/
float* Vec2::min(float *out, float *a, float *b) {
out[0] = fmin(a[0], b[0]);
out[1] = fmin(a[1], b[1]);
return out;
}
/**
* Returns the maximum of two vec2's
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec2} out
*/
float* Vec2::max(float *out, float *a, float *b) {
out[0] = fmax(a[0], b[0]);
out[1] = fmax(a[1], b[1]);
return out;
}
/**
* Scales a vec2 by a scalar number
* @param {vec2} out the receiving vector
* @param {vec2} a the vector to scale
* @param {Number} b amount to scale the vector by
* @returns {vec2} out
*/
float* Vec2::scale(float *out, float *a, float b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
return out;
}
/**
* Adds two vec2's after scaling the second operand by a scalar value
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @param {Number} scale the amount to scale b by before adding
* @returns {vec2} out
*/
float* Vec2::scaleAndAdd(float *out, float *a, float *b, float scale) {
out[0] = a[0] + (b[0] * scale);
out[1] = a[1] + (b[1] * scale);
return out;
}
/**
* Calculates the euclidian distance between two vec2's
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {Number} distance between a and b
*/
float Vec2::distance(float *a, float *b) {
float x = b[0] - a[0],
y = b[1] - a[1];
return sqrt(x*x + y*y);
}
/**
* Alias for {@link vec2.distance}
* @function
*/
float Vec2::dist(float *a, float *b) {
return distance(a, b);
}
/**
* Calculates the squared euclidian distance between two vec2's
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {Number} squared distance between a and b
*/
float Vec2::squaredDistance(float *a, float *b) {
float x = b[0] - a[0],
y = b[1] - a[1];
return x*x + y*y;
}
/**
* Alias for {@link vec2.squaredDistance}
* @function
*/
float Vec2::sqrDist(float *a, float *b) {
return squaredDistance(a, b);
}
/**
* Calculates the length of a vec2
* @param {vec2} a vector to calculate length of
* @returns {Number} length of a
*/
float Vec2::length(float *a) {
float x = a[0],
y = a[1];
return sqrt(x*x + y*y);
}
/**
* Alias for {@link vec2.length}
* @function
*/
float Vec2::len(float *a) {
return length(a);
}
/**
* Calculates the squared length of a vec2
* @param {vec2} a vector to calculate squared length of
* @returns {Number} squared length of a
*/
float Vec2::squaredLength(float *a) {
float x = a[0],
y = a[1];
return x*x + y*y;
}
/**
* Alias for {@link vec2.squaredLength}
* @function
*/
float Vec2::sqrLen(float *a) {
return squaredLength(a);
}
/**
* Negates the components of a vec2
* @param {vec2} out the receiving vector
* @param {vec2} a vector to negate
* @returns {vec2} out
*/
float* Vec2::negate(float *out, float *a) {
out[0] = -a[0];
out[1] = -a[1];
return out;
}
/**
* Normalize a vec2
* @param {vec2} out the receiving vector
* @param {vec2} a vector to normalize
* @returns {vec2} out
*/
float* Vec2::normalize(float *out, float *a) {
float x = a[0],
y = a[1];
float len = x*x + y*y;
if (len > 0) {
//TODO: evaluate use of glm_invsqrt here?
len = 1.0 / sqrt(len);
out[0] = a[0] * len;
out[1] = a[1] * len;
}
return out;
}
/**
* Calculates the dot product of two vec2's
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {Number} dot product of a and b
*/
float Vec2::dot(float *a, float *b) {
return a[0] * b[0] + a[1] * b[1];
}
/**
* Computes the cross product of two vec2's
* Note that the cross product must by definition produce a 3D vector
* @param {vec3} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @returns {vec3} out
*/
float* Vec2::cross(float *out, float *a, float *b) {
float z = a[0] * b[1] - a[1] * b[0];
out[0] = out[1] = 0;
out[2] = z;
return out;
}
/**
* Performs a linear interpolation between two vec2's
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @param {Number} t interpolation amount between the two inputs
* @returns {vec2} out
*/
float* Vec2::lerp(float *out, float *a, float *b, float t) {
float ax = a[0],
ay = a[1];
out[0] = ax + t * (b[0] - ax);
out[1] = ay + t * (b[1] - ay);
return out;
}
/**
* Transforms the vec2 with a mat2
* @param {vec2} out the receiving vector
* @param {vec2} a the vector to transform
* @param {mat2} m matrix to transform with
* @returns {vec2} out
*/
float* Vec2::transformMat2(float *out, float *a, float *m) {
float x = a[0],
y = a[1];
out[0] = m[0] * x + m[2] * y;
out[1] = m[1] * x + m[3] * y;
return out;
}
/**
* Transforms the vec2 with a mat2d
* @param {vec2} out the receiving vector
* @param {vec2} a the vector to transform
* @param {mat2d} m matrix to transform with
* @returns {vec2} out
*/
float* Vec2::transformMat2d(float *out, float *a, float *m) {
float x = a[0],
y = a[1];
out[0] = m[0] * x + m[2] * y + m[4];
out[1] = m[1] * x + m[3] * y + m[5];
return out;
}
/**
* Transforms the vec2 with a mat3
* 3rd vector component is implicitly '1'
* @param {vec2} out the receiving vector
* @param {vec2} a the vector to transform
* @param {mat3} m matrix to transform with
* @returns {vec2} out
*/
float* Vec2::transformMat3(float *out, float *a, float *m) {
float x = a[0],
y = a[1];
out[0] = m[0] * x + m[3] * y + m[6];
out[1] = m[1] * x + m[4] * y + m[7];
return out;
}
/**
* Transforms the vec2 with a mat4
* 3rd vector component is implicitly '0'
* 4th vector component is implicitly '1'
* @param {vec2} out the receiving vector
* @param {vec2} a the vector to transform
* @param {mat4} m matrix to transform with
* @returns {vec2} out
*/
float* Vec2::transformMat4(float *out, float *a, float *m) {
float x = a[0],
y = a[1];
out[0] = m[0] * x + m[4] * y + m[12];
out[1] = m[1] * x + m[5] * y + m[13];
return out;
}
/**
* Creates a new, empty vec3
* @returns {vec3} a new 3D vector
*/
float* Vec3::create() {
float* out = new float[3];
out[0] = 0;
out[1] = 0;
out[2] = 0;
return out;
}
/**
* Creates a new vec3 initialized with values from an existing vector
* @param {vec3} a vector to clone
* @returns {vec3} a new 3D vector
*/
float* Vec3::clone(float* a) {
float* out = new float[3];
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
return out;
}
/**
* Creates a new vec3 initialized with the given values
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @returns {vec3} a new 3D vector
*/
float* Vec3::fromValues(float x, float y, float z) {
float* out = new float[3];
out[0] = x;
out[1] = y;
out[2] = z;
return out;
}
/**
* Copy the values from one vec3 to another
* @param {vec3} out the receiving vector
* @param {vec3} a the source vector
* @returns {vec3} out
*/
float* Vec3::copy(float* out, const float* a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
return out;
}
/**
* Set the components of a vec3 to the given values
* @param {vec3} out the receiving vector
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @returns {vec3} out
*/
float* Vec3::set(float* out, float x, float y, float z) {
out[0] = x;
out[1] = y;
out[2] = z;
return out;
}
/*
* Adds two vec3's
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {vec3} out
*/
float* Vec3::add(float* out, float* a, float* b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
return out;
}
float* Vec3::add(float* out, float* a, float b) {
out[0] = a[0] + b;
out[1] = a[1] + b;
out[2] = a[2] + b;
return out;
}
/**
* Subtracts vector b from vector a
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {vec3} out
*/
float* Vec3::subtract(float* out, float* a, float* b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
return out;
}
/**
* Alias for {@link float* Vec3::subtract}
* @function
*/
float* Vec3::sub(float* out, float* a, float* b) {
return Vec3::subtract( out, a, b);
}
/**
* Multiplies two vec3's
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {vec3} out
*/
float* Vec3::multiply(float* out, float* a, float* b) {
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
out[2] = a[2] * b[2];
return out;
}
/**
* Alias for {@link float* Vec3::multiply}
* @function
*/
float* Vec3::mul(float* out, float* a, float* b) {
return Vec3::multiply( out, a, b);
}
/**
* Divides two vec3's
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {vec3} out
*/
float* Vec3::divide(float* out, float* a, float* b) {
out[0] = a[0] / b[0];
out[1] = a[1] / b[1];
out[2] = a[2] / b[2];
return out;
}
/**
* Alias for {@link float* Vec3::divide}
* @function
*/
float* Vec3::div(float* out, float* a, float* b) {
return Vec3::divide( out, a, b);
}
/**
* Returns the minimum of two vec3's
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {vec3} out
*/
float* Vec3::min(float* out, float* a, float* b) {
out[0] = fmin(a[0], b[0]);
out[1] = fmin(a[1], b[1]);
out[2] = fmin(a[2], b[2]);
return out;
}
/**
* Returns the maximum of two vec3's
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {vec3} out
*/
float* Vec3::max(float* out, float* a, float* b) {
out[0] = fmax(a[0], b[0]);
out[1] = fmax(a[1], b[1]);
out[2] = fmax(a[2], b[2]);
return out;
}
/**
* Scales a vec3 by a scalar number
* @param {vec3} out the receiving vector
* @param {vec3} a the vector to scale
* @param {Number} b amount to scale the vector by
* @returns {vec3} out
*/
float* Vec3::scale(float* out, float* a, float b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
return out;
}
/**
* Adds two vec3's after scaling the second operand by a scalar value
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @param {Number} scale the amount to scale b by before adding
* @returns {vec3} out
*/
float* Vec3::scaleAndAdd(float* out, float* a, float* b, float scale) {
out[0] = a[0] + (b[0] * scale);
out[1] = a[1] + (b[1] * scale);
out[2] = a[2] + (b[2] * scale);
return out;
}
/**
* Calculates the euclidian distance between two vec3's
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {Number} distance between a and b
*/
float Vec3::distance(float* a, float* b) {
float x = b[0] - a[0],
y = b[1] - a[1],
z = b[2] - a[2];
return sqrt(x*x + y*y + z*z);
}
/*
* Alias for {@link float* Vec3::distance}
* @function
*/
float Vec3::dist(float* a, float* b) {
return Vec3::distance(a, b);
}
/**
* Calculates the squared euclidian distance between two vec3's
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {Number} squared distance between a and b
*/
float Vec3::squaredDistance(float* a, float* b) {
float x = b[0] - a[0],
y = b[1] - a[1],
z = b[2] - a[2];
return x*x + y*y + z*z;
};
/**
* Alias for {@link float* Vec3::squaredDistance}
* @function
*/
float Vec3::sqrDist(float* a, float* b) {
return Vec3::squaredDistance(a, b);
}
/**
* Calculates the length of a vec3
* @param {vec3} a vector to calculate length of
* @returns {Number} length of a
*/
float Vec3::length (float* a) {
float x = a[0],
y = a[1],
z = a[2];
return sqrt(x*x + y*y + z*z);
}
/**
* Alias for {@link float* Vec3::length}
* @function
*/
float Vec3::len (float* a) {
return Vec3::length (a);
}
/**
* Calculates the squared length of a vec3
* @param {vec3} a vector to calculate squared length of
* @returns {Number} squared length of a
*/
float Vec3::squaredLength(float* a) {
float x = a[0],
y = a[1],
z = a[2];
return x*x + y*y + z*z;
}
/**
* Alias for {@link float* Vec3::squaredLength}
* @function
*/
float Vec3::sqrLen(float* a) {
return Vec3::squaredLength(a);
}
/**
* Negates the components of a vec3
* @param {vec3} out the receiving vector
* @param {vec3} a vector to negate
* @returns {vec3} out
*/
float* Vec3::negate(float* out, float* a) {
out[0] = -a[0];
out[1] = -a[1];
out[2] = -a[2];
return out;
}
/**
* Normalize a vec3
* @param {vec3} out the receiving vector
* @param {vec3} a vector to normalize
* @returns {vec3} out
*/
float* Vec3::normalize(float* out, float* a) {
float x = a[0],
y = a[1],
z = a[2];
float len = x*x + y*y + z*z;
if (len > 0) {
//TODO: evaluate use of glm_invsqrt here?
len = 1.0 / sqrt(len);
out[0] = a[0] * len;
out[1] = a[1] * len;
out[2] = a[2] * len;
}
return out;
}
/**
* Calculates the dot product of two vec3's
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {Number} dot product of a and b
*/
float Vec3::dot (float* a, float* b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
/**
* Computes the cross product of two vec3's
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @returns {vec3} out
*/
float* Vec3::cross(float* out, float* a, float* b) {
float ax = a[0], ay = a[1], az = a[2],
bx = b[0], by = b[1], bz = b[2];
out[0] = ay * bz - az * by;
out[1] = az * bx - ax * bz;
out[2] = ax * by - ay * bx;
return out;
}
/**
* Performs a linear interpolation between two vec3's
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @param {Number} t interpolation amount between the two inputs
* @returns {vec3} out
*/
float* Vec3::lerp (float* out, float* a, float* b, float t) {
float ax = a[0],
ay = a[1],
az = a[2];
out[0] = ax + t * (b[0] - ax);
out[1] = ay + t * (b[1] - ay);
out[2] = az + t * (b[2] - az);
return out;
}
/**
* Transforms the vec3 with a mat3.
* @param {vec3} out the receiving vector
* @param {vec3} a the vector to transform
* @param {mat4} m the 3x3 matrix to transform with
* @returns {vec3} out
*/
float* Vec3::transformMat3(float* out, float* a, float* m) {
float x = a[0], y = a[1], z = a[2];
out[0] = x * m[0] + y * m[3] + z * m[6];
out[1] = x * m[1] + y * m[4] + z * m[7];
out[2] = x * m[2] + y * m[5] + z * m[8];
return out;
}
float* Vec3::transformMat4(float* out, float* a, float* m) {
float x = a[0], y = a[1], z = a[2];
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12];
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13];
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14];
return out;
}
float* Vec3::transformQuat(float* out, float* a, float* q) {
float x = a[0], y = a[1], z = a[2],
qx = q[0], qy = q[1], qz = q[2], qw = q[3],
// calculate quat * vec
ix = qw * x + qy * z - qz * y,
iy = qw * y + qz * x - qx * z,
iz = qw * z + qx * y - qy * x,
iw = -qx * x - qy * y - qz * z;
// calculate result * inverse quat
out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return out;
};
/**
* Creates a new, empty vec4
* @returns {vec4} a new 2D vector
*/
float* Vec4::create() {
float * out = new float[4];
out[0] = 0;
out[1] = 0;
out[0] = 0;
out[1] = 0;
return out;
};
/**
* Performs a linear interpolation between two vec4's
*
* @param {vec4} out the receiving vector
* @param {vec4} a the first operand
* @param {vec4} b the second operand
* @param {Number} t interpolation amount between the two inputs
* @returns {vec4} out
*/
float* Vec4::lerp(float *out, float *a, float *b, float t) {
float ax = a[0],
ay = a[1],
az = a[2],
aw = a[3];
out[0] = ax + t * (b[0] - ax);
out[1] = ay + t * (b[1] - ay);
out[2] = az + t * (b[2] - az);
out[3] = aw + t * (b[3] - aw);
return out;
};
/**
* Normalize a vec4
*
* @param {vec4} out the receiving vector
* @param {vec4} a vector to normalize
* @returns {vec4} out
*/
float* Vec4::normalize(float* out, float* a) {
float x = a[0],
y = a[1],
z = a[2],
w = a[3];
float len = x*x + y*y + z*z + w*w;
if (len > 0) {
len = 1.0 / sqrt(len);
out[0] = a[0] * len;
out[1] = a[1] * len;
out[2] = a[2] * len;
out[3] = a[3] * len;
}
return out;
};
float* Vec4::copy(float* out, float* a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
return out;
};
/**
* Creates a new, empty quaternion
* @returns {vec4}
*/
float* Quat::create() {
float * out = new float[4];
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 1;
return out;
};
float* Quat::copy(float* out, float* a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
return out;
};
float* Quat::fill(float* out) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 1;
return out;
};
/**
* Creates a quaternion from the given 3x3 rotation matrix.
*
* NOTE: The resultant quaternion is not normalized, so you should be sure
* to renormalize the quaternion yourself where necessary.
*
* @param {quat} out the receiving quaternion
* @param {mat3} m rotation matrix
* @returns {quat} out
* @function
*/
float* Quat::fromMat3(float *out, float *m) {
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
float fTrace = m[0] + m[4] + m[8];
float fRoot;
float s_iNext[3]{1,2,0};
if ( fTrace > 0.0 ) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = sqrtf(fTrace + 1.0); // 2w
out[3] = 0.5 * fRoot;
fRoot = 0.5/fRoot; // 1/(4w)
out[0] = (m[7]-m[5])*fRoot;
out[1] = (m[2]-m[6])*fRoot;
out[2] = (m[3]-m[1])*fRoot;
} else {
// |w| <= 1/2
int i = 0;
if ( m[4] > m[0] )
i = 1;
if ( m[8] > m[i*3+i] )
i = 2;
int j = s_iNext[i];
int k = s_iNext[j];
fRoot = sqrtf(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0);
out[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
out[3] = (m[k*3+j] - m[j*3+k]) * fRoot;
out[j] = (m[j*3+i] + m[i*3+j]) * fRoot;
out[k] = (m[k*3+i] + m[i*3+k]) * fRoot;
}
return out;
};
float* Quat::fromRotationXYZ(float *out, float *a){
float sx = (float) sin(a[0]/2);
float sy = (float) sin(a[1]/2);
float sz = (float) sin(a[2]/2);
float cx = (float) cos(a[0]/2);
float cy = (float) cos(a[1]/2);
float cz = (float) cos(a[2]/2);
out[0] = cx*cy*cz + sx*sy*sz;
out[1] = sx*cy*cz - cx*sy*sz;
out[2] = cx*sy*cz + sx*cy*sz;
out[3] = cx*cy*sz - sx*sy*cz;
return out;
}
/**
* Calculates the inverse of a quat
*
* @param {quat} out the receiving quaternion
* @param {quat} a quat to calculate inverse of
* @returns {quat} out
*/
float* Quat::invert(float *out, float *a){
float a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
dot = a0*a0 + a1*a1 + a2*a2 + a3*a3,
invDot = dot ? 1.0/dot : 0;
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
out[0] = -a0*invDot;
out[1] = -a1*invDot;
out[2] = -a2*invDot;
out[3] = a3*invDot;
return out;
};
float* Quat::lerp(float* out, float* a, float* b, float t){
return Vec4::lerp(out,a,b,t);
}
/**
* Performs a spherical linear interpolation between two quat
*
* @param {quat} out the receiving quaternion
* @param {quat} a the first operand
* @param {quat} b the second operand
* @param {Number} t interpolation amount between the two inputs
* @returns {quat} out
*/
float* Quat::slerp(float* out, float* a, float* b, float t){
float ax = a[0], ay = a[1], az = a[2], aw = a[3],
bx = b[0], by = b[1], bz = b[2], bw = b[3];
float omega, cosom, sinom, scale0, scale1;
// calc cosine
cosom = ax * bx + ay * by + az * bz + aw * bw;
// adjust signs (if necessary)
if ( cosom < 0.0 ) {
cosom = -cosom;
bx = - bx;
by = - by;
bz = - bz;
bw = - bw;
}
// calculate coefficients
if ( (1.0 - cosom) > 0.000001 ) {
// standard case (slerp)
omega = acos(cosom);
sinom = sin(omega);
scale0 = sin((1.0 - t) * omega) / sinom;
scale1 = sin(t * omega) / sinom;
} else {
// "from" and "to" quaternions are very close
// ... so we can do a linear interpolation
scale0 = 1.0 - t;
scale1 = t;
}
// calculate final values
out[0] = scale0 * ax + scale1 * bx;
out[1] = scale0 * ay + scale1 * by;
out[2] = scale0 * az + scale1 * bz;
out[3] = scale0 * aw + scale1 * bw;
return out;
};
/**
* Sets a quat from the given angle and rotation axis,