4af60c8c84adc1022d424071ef4438130ab8ee88
1 /* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved
19 * 5.d Raycast & Spherecasts
29 #include "vg_platform.h"
33 #define VG_PIf 3.14159265358979323846264338327950288f
34 #define VG_TAUf 6.28318530717958647692528676655900576f
37 * -----------------------------------------------------------------------------
38 * Section 0. Misc Operations
39 * -----------------------------------------------------------------------------
42 /* get the f32 as the raw bits in a u32 without converting */
43 static u32
vg_ftu32( f32 a
)
45 u32
*ptr
= (u32
*)(&a
);
49 /* check if f32 is infinite */
50 static int vg_isinff( f32 a
)
52 return ((vg_ftu32(a
)) & 0x7FFFFFFFU
) == 0x7F800000U
;
55 /* check if f32 is not a number */
56 static int vg_isnanf( f32 a
)
58 return !vg_isinff(a
) && ((vg_ftu32(a
)) & 0x7F800000U
) == 0x7F800000U
;
61 /* check if f32 is a number and is not infinite */
62 static int vg_validf( f32 a
)
64 return ((vg_ftu32(a
)) & 0x7F800000U
) != 0x7F800000U
;
67 static int v3_valid( v3f a
){
68 for( u32 i
=0; i
<3; i
++ )
69 if( !vg_validf(a
[i
]) ) return 0;
74 * -----------------------------------------------------------------------------
75 * Section 1. Scalar Operations
76 * -----------------------------------------------------------------------------
79 static inline f32
vg_minf( f32 a
, f32 b
){ return a
< b
? a
: b
; }
80 static inline f32
vg_maxf( f32 a
, f32 b
){ return a
> b
? a
: b
; }
82 static inline int vg_min( int a
, int b
){ return a
< b
? a
: b
; }
83 static inline int vg_max( int a
, int b
){ return a
> b
? a
: b
; }
85 static inline f32
vg_clampf( f32 a
, f32 min
, f32 max
)
87 return vg_minf( max
, vg_maxf( a
, min
) );
90 static inline f32
vg_signf( f32 a
)
92 return a
< 0.0f
? -1.0f
: 1.0f
;
95 static inline f32
vg_fractf( f32 a
)
97 return a
- floorf( a
);
100 static inline f64
vg_fractf64( f64 a
){
101 return a
- floor( a
);
104 static f32
vg_cfrictf( f32 velocity
, f32 F
)
106 return -vg_signf(velocity
) * vg_minf( F
, fabsf(velocity
) );
109 static inline f32
vg_rad( f32 deg
)
111 return deg
* VG_PIf
/ 180.0f
;
114 /* angle to reach b from a */
115 static f32
vg_angle_diff( f32 a
, f32 b
){
116 f32 d
= fmod(b
,VG_TAUf
)-fmodf(a
,VG_TAUf
);
117 if( fabsf(d
) > VG_PIf
)
118 d
= -vg_signf(d
) * (VG_TAUf
- fabsf(d
));
124 * quantize float to bit count
126 static u32
vg_quantf( f32 a
, u32 bits
, f32 min
, f32 max
){
127 u32 mask
= (0x1 << bits
) - 1;
128 return vg_clampf((a
- min
) * ((f32
)mask
/(max
-min
)), 0.0f
, mask
);
132 * un-quantize discreet to float
134 static f32
vg_dequantf( u32 q
, u32 bits
, f32 min
, f32 max
){
135 u32 mask
= (0x1 << bits
) - 1;
136 return min
+ (f32
)q
* ((max
-min
) / (f32
)mask
);
139 /* https://iquilezles.org/articles/functions/
141 * Use k to control the stretching of the function. Its maximum, which is 1,
142 * happens at exactly x = 1/k.
144 static f32
vg_exp_impulse( f32 x
, f32 k
){
146 return h
*expf(1.0f
-h
);
150 * -----------------------------------------------------------------------------
151 * Section 2.a 2D Vectors
152 * -----------------------------------------------------------------------------
155 static inline void v2_copy( v2f a
, v2f d
)
157 d
[0] = a
[0]; d
[1] = a
[1];
160 static inline void v2_zero( v2f a
)
162 a
[0] = 0.f
; a
[1] = 0.f
;
165 static inline void v2_add( v2f a
, v2f b
, v2f d
)
167 d
[0] = a
[0]+b
[0]; d
[1] = a
[1]+b
[1];
170 static inline void v2_sub( v2f a
, v2f b
, v2f d
)
172 d
[0] = a
[0]-b
[0]; d
[1] = a
[1]-b
[1];
175 static inline void v2_minv( v2f a
, v2f b
, v2f dest
)
177 dest
[0] = vg_minf(a
[0], b
[0]);
178 dest
[1] = vg_minf(a
[1], b
[1]);
181 static inline void v2_maxv( v2f a
, v2f b
, v2f dest
)
183 dest
[0] = vg_maxf(a
[0], b
[0]);
184 dest
[1] = vg_maxf(a
[1], b
[1]);
187 static inline f32
v2_dot( v2f a
, v2f b
)
189 return a
[0] * b
[0] + a
[1] * b
[1];
192 static inline f32
v2_cross( v2f a
, v2f b
)
194 return a
[0]*b
[1] - a
[1]*b
[0];
197 static inline void v2_abs( v2f a
, v2f d
)
199 d
[0] = fabsf( a
[0] );
200 d
[1] = fabsf( a
[1] );
203 static inline void v2_muls( v2f a
, f32 s
, v2f d
)
205 d
[0] = a
[0]*s
; d
[1] = a
[1]*s
;
208 static inline void v2_divs( v2f a
, f32 s
, v2f d
)
210 d
[0] = a
[0]/s
; d
[1] = a
[1]/s
;
213 static inline void v2_mul( v2f a
, v2f b
, v2f d
)
219 static inline void v2_div( v2f a
, v2f b
, v2f d
)
221 d
[0] = a
[0]/b
[0]; d
[1] = a
[1]/b
[1];
224 static inline void v2_muladd( v2f a
, v2f b
, v2f s
, v2f d
)
226 d
[0] = a
[0]+b
[0]*s
[0];
227 d
[1] = a
[1]+b
[1]*s
[1];
230 static inline void v2_muladds( v2f a
, v2f b
, f32 s
, v2f d
)
236 static inline f32
v2_length2( v2f a
)
238 return a
[0]*a
[0] + a
[1]*a
[1];
241 static inline f32
v2_length( v2f a
)
243 return sqrtf( v2_length2( a
) );
246 static inline f32
v2_dist2( v2f a
, v2f b
)
249 v2_sub( a
, b
, delta
);
250 return v2_length2( delta
);
253 static inline f32
v2_dist( v2f a
, v2f b
)
255 return sqrtf( v2_dist2( a
, b
) );
258 static inline void v2_lerp( v2f a
, v2f b
, f32 t
, v2f d
)
260 d
[0] = a
[0] + t
*(b
[0]-a
[0]);
261 d
[1] = a
[1] + t
*(b
[1]-a
[1]);
264 static inline void v2_normalize( v2f a
)
266 v2_muls( a
, 1.0f
/ v2_length( a
), a
);
269 static void v2_normalize_clamp( v2f a
)
271 f32 l2
= v2_length2( a
);
273 v2_muls( a
, 1.0f
/sqrtf(l2
), a
);
276 static inline void v2_floor( v2f a
, v2f b
)
278 b
[0] = floorf( a
[0] );
279 b
[1] = floorf( a
[1] );
282 static inline void v2_fill( v2f a
, f32 v
)
288 static inline void v2_copysign( v2f a
, v2f b
)
290 a
[0] = copysignf( a
[0], b
[0] );
291 a
[1] = copysignf( a
[1], b
[1] );
295 * ---------------- */
297 static inline void v2i_copy( v2i a
, v2i b
)
299 b
[0] = a
[0]; b
[1] = a
[1];
302 static inline int v2i_eq( v2i a
, v2i b
)
304 return ((a
[0] == b
[0]) && (a
[1] == b
[1]));
307 static inline void v2i_add( v2i a
, v2i b
, v2i d
)
309 d
[0] = a
[0]+b
[0]; d
[1] = a
[1]+b
[1];
312 static inline void v2i_sub( v2i a
, v2i b
, v2i d
)
314 d
[0] = a
[0]-b
[0]; d
[1] = a
[1]-b
[1];
318 * -----------------------------------------------------------------------------
319 * Section 2.b 3D Vectors
320 * -----------------------------------------------------------------------------
323 static inline void v3_copy( v3f a
, v3f b
)
325 b
[0] = a
[0]; b
[1] = a
[1]; b
[2] = a
[2];
328 static inline void v3_zero( v3f a
)
330 a
[0] = 0.f
; a
[1] = 0.f
; a
[2] = 0.f
;
333 static inline void v3_add( v3f a
, v3f b
, v3f d
)
335 d
[0] = a
[0]+b
[0]; d
[1] = a
[1]+b
[1]; d
[2] = a
[2]+b
[2];
338 static inline void v3i_add( v3i a
, v3i b
, v3i d
)
340 d
[0] = a
[0]+b
[0]; d
[1] = a
[1]+b
[1]; d
[2] = a
[2]+b
[2];
343 static inline void v3_sub( v3f a
, v3f b
, v3f d
)
345 d
[0] = a
[0]-b
[0]; d
[1] = a
[1]-b
[1]; d
[2] = a
[2]-b
[2];
348 static inline void v3i_sub( v3i a
, v3i b
, v3i d
)
350 d
[0] = a
[0]-b
[0]; d
[1] = a
[1]-b
[1]; d
[2] = a
[2]-b
[2];
353 static inline void v3_mul( v3f a
, v3f b
, v3f d
)
355 d
[0] = a
[0]*b
[0]; d
[1] = a
[1]*b
[1]; d
[2] = a
[2]*b
[2];
358 static inline void v3_div( v3f a
, v3f b
, v3f d
)
360 d
[0] = b
[0]!=0.0f
? a
[0]/b
[0]: INFINITY
;
361 d
[1] = b
[1]!=0.0f
? a
[1]/b
[1]: INFINITY
;
362 d
[2] = b
[2]!=0.0f
? a
[2]/b
[2]: INFINITY
;
365 static inline void v3_muls( v3f a
, f32 s
, v3f d
)
367 d
[0] = a
[0]*s
; d
[1] = a
[1]*s
; d
[2] = a
[2]*s
;
370 static inline void v3_fill( v3f a
, f32 v
)
377 static inline void v3_divs( v3f a
, f32 s
, v3f d
)
380 v3_fill( d
, INFINITY
);
389 static inline void v3_muladds( v3f a
, v3f b
, f32 s
, v3f d
)
391 d
[0] = a
[0]+b
[0]*s
; d
[1] = a
[1]+b
[1]*s
; d
[2] = a
[2]+b
[2]*s
;
394 static inline void v3_muladd( v2f a
, v2f b
, v2f s
, v2f d
)
396 d
[0] = a
[0]+b
[0]*s
[0];
397 d
[1] = a
[1]+b
[1]*s
[1];
398 d
[2] = a
[2]+b
[2]*s
[2];
401 static inline f32
v3_dot( v3f a
, v3f b
)
403 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
406 static inline void v3_cross( v3f a
, v3f b
, v3f dest
)
409 d
[0] = a
[1]*b
[2] - a
[2]*b
[1];
410 d
[1] = a
[2]*b
[0] - a
[0]*b
[2];
411 d
[2] = a
[0]*b
[1] - a
[1]*b
[0];
415 static inline f32
v3_length2( v3f a
)
417 return v3_dot( a
, a
);
420 static inline f32
v3_length( v3f a
)
422 return sqrtf( v3_length2( a
) );
425 static inline f32
v3_dist2( v3f a
, v3f b
)
428 v3_sub( a
, b
, delta
);
429 return v3_length2( delta
);
432 static inline f32
v3_dist( v3f a
, v3f b
)
434 return sqrtf( v3_dist2( a
, b
) );
437 static inline void v3_normalize( v3f a
)
439 v3_muls( a
, 1.f
/ v3_length( a
), a
);
442 static inline f32
vg_lerpf( f32 a
, f32 b
, f32 t
){
446 static inline f64
vg_lerp( f64 a
, f64 b
, f64 t
)
451 static inline void vg_slewf( f32
*a
, f32 b
, f32 speed
){
452 f32 d
= vg_signf( b
-*a
),
454 *a
= vg_minf( b
*d
, c
*d
) * d
;
457 static inline f32
vg_smoothstepf( f32 x
){
458 return x
*x
*(3.0f
- 2.0f
*x
);
462 /* correctly lerp around circular period -pi -> pi */
463 static f32
vg_alerpf( f32 a
, f32 b
, f32 t
)
465 f32 d
= fmodf( b
-a
, VG_TAUf
),
466 s
= fmodf( 2.0f
*d
, VG_TAUf
) - d
;
470 static inline void v3_lerp( v3f a
, v3f b
, f32 t
, v3f d
)
472 d
[0] = a
[0] + t
*(b
[0]-a
[0]);
473 d
[1] = a
[1] + t
*(b
[1]-a
[1]);
474 d
[2] = a
[2] + t
*(b
[2]-a
[2]);
477 static inline void v3_minv( v3f a
, v3f b
, v3f dest
)
479 dest
[0] = vg_minf(a
[0], b
[0]);
480 dest
[1] = vg_minf(a
[1], b
[1]);
481 dest
[2] = vg_minf(a
[2], b
[2]);
484 static inline void v3_maxv( v3f a
, v3f b
, v3f dest
)
486 dest
[0] = vg_maxf(a
[0], b
[0]);
487 dest
[1] = vg_maxf(a
[1], b
[1]);
488 dest
[2] = vg_maxf(a
[2], b
[2]);
491 static inline f32
v3_minf( v3f a
)
493 return vg_minf( vg_minf( a
[0], a
[1] ), a
[2] );
496 static inline f32
v3_maxf( v3f a
)
498 return vg_maxf( vg_maxf( a
[0], a
[1] ), a
[2] );
501 static inline void v3_floor( v3f a
, v3f b
)
503 b
[0] = floorf( a
[0] );
504 b
[1] = floorf( a
[1] );
505 b
[2] = floorf( a
[2] );
508 static inline void v3_ceil( v3f a
, v3f b
)
510 b
[0] = ceilf( a
[0] );
511 b
[1] = ceilf( a
[1] );
512 b
[2] = ceilf( a
[2] );
515 static inline void v3_negate( v3f a
, v3f b
)
522 static inline void v3_rotate( v3f v
, f32 angle
, v3f axis
, v3f d
)
533 v3_cross( k
, v
, v2
);
534 v3_muls( v2
, s
, v2
);
535 v3_add( v1
, v2
, v1
);
536 v3_muls( k
, v3_dot(k
, v
) * (1.0f
- c
), v2
);
540 static void v3_tangent_basis( v3f n
, v3f tx
, v3f ty
){
541 /* Compute tangent basis (box2d) */
542 if( fabsf( n
[0] ) >= 0.57735027f
){
554 v3_cross( n
, tx
, ty
);
558 * Compute yaw and pitch based of a normalized vector representing forward
560 * result -> (YAW,PITCH,0.0)
562 static void v3_angles( v3f v
, v3f out_angles
){
563 float yaw
= atan2f( v
[0], -v
[2] ),
567 v
[0]*v
[0] + v
[2]*v
[2]
572 out_angles
[1] = pitch
;
573 out_angles
[2] = 0.0f
;
577 * Compute the forward vector from (YAW,PITCH,ROLL)
580 static void v3_angles_vector( v3f angles
, v3f out_v
){
581 out_v
[0] = sinf( angles
[0] ) * cosf( angles
[1] );
582 out_v
[1] = -sinf( angles
[1] );
583 out_v
[2] = -cosf( angles
[0] ) * cosf( angles
[1] );
587 * -----------------------------------------------------------------------------
588 * Section 2.c 4D Vectors
589 * -----------------------------------------------------------------------------
592 static inline void v4_copy( v4f a
, v4f b
)
594 b
[0] = a
[0]; b
[1] = a
[1]; b
[2] = a
[2]; b
[3] = a
[3];
597 static inline void v4_add( v4f a
, v4f b
, v4f d
)
605 static inline void v4_zero( v4f a
)
607 a
[0] = 0.f
; a
[1] = 0.f
; a
[2] = 0.f
; a
[3] = 0.f
;
610 static inline void v4_muls( v4f a
, f32 s
, v4f d
)
618 static inline void v4_muladds( v4f a
, v4f b
, f32 s
, v4f d
)
626 static inline void v4_lerp( v4f a
, v4f b
, f32 t
, v4f d
)
628 d
[0] = a
[0] + t
*(b
[0]-a
[0]);
629 d
[1] = a
[1] + t
*(b
[1]-a
[1]);
630 d
[2] = a
[2] + t
*(b
[2]-a
[2]);
631 d
[3] = a
[3] + t
*(b
[3]-a
[3]);
634 static inline f32
v4_dot( v4f a
, v4f b
)
636 return a
[0]*b
[0] + a
[1]*b
[1] + a
[2]*b
[2] + a
[3]*b
[3];
639 static inline f32
v4_length( v4f a
)
641 return sqrtf( v4_dot(a
,a
) );
645 * -----------------------------------------------------------------------------
646 * Section 3 Quaternions
647 * -----------------------------------------------------------------------------
650 static inline void q_identity( v4f q
)
652 q
[0] = 0.0f
; q
[1] = 0.0f
; q
[2] = 0.0f
; q
[3] = 1.0f
;
655 static inline void q_axis_angle( v4f q
, v3f axis
, f32 angle
)
667 static inline void q_mul( v4f q
, v4f q1
, v4f d
)
670 t
[0] = q
[3]*q1
[0] + q
[0]*q1
[3] + q
[1]*q1
[2] - q
[2]*q1
[1];
671 t
[1] = q
[3]*q1
[1] - q
[0]*q1
[2] + q
[1]*q1
[3] + q
[2]*q1
[0];
672 t
[2] = q
[3]*q1
[2] + q
[0]*q1
[1] - q
[1]*q1
[0] + q
[2]*q1
[3];
673 t
[3] = q
[3]*q1
[3] - q
[0]*q1
[0] - q
[1]*q1
[1] - q
[2]*q1
[2];
677 static inline void q_normalize( v4f q
)
679 f32 l2
= v4_dot(q
,q
);
680 if( l2
< 0.00001f
) q_identity( q
);
682 f32 s
= 1.0f
/sqrtf(l2
);
690 static inline void q_inv( v4f q
, v4f d
)
692 f32 s
= 1.0f
/ v4_dot(q
,q
);
699 static inline void q_nlerp( v4f a
, v4f b
, f32 t
, v4f d
){
700 if( v4_dot(a
,b
) < 0.0f
){
702 v4_muls( b
, -1.0f
, c
);
703 v4_lerp( a
, c
, t
, d
);
706 v4_lerp( a
, b
, t
, d
);
711 static inline void q_m3x3( v4f q
, m3x3f d
)
715 s
= l
> 0.0f
? 2.0f
/l
: 0.0f
,
717 xx
= s
*q
[0]*q
[0], xy
= s
*q
[0]*q
[1], wx
= s
*q
[3]*q
[0],
718 yy
= s
*q
[1]*q
[1], yz
= s
*q
[1]*q
[2], wy
= s
*q
[3]*q
[1],
719 zz
= s
*q
[2]*q
[2], xz
= s
*q
[0]*q
[2], wz
= s
*q
[3]*q
[2];
721 d
[0][0] = 1.0f
- yy
- zz
;
722 d
[1][1] = 1.0f
- xx
- zz
;
723 d
[2][2] = 1.0f
- xx
- yy
;
732 static void q_mulv( v4f q
, v3f v
, v3f d
)
736 v3_muls( q
, 2.0f
*v3_dot(q
,v
), v1
);
737 v3_muls( v
, q
[3]*q
[3] - v3_dot(q
,q
), v2
);
738 v3_add( v1
, v2
, v1
);
739 v3_cross( q
, v
, v2
);
740 v3_muls( v2
, 2.0f
*q
[3], v2
);
744 static f32
q_dist( v4f q0
, v4f q1
){
745 return acosf( 2.0f
* v4_dot(q0
,q1
) -1.0f
);
749 * -----------------------------------------------------------------------------
750 * Section 4.a 2x2 matrices
751 * -----------------------------------------------------------------------------
754 #define M2X2_INDENTIY {{1.0f, 0.0f, }, \
757 #define M2X2_ZERO {{0.0f, 0.0f, }, \
760 static inline void m2x2_copy( m2x2f a
, m2x2f b
)
762 v2_copy( a
[0], b
[0] );
763 v2_copy( a
[1], b
[1] );
766 static inline void m2x2_identity( m2x2f a
)
768 m2x2f id
= M2X2_INDENTIY
;
772 static inline void m2x2_create_rotation( m2x2f a
, f32 theta
)
785 static inline void m2x2_mulv( m2x2f m
, v2f v
, v2f d
)
789 res
[0] = m
[0][0]*v
[0] + m
[1][0]*v
[1];
790 res
[1] = m
[0][1]*v
[0] + m
[1][1]*v
[1];
796 * -----------------------------------------------------------------------------
797 * Section 4.b 3x3 matrices
798 * -----------------------------------------------------------------------------
801 #define M3X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\
802 { 0.0f, 1.0f, 0.0f, },\
803 { 0.0f, 0.0f, 1.0f, }}
805 #define M3X3_ZERO {{0.0f, 0.0f, 0.0f, },\
806 { 0.0f, 0.0f, 0.0f, },\
807 { 0.0f, 0.0f, 0.0f, }}
810 static void euler_m3x3( v3f angles
, m3x3f d
)
812 f32 cosY
= cosf( angles
[0] ),
813 sinY
= sinf( angles
[0] ),
814 cosP
= cosf( angles
[1] ),
815 sinP
= sinf( angles
[1] ),
816 cosR
= cosf( angles
[2] ),
817 sinR
= sinf( angles
[2] );
819 d
[2][0] = -sinY
* cosP
;
821 d
[2][2] = cosY
* cosP
;
823 d
[0][0] = cosY
* cosR
;
825 d
[0][2] = sinY
* cosR
;
827 v3_cross( d
[0], d
[2], d
[1] );
830 static void m3x3_q( m3x3f m
, v4f q
)
834 diag
= m
[0][0] + m
[1][1] + m
[2][2];
837 r
= sqrtf( 1.0f
+ diag
);
839 q
[0] = rinv
* (m
[1][2] - m
[2][1]);
840 q
[1] = rinv
* (m
[2][0] - m
[0][2]);
841 q
[2] = rinv
* (m
[0][1] - m
[1][0]);
844 else if( m
[0][0] >= m
[1][1] && m
[0][0] >= m
[2][2] )
846 r
= sqrtf( 1.0f
- m
[1][1] - m
[2][2] + m
[0][0] );
849 q
[1] = rinv
* (m
[0][1] + m
[1][0]);
850 q
[2] = rinv
* (m
[0][2] + m
[2][0]);
851 q
[3] = rinv
* (m
[1][2] - m
[2][1]);
853 else if( m
[1][1] >= m
[2][2] )
855 r
= sqrtf( 1.0f
- m
[0][0] - m
[2][2] + m
[1][1] );
857 q
[0] = rinv
* (m
[0][1] + m
[1][0]);
859 q
[2] = rinv
* (m
[1][2] + m
[2][1]);
860 q
[3] = rinv
* (m
[2][0] - m
[0][2]);
864 r
= sqrtf( 1.0f
- m
[0][0] - m
[1][1] + m
[2][2] );
866 q
[0] = rinv
* (m
[0][2] + m
[2][0]);
867 q
[1] = rinv
* (m
[1][2] + m
[2][1]);
869 q
[3] = rinv
* (m
[0][1] - m
[1][0]);
873 /* a X b == [b]T a == ...*/
874 static void m3x3_skew_symetric( m3x3f a
, v3f v
)
887 /* aka kronecker product */
888 static void m3x3_outer_product( m3x3f out_m
, v3f a
, v3f b
)
890 out_m
[0][0] = a
[0]*b
[0];
891 out_m
[0][1] = a
[0]*b
[1];
892 out_m
[0][2] = a
[0]*b
[2];
893 out_m
[1][0] = a
[1]*b
[0];
894 out_m
[1][1] = a
[1]*b
[1];
895 out_m
[1][2] = a
[1]*b
[2];
896 out_m
[2][0] = a
[2]*b
[0];
897 out_m
[2][1] = a
[2]*b
[1];
898 out_m
[2][2] = a
[2]*b
[2];
901 static void m3x3_add( m3x3f a
, m3x3f b
, m3x3f d
)
903 v3_add( a
[0], b
[0], d
[0] );
904 v3_add( a
[1], b
[1], d
[1] );
905 v3_add( a
[2], b
[2], d
[2] );
908 static void m3x3_sub( m3x3f a
, m3x3f b
, m3x3f d
)
910 v3_sub( a
[0], b
[0], d
[0] );
911 v3_sub( a
[1], b
[1], d
[1] );
912 v3_sub( a
[2], b
[2], d
[2] );
915 static inline void m3x3_copy( m3x3f a
, m3x3f b
)
917 v3_copy( a
[0], b
[0] );
918 v3_copy( a
[1], b
[1] );
919 v3_copy( a
[2], b
[2] );
922 static inline void m3x3_identity( m3x3f a
)
924 m3x3f id
= M3X3_IDENTITY
;
928 static void m3x3_diagonal( m3x3f out_a
, f32 v
)
930 m3x3_identity( out_a
);
936 static void m3x3_setdiagonalv3( m3x3f a
, v3f v
)
943 static inline void m3x3_zero( m3x3f a
)
949 static inline void m3x3_inv( m3x3f src
, m3x3f dest
)
951 f32 a
= src
[0][0], b
= src
[0][1], c
= src
[0][2],
952 d
= src
[1][0], e
= src
[1][1], f
= src
[1][2],
953 g
= src
[2][0], h
= src
[2][1], i
= src
[2][2];
960 dest
[0][0] = (e
*i
-h
*f
)*det
;
961 dest
[0][1] = -(b
*i
-c
*h
)*det
;
962 dest
[0][2] = (b
*f
-c
*e
)*det
;
963 dest
[1][0] = -(d
*i
-f
*g
)*det
;
964 dest
[1][1] = (a
*i
-c
*g
)*det
;
965 dest
[1][2] = -(a
*f
-d
*c
)*det
;
966 dest
[2][0] = (d
*h
-g
*e
)*det
;
967 dest
[2][1] = -(a
*h
-g
*b
)*det
;
968 dest
[2][2] = (a
*e
-d
*b
)*det
;
971 static f32
m3x3_det( m3x3f m
)
973 return m
[0][0] * (m
[1][1] * m
[2][2] - m
[2][1] * m
[1][2])
974 - m
[0][1] * (m
[1][0] * m
[2][2] - m
[1][2] * m
[2][0])
975 + m
[0][2] * (m
[1][0] * m
[2][1] - m
[1][1] * m
[2][0]);
978 static inline void m3x3_transpose( m3x3f src
, m3x3f dest
)
980 f32 a
= src
[0][0], b
= src
[0][1], c
= src
[0][2],
981 d
= src
[1][0], e
= src
[1][1], f
= src
[1][2],
982 g
= src
[2][0], h
= src
[2][1], i
= src
[2][2];
995 static inline void m3x3_mul( m3x3f a
, m3x3f b
, m3x3f d
)
997 f32 a00
= a
[0][0], a01
= a
[0][1], a02
= a
[0][2],
998 a10
= a
[1][0], a11
= a
[1][1], a12
= a
[1][2],
999 a20
= a
[2][0], a21
= a
[2][1], a22
= a
[2][2],
1001 b00
= b
[0][0], b01
= b
[0][1], b02
= b
[0][2],
1002 b10
= b
[1][0], b11
= b
[1][1], b12
= b
[1][2],
1003 b20
= b
[2][0], b21
= b
[2][1], b22
= b
[2][2];
1005 d
[0][0] = a00
*b00
+ a10
*b01
+ a20
*b02
;
1006 d
[0][1] = a01
*b00
+ a11
*b01
+ a21
*b02
;
1007 d
[0][2] = a02
*b00
+ a12
*b01
+ a22
*b02
;
1008 d
[1][0] = a00
*b10
+ a10
*b11
+ a20
*b12
;
1009 d
[1][1] = a01
*b10
+ a11
*b11
+ a21
*b12
;
1010 d
[1][2] = a02
*b10
+ a12
*b11
+ a22
*b12
;
1011 d
[2][0] = a00
*b20
+ a10
*b21
+ a20
*b22
;
1012 d
[2][1] = a01
*b20
+ a11
*b21
+ a21
*b22
;
1013 d
[2][2] = a02
*b20
+ a12
*b21
+ a22
*b22
;
1016 static inline void m3x3_mulv( m3x3f m
, v3f v
, v3f d
)
1020 res
[0] = m
[0][0]*v
[0] + m
[1][0]*v
[1] + m
[2][0]*v
[2];
1021 res
[1] = m
[0][1]*v
[0] + m
[1][1]*v
[1] + m
[2][1]*v
[2];
1022 res
[2] = m
[0][2]*v
[0] + m
[1][2]*v
[1] + m
[2][2]*v
[2];
1027 static inline void m3x3_projection( m3x3f dst
,
1028 f32
const left
, f32
const right
, f32
const bottom
, f32
const top
)
1034 rl
= 1.0f
/ (right
- left
);
1035 tb
= 1.0f
/ (top
- bottom
);
1037 dst
[0][0] = 2.0f
* rl
;
1038 dst
[1][1] = 2.0f
* tb
;
1042 static inline void m3x3_translate( m3x3f m
, v3f v
)
1044 m
[2][0] = m
[0][0] * v
[0] + m
[1][0] * v
[1] + m
[2][0];
1045 m
[2][1] = m
[0][1] * v
[0] + m
[1][1] * v
[1] + m
[2][1];
1046 m
[2][2] = m
[0][2] * v
[0] + m
[1][2] * v
[1] + m
[2][2];
1049 static inline void m3x3_scale( m3x3f m
, v3f v
)
1051 v3_muls( m
[0], v
[0], m
[0] );
1052 v3_muls( m
[1], v
[1], m
[1] );
1053 v3_muls( m
[2], v
[2], m
[2] );
1056 static inline void m3x3_scalef( m3x3f m
, f32 f
)
1063 static inline void m3x3_rotate( m3x3f m
, f32 angle
)
1065 f32 m00
= m
[0][0], m10
= m
[1][0],
1066 m01
= m
[0][1], m11
= m
[1][1],
1067 m02
= m
[0][2], m12
= m
[1][2];
1073 m
[0][0] = m00
* c
+ m10
* s
;
1074 m
[0][1] = m01
* c
+ m11
* s
;
1075 m
[0][2] = m02
* c
+ m12
* s
;
1077 m
[1][0] = m00
* -s
+ m10
* c
;
1078 m
[1][1] = m01
* -s
+ m11
* c
;
1079 m
[1][2] = m02
* -s
+ m12
* c
;
1083 * -----------------------------------------------------------------------------
1084 * Section 4.c 4x3 matrices
1085 * -----------------------------------------------------------------------------
1088 #define M4X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\
1089 { 0.0f, 1.0f, 0.0f, },\
1090 { 0.0f, 0.0f, 1.0f, },\
1091 { 0.0f, 0.0f, 0.0f }}
1093 static inline void m4x3_to_3x3( m4x3f a
, m3x3f b
)
1095 v3_copy( a
[0], b
[0] );
1096 v3_copy( a
[1], b
[1] );
1097 v3_copy( a
[2], b
[2] );
1100 static inline void m4x3_invert_affine( m4x3f a
, m4x3f b
)
1102 m3x3_transpose( a
, b
);
1103 m3x3_mulv( b
, a
[3], b
[3] );
1104 v3_negate( b
[3], b
[3] );
1107 static void m4x3_invert_full( m4x3f src
, m4x3f dst
)
1111 a
= src
[0][0], b
= src
[0][1], c
= src
[0][2],
1112 e
= src
[1][0], f
= src
[1][1], g
= src
[1][2],
1113 i
= src
[2][0], j
= src
[2][1], k
= src
[2][2],
1114 m
= src
[3][0], n
= src
[3][1], o
= src
[3][2];
1120 dst
[0][0] = f
*k
- g
*j
;
1121 dst
[1][0] =-(e
*k
- g
*i
);
1122 dst
[2][0] = e
*j
- f
*i
;
1123 dst
[3][0] =-(e
*t2
- f
*t4
+ g
*t5
);
1125 dst
[0][1] =-(b
*k
- c
*j
);
1126 dst
[1][1] = a
*k
- c
*i
;
1127 dst
[2][1] =-(a
*j
- b
*i
);
1128 dst
[3][1] = a
*t2
- b
*t4
+ c
*t5
;
1134 dst
[0][2] = b
*g
- c
*f
;
1135 dst
[1][2] =-(a
*g
- c
*e
);
1136 dst
[2][2] = a
*f
- b
*e
;
1137 dst
[3][2] =-(a
*t2
- b
*t4
+ c
* t5
);
1139 det
= 1.0f
/ (a
* dst
[0][0] + b
* dst
[1][0] + c
* dst
[2][0]);
1140 v3_muls( dst
[0], det
, dst
[0] );
1141 v3_muls( dst
[1], det
, dst
[1] );
1142 v3_muls( dst
[2], det
, dst
[2] );
1143 v3_muls( dst
[3], det
, dst
[3] );
1146 static inline void m4x3_copy( m4x3f a
, m4x3f b
)
1148 v3_copy( a
[0], b
[0] );
1149 v3_copy( a
[1], b
[1] );
1150 v3_copy( a
[2], b
[2] );
1151 v3_copy( a
[3], b
[3] );
1154 static inline void m4x3_identity( m4x3f a
)
1156 m4x3f id
= M4X3_IDENTITY
;
1160 static void m4x3_mul( m4x3f a
, m4x3f b
, m4x3f d
)
1163 a00
= a
[0][0], a01
= a
[0][1], a02
= a
[0][2],
1164 a10
= a
[1][0], a11
= a
[1][1], a12
= a
[1][2],
1165 a20
= a
[2][0], a21
= a
[2][1], a22
= a
[2][2],
1166 a30
= a
[3][0], a31
= a
[3][1], a32
= a
[3][2],
1167 b00
= b
[0][0], b01
= b
[0][1], b02
= b
[0][2],
1168 b10
= b
[1][0], b11
= b
[1][1], b12
= b
[1][2],
1169 b20
= b
[2][0], b21
= b
[2][1], b22
= b
[2][2],
1170 b30
= b
[3][0], b31
= b
[3][1], b32
= b
[3][2];
1172 d
[0][0] = a00
*b00
+ a10
*b01
+ a20
*b02
;
1173 d
[0][1] = a01
*b00
+ a11
*b01
+ a21
*b02
;
1174 d
[0][2] = a02
*b00
+ a12
*b01
+ a22
*b02
;
1175 d
[1][0] = a00
*b10
+ a10
*b11
+ a20
*b12
;
1176 d
[1][1] = a01
*b10
+ a11
*b11
+ a21
*b12
;
1177 d
[1][2] = a02
*b10
+ a12
*b11
+ a22
*b12
;
1178 d
[2][0] = a00
*b20
+ a10
*b21
+ a20
*b22
;
1179 d
[2][1] = a01
*b20
+ a11
*b21
+ a21
*b22
;
1180 d
[2][2] = a02
*b20
+ a12
*b21
+ a22
*b22
;
1181 d
[3][0] = a00
*b30
+ a10
*b31
+ a20
*b32
+ a30
;
1182 d
[3][1] = a01
*b30
+ a11
*b31
+ a21
*b32
+ a31
;
1183 d
[3][2] = a02
*b30
+ a12
*b31
+ a22
*b32
+ a32
;
1186 #if 0 /* shat appf mingw wstringop-overflow */
1189 static void m4x3_mulv( m4x3f m
, v3f v
, v3f d
)
1193 res
[0] = m
[0][0]*v
[0] + m
[1][0]*v
[1] + m
[2][0]*v
[2] + m
[3][0];
1194 res
[1] = m
[0][1]*v
[0] + m
[1][1]*v
[1] + m
[2][1]*v
[2] + m
[3][1];
1195 res
[2] = m
[0][2]*v
[0] + m
[1][2]*v
[1] + m
[2][2]*v
[2] + m
[3][2];
1201 * Transform plane ( xyz, distance )
1203 static void m4x3_mulp( m4x3f m
, v4f p
, v4f d
)
1207 v3_muls( p
, p
[3], o
);
1208 m4x3_mulv( m
, o
, o
);
1209 m3x3_mulv( m
, p
, d
);
1211 d
[3] = v3_dot( o
, d
);
1218 static void m4x3_translate( m4x3f m
, v3f v
)
1220 v3_muladds( m
[3], m
[0], v
[0], m
[3] );
1221 v3_muladds( m
[3], m
[1], v
[1], m
[3] );
1222 v3_muladds( m
[3], m
[2], v
[2], m
[3] );
1225 static void m4x3_rotate_x( m4x3f m
, f32 angle
)
1227 m4x3f t
= M4X3_IDENTITY
;
1238 m4x3_mul( m
, t
, m
);
1241 static void m4x3_rotate_y( m4x3f m
, f32 angle
)
1243 m4x3f t
= M4X3_IDENTITY
;
1254 m4x3_mul( m
, t
, m
);
1257 static void m4x3_rotate_z( m4x3f m
, f32 angle
)
1259 m4x3f t
= M4X3_IDENTITY
;
1270 m4x3_mul( m
, t
, m
);
1273 static void m4x3_expand( m4x3f m
, m4x4f d
)
1275 v3_copy( m
[0], d
[0] );
1276 v3_copy( m
[1], d
[1] );
1277 v3_copy( m
[2], d
[2] );
1278 v3_copy( m
[3], d
[3] );
1285 static void m4x3_decompose( m4x3f m
, v3f co
, v4f q
, v3f s
)
1287 v3_copy( m
[3], co
);
1288 s
[0] = v3_length(m
[0]);
1289 s
[1] = v3_length(m
[1]);
1290 s
[2] = v3_length(m
[2]);
1293 v3_divs( m
[0], s
[0], rot
[0] );
1294 v3_divs( m
[1], s
[1], rot
[1] );
1295 v3_divs( m
[2], s
[2], rot
[2] );
1300 static void m4x3_expand_aabb_point( m4x3f m
, boxf box
, v3f point
){
1302 m4x3_mulv( m
, point
, v
);
1304 v3_minv( box
[0], v
, box
[0] );
1305 v3_maxv( box
[1], v
, box
[1] );
1308 static void m4x3_expand_aabb_aabb( m4x3f m
, boxf boxa
, boxf boxb
){
1310 v3_copy( boxb
[0], a
);
1311 v3_copy( boxb
[1], b
);
1312 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ a
[0], a
[1], a
[2] } );
1313 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ a
[0], b
[1], a
[2] } );
1314 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ b
[0], b
[1], a
[2] } );
1315 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ b
[0], a
[1], a
[2] } );
1316 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ a
[0], a
[1], b
[2] } );
1317 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ a
[0], b
[1], b
[2] } );
1318 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ b
[0], b
[1], b
[2] } );
1319 m4x3_expand_aabb_point( m
, boxa
, (v3f
){ b
[0], a
[1], b
[2] } );
1321 static inline void m4x3_lookat( m4x3f m
, v3f pos
, v3f target
, v3f up
)
1324 v3_sub( target
, pos
, dir
);
1325 v3_normalize( dir
);
1327 v3_copy( dir
, m
[2] );
1329 v3_cross( up
, m
[2], m
[0] );
1330 v3_normalize( m
[0] );
1332 v3_cross( m
[2], m
[0], m
[1] );
1333 v3_copy( pos
, m
[3] );
1337 * -----------------------------------------------------------------------------
1338 * Section 4.d 4x4 matrices
1339 * -----------------------------------------------------------------------------
1342 #define M4X4_IDENTITY {{1.0f, 0.0f, 0.0f, 0.0f },\
1343 { 0.0f, 1.0f, 0.0f, 0.0f },\
1344 { 0.0f, 0.0f, 1.0f, 0.0f },\
1345 { 0.0f, 0.0f, 0.0f, 1.0f }}
1346 #define M4X4_ZERO {{0.0f, 0.0f, 0.0f, 0.0f },\
1347 { 0.0f, 0.0f, 0.0f, 0.0f },\
1348 { 0.0f, 0.0f, 0.0f, 0.0f },\
1349 { 0.0f, 0.0f, 0.0f, 0.0f }}
1351 static void m4x4_projection( m4x4f m
, f32 angle
,
1352 f32 ratio
, f32 fnear
, f32 ffar
)
1354 f32 scale
= tanf( angle
* 0.5f
* VG_PIf
/ 180.0f
) * fnear
,
1360 m
[0][0] = 2.0f
* fnear
/ (r
- l
);
1366 m
[1][1] = 2.0f
* fnear
/ (t
- b
);
1370 m
[2][0] = (r
+ l
) / (r
- l
);
1371 m
[2][1] = (t
+ b
) / (t
- b
);
1372 m
[2][2] = -(ffar
+ fnear
) / (ffar
- fnear
);
1377 m
[3][2] = -2.0f
* ffar
* fnear
/ (ffar
- fnear
);
1381 static void m4x4_translate( m4x4f m
, v3f v
)
1383 v4_muladds( m
[3], m
[0], v
[0], m
[3] );
1384 v4_muladds( m
[3], m
[1], v
[1], m
[3] );
1385 v4_muladds( m
[3], m
[2], v
[2], m
[3] );
1388 static inline void m4x4_copy( m4x4f a
, m4x4f b
)
1390 v4_copy( a
[0], b
[0] );
1391 v4_copy( a
[1], b
[1] );
1392 v4_copy( a
[2], b
[2] );
1393 v4_copy( a
[3], b
[3] );
1396 static inline void m4x4_identity( m4x4f a
)
1398 m4x4f id
= M4X4_IDENTITY
;
1402 static inline void m4x4_zero( m4x4f a
)
1404 m4x4f zero
= M4X4_ZERO
;
1405 m4x4_copy( zero
, a
);
1408 static inline void m4x4_mul( m4x4f a
, m4x4f b
, m4x4f d
)
1410 f32 a00
= a
[0][0], a01
= a
[0][1], a02
= a
[0][2], a03
= a
[0][3],
1411 a10
= a
[1][0], a11
= a
[1][1], a12
= a
[1][2], a13
= a
[1][3],
1412 a20
= a
[2][0], a21
= a
[2][1], a22
= a
[2][2], a23
= a
[2][3],
1413 a30
= a
[3][0], a31
= a
[3][1], a32
= a
[3][2], a33
= a
[3][3],
1415 b00
= b
[0][0], b01
= b
[0][1], b02
= b
[0][2], b03
= b
[0][3],
1416 b10
= b
[1][0], b11
= b
[1][1], b12
= b
[1][2], b13
= b
[1][3],
1417 b20
= b
[2][0], b21
= b
[2][1], b22
= b
[2][2], b23
= b
[2][3],
1418 b30
= b
[3][0], b31
= b
[3][1], b32
= b
[3][2], b33
= b
[3][3];
1420 d
[0][0] = a00
*b00
+ a10
*b01
+ a20
*b02
+ a30
*b03
;
1421 d
[0][1] = a01
*b00
+ a11
*b01
+ a21
*b02
+ a31
*b03
;
1422 d
[0][2] = a02
*b00
+ a12
*b01
+ a22
*b02
+ a32
*b03
;
1423 d
[0][3] = a03
*b00
+ a13
*b01
+ a23
*b02
+ a33
*b03
;
1424 d
[1][0] = a00
*b10
+ a10
*b11
+ a20
*b12
+ a30
*b13
;
1425 d
[1][1] = a01
*b10
+ a11
*b11
+ a21
*b12
+ a31
*b13
;
1426 d
[1][2] = a02
*b10
+ a12
*b11
+ a22
*b12
+ a32
*b13
;
1427 d
[1][3] = a03
*b10
+ a13
*b11
+ a23
*b12
+ a33
*b13
;
1428 d
[2][0] = a00
*b20
+ a10
*b21
+ a20
*b22
+ a30
*b23
;
1429 d
[2][1] = a01
*b20
+ a11
*b21
+ a21
*b22
+ a31
*b23
;
1430 d
[2][2] = a02
*b20
+ a12
*b21
+ a22
*b22
+ a32
*b23
;
1431 d
[2][3] = a03
*b20
+ a13
*b21
+ a23
*b22
+ a33
*b23
;
1432 d
[3][0] = a00
*b30
+ a10
*b31
+ a20
*b32
+ a30
*b33
;
1433 d
[3][1] = a01
*b30
+ a11
*b31
+ a21
*b32
+ a31
*b33
;
1434 d
[3][2] = a02
*b30
+ a12
*b31
+ a22
*b32
+ a32
*b33
;
1435 d
[3][3] = a03
*b30
+ a13
*b31
+ a23
*b32
+ a33
*b33
;
1438 static inline void m4x4_mulv( m4x4f m
, v4f v
, v4f d
)
1442 res
[0] = m
[0][0]*v
[0] + m
[1][0]*v
[1] + m
[2][0]*v
[2] + m
[3][0]*v
[3];
1443 res
[1] = m
[0][1]*v
[0] + m
[1][1]*v
[1] + m
[2][1]*v
[2] + m
[3][1]*v
[3];
1444 res
[2] = m
[0][2]*v
[0] + m
[1][2]*v
[1] + m
[2][2]*v
[2] + m
[3][2]*v
[3];
1445 res
[3] = m
[0][3]*v
[0] + m
[1][3]*v
[1] + m
[2][3]*v
[2] + m
[3][3]*v
[3];
1450 static inline void m4x4_inv( m4x4f a
, m4x4f d
)
1452 f32 a00
= a
[0][0], a01
= a
[0][1], a02
= a
[0][2], a03
= a
[0][3],
1453 a10
= a
[1][0], a11
= a
[1][1], a12
= a
[1][2], a13
= a
[1][3],
1454 a20
= a
[2][0], a21
= a
[2][1], a22
= a
[2][2], a23
= a
[2][3],
1455 a30
= a
[3][0], a31
= a
[3][1], a32
= a
[3][2], a33
= a
[3][3],
1459 t
[0] = a22
*a33
- a32
*a23
;
1460 t
[1] = a21
*a33
- a31
*a23
;
1461 t
[2] = a21
*a32
- a31
*a22
;
1462 t
[3] = a20
*a33
- a30
*a23
;
1463 t
[4] = a20
*a32
- a30
*a22
;
1464 t
[5] = a20
*a31
- a30
*a21
;
1466 d
[0][0] = a11
*t
[0] - a12
*t
[1] + a13
*t
[2];
1467 d
[1][0] =-(a10
*t
[0] - a12
*t
[3] + a13
*t
[4]);
1468 d
[2][0] = a10
*t
[1] - a11
*t
[3] + a13
*t
[5];
1469 d
[3][0] =-(a10
*t
[2] - a11
*t
[4] + a12
*t
[5]);
1471 d
[0][1] =-(a01
*t
[0] - a02
*t
[1] + a03
*t
[2]);
1472 d
[1][1] = a00
*t
[0] - a02
*t
[3] + a03
*t
[4];
1473 d
[2][1] =-(a00
*t
[1] - a01
*t
[3] + a03
*t
[5]);
1474 d
[3][1] = a00
*t
[2] - a01
*t
[4] + a02
*t
[5];
1476 t
[0] = a12
*a33
- a32
*a13
;
1477 t
[1] = a11
*a33
- a31
*a13
;
1478 t
[2] = a11
*a32
- a31
*a12
;
1479 t
[3] = a10
*a33
- a30
*a13
;
1480 t
[4] = a10
*a32
- a30
*a12
;
1481 t
[5] = a10
*a31
- a30
*a11
;
1483 d
[0][2] = a01
*t
[0] - a02
*t
[1] + a03
*t
[2];
1484 d
[1][2] =-(a00
*t
[0] - a02
*t
[3] + a03
*t
[4]);
1485 d
[2][2] = a00
*t
[1] - a01
*t
[3] + a03
*t
[5];
1486 d
[3][2] =-(a00
*t
[2] - a01
*t
[4] + a02
*t
[5]);
1488 t
[0] = a12
*a23
- a22
*a13
;
1489 t
[1] = a11
*a23
- a21
*a13
;
1490 t
[2] = a11
*a22
- a21
*a12
;
1491 t
[3] = a10
*a23
- a20
*a13
;
1492 t
[4] = a10
*a22
- a20
*a12
;
1493 t
[5] = a10
*a21
- a20
*a11
;
1495 d
[0][3] =-(a01
*t
[0] - a02
*t
[1] + a03
*t
[2]);
1496 d
[1][3] = a00
*t
[0] - a02
*t
[3] + a03
*t
[4];
1497 d
[2][3] =-(a00
*t
[1] - a01
*t
[3] + a03
*t
[5]);
1498 d
[3][3] = a00
*t
[2] - a01
*t
[4] + a02
*t
[5];
1500 det
= 1.0f
/ (a00
*d
[0][0] + a01
*d
[1][0] + a02
*d
[2][0] + a03
*d
[3][0]);
1501 v4_muls( d
[0], det
, d
[0] );
1502 v4_muls( d
[1], det
, d
[1] );
1503 v4_muls( d
[2], det
, d
[2] );
1504 v4_muls( d
[3], det
, d
[3] );
1508 * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
1510 static void m4x4_clip_projection( m4x4f mat
, v4f plane
){
1513 (vg_signf(plane
[0]) + mat
[2][0]) / mat
[0][0],
1514 (vg_signf(plane
[1]) + mat
[2][1]) / mat
[1][1],
1516 (1.0f
+ mat
[2][2]) / mat
[3][2]
1519 v4_muls( plane
, 2.0f
/ v4_dot(plane
,c
), c
);
1523 mat
[2][2] = c
[2] + 1.0f
;
1528 * Undoes the above operation
1530 static void m4x4_reset_clipping( m4x4f mat
, float ffar
, float fnear
){
1533 mat
[2][2] = -(ffar
+ fnear
) / (ffar
- fnear
);
1534 mat
[3][2] = -2.0f
* ffar
* fnear
/ (ffar
- fnear
);
1538 * -----------------------------------------------------------------------------
1540 * -----------------------------------------------------------------------------
1543 static inline void box_addpt( boxf a
, v3f pt
)
1545 v3_minv( a
[0], pt
, a
[0] );
1546 v3_maxv( a
[1], pt
, a
[1] );
1549 static inline void box_concat( boxf a
, boxf b
)
1551 v3_minv( a
[0], b
[0], a
[0] );
1552 v3_maxv( a
[1], b
[1], a
[1] );
1555 static inline void box_copy( boxf a
, boxf b
)
1557 v3_copy( a
[0], b
[0] );
1558 v3_copy( a
[1], b
[1] );
1561 static inline int box_overlap( boxf a
, boxf b
)
1564 ( a
[0][0] <= b
[1][0] && a
[1][0] >= b
[0][0] ) &&
1565 ( a
[0][1] <= b
[1][1] && a
[1][1] >= b
[0][1] ) &&
1566 ( a
[0][2] <= b
[1][2] && a
[1][2] >= b
[0][2] )
1570 static int box_within( boxf greater
, boxf lesser
)
1573 v3_sub( lesser
[0], greater
[0], a
);
1574 v3_sub( lesser
[1], greater
[1], b
);
1576 if( (a
[0] >= 0.0f
) && (a
[1] >= 0.0f
) && (a
[2] >= 0.0f
) &&
1577 (b
[0] <= 0.0f
) && (b
[1] <= 0.0f
) && (b
[2] <= 0.0f
) )
1585 static inline void box_init_inf( boxf box
){
1586 v3_fill( box
[0], INFINITY
);
1587 v3_fill( box
[1], -INFINITY
);
1591 * -----------------------------------------------------------------------------
1592 * Section 5.b Planes
1593 * -----------------------------------------------------------------------------
1596 static inline void tri_to_plane( f64 a
[3], f64 b
[3],
1597 f64 c
[3], f64 p
[4] )
1603 edge0
[0] = b
[0] - a
[0];
1604 edge0
[1] = b
[1] - a
[1];
1605 edge0
[2] = b
[2] - a
[2];
1607 edge1
[0] = c
[0] - a
[0];
1608 edge1
[1] = c
[1] - a
[1];
1609 edge1
[2] = c
[2] - a
[2];
1611 p
[0] = edge0
[1] * edge1
[2] - edge0
[2] * edge1
[1];
1612 p
[1] = edge0
[2] * edge1
[0] - edge0
[0] * edge1
[2];
1613 p
[2] = edge0
[0] * edge1
[1] - edge0
[1] * edge1
[0];
1615 l
= sqrt(p
[0] * p
[0] + p
[1] * p
[1] + p
[2] * p
[2]);
1616 p
[3] = (p
[0] * a
[0] + p
[1] * a
[1] + p
[2] * a
[2]) / l
;
1623 static int plane_intersect3( v4f a
, v4f b
, v4f c
, v3f p
)
1625 f32
const epsilon
= 1e-6f
;
1628 v3_cross( a
, b
, x
);
1629 f32 d
= v3_dot( x
, c
);
1631 if( (d
< epsilon
) && (d
> -epsilon
) ) return 0;
1634 v3_cross( b
, c
, v0
);
1635 v3_cross( c
, a
, v1
);
1636 v3_cross( a
, b
, v2
);
1638 v3_muls( v0
, a
[3], p
);
1639 v3_muladds( p
, v1
, b
[3], p
);
1640 v3_muladds( p
, v2
, c
[3], p
);
1646 static int plane_intersect2( v4f a
, v4f b
, v3f p
, v3f n
)
1648 f32
const epsilon
= 1e-6f
;
1651 v3_cross( a
, b
, c
);
1652 f32 d
= v3_length2( c
);
1654 if( (d
< epsilon
) && (d
> -epsilon
) )
1658 v3_cross( c
, b
, v0
);
1659 v3_cross( a
, c
, v1
);
1661 v3_muls( v0
, a
[3], vx
);
1662 v3_muladds( vx
, v1
, b
[3], vx
);
1663 v3_divs( vx
, d
, p
);
1669 static int plane_segment( v4f plane
, v3f a
, v3f b
, v3f co
)
1671 f32 d0
= v3_dot( a
, plane
) - plane
[3],
1672 d1
= v3_dot( b
, plane
) - plane
[3];
1676 f32 tot
= 1.0f
/( fabsf(d0
)+fabsf(d1
) );
1678 v3_muls( a
, fabsf(d1
) * tot
, co
);
1679 v3_muladds( co
, b
, fabsf(d0
) * tot
, co
);
1686 static inline f64
plane_polarity( f64 p
[4], f64 a
[3] )
1689 (a
[0] * p
[0] + a
[1] * p
[1] + a
[2] * p
[2])
1690 -(p
[0]*p
[3] * p
[0] + p
[1]*p
[3] * p
[1] + p
[2]*p
[3] * p
[2])
1694 static f32
ray_plane( v4f plane
, v3f co
, v3f dir
){
1695 f32 d
= v3_dot( plane
, dir
);
1696 if( fabsf(d
) > 1e-6f
){
1698 v3_muls( plane
, plane
[3], v0
);
1699 v3_sub( v0
, co
, v0
);
1700 return v3_dot( v0
, plane
) / d
;
1702 else return INFINITY
;
1706 * -----------------------------------------------------------------------------
1707 * Section 5.c Closest point functions
1708 * -----------------------------------------------------------------------------
1712 * These closest point tests were learned from Real-Time Collision Detection by
1715 static f32
closest_segment_segment( v3f p1
, v3f q1
, v3f p2
, v3f q2
,
1716 f32
*s
, f32
*t
, v3f c1
, v3f c2
)
1719 v3_sub( q1
, p1
, d1
);
1720 v3_sub( q2
, p2
, d2
);
1721 v3_sub( p1
, p2
, r
);
1723 f32 a
= v3_length2( d1
),
1724 e
= v3_length2( d2
),
1725 f
= v3_dot( d2
, r
);
1727 const f32 kEpsilon
= 0.0001f
;
1729 if( a
<= kEpsilon
&& e
<= kEpsilon
)
1737 v3_sub( c1
, c2
, v0
);
1739 return v3_length2( v0
);
1745 *t
= vg_clampf( f
/ e
, 0.0f
, 1.0f
);
1749 f32 c
= v3_dot( d1
, r
);
1753 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
1757 f32 b
= v3_dot(d1
,d2
),
1762 *s
= vg_clampf((b
*f
- c
*e
)/d
, 0.0f
, 1.0f
);
1769 *t
= (b
*(*s
)+f
) / e
;
1774 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
1776 else if( *t
> 1.0f
)
1779 *s
= vg_clampf((b
-c
)/a
,0.0f
,1.0f
);
1784 v3_muladds( p1
, d1
, *s
, c1
);
1785 v3_muladds( p2
, d2
, *t
, c2
);
1788 v3_sub( c1
, c2
, v0
);
1789 return v3_length2( v0
);
1792 static int point_inside_aabb( boxf box
, v3f point
)
1794 if((point
[0]<=box
[1][0]) && (point
[1]<=box
[1][1]) && (point
[2]<=box
[1][2]) &&
1795 (point
[0]>=box
[0][0]) && (point
[1]>=box
[0][1]) && (point
[2]>=box
[0][2]) )
1801 static void closest_point_aabb( v3f p
, boxf box
, v3f dest
)
1803 v3_maxv( p
, box
[0], dest
);
1804 v3_minv( dest
, box
[1], dest
);
1807 static void closest_point_obb( v3f p
, boxf box
,
1808 m4x3f mtx
, m4x3f inv_mtx
, v3f dest
)
1811 m4x3_mulv( inv_mtx
, p
, local
);
1812 closest_point_aabb( local
, box
, local
);
1813 m4x3_mulv( mtx
, local
, dest
);
1816 static f32
closest_point_segment( v3f a
, v3f b
, v3f point
, v3f dest
)
1820 v3_sub( point
, a
, v1
);
1822 f32 t
= v3_dot( v1
, v0
) / v3_length2(v0
);
1823 t
= vg_clampf(t
,0.0f
,1.0f
);
1824 v3_muladds( a
, v0
, t
, dest
);
1828 static void closest_on_triangle( v3f p
, v3f tri
[3], v3f dest
)
1833 /* Region outside A */
1834 v3_sub( tri
[1], tri
[0], ab
);
1835 v3_sub( tri
[2], tri
[0], ac
);
1836 v3_sub( p
, tri
[0], ap
);
1840 if( d1
<= 0.0f
&& d2
<= 0.0f
)
1842 v3_copy( tri
[0], dest
);
1843 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
1847 /* Region outside B */
1851 v3_sub( p
, tri
[1], bp
);
1852 d3
= v3_dot( ab
, bp
);
1853 d4
= v3_dot( ac
, bp
);
1855 if( d3
>= 0.0f
&& d4
<= d3
)
1857 v3_copy( tri
[1], dest
);
1858 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
1862 /* Edge region of AB */
1863 f32 vc
= d1
*d4
- d3
*d2
;
1864 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
1866 f32 v
= d1
/ (d1
-d3
);
1867 v3_muladds( tri
[0], ab
, v
, dest
);
1868 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
1872 /* Region outside C */
1875 v3_sub( p
, tri
[2], cp
);
1876 d5
= v3_dot(ab
, cp
);
1877 d6
= v3_dot(ac
, cp
);
1879 if( d6
>= 0.0f
&& d5
<= d6
)
1881 v3_copy( tri
[2], dest
);
1882 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
1887 f32 vb
= d5
*d2
- d1
*d6
;
1888 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
1890 f32 w
= d2
/ (d2
-d6
);
1891 v3_muladds( tri
[0], ac
, w
, dest
);
1892 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
1897 f32 va
= d3
*d6
- d5
*d4
;
1898 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
1900 f32 w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
1902 v3_sub( tri
[2], tri
[1], bc
);
1903 v3_muladds( tri
[1], bc
, w
, dest
);
1904 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
1908 /* P inside region, Q via barycentric coordinates uvw */
1909 f32 d
= 1.0f
/(va
+vb
+vc
),
1913 v3_muladds( tri
[0], ab
, v
, dest
);
1914 v3_muladds( dest
, ac
, w
, dest
);
1919 k_contact_type_default
,
1920 k_contact_type_disabled
,
1924 static enum contact_type
closest_on_triangle_1( v3f p
, v3f tri
[3], v3f dest
)
1929 /* Region outside A */
1930 v3_sub( tri
[1], tri
[0], ab
);
1931 v3_sub( tri
[2], tri
[0], ac
);
1932 v3_sub( p
, tri
[0], ap
);
1936 if( d1
<= 0.0f
&& d2
<= 0.0f
)
1938 v3_copy( tri
[0], dest
);
1939 return k_contact_type_default
;
1942 /* Region outside B */
1946 v3_sub( p
, tri
[1], bp
);
1947 d3
= v3_dot( ab
, bp
);
1948 d4
= v3_dot( ac
, bp
);
1950 if( d3
>= 0.0f
&& d4
<= d3
)
1952 v3_copy( tri
[1], dest
);
1953 return k_contact_type_edge
;
1956 /* Edge region of AB */
1957 f32 vc
= d1
*d4
- d3
*d2
;
1958 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
1960 f32 v
= d1
/ (d1
-d3
);
1961 v3_muladds( tri
[0], ab
, v
, dest
);
1962 return k_contact_type_edge
;
1965 /* Region outside C */
1968 v3_sub( p
, tri
[2], cp
);
1969 d5
= v3_dot(ab
, cp
);
1970 d6
= v3_dot(ac
, cp
);
1972 if( d6
>= 0.0f
&& d5
<= d6
)
1974 v3_copy( tri
[2], dest
);
1975 return k_contact_type_edge
;
1979 f32 vb
= d5
*d2
- d1
*d6
;
1980 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
1982 f32 w
= d2
/ (d2
-d6
);
1983 v3_muladds( tri
[0], ac
, w
, dest
);
1984 return k_contact_type_edge
;
1988 f32 va
= d3
*d6
- d5
*d4
;
1989 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
1991 f32 w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
1993 v3_sub( tri
[2], tri
[1], bc
);
1994 v3_muladds( tri
[1], bc
, w
, dest
);
1995 return k_contact_type_edge
;
1998 /* P inside region, Q via barycentric coordinates uvw */
1999 f32 d
= 1.0f
/(va
+vb
+vc
),
2003 v3_muladds( tri
[0], ab
, v
, dest
);
2004 v3_muladds( dest
, ac
, w
, dest
);
2006 return k_contact_type_default
;
2009 static void closest_point_elipse( v2f p
, v2f e
, v2f o
)
2011 v2f pabs
, ei
, e2
, ve
, t
;
2014 v2_div( (v2f
){ 1.0f
, 1.0f
}, e
, ei
);
2016 v2_mul( ei
, (v2f
){ e2
[0]-e2
[1], e2
[1]-e2
[0] }, ve
);
2018 v2_fill( t
, 0.70710678118654752f
);
2020 for( int i
=0; i
<3; i
++ ){
2023 v2_mul( ve
, t
, v
); /* ve*t*t*t */
2027 v2_sub( pabs
, v
, u
);
2031 v2_sub( ud
, v
, ud
);
2033 v2_muls( u
, v2_length( ud
), u
);
2038 v2_maxv( (v2f
){0.0f
,0.0f
}, w
, t
);
2043 v2_copysign( o
, p
);
2047 * -----------------------------------------------------------------------------
2048 * Section 5.d Raycasts & Spherecasts
2049 * -----------------------------------------------------------------------------
2052 static int ray_aabb1( boxf box
, v3f co
, v3f dir_inv
, f32 dist
)
2057 v3_sub( box
[0], co
, v0
);
2058 v3_sub( box
[1], co
, v1
);
2060 v3_mul( v0
, dir_inv
, v0
);
2061 v3_mul( v1
, dir_inv
, v1
);
2063 tmin
= vg_minf( v0
[0], v1
[0] );
2064 tmax
= vg_maxf( v0
[0], v1
[0] );
2065 tmin
= vg_maxf( tmin
, vg_minf( v0
[1], v1
[1] ));
2066 tmax
= vg_minf( tmax
, vg_maxf( v0
[1], v1
[1] ));
2067 tmin
= vg_maxf( tmin
, vg_minf( v0
[2], v1
[2] ));
2068 tmax
= vg_minf( tmax
, vg_maxf( v0
[2], v1
[2] ));
2070 return (tmax
>= tmin
) && (tmin
<= dist
) && (tmax
>= 0.0f
);
2073 /* Time of intersection with ray vs triangle */
2074 static int ray_tri( v3f tri
[3], v3f co
,
2075 v3f dir
, f32
*dist
, int backfaces
)
2077 f32
const kEpsilon
= 0.00001f
;
2079 v3f v0
, v1
, h
, s
, q
, n
;
2086 v3_sub( pb
, pa
, v0
);
2087 v3_sub( pc
, pa
, v1
);
2088 v3_cross( dir
, v1
, h
);
2089 v3_cross( v0
, v1
, n
);
2091 if( (v3_dot( n
, dir
) > 0.0f
) && !backfaces
) /* Backface culling */
2095 a
= v3_dot( v0
, h
);
2097 if( a
> -kEpsilon
&& a
< kEpsilon
)
2101 v3_sub( co
, pa
, s
);
2103 u
= f
* v3_dot(s
, h
);
2104 if( u
< 0.0f
|| u
> 1.0f
)
2107 v3_cross( s
, v0
, q
);
2108 v
= f
* v3_dot( dir
, q
);
2109 if( v
< 0.0f
|| u
+v
> 1.0f
)
2112 t
= f
* v3_dot(v1
, q
);
2121 /* time of intersection with ray vs sphere */
2122 static int ray_sphere( v3f c
, f32 r
,
2123 v3f co
, v3f dir
, f32
*t
)
2128 f32 b
= v3_dot( m
, dir
),
2129 c1
= v3_dot( m
, m
) - r
*r
;
2131 /* Exit if r’s origin outside s (c > 0) and r pointing away from s (b > 0) */
2132 if( c1
> 0.0f
&& b
> 0.0f
)
2135 f32 discr
= b
*b
- c1
;
2137 /* A negative discriminant corresponds to ray missing sphere */
2142 * Ray now found to intersect sphere, compute smallest t value of
2145 *t
= -b
- sqrtf( discr
);
2147 /* If t is negative, ray started inside sphere so clamp t to zero */
2155 * time of intersection of ray vs cylinder
2156 * The cylinder does not have caps but is finite
2158 * Heavily adapted from regular segment vs cylinder from:
2159 * Real-Time Collision Detection
2161 static int ray_uncapped_finite_cylinder( v3f q
, v3f p
, f32 r
,
2162 v3f co
, v3f dir
, f32
*t
)
2165 v3_muladds( co
, dir
, 1.0f
, sb
);
2169 v3_sub( sb
, co
, n
);
2171 f32 md
= v3_dot( m
, d
),
2172 nd
= v3_dot( n
, d
),
2173 dd
= v3_dot( d
, d
),
2174 nn
= v3_dot( n
, n
),
2175 mn
= v3_dot( m
, n
),
2177 k
= v3_dot( m
, m
) - r
*r
,
2180 if( fabsf(a
) < 0.00001f
)
2182 /* Segment runs parallel to cylinder axis */
2186 f32 b
= dd
*mn
- nd
*md
,
2190 return 0; /* No real roots; no intersection */
2192 *t
= (-b
- sqrtf(discr
)) / a
;
2194 return 0; /* Intersection behind ray */
2196 /* Check within cylinder segment */
2197 if( md
+ (*t
)*nd
< 0.0f
)
2200 if( md
+ (*t
)*nd
> dd
)
2203 /* Segment intersects cylinder between the endcaps; t is correct */
2208 * Time of intersection of sphere and triangle. Origin must be outside the
2209 * colliding area. This is a fairly long procedure.
2211 static int spherecast_triangle( v3f tri
[3],
2212 v3f co
, v3f dir
, f32 r
, f32
*t
, v3f n
)
2217 v3_sub( tri
[1], tri
[0], v0
);
2218 v3_sub( tri
[2], tri
[0], v1
);
2219 v3_cross( v0
, v1
, n
);
2221 v3_muladds( tri
[0], n
, r
, sum
[0] );
2222 v3_muladds( tri
[1], n
, r
, sum
[1] );
2223 v3_muladds( tri
[2], n
, r
, sum
[2] );
2226 f32 t_min
= INFINITY
,
2229 if( ray_tri( sum
, co
, dir
, &t1
, 0 ) ){
2230 t_min
= vg_minf( t_min
, t1
);
2235 * Currently disabled; ray_sphere requires |d| = 1. it is not very important.
2238 for( int i
=0; i
<3; i
++ ){
2239 if( ray_sphere( tri
[i
], r
, co
, dir
, &t1
) ){
2240 t_min
= vg_minf( t_min
, t1
);
2246 for( int i
=0; i
<3; i
++ ){
2250 if( ray_uncapped_finite_cylinder( tri
[i0
], tri
[i1
], r
, co
, dir
, &t1
) ){
2255 v3_add( dir
, co
, co1
);
2256 v3_lerp( co
, co1
, t_min
, ct
);
2258 closest_point_segment( tri
[i0
], tri
[i1
], ct
, cx
);
2259 v3_sub( ct
, cx
, n
);
2272 * -----------------------------------------------------------------------------
2273 * Section 5.e Curves
2274 * -----------------------------------------------------------------------------
2277 static void eval_bezier_time( v3f p0
, v3f p1
, v3f h0
, v3f h1
, f32 t
, v3f p
)
2282 v3_muls( p1
, ttt
, p
);
2283 v3_muladds( p
, h1
, 3.0f
*tt
-3.0f
*ttt
, p
);
2284 v3_muladds( p
, h0
, 3.0f
*ttt
-6.0f
*tt
+3.0f
*t
, p
);
2285 v3_muladds( p
, p0
, 3.0f
*tt
-ttt
-3.0f
*t
+1.0f
, p
);
2288 static void eval_bezier3( v3f p0
, v3f p1
, v3f p2
, f32 t
, v3f p
)
2292 v3_muls( p0
, u
*u
, p
);
2293 v3_muladds( p
, p1
, 2.0f
*u
*t
, p
);
2294 v3_muladds( p
, p2
, t
*t
, p
);
2298 * -----------------------------------------------------------------------------
2299 * Section 5.f Volumes
2300 * -----------------------------------------------------------------------------
2303 static f32
vg_sphere_volume( f32 r
){
2304 return (4.0f
/3.0f
) * VG_PIf
* r
*r
*r
;
2307 static f32
vg_box_volume( boxf box
){
2309 v3_sub( box
[1], box
[0], e
);
2310 return e
[0]*e
[1]*e
[2];
2313 static f32
vg_cylinder_volume( f32 r
, f32 h
){
2314 return VG_PIf
* r
*r
* h
;
2317 static f32
vg_capsule_volume( f32 r
, f32 h
){
2318 return vg_sphere_volume( r
) + vg_cylinder_volume( r
, h
-r
*2.0f
);
2321 static void vg_sphere_bound( f32 r
, boxf out_box
){
2322 v3_fill( out_box
[0], -r
);
2323 v3_fill( out_box
[1], r
);
2326 static void vg_capsule_bound( f32 r
, f32 h
, boxf out_box
){
2327 v3_copy( (v3f
){-r
,-h
*0.5f
,r
}, out_box
[0] );
2328 v3_copy( (v3f
){-r
, h
*0.5f
,r
}, out_box
[1] );
2333 * -----------------------------------------------------------------------------
2334 * Section 5.g Inertia Tensors
2335 * -----------------------------------------------------------------------------
2339 * Translate existing inertia tensor
2341 static void vg_translate_inertia( m3x3f inout_inertia
, f32 mass
, v3f d
){
2343 * I = I_0 + m*[(d.d)E_3 - d(X)d]
2346 * I_0: original tensor
2348 * d: translation vector
2349 * (X): outer product
2350 * E_3: identity matrix
2352 m3x3f t
, outer
, scale
;
2353 m3x3_diagonal( t
, v3_dot(d
,d
) );
2354 m3x3_outer_product( outer
, d
, d
);
2355 m3x3_sub( t
, outer
, t
);
2356 m3x3_diagonal( scale
, mass
);
2357 m3x3_mul( scale
, t
, t
);
2358 m3x3_add( inout_inertia
, t
, inout_inertia
);
2362 * Rotate existing inertia tensor
2364 static void vg_rotate_inertia( m3x3f inout_inertia
, m3x3f rotation
){
2369 * I_0: original tensor
2370 * R: rotation matrix
2371 * R^T: tranposed rotation matrix
2375 m3x3_transpose( rotation
, Rt
);
2376 m3x3_mul( rotation
, inout_inertia
, inout_inertia
);
2377 m3x3_mul( inout_inertia
, Rt
, inout_inertia
);
2380 * Create inertia tensor for box
2382 static void vg_box_inertia( boxf box
, f32 mass
, m3x3f out_inertia
){
2384 v3_sub( box
[1], box
[0], e
);
2385 v3_muladds( box
[0], e
, 0.5f
, com
);
2387 f32 ex2
= e
[0]*e
[0],
2390 ix
= (ey2
+ez2
) * mass
* (1.0f
/12.0f
),
2391 iy
= (ex2
+ez2
) * mass
* (1.0f
/12.0f
),
2392 iz
= (ex2
+ey2
) * mass
* (1.0f
/12.0f
);
2394 m3x3_identity( out_inertia
);
2395 m3x3_setdiagonalv3( out_inertia
, (v3f
){ ix
, iy
, iz
} );
2396 vg_translate_inertia( out_inertia
, mass
, com
);
2400 * Create inertia tensor for sphere
2402 static void vg_sphere_inertia( f32 r
, f32 mass
, m3x3f out_inertia
){
2403 f32 ixyz
= r
*r
* mass
* (2.0f
/5.0f
);
2405 m3x3_identity( out_inertia
);
2406 m3x3_setdiagonalv3( out_inertia
, (v3f
){ ixyz
, ixyz
, ixyz
} );
2410 * Create inertia tensor for capsule
2412 static void vg_capsule_inertia( f32 r
, f32 h
, f32 mass
, m3x3f out_inertia
){
2413 f32 density
= mass
/ vg_capsule_volume( r
, h
),
2414 ch
= h
-r
*2.0f
, /* cylinder height */
2415 cm
= VG_PIf
* ch
*r
*r
* density
, /* cylinder mass */
2416 hm
= VG_TAUf
* (1.0f
/3.0f
) * r
*r
*r
* density
, /* hemisphere mass */
2419 ixz
= iy
* 0.5f
+ cm
*ch
*ch
*(1.0f
/12.0f
),
2421 aux0
= (hm
*2.0f
*r
*r
)/5.0f
;
2426 aux2
= aux0
+ hm
*(aux1
*aux1
+ 3.0f
*(1.0f
/8.0f
)*ch
*r
);
2430 m3x3_identity( out_inertia
);
2431 m3x3_setdiagonalv3( out_inertia
, (v3f
){ ixz
, iy
, ixz
} );
2435 * -----------------------------------------------------------------------------
2436 * Section 6.a PSRNG and some distributions
2437 * -----------------------------------------------------------------------------
2440 /* An implementation of the MT19937 Algorithm for the Mersenne Twister
2441 * by Evan Sultanik. Based upon the pseudocode in: M. Matsumoto and
2442 * T. Nishimura, "Mersenne Twister: A 623-dimensionally
2443 * equidistributed uniform pseudorandom number generator," ACM
2444 * Transactions on Modeling and Computer Simulation Vol. 8, No. 1,
2445 * January pp.3-30 1998.
2447 * http://www.sultanik.com/Mersenne_twister
2448 * https://github.com/ESultanik/mtwister/blob/master/mtwister.c
2451 #define MT_UPPER_MASK 0x80000000
2452 #define MT_LOWER_MASK 0x7fffffff
2453 #define MT_TEMPERING_MASK_B 0x9d2c5680
2454 #define MT_TEMPERING_MASK_C 0xefc60000
2456 #define MT_STATE_VECTOR_LENGTH 624
2458 /* changes to STATE_VECTOR_LENGTH also require changes to this */
2459 #define MT_STATE_VECTOR_M 397
2461 typedef struct vg_rand vg_rand
;
2463 u32 mt
[MT_STATE_VECTOR_LENGTH
];
2467 static void vg_rand_seed( vg_rand
*rand
, unsigned long seed
) {
2468 /* set initial seeds to mt[STATE_VECTOR_LENGTH] using the generator
2469 * from Line 25 of Table 1 in: Donald Knuth, "The Art of Computer
2470 * Programming," Vol. 2 (2nd Ed.) pp.102.
2472 rand
->mt
[0] = seed
& 0xffffffff;
2473 for( rand
->index
=1; rand
->index
<MT_STATE_VECTOR_LENGTH
; rand
->index
++){
2474 rand
->mt
[rand
->index
] = (6069 * rand
->mt
[rand
->index
-1]) & 0xffffffff;
2479 * Generates a pseudo-randomly generated long.
2481 static u32
vg_randu32( vg_rand
*rand
) {
2483 /* mag[x] = x * 0x9908b0df for x = 0,1 */
2484 static u32 mag
[2] = {0x0, 0x9908b0df};
2485 if( rand
->index
>= MT_STATE_VECTOR_LENGTH
|| rand
->index
< 0 ){
2486 /* generate STATE_VECTOR_LENGTH words at a time */
2488 if( rand
->index
>= MT_STATE_VECTOR_LENGTH
+1 || rand
->index
< 0 ){
2489 vg_rand_seed( rand
, 4357 );
2491 for( kk
=0; kk
<MT_STATE_VECTOR_LENGTH
-MT_STATE_VECTOR_M
; kk
++ ){
2492 y
= (rand
->mt
[kk
] & MT_UPPER_MASK
) |
2493 (rand
->mt
[kk
+1] & MT_LOWER_MASK
);
2494 rand
->mt
[kk
] = rand
->mt
[kk
+MT_STATE_VECTOR_M
] ^ (y
>>1) ^ mag
[y
& 0x1];
2496 for( ; kk
<MT_STATE_VECTOR_LENGTH
-1; kk
++ ){
2497 y
= (rand
->mt
[kk
] & MT_UPPER_MASK
) |
2498 (rand
->mt
[kk
+1] & MT_LOWER_MASK
);
2500 rand
->mt
[ kk
+(MT_STATE_VECTOR_M
-MT_STATE_VECTOR_LENGTH
)] ^
2501 (y
>> 1) ^ mag
[y
& 0x1];
2503 y
= (rand
->mt
[MT_STATE_VECTOR_LENGTH
-1] & MT_UPPER_MASK
) |
2504 (rand
->mt
[0] & MT_LOWER_MASK
);
2505 rand
->mt
[MT_STATE_VECTOR_LENGTH
-1] =
2506 rand
->mt
[MT_STATE_VECTOR_M
-1] ^ (y
>> 1) ^ mag
[y
& 0x1];
2509 y
= rand
->mt
[rand
->index
++];
2511 y
^= (y
<< 7) & MT_TEMPERING_MASK_B
;
2512 y
^= (y
<< 15) & MT_TEMPERING_MASK_C
;
2518 * Generates a pseudo-randomly generated f64 in the range [0..1].
2520 static inline f64
vg_randf64( vg_rand
*rand
){
2521 return (f64
)vg_randu32(rand
)/(f64
)0xffffffff;
2524 static inline f64
vg_randf64_range( vg_rand
*rand
, f64 min
, f64 max
){
2525 return vg_lerp( min
, max
, (f64
)vg_randf64(rand
) );
2528 static inline void vg_rand_dir( vg_rand
*rand
, v3f dir
){
2529 dir
[0] = vg_randf64(rand
);
2530 dir
[1] = vg_randf64(rand
);
2531 dir
[2] = vg_randf64(rand
);
2533 /* warning: *could* be 0 length.
2534 * very unlikely.. 1 in (2^32)^3. but its mathematically wrong. */
2536 v3_muls( dir
, 2.0f
, dir
);
2537 v3_sub( dir
, (v3f
){1.0f
,1.0f
,1.0f
}, dir
);
2539 v3_normalize( dir
);
2542 static inline void vg_rand_sphere( vg_rand
*rand
, v3f co
){
2543 vg_rand_dir(rand
,co
);
2544 v3_muls( co
, cbrtf( vg_randf64(rand
) ), co
);
2547 static void vg_rand_disc( vg_rand
*rand
, v2f co
){
2548 f32 a
= vg_randf64(rand
) * VG_TAUf
;
2551 v2_muls( co
, sqrtf( vg_randf64(rand
) ), co
);
2554 static void vg_rand_cone( vg_rand
*rand
, v3f out_dir
, f32 angle
){
2555 f32 r
= sqrtf(vg_randf64(rand
)) * angle
* 0.5f
,
2556 a
= vg_randf64(rand
) * VG_TAUf
;
2558 out_dir
[0] = sinf(a
) * sinf(r
);
2559 out_dir
[1] = cosf(a
) * sinf(r
);
2560 out_dir
[2] = cosf(r
);
2563 static void vg_hsv_rgb( v3f hsv
, v3f rgb
){
2564 i32 i
= floorf( hsv
[0]*6.0f
);
2566 f
= hsv
[0] * 6.0f
- (f32
)i
,
2567 p
= v
* (1.0f
-hsv
[1]),
2568 q
= v
* (1.0f
-f
*hsv
[1]),
2569 t
= v
* (1.0f
-(1.0f
-f
)*hsv
[1]);
2572 case 0: rgb
[0] = v
; rgb
[1] = t
; rgb
[2] = p
; break;
2573 case 1: rgb
[0] = q
; rgb
[1] = v
; rgb
[2] = p
; break;
2574 case 2: rgb
[0] = p
; rgb
[1] = v
; rgb
[2] = t
; break;
2575 case 3: rgb
[0] = p
; rgb
[1] = q
; rgb
[2] = v
; break;
2576 case 4: rgb
[0] = t
; rgb
[1] = p
; rgb
[2] = v
; break;
2577 case 5: rgb
[0] = v
; rgb
[1] = p
; rgb
[2] = q
; break;
2581 static void vg_rgb_hsv( v3f rgb
, v3f hsv
){
2582 f32 min
= v3_minf( rgb
),
2583 max
= v3_maxf( rgb
),
2585 k_epsilon
= 0.00001f
;
2588 if( range
< k_epsilon
){
2594 if( max
> k_epsilon
){
2604 hsv
[0] = (rgb
[1]-rgb
[2])/range
;
2605 else if( max
== rgb
[1] )
2606 hsv
[0] = 2.0f
+(rgb
[2]-rgb
[0])/range
;
2608 hsv
[0] = 4.0f
+(rgb
[0]-rgb
[1])/range
;
2610 hsv
[0] = vg_fractf( hsv
[0] * (60.0f
/360.0f
) );