-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjrc_math.h
1749 lines (1411 loc) · 41.3 KB
/
jrc_math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef INCLUDE_JRC_MATH_H
#define INCLUDE_JRC_MATH_H
#ifdef __cplusplus
extern "C" {
#endif
// PI IS EXACTLY THREE
#define MY_PI 3.14159265358979323846
#define MY_2PI 6.28318530717958647692
typedef int ivec_t;
typedef ivec_t ivec2_t[2];
typedef ivec_t ivec3_t[3];
typedef ivec_t ivec4_t[4];
typedef float vec_t;
typedef vec_t vec2_t[2];
typedef vec_t vec3_t[3];
typedef vec_t vec4_t[4];
typedef vec_t mat4_t[16];
typedef vec_t mat4x3_t[12];
/* Quaternion is split into vector [0-2] and scalar [3]. */
typedef vec4_t quat_t;
/* plane is the unit normal [0-2] and the distance from origin[3], point on plane can be derived by multiplying the normal by the distance. */
typedef vec4_t plane_t;
/* sphere is the origin [0-2] and the radius[3] */
typedef vec4_t sphere_t;
typedef struct line_s
{
vec3_t pos;
vec3_t dir;
} line_t;
typedef line_t ray_t;
typedef vec3_t triangle_t[3];
typedef vec3_t aabb_t[2];
#define EPSILON 0.00000001f
void jrcMath_SetupSse2();
float FastSqrt(float x);
float FastInvSqrt(float x);
int FloatToInt(float f);
float IntToFloat(int ival);
#ifdef NO_MACROS
void Vec2Copy(vec2_t r, vec2_t a);
void Vec3Copy(vec3_t r, vec3_t a);
void Vec4Copy(vec4_t r, vec4_t a);
void Vec2Set(vec2_t r, vec_t x, vec_t y);
void Vec3Set(vec3_t r, vec_t x, vec_t y, vec_t z);
void Vec4Set(vec4_t r, vec_t x, vec_t y, vec_t z, vec_t w);
vec_t DotProduct3(vec3_t a, vec3_t b);
void CrossProduct3(vec3_t r, vec3_t a, vec3_t b);
void Vec3Add(vec3_t r, vec3_t a, vec3_t b);
void Vec3MultiplyAdd(vec3_t r, vec3_t a, vec_t s, vec3_t b);
void Vec3Subtract(vec3_t r, vec3_t a, vec3_t b);
void Vec3Scale(vec3_t r, vec_t s, vec3_t a);
vec_t VecLerp(vec_t a, vec_t s, vec_t b);
#else
#define Vec2Copy(r, a) \
(((r)[0] = (a)[0]), \
((r)[1] = (a)[1])) \
#define Vec3Copy(r, a) \
(((r)[0] = (a)[0]), \
((r)[1] = (a)[1]), \
((r)[2] = (a)[2]))
#define Vec4Copy(r, a) \
(((r)[0] = (a)[0]), \
((r)[1] = (a)[1]), \
((r)[2] = (a)[2]), \
((r)[3] = (a)[3]))
#define Vec2Set(r, x, y) \
(((r)[0] = x), \
((r)[1] = y))
#define Vec3Set(r, x, y, z) \
(((r)[0] = x), \
((r)[1] = y), \
((r)[2] = z))
#define Vec4Set(r, x, y, z, w) \
(((r)[0] = x), \
((r)[1] = y), \
((r)[2] = z), \
((r)[3] = w))
#define Vec2Add(r, a, b) \
(((r)[0] = (a)[0] + (b)[0]), \
((r)[1] = (a)[1] + (b)[1]))
#define Vec2MultiplyAdd(r, a, s, b) \
(((r)[0] = (a)[0] + (s) * (b)[0]), \
((r)[1] = (a)[1] + (s) * (b)[1]))
#define Vec2Subtract(r, a, b) \
(((r)[0] = (a)[0] - (b)[0]), \
((r)[1] = (a)[1] - (b)[1]))
#define Vec2Scale(r, s, a) \
(((r)[0] = (s) * (a)[0]), \
((r)[1] = (s) * (a)[1]))
#define DotProduct2(a, b) \
((a)[0] * (b)[0] + (a)[1] * (b)[1])
#define Vec3Add(r, a, b) \
(((r)[0] = (a)[0] + (b)[0]), \
((r)[1] = (a)[1] + (b)[1]), \
((r)[2] = (a)[2] + (b)[2]))
#define Vec4Add(r, a, b) \
(((r)[0] = (a)[0] + (b)[0]), \
((r)[1] = (a)[1] + (b)[1]), \
((r)[2] = (a)[2] + (b)[2]), \
((r)[3] = (a)[3] + (b)[3]))
#define Vec3MultiplyAdd(r, a, s, b) \
(((r)[0] = (a)[0] + (s) * (b)[0]), \
((r)[1] = (a)[1] + (s) * (b)[1]), \
((r)[2] = (a)[2] + (s) * (b)[2]))
#define Vec4MultiplyAdd(r, a, s, b) \
(((r)[0] = (a)[0] + (s) * (b)[0]), \
((r)[1] = (a)[1] + (s) * (b)[1]), \
((r)[2] = (a)[2] + (s) * (b)[2]), \
((r)[3] = (a)[3] + (s) * (b)[3]))
#define Vec3Subtract(r, a, b) \
(((r)[0] = (a)[0] - (b)[0]), \
((r)[1] = (a)[1] - (b)[1]), \
((r)[2] = (a)[2] - (b)[2]))
#define Vec4Subtract(r, a, b) \
(((r)[0] = (a)[0] - (b)[0]), \
((r)[1] = (a)[1] - (b)[1]), \
((r)[2] = (a)[2] - (b)[2]), \
((r)[3] = (a)[3] - (b)[3]))
#define Vec3Scale(r, s, a) \
(((r)[0] = (s) * (a)[0]), \
((r)[1] = (s) * (a)[1]), \
((r)[2] = (s) * (a)[2]))
#define Vec4Scale(r, s, a) \
(((r)[0] = (s) * (a)[0]), \
((r)[1] = (s) * (a)[1]), \
((r)[2] = (s) * (a)[2]), \
((r)[3] = (s) * (a)[3]))
#define DotProduct3(a, b) \
((a)[0] * (b)[0] + (a)[1] * (b)[1] + (a)[2] * (b)[2])
#define DotProduct4(a, b) \
((a)[0] * (b)[0] + (a)[1] * (b)[1] + (a)[2] * (b)[2] + (a)[3] * (b)[3])
#define CrossProduct3(r, a, b) \
(((r)[0] = (a)[1] * (b)[2] - (a)[2] * (b)[1]), \
((r)[1] = (a)[2] * (b)[0] - (a)[0] * (b)[2]), \
((r)[2] = (a)[0] * (b)[1] - (a)[1] * (b)[0]))
#define VecLerp(a, s, b) \
((a) + ((b) - (a)) * (s))
#define Vec2Lerp(r, a, s, b) \
(((a)[0] + ((b)[0] - (a)[0]) * (s)), \
((a)[1] + ((b)[1] - (a)[1]) * (s)))
#define Vec3Lerp(r, a, s, b) \
(((r)[0] = (a)[0] + ((b)[0] - (a)[0]) * (s)), \
((r)[1] = (a)[1] + ((b)[1] - (a)[1]) * (s)), \
((r)[2] = (a)[2] + ((b)[2] - (a)[2]) * (s)))
#define Vec2Equal(a, b) \
(((a)[0] == (b)[0]) && \
((a)[1] == (b)[1]))
#define Vec3Equal(a, b) \
(((a)[0] == (b)[0]) && \
((a)[1] == (b)[1]) && \
((a)[2] == (b)[2]))
#endif
void Vec3Normalize(vec3_t a);
vec_t Vec3Length(vec3_t a);
void Vec4Normalize(vec4_t a);
void Mat4Identity(mat4_t r);
void Mat4Copy(mat4_t r, mat4_t a);
extern void (*Mat4Multiply)(mat4_t r, mat4_t a, mat4_t b);
extern void (*Mat4MultiplyVec3_0)(vec3_t r, mat4_t a, vec3_t b);
extern void (*Mat4MultiplyVec3_1)(vec3_t r, mat4_t a, vec3_t b);
extern void (*Mat4MultiplyVec3_1_ToVec4)(vec4_t r, mat4_t a, vec3_t b);
extern void (*Mat4MultiplyVec4)(vec4_t r, mat4_t a, vec4_t b);
void Mat4Model(mat4_t model, vec3_t right, vec3_t up, vec3_t back, vec3_t pos);
void Mat4View(mat4_t view, vec3_t facing, vec3_t up, vec3_t pos);
void Mat4Ortho(mat4_t ortho, vec_t left, vec_t right, vec_t top, vec_t bottom, vec_t nearPlane, vec_t farPlane);
void Mat4Perspective(mat4_t projection, vec_t width, vec_t height, vec_t nearPlane, vec_t farPlane);
void Mat4InvertSimple(mat4_t r, mat4_t a);
void Mat4InvertSimple2(mat4_t r, mat4_t a);
void Mat4x3_FromTranslateRotateScale(mat4x3_t m, vec3_t translate, quat_t rotation, vec3_t scale);
void Mat4x3_Copy(mat4x3_t r, mat4x3_t a);
void Mat4x3_Multiply(mat4x3_t r, mat4x3_t a, mat4x3_t b);
void Mat4x3_MultiplyVec3(vec3_t r, mat4x3_t a, vec3_t b);
void Mat4x3_MultiplyVec4(vec4_t r, mat4x3_t a, vec4_t b);
void Mat4x3_InvertSimple(mat4x3_t r, mat4x3_t a);
int Vec2PointInTriangle(vec2_t point, vec2_t t1, vec2_t t2, vec2_t t3);
char LinePlaneIntersection(line_t line, plane_t plane, vec_t *distance);
char RayPlaneIntersection(ray_t ray, plane_t plane, vec_t *distance);
char RaySphereIntersection(ray_t ray, sphere_t sphere, vec_t *distance);
char RayTriangleIntersection(ray_t ray, triangle_t triangle, plane_t triPlane, vec4_t triCache, vec_t *distance);
void TriangleCalcCaches(triangle_t triangle, plane_t plane, vec4_t triCache);
#define SWEEP_ALL_OUT 0
#define SWEEP_IN_TO_OUT 1
#define SWEEP_OUT_TO_IN 2
#define SWEEP_ALL_IN 3
typedef struct
{
aabb_t aabb;
plane_t *planes;
int numPlanes;
}
convexHull_t;
void Aabb_Clear(aabb_t aabb);
void Aabb_SetToPoint(aabb_t aabb, vec3_t point);
void Aabb_AddPoint(aabb_t aabb, vec3_t point);
void Aabb_Copy(aabb_t a, aabb_t b);
void Aabb_Add(aabb_t r, aabb_t a, aabb_t b);
int Aabb_Intersect(aabb_t a, aabb_t b);
int Aabb_SweepCollision(aabb_t bounds1, vec3_t move1, aabb_t bounds2, vec3_t move2, vec_t *impactTime);
void Aabb_ToPosAndSize(vec3_t pos, vec3_t size, aabb_t aabb);
void Aabb_FromPosAndSize(aabb_t aabb, vec3_t pos, vec3_t size);
void CalcTexVectors(vec3_t sdir, vec3_t tdir, vec3_t v1, vec3_t v2, vec3_t v3, vec2_t w1, vec2_t w2, vec2_t w3);
vec_t CalcTangentSpace(vec3_t tangent, vec3_t bitangent, vec3_t normal, vec3_t sdir, vec3_t tdir);
void QuatFromAxes(vec4_t quat, vec3_t x, vec3_t y, vec3_t z);
void AxesFromPitchYaw(vec3_t x, vec3_t y, vec3_t z, vec_t radYaw,
vec_t radPitch);
int PointInsidePlane(vec3_t point, plane_t plane);
int SweepLineWithPlane(vec3_t startPos, vec3_t move, plane_t plane, vec_t *hitFraction);
int SweepAabbWithPlane(aabb_t aabb, vec3_t move, plane_t plane, vec_t *hitFraction);
void ConvexHullFromAabb(convexHull_t *ch, aabb_t aabb);
int SweepAabbWithConvexHull(aabb_t aabb, vec3_t move, convexHull_t *ch, vec_t *hitFraction, plane_t hitPlane);
int Aabb_SweepCollision2(aabb_t bounds1, vec3_t move1, aabb_t bounds2, vec3_t move2, vec_t *hitDistance, plane_t hitPlane);
int CheckAndResolveCollision(aabb_t staticBounds, aabb_t movingBounds, vec3_t correction);
int Aabb_TraceRayToInsideFace(aabb_t aabb, vec3_t pos, vec3_t dir, int *outFace, float *outDist, vec3_t outImpact);
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN3(a,b,c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
#define MAX3(a,b,c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
#define CLAMP(t,a,b) ((t) > (b) ? (b) : (t) < (a) ? (a) : (t))
#define QuatConjugate(r, a) \
(((r)[0] = -(a)[0]), \
((r)[1] = -(a)[1]), \
((r)[2] = -(a)[2]), \
((r)[3] = (a)[3]))
#define QuatMultiply(r, a, b) \
(((r)[0] = (a)[3] * (b)[0] + (a)[0] * (b)[3] + (a)[1] * (b)[2] - (a)[2] * (b)[1]), \
((r)[1] = (a)[3] * (b)[1] + (a)[1] * (b)[3] + (a)[2] * (b)[0] - (a)[0] * (b)[2]), \
((r)[2] = (a)[3] * (b)[2] + (a)[2] * (b)[3] + (a)[0] * (b)[1] - (a)[1] * (b)[0]), \
((r)[3] = (a)[3] * (b)[3] - (a)[0] * (b)[0] - (a)[1] * (b)[1] - (a)[2] * (b)[2]))
#define Aabb_PointInside(a, v) \
(((v)[0] > a[0][0]) && ((v)[1] > a[0][1]) && ((v)[2] > a[0][2]) \
&& ((v)[0] < a[1][0]) && ((v)[1] < a[1][1]) && ((v)[2] < a[1][2]))
#define Aabb_PointInsideInclusive(a, v) \
(((v)[0] >= a[0][0]) && ((v)[1] >= a[0][1]) && ((v)[2] >= a[0][2]) \
&& ((v)[0] <= a[1][0]) && ((v)[1] <= a[1][1]) && ((v)[2] <= a[1][2]))
#ifdef __cplusplus
}
#endif
#endif
#ifdef JRC_MATH_IMPLEMENTATION
#include <assert.h>
#include <stdio.h>
#include <math.h>
#ifdef NO_MACROS
void Vec2Copy(vec2_t r, vec2_t a)
{
r[0] = a[0];
r[1] = a[1];
}
void Vec3Copy(vec3_t r, vec3_t a)
{
r[0] = a[0];
r[1] = a[1];
r[2] = a[2];
}
void Vec4Copy(vec4_t r, vec4_t a)
{
r[0] = a[0];
r[1] = a[1];
r[2] = a[2];
r[3] = a[3];
}
void Vec2Set(vec2_t r, vec_t x, vec_t y)
{
r[0] = x; r[1] = y;
}
void Vec3Set(vec3_t r, vec_t x, vec_t y, vec_t z)
{
r[0] = x; r[1] = y; r[2] = z;
}
void Vec4Set(vec4_t r, vec_t x, vec_t y, vec_t z, vec_t w)
{
r[0] = x; r[1] = y; r[2] = z; r[3] = w;
}
vec_t DotProduct3(vec3_t a, vec3_t b)
{
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
void CrossProduct3(vec3_t r, vec3_t a, vec3_t b)
{
r[0] = a[1] * b[2] - a[2] * b[1];
r[1] = a[2] * b[0] - a[0] * b[2];
r[2] = a[0] * b[1] - a[1] * b[0];
}
void Vec3Add(vec3_t r, vec3_t a, vec3_t b)
{
r[0] = a[0] + b[0];
r[1] = a[1] + b[1];
r[2] = a[2] + b[2];
}
void Vec3MultiplyAdd(vec3_t r, vec3_t a, vec_t s, vec3_t b)
{
r[0] = a[0] + s * b[0];
r[1] = a[1] + s * b[1];
r[2] = a[2] + s * b[2];
}
void Vec3Subtract(vec3_t r, vec3_t a, vec3_t b)
{
r[0] = a[0] - b[0];
r[1] = a[1] - b[1];
r[2] = a[2] - b[2];
}
void Vec3Scale(vec3_t r, vec_t s, vec3_t a)
{
r[0] = s * a[0];
r[1] = s * a[1];
r[2] = s * a[2];
}
vec_t VecLerp(vec_t a, vec_t s, vec_t b)
{
//return a * (1.0f - s) + b * s;
return a + (b - a) * s;
}
void Vec3Lerp(vec3_t r, vec3_t a, vec_t s, vec3_t b)
{
vec3_t d;
Vec3Subtract(d, b, a);
Vec3MultiplyAdd(r, a, s, d);
}
#endif
void Vec3Normalize(vec3_t a)
{
vec_t invLen = 1.0f / sqrt(DotProduct3(a,a));
Vec3Scale(a, invLen, a);
}
vec_t Vec3Length(vec3_t a)
{
return sqrt(DotProduct3(a,a));
}
void Vec4Normalize(vec4_t a)
{
vec_t invLen = 1.0f / sqrt(DotProduct4(a,a));
Vec4Scale(a, invLen, a);
}
void Mat4Identity(mat4_t r)
{
r[ 0] = 1.0f;
r[ 1] = 0.0f;
r[ 2] = 0.0f;
r[ 3] = 0.0f;
r[ 4] = 0.0f;
r[ 5] = 1.0f;
r[ 6] = 0.0f;
r[ 7] = 0.0f;
r[ 8] = 0.0f;
r[ 9] = 0.0f;
r[10] = 1.0f;
r[11] = 0.0f;
r[12] = 0.0f;
r[13] = 0.0f;
r[14] = 0.0f;
r[15] = 1.0f;
}
void Mat4Copy(mat4_t r, mat4_t a)
{
r[ 0] = a[ 0];
r[ 1] = a[ 1];
r[ 2] = a[ 2];
r[ 3] = a[ 3];
r[ 4] = a[ 4];
r[ 5] = a[ 5];
r[ 6] = a[ 6];
r[ 7] = a[ 7];
r[ 8] = a[ 8];
r[ 9] = a[ 9];
r[10] = a[10];
r[11] = a[11];
r[12] = a[12];
r[13] = a[13];
r[14] = a[14];
r[15] = a[15];
}
void Default_Mat4Multiply(mat4_t r, mat4_t a, mat4_t b)
{
r[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3];
r[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3];
r[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3];
r[ 3] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3];
r[ 4] = a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7];
r[ 5] = a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7];
r[ 6] = a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7];
r[ 7] = a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7];
r[ 8] = a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11];
r[ 9] = a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11];
r[10] = a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11];
r[11] = a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11];
r[12] = a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15];
r[13] = a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15];
r[14] = a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15];
r[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15];
}
void Default_Mat4MultiplyVec3_0(vec3_t r, mat4_t a, vec3_t b)
{
r[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2];
r[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2];
r[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2];
}
void Default_Mat4MultiplyVec3_1(vec3_t r, mat4_t a, vec3_t b)
{
r[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12];
r[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13];
r[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14];
}
void Default_Mat4MultiplyVec3_1_ToVec4(vec4_t r, mat4_t a, vec3_t b)
{
r[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12];
r[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13];
r[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14];
r[ 3] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15];
}
void Default_Mat4MultiplyVec4(vec4_t r, mat4_t a, vec4_t b)
{
r[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3];
r[ 1] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3];
r[ 2] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3];
r[ 3] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3];
}
#ifdef __SSE2__
#include <smmintrin.h>
void SSE2_Mat4Multiply(mat4_t r, mat4_t a, mat4_t b)
{
__m128 mr;
mr = _mm_mul_ps(_mm_load1_ps(&b[ 0]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[ 1]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[ 2]), _mm_load_ps(&a[ 8])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[ 3]), _mm_load_ps(&a[12])));
_mm_store_ps(&r[ 0], mr);
mr = _mm_mul_ps(_mm_load1_ps(&b[ 4]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[ 5]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[ 6]), _mm_load_ps(&a[ 8])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[ 7]), _mm_load_ps(&a[12])));
_mm_store_ps(&r[ 4], mr);
mr = _mm_mul_ps(_mm_load1_ps(&b[ 8]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[ 9]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[10]), _mm_load_ps(&a[ 8])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[11]), _mm_load_ps(&a[12])));
_mm_store_ps(&r[ 8], mr);
mr = _mm_mul_ps(_mm_load1_ps(&b[12]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[13]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[14]), _mm_load_ps(&a[ 8])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[15]), _mm_load_ps(&a[12])));
_mm_store_ps(&r[12], mr);
}
void SSE2_Mat4MultiplyVec3_0(vec3_t r, mat4_t a, vec3_t b)
{
__m128 mr;
mr = _mm_mul_ps(_mm_load1_ps(&b[0]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[1]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[2]), _mm_load_ps(&a[ 8])));
Vec3Copy(r, (float *)&mr);
}
void SSE2_Mat4MultiplyVec3_1(vec3_t r, mat4_t a, vec3_t b)
{
__m128 mr;
mr = _mm_mul_ps(_mm_load1_ps(&b[0]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[1]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[2]), _mm_load_ps(&a[ 8])));
mr = _mm_add_ps(mr, _mm_load_ps(&a[12]));
Vec3Copy(r, (float *)&mr);
}
void SSE2_Mat4MultiplyVec3_1_ToVec4(vec4_t r, mat4_t a, vec3_t b)
{
__m128 mr;
mr = _mm_mul_ps(_mm_load1_ps(&b[0]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[1]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[2]), _mm_load_ps(&a[ 8])));
mr = _mm_add_ps(mr, _mm_load_ps(&a[12]));
_mm_store_ps(r, mr);
}
void SSE2_Mat4MultiplyVec4(vec4_t r, mat4_t a, vec4_t b)
{
__m128 mr;
mr = _mm_mul_ps(_mm_load1_ps(&b[0]), _mm_load_ps(&a[ 0]));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[1]), _mm_load_ps(&a[ 4])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[2]), _mm_load_ps(&a[ 8])));
mr = _mm_add_ps(mr, _mm_mul_ps(_mm_load1_ps(&b[3]), _mm_load_ps(&a[12])));
_mm_store_ps(r, mr);
}
#endif
void (*Mat4Multiply)(mat4_t r, mat4_t a, mat4_t b) = Default_Mat4Multiply;
void (*Mat4MultiplyVec3_0)(vec3_t r, mat4_t a, vec3_t b) = Default_Mat4MultiplyVec3_0;
void (*Mat4MultiplyVec3_1)(vec3_t r, mat4_t a, vec3_t b) = Default_Mat4MultiplyVec3_1;
void (*Mat4MultiplyVec3_1_ToVec4)(vec4_t r, mat4_t a, vec3_t b) = Default_Mat4MultiplyVec3_1_ToVec4;
void (*Mat4MultiplyVec4)(vec4_t r, mat4_t a, vec4_t b) = Default_Mat4MultiplyVec4;
void jrcMath_SetupSse2()
{
#ifdef __SSE2__
if( __builtin_cpu_supports("sse2"))
{
Mat4Multiply = SSE2_Mat4Multiply;
Mat4MultiplyVec3_0 = SSE2_Mat4MultiplyVec3_0;
Mat4MultiplyVec3_1 = SSE2_Mat4MultiplyVec3_1;
Mat4MultiplyVec3_1_ToVec4 = SSE2_Mat4MultiplyVec3_1_ToVec4;
Mat4MultiplyVec4 = SSE2_Mat4MultiplyVec4;
}
#endif
}
void Mat4InvertSimple(mat4_t r, mat4_t a)
{
r[ 0] = a[ 0]; r[ 4] = a[ 1]; r[ 8] = a[ 2]; r[12] = -(r[ 0] * a[12] + r[ 4] * a[13] + r[ 8] * a[14]);
r[ 1] = a[ 4]; r[ 5] = a[ 5]; r[ 9] = a[ 6]; r[13] = -(r[ 0] * a[12] + r[ 4] * a[13] + r[ 8] * a[14]);
r[ 2] = a[ 8]; r[ 6] = a[ 9]; r[10] = a[10]; r[14] = -(r[ 0] * a[12] + r[ 4] * a[13] + r[ 8] * a[14]);
r[ 3] = 0.0f; r[ 7] = 0.0f; r[11] = 0.0f; r[15] = 1.0f;
}
void Mat4Model(mat4_t r, vec3_t right, vec3_t up, vec3_t back, vec3_t pos)
{
r[ 0] = right[0];
r[ 1] = right[1];
r[ 2] = right[2];
r[ 3] = 0.0f;
r[ 4] = up[0];
r[ 5] = up[1];
r[ 6] = up[2];
r[ 7] = 0.0f;
r[ 8] = back[0];
r[ 9] = back[1];
r[10] = back[2];
r[11] = 0.0f;
r[12] = pos[0];
r[13] = pos[1];
r[14] = pos[2];
r[15] = 1.0f;
}
void Mat4View(mat4_t view, vec3_t facing, vec3_t up, vec3_t pos)
{
vec3_t right;
CrossProduct3(right, facing, up);
Vec3Normalize(right);
CrossProduct3(up, right, facing);
Vec3Normalize(up);
view[ 0] = right[0];
view[ 1] = up[0];
view[ 2] = -facing[0];
view[ 3] = 0.0f;
view[ 4] = right[1];
view[ 5] = up[1];
view[ 6] = -facing[1];
view[ 7] = 0.0f;
view[ 8] = right[2];
view[ 9] = up[2];
view[10] = -facing[2];
view[11] = 0.0f;
view[12] = -(view[ 0] * pos[0] + view[ 4] * pos[1] + view[ 8] * pos[2]);
view[13] = -(view[ 1] * pos[0] + view[ 5] * pos[1] + view[ 9] * pos[2]);
view[14] = -(view[ 2] * pos[0] + view[ 6] * pos[1] + view[10] * pos[2]);
view[15] = 1.0f;
}
void Mat4Ortho(mat4_t r, vec_t left, vec_t right, vec_t top, vec_t bottom, vec_t nearPlane, vec_t farPlane)
{
r[ 0] = 2.0f / (right - left);
r[ 1] = 0.0f;
r[ 2] = 0.0f;
r[ 3] = 0.0f;
r[ 4] = 0.0f;
r[ 5] = 2.0f / (top - bottom);
r[ 6] = 0.0f;
r[ 7] = 0.0f;
r[ 8] = 0.0f;
r[ 9] = 0.0f;
r[10] = -2.0f / (farPlane - nearPlane);
r[11] = 0.0f;
r[12] = -(right + left) / (right - left);
r[13] = -(top + bottom) / (top - bottom);
r[14] = -(farPlane + nearPlane) / (farPlane - nearPlane);
r[15] = 1.0f;
}
void Mat4Perspective(mat4_t r, vec_t width, vec_t height, vec_t nearPlane, vec_t farPlane)
{
vec_t nmf = nearPlane - farPlane;
r[ 0] = nearPlane * 2.0f / width;
r[ 1] = 0.0f;
r[ 2] = 0.0f;
r[ 3] = 0.0f;
r[ 4] = 0.0f;
r[ 5] = nearPlane * 2.0f / height;
r[ 6] = 0.0f;
r[ 7] = 0.0f;
r[ 8] = 0.0f;
r[ 9] = 0.0f;
r[10] = (farPlane + nearPlane) / nmf;
r[11] = -1.0f;
r[12] = 0.0f;
r[13] = 0.0f;
r[14] = (2.0f * farPlane * nearPlane) / nmf;
r[15] = 0.0f;
}
// assumes matrix is a rotation/scale followed by a translation
void Mat4InvertSimple2(mat4_t r, mat4_t a)
{
float scale, *v;
r[ 0] = a[ 0]; r[ 4] = a[ 1]; r[ 8] = a[ 2];
r[ 1] = a[ 4]; r[ 5] = a[ 5]; r[ 9] = a[ 6];
r[ 2] = a[ 8]; r[ 6] = a[ 9]; r[10] = a[10];
r[ 3] = 0.0f; r[ 7] = 0.0f; r[11] = 0.0f;
v = a + 0; scale = 1.0f / DotProduct3(v, v); r[ 0] *= scale; r[ 4] *= scale; r[ 8] *= scale;
v = a + 4; scale = 1.0f / DotProduct3(v, v); r[ 1] *= scale; r[ 5] *= scale; r[ 9] *= scale;
v = a + 8; scale = 1.0f / DotProduct3(v, v); r[ 2] *= scale; r[ 6] *= scale; r[10] *= scale;
r[12] = -(r[ 0] * a[12] + r[ 4] * a[13] + r[ 8] * a[14]);
r[13] = -(r[ 1] * a[12] + r[ 5] * a[13] + r[ 9] * a[14]);
r[14] = -(r[ 2] * a[12] + r[ 6] * a[13] + r[10] * a[14]);
r[15] = 0.0f;
}
void Mat4Dump(mat4_t a)
{
printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",
a[ 0], a[ 4], a[ 8], a[12],
a[ 1], a[ 5], a[ 9], a[13],
a[ 2], a[ 6], a[10], a[14],
a[ 3], a[ 7], a[11], a[15]);
}
void Mat4x3_FromTranslateRotateScale(mat4x3_t m, vec3_t translate, quat_t rotation, vec3_t scale)
{
vec_t xx, xy, xz, xw, yy, yz, yw, zz, zw;
quat_t nq;
Vec4Copy(nq, rotation);
Vec4Normalize(nq);
xx = nq[0] * nq[0];
xy = nq[0] * nq[1];
xz = nq[0] * nq[2];
xw = nq[0] * nq[3];
yy = nq[1] * nq[1];
yz = nq[1] * nq[2];
yw = nq[1] * nq[3];
zz = nq[2] * nq[2];
zw = nq[2] * nq[3];
m[0] = 0.5f - yy - zz;
m[1] = xy + zw;
m[2] = xz - yw;
m[3] = xy - zw;
m[4] = 0.5f - xx - zz;
m[5] = yz + xw;
m[6] = xz + yw;
m[7] = yz - xw;
m[8] = 0.5f - xx - yy;
Vec3Scale(&m[0], 2.0f * scale[0], &m[0]);
Vec3Scale(&m[3], 2.0f * scale[1], &m[3]);
Vec3Scale(&m[6], 2.0f * scale[2], &m[6]);
Vec3Copy(&m[9], translate);
}
void Mat4x3_Copy(mat4x3_t r, mat4x3_t a)
{
r[ 0] = a[ 0];
r[ 1] = a[ 1];
r[ 2] = a[ 2];
r[ 3] = a[ 3];
r[ 4] = a[ 4];
r[ 5] = a[ 5];
r[ 6] = a[ 6];
r[ 7] = a[ 7];
r[ 8] = a[ 8];
r[ 9] = a[ 9];
r[10] = a[10];
r[11] = a[11];
}
void Mat4x3_Multiply(mat4x3_t r, mat4x3_t a, mat4x3_t b)
{
r[ 0] = a[0] * b[0] + a[3] * b[ 1] + a[6] * b[ 2];
r[ 1] = a[1] * b[0] + a[4] * b[ 1] + a[7] * b[ 2];
r[ 2] = a[2] * b[0] + a[5] * b[ 1] + a[8] * b[ 2];
r[ 3] = a[0] * b[3] + a[3] * b[ 4] + a[6] * b[ 5];
r[ 4] = a[1] * b[3] + a[4] * b[ 4] + a[7] * b[ 5];
r[ 5] = a[2] * b[3] + a[5] * b[ 4] + a[8] * b[ 5];
r[ 6] = a[0] * b[6] + a[3] * b[ 7] + a[6] * b[ 8];
r[ 7] = a[1] * b[6] + a[4] * b[ 7] + a[7] * b[ 8];
r[ 8] = a[2] * b[6] + a[5] * b[ 7] + a[8] * b[ 8];
r[ 9] = a[0] * b[9] + a[3] * b[10] + a[6] * b[11] + a[ 9];
r[10] = a[1] * b[9] + a[4] * b[10] + a[7] * b[11] + a[10];
r[11] = a[2] * b[9] + a[5] * b[10] + a[8] * b[11] + a[11];
}
void Mat4x3_MultiplyVec3(vec3_t r, mat4x3_t a, vec3_t b)
{
r[ 0] = a[0] * b[0] + a[3] * b[ 1] + a[6] * b[ 2];
r[ 1] = a[1] * b[0] + a[4] * b[ 1] + a[7] * b[ 2];
r[ 2] = a[2] * b[0] + a[5] * b[ 1] + a[8] * b[ 2];
}
void Mat4x3_MultiplyVec4(vec4_t r, mat4x3_t a, vec3_t b)
{
r[ 0] = a[0] * b[0] + a[3] * b[ 1] + a[6] * b[ 2] + a[ 9];
r[ 1] = a[1] * b[0] + a[4] * b[ 1] + a[7] * b[ 2] + a[10];
r[ 2] = a[2] * b[0] + a[5] * b[ 1] + a[8] * b[ 2] + a[11];
}
void Mat4x3_InvertSimple(mat4x3_t r, mat4x3_t a)
{
vec_t s;
s = 1.0f / DotProduct3( a, a); r[0] = a[0] * s; r[3] = a[1] * s; r[6] = a[2] * s;
s = 1.0f / DotProduct3(&a[3], &a[3]); r[1] = a[3] * s; r[4] = a[4] * s; r[7] = a[5] * s;
s = 1.0f / DotProduct3(&a[6], &a[6]); r[2] = a[6] * s; r[5] = a[7] * s; r[8] = a[8] * s;
r[ 9] = -(r[0] * a[ 9] + r[3] * a[10] + r[6] * a[11]);
r[10] = -(r[1] * a[ 9] + r[4] * a[10] + r[7] * a[11]);
r[11] = -(r[2] * a[ 9] + r[5] * a[10] + r[8] * a[11]);
}
// derived from http://en.wikipedia.org/wiki/Barycentric_coordinate_system
int Vec2PointInTriangle(vec2_t point, vec2_t t1, vec2_t t2, vec2_t t3)
{
vec_t a, b, c, d;
a = (t2[1] - t3[1]) * (point[0] - t3[0]) + (t3[0] - t2[0]) * (point[1] - t3[1]);
b = (t3[1] - t1[1]) * (point[0] - t3[0]) + (t1[0] - t3[0]) * (point[1] - t3[1]);
d = (t2[1] - t3[1]) * (t1[0] - t3[0]) + (t3[0] - t2[0]) * (t1[1] - t3[1]);
#if 0
if (d == 0.0f)
return 0;
#endif
if (d < 0.0f)
{
a = -a;
b = -b;
d = -d;
}
c = a + b;
if ((a < 0.0f) || (b < 0.0f) || (c < 0.0f) || (a > d) || (b > d) || (c > d))
return 0;
return 1;
}
// Line-plane intersection
// based on http://en.wikipedia.org/wiki/Line-plane_intersection
char LinePlaneIntersection(line_t line, plane_t plane, vec_t *distance)
{
vec_t ldotn;
vec3_t p0, l2p;
ldotn = DotProduct3(line.dir, plane);
#if 0 // no backface cull
if (fabs(ldotn) < EPSILON)
{
return 0;
}
#else // backface cull
if (ldotn > 0.0f)
{
return 0;
}
#endif
Vec3Scale(p0, plane[3], plane);
Vec3Subtract(l2p, p0, line.pos);
*distance = DotProduct3(plane, l2p) / ldotn;
return 1;
}
char RayPlaneIntersection(ray_t ray, plane_t plane, vec_t *distance)
{
char result = LinePlaneIntersection(ray, plane, distance);
if (!result || (*distance < 0.0f))
return 0;
return 1;
}
// Line-sphere intersection
// based on http://en.wikipedia.org/wiki/Line-sphere_intersection
//
// returns number of intersections, 0, 1, or 2, and distances in dist1 and dist2
// dist1 is always lower than dist2
int LineSphereIntersection(line_t line, sphere_t sphere, vec_t *dist1, vec_t *dist2)
{
vec3_t c;
vec_t cc, rr, ldotc, dd, d;
Vec3Subtract(c, sphere, line.pos);
rr = sphere[3] * sphere[3];
cc = DotProduct3(c, c);
ldotc = DotProduct3(line.dir, c);
dd = ldotc * ldotc - cc + rr;
if (dd < 0.0f)
{
// no solutions
return 0;
}
// should sqrt dd first, but this is good enough
if (dd < EPSILON)
{
// one solution
*dist1 = ldotc;
return 1;
}
// two solutions
d = sqrt(dd);
*dist1 = ldotc - d;
*dist2 = ldotc + d;
return 2;
}
char RaySphereIntersection(ray_t ray, sphere_t sphere, vec_t *distance)
{
vec_t dist1, dist2;
int numIntersects;
numIntersects = LineSphereIntersection(ray, sphere, &dist1, &dist2);
if (numIntersects == 0)
{
return 0;
}
else if (numIntersects == 1)
{
if (dist1 < 0.0f)
{
return 0;
}
*distance = dist1;
return 1;
}
// else if (numIntersects == 2)
if (dist2 < 0.0f)
{
return 0;
}