// Util
// ==================================================================================================================
+#define CSR_PIf 3.14159265358979323846264338327950288f
+
+// Simple min/max replacements
float csr_minf( float a, float b )
{
- if( a < b )
- return a;
- return b;
+ return a < b? a: b;
}
float csr_maxf( float a, float b )
{
- if( a > b )
- return a;
- return b;
+ return a > b? a: b;
}
int csr_min( int a, int b )
{
- if( a < b )
- return a;
- return b;
+ return a < b? a: b;
}
int csr_max( int a, int b )
{
- if( a > b )
- return a;
- return b;
+ return a > b? a: b;
}
+// Convert double precision vec3 into single
void v3d_v3f( double a[3], float b[3] )
{
b[0] = a[0];
b[2] = a[2];
}
+// Convert degrees to radians
+float csr_rad( float deg )
+{
+ return deg * CSR_PIf / 180.0f;
+}
+
// Vector 2
// ==================================================================================================================
dest[1] = csr_maxf(a[1], b[1]);
}
+void v2_sub( v2f a, v2f b, v2f d )
+{
+ d[0] = a[0]-b[0]; d[1] = a[1]-b[1];
+}
+
+float v2_cross( v2f a, v2f b )
+{
+ return a[0] * b[1] - a[1] * b[0];
+}
+
+void v2_add( v2f a, v2f b, v2f d )
+{
+ d[0] = a[0]+b[0]; d[1] = a[1]+b[1];
+}
+
+void v2_muls( v2f a, float s, v2f d )
+{
+ d[0] = a[0]*s; d[1] = a[1]*s;
+}
+
+void v2_mul( v2f a, v2f b, v2f d )
+{
+ d[0] = a[0]*b[0]; d[1] = a[1]*b[1];
+}
+
// Vector 3
// ==================================================================================================================
+void v3_zero( v3f a )
+{
+ a[0] = 0.f; a[1] = 0.f; a[2] = 0.f;
+}
+
void v3_copy( v3f a, v3f b )
{
b[0] = a[0]; b[1] = a[1]; b[2] = a[2];
a[2] = v;
}
+// Vector 4
+// ==================================================================================================================
+
+void v4_copy( v4f a, v4f b )
+{
+ b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3];
+}
+
+void v4_zero( v4f a )
+{
+ a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; a[3] = 0.f;
+}
+
// Matrix 3x3
//======================================================================================================
b10 = b[1][0], b11 = b[1][1], b12 = b[1][2],
b20 = b[2][0], b21 = b[2][1], b22 = b[2][2],
b30 = b[3][0], b31 = b[3][1], b32 = b[3][2];
-
- d[0][0] = a00*b00 + a10*b01 + a20*b02 + a30;
- d[0][1] = a01*b00 + a11*b01 + a21*b02 + a31;
- d[0][2] = a02*b00 + a12*b01 + a22*b02 + a32;
- d[1][0] = a00*b10 + a10*b11 + a20*b12 + a30;
- d[1][1] = a01*b10 + a11*b11 + a21*b12 + a31;
- d[1][2] = a02*b10 + a12*b11 + a22*b12 + a32;
- d[2][0] = a00*b20 + a10*b21 + a20*b22 + a30;
- d[2][1] = a01*b20 + a11*b21 + a21*b22 + a31;
- d[2][2] = a02*b20 + a12*b21 + a22*b22 + a32;
+
+ d[0][0] = a00*b00 + a10*b01 + a20*b02;
+ d[0][1] = a01*b00 + a11*b01 + a21*b02;
+ d[0][2] = a02*b00 + a12*b01 + a22*b02;
+ d[1][0] = a00*b10 + a10*b11 + a20*b12;
+ d[1][1] = a01*b10 + a11*b11 + a21*b12;
+ d[1][2] = a02*b10 + a12*b11 + a22*b12;
+ d[2][0] = a00*b20 + a10*b21 + a20*b22;
+ d[2][1] = a01*b20 + a11*b21 + a21*b22;
+ d[2][2] = a02*b20 + a12*b21 + a22*b22;
d[3][0] = a00*b30 + a10*b31 + a20*b32 + a30;
d[3][1] = a01*b30 + a11*b31 + a21*b32 + a31;
d[3][2] = a02*b30 + a12*b31 + a22*b32 + a32;
m4x3_mul( m, t, m );
}
+// Warning: These functions are unoptimized..
+void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point )
+{
+ v3f v;
+ m4x3_mulv( m, point, v );
+
+ v3_minv( box[0], v, box[0] );
+ v3_maxv( box[1], v, box[1] );
+}
+
+void box_concat( boxf a, boxf b )
+{
+ v3_minv( a[0], b[0], a[0] );
+ v3_maxv( a[1], b[1], a[1] );
+}
+
+void box_copy( boxf a, boxf b )
+{
+ v3_copy( a[0], b[0] );
+ v3_copy( a[1], b[1] );
+}
+
+void m4x3_transform_aabb( m4x3f m, boxf box )
+{
+ v3f a; v3f b;
+
+ v3_copy( box[0], a );
+ v3_copy( box[1], b );
+ v3_fill( box[0], INFINITY );
+ v3_fill( box[1], -INFINITY );
+
+ m4x3_expand_aabb_point( m, box, a );
+ m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], a[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], a[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], a[2] } );
+ m4x3_expand_aabb_point( m, box, b );
+ m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], b[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], b[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } );
+}
+
// Planes (double precision)
// ==================================================================================================================
-(p[0]*p[3] * p[0] + p[1]*p[3] * p[1] + p[2]*p[3] * p[2])
;
}
-
-// Raycasting
-// ==================================================================================================================
-
-int csr_slabs( v3f box[2], v3f o, v3f id )
-{
- v3f t0; v3f t1;
- v3f tmin; v3f tmax;
-
- v3_sub( box[0], o, t0 );
- v3_sub( box[1], o, t1 );
- v3_mul( t0, id, t0 );
- v3_mul( t1, id, t1 );
-
- v3_minv( t0, t1, tmin );
- v3_maxv( t0, t1, tmax );
-
- return v3_maxf( tmin ) <= v3_minf( tmax );
-}
-
-float csr_ray_tri( v3f o, v3f d, v3f v0, v3f v1, v3f v2, float *u, float *v )
-{
- float const k_cullEpsilon = 0.0001f;
-
- v3f v0v1;
- v3f v0v2;
- v3f p;
- float det, inv;
-
- v3f tv;
- v3f qv;
-
- v3_sub( v1, v0, v0v1 );
- v3_sub( v2, v0, v0v2 );
- v3_cross( d, v0v2, p );
-
- det = v3_dot( v0v1, p );
-
- if( det < k_cullEpsilon ) return INFINITY;
-
- inv = 1.f / det;
-
- v3_sub( o, v0, tv );
- *u = v3_dot( tv, p ) * inv;
-
- if( *u < 0.f || *u > 1.f ) return INFINITY;
-
- v3_cross( tv, v0v1, qv );
- *v = v3_dot( d, qv ) * inv;
-
- if( *v < 0.f || *u + *v > 1.f ) return INFINITY;
-
- return v3_dot( v0v2, qv ) * inv;
-}