-// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
-
-// Util
-// ==================================================================================================================
+/* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */
#define VG_PIf 3.14159265358979323846264338327950288f
#define VG_TAUf 6.28318530717958647692528676655900576f
-// Simple min/max replacements
static inline float vg_minf( float a, float b )
{
return a < b? a: b;
return vg_minf( max, vg_maxf( a, min ) );
}
+static inline float vg_signf( float a )
+{
+ return a < 0.0f? -1.0f: 1.0f;
+}
+
+static inline float vg_randf(void)
+{
+ return (float)rand()/(float)(RAND_MAX);
+}
+
+static inline float vg_randint(int max)
+{
+ return rand()%max;
+}
+
#define VG_MIN( A, B ) ((A)<(B)?(A):(B))
#define VG_MAX( A, B ) ((A)>(B)?(A):(B))
-// Hopefully deprecate this!!
static inline int vg_min( int a, int b )
{
return a < b? a: b;
return a > b? a: b;
}
-// Convert degrees to radians
static inline float vg_rad( float deg )
{
return deg * VG_PIf / 180.0f;
}
-// Vector 2
-// ==================================================================================================================
-
+/*
+ * Vector 3
+ */
static inline void v2_copy( v2f a, v2f b )
{
b[0] = a[0]; b[1] = a[1];
d[0] = a[0]-b[0]; d[1] = a[1]-b[1];
}
+static inline float v2_dot( v2f a, v2f b )
+{
+ return a[0] * b[0] + a[1] * b[1];
+}
+
static inline float v2_cross( v2f a, v2f b )
{
return a[0] * b[1] - a[1] * b[0];
d[0] = a[0]/s; d[1] = a[1]/s;
}
-
static inline void v2_mul( v2f a, v2f b, v2f d )
{
d[0] = a[0]*b[0];
d[1] = a[1] + t*(b[1]-a[1]);
}
-// Vector 3
-// ==================================================================================================================
+static inline void v2_normalize( v2f a )
+{
+ v2_muls( a, 1.f / v2_length( a ), a );
+}
+/*
+ * Vector 3
+ */
static inline void v3_zero( v3f a )
{
a[0] = 0.f; a[1] = 0.f; a[2] = 0.f;
d[0] = a[0]+b[0]*s; d[1] = a[1]+b[1]*s; d[2] = a[2]+b[2]*s;
}
+static inline void v3_muladd( v2f a, v2f b, v2f s, v2f d )
+{
+ d[0] = a[0]+b[0]*s[0];
+ d[1] = a[1]+b[1]*s[1];
+ d[2] = a[2]+b[2]*s[2];
+}
+
static inline float v3_dot( v3f a, v3f b )
{
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
a[2] = v;
}
-// Vector 4
-// ==================================================================================================================
+static inline void v3_floor( v3f a, v3f b )
+{
+ b[0] = floorf( a[0] );
+ b[1] = floorf( a[1] );
+ b[2] = floorf( a[2] );
+}
+
+static inline void v3_ceil( v3f a, v3f b )
+{
+ b[0] = ceilf( a[0] );
+ b[1] = ceilf( a[1] );
+ b[2] = ceilf( a[2] );
+}
+
+static inline void v3_negate( v3f a, v3f b )
+{
+ b[0] = -a[0];
+ b[1] = -a[1];
+ b[2] = -a[2];
+}
+
+static inline void v3_rotate( v3f v, float angle, v3f axis, v3f d )
+{
+ v3f v1, v2, k;
+ float c, s;
+ c = cosf( angle );
+ s = sinf( angle );
+
+ v3_copy( axis, k );
+ v3_normalize( k );
+ v3_muls( v, c, v1 );
+ v3_cross( k, v, v2 );
+ v3_muls( v2, s, v2 );
+ v3_add( v1, v2, v1 );
+ v3_muls( k, v3_dot(k, v) * (1.0f - c), v2);
+ v3_add( v1, v2, d );
+}
+
+/*
+ * Vector 4
+ */
static inline void v4_copy( v4f a, v4f b )
{
b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3];
a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; a[3] = 0.f;
}
-// Matrix 2x2
-// ===========================================================================================================
+static inline void v4_muladds( v3f a, v3f b, float s, v3f d )
+{
+ d[0] = a[0]+b[0]*s;
+ d[1] = a[1]+b[1]*s;
+ d[2] = a[2]+b[2]*s;
+ d[3] = a[3]+b[3]*s;
+}
+
+static inline void v4_lerp( v4f a, v4f b, float t, v4f d )
+{
+ d[0] = a[0] + t*(b[0]-a[0]);
+ d[1] = a[1] + t*(b[1]-a[1]);
+ d[2] = a[2] + t*(b[2]-a[2]);
+ d[3] = a[3] + t*(b[3]-a[3]);
+}
+
+static inline float v4_dot( v4f a, v4f b )
+{
+ return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*a[3];
+}
+
+static inline float v4_length( v4f a )
+{
+ return sqrtf( v4_dot(a,a) );
+}
+
+/*
+ * Matrix 2x2
+ */
#define M2X2_INDENTIY {{1.0f, 0.0f, }, \
{ 0.0f, 1.0f, }}
a[1][1] = c;
}
-// Matrix 3x3
-//======================================================================================================
+/*
+ * Matrix 3x3
+ */
#define M3X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\
{ 0.0f, 1.0f, 0.0f, },\
v3_copy( res, d );
}
-static inline void m3x3_projection( m3x3f dst, float const left, float const right, float const bottom, float const top )
+static inline void m3x3_projection( m3x3f dst,
+ float const left, float const right, float const bottom, float const top )
{
float rl, tb;
m[1][2] = m02 * -s + m12 * c;
}
-// Matrix 4x3
-// ==================================================================================================================
+/*
+ * Matrix 4x3
+ */
#define M4X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\
{ 0.0f, 1.0f, 0.0f, },\
v3_copy( res, d );
}
-// Affine transforms
-// ====================================================================================================================
+/*
+ * Affine transforms
+ */
static inline void m4x3_translate( m4x3f m, v3f v )
{
m4x3_mul( m, t, m );
}
-// Warning: These functions are unoptimized..
+static inline void m4x3_expand( m4x3f m, m4x4f d )
+{
+ v3_copy( m[0], d[0] );
+ v3_copy( m[1], d[1] );
+ v3_copy( m[2], d[2] );
+ v3_copy( m[3], d[3] );
+ d[0][3] = 0.0f;
+ d[1][3] = 0.0f;
+ d[2][3] = 0.0f;
+ d[3][3] = 1.0f;
+}
+
static inline void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point )
{
v3f v;
m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } );
}
-// Planes (double precision)
-// ==================================================================================================================
+static inline void m4x3_lookat( m4x3f m, v3f pos, v3f target, v3f up )
+{
+ v3f dir;
+ v3_sub( target, pos, dir );
+ v3_normalize( dir );
+
+ v3_negate( dir, m[2] );
+
+ v3_cross( up, m[2], m[0] );
+ v3_normalize( m[0] );
-static inline void tri_to_plane( double a[3], double b[3], double c[3], double p[4] )
+ v3_cross( m[2], m[0], m[1] );
+ v3_copy( pos, m[3] );
+}
+
+/*
+ * Matrix 4x4
+ */
+
+#define M4X4_IDENTITY {{1.0f, 0.0f, 0.0f, 0.0f },\
+ { 0.0f, 1.0f, 0.0f, 0.0f },\
+ { 0.0f, 0.0f, 1.0f, 0.0f },\
+ { 0.0f, 0.0f, 0.0f, 1.0f }}
+
+static void m4x4_projection( m4x4f m, float angle,
+ float ratio, float near, float far )
+{
+ float scale = tanf( angle * 0.5f * VG_PIf / 180.0f ) * near,
+ r = ratio * scale,
+ l = -r,
+ t = scale,
+ b = -t;
+
+ m[0][0] = 2.0f * near / (r - l);
+ m[0][1] = 0.0f;
+ m[0][2] = 0.0f;
+ m[0][3] = 0.0f;
+ m[1][0] = 0.0f;
+ m[1][1] = 2.0f * near / (t - b);
+ m[1][2] = 0.0f;
+ m[1][3] = 0.0f;
+ m[2][0] = (r + l) / (r - l);
+ m[2][1] = (t + b) / (t - b);
+ m[2][2] = -(far + near) / (far - near);
+ m[2][3] = -1.0f;
+ m[3][0] = 0.0f;
+ m[3][1] = 0.0f;
+ m[3][2] = -2.0f * far * near / (far - near);
+ m[3][3] = 0.0f;
+}
+
+static void m4x4_translate( m4x4f m, v3f v )
+{
+ v4_muladds( m[3], m[0], v[0], m[3] );
+ v4_muladds( m[3], m[1], v[1], m[3] );
+ v4_muladds( m[3], m[2], v[2], m[3] );
+}
+
+static inline void m4x4_copy( m4x4f a, m4x4f b )
+{
+ v4_copy( a[0], b[0] );
+ v4_copy( a[1], b[1] );
+ v4_copy( a[2], b[2] );
+ v4_copy( a[3], b[3] );
+}
+
+static inline void m4x4_identity( m4x4f a )
+{
+ m4x4f id = M4X4_IDENTITY;
+ m4x4_copy( id, a );
+}
+
+static inline void m4x4_mul( m4x4f a, m4x4f b, m4x4f d )
+{
+ float a00 = a[0][0], a01 = a[0][1], a02 = a[0][2], a03 = a[0][3],
+ a10 = a[1][0], a11 = a[1][1], a12 = a[1][2], a13 = a[1][3],
+ a20 = a[2][0], a21 = a[2][1], a22 = a[2][2], a23 = a[2][3],
+ a30 = a[3][0], a31 = a[3][1], a32 = a[3][2], a33 = a[3][3],
+
+ b00 = b[0][0], b01 = b[0][1], b02 = b[0][2], b03 = b[0][3],
+ b10 = b[1][0], b11 = b[1][1], b12 = b[1][2], b13 = b[1][3],
+ b20 = b[2][0], b21 = b[2][1], b22 = b[2][2], b23 = b[2][3],
+ b30 = b[3][0], b31 = b[3][1], b32 = b[3][2], b33 = b[3][3];
+
+ d[0][0] = a00*b00 + a10*b01 + a20*b02 + a30*b03;
+ d[0][1] = a01*b00 + a11*b01 + a21*b02 + a31*b03;
+ d[0][2] = a02*b00 + a12*b01 + a22*b02 + a32*b03;
+ d[0][3] = a03*b00 + a13*b01 + a23*b02 + a33*b03;
+ d[1][0] = a00*b10 + a10*b11 + a20*b12 + a30*b13;
+ d[1][1] = a01*b10 + a11*b11 + a21*b12 + a31*b13;
+ d[1][2] = a02*b10 + a12*b11 + a22*b12 + a32*b13;
+ d[1][3] = a03*b10 + a13*b11 + a23*b12 + a33*b13;
+ d[2][0] = a00*b20 + a10*b21 + a20*b22 + a30*b23;
+ d[2][1] = a01*b20 + a11*b21 + a21*b22 + a31*b23;
+ d[2][2] = a02*b20 + a12*b21 + a22*b22 + a32*b23;
+ d[2][3] = a03*b20 + a13*b21 + a23*b22 + a33*b23;
+ d[3][0] = a00*b30 + a10*b31 + a20*b32 + a30*b33;
+ d[3][1] = a01*b30 + a11*b31 + a21*b32 + a31*b33;
+ d[3][2] = a02*b30 + a12*b31 + a22*b32 + a32*b33;
+ d[3][3] = a03*b30 + a13*b31 + a23*b32 + a33*b33;
+}
+
+/*
+ * Planes (double precision)
+ */
+static inline void tri_to_plane( double a[3], double b[3],
+ double c[3], double p[4] )
{
double edge0[3];
double edge1[3];
p[2] = p[2] / l;
}
-static inline int plane_intersect( double a[4], double b[4], double c[4], double p[4] )
+static inline int plane_intersect( double a[4], double b[4],
+ double c[4], double p[4] )
{
double const epsilon = 1e-8f;
-(p[0]*p[3] * p[0] + p[1]*p[3] * p[1] + p[2]*p[3] * p[2])
;
}
+
+/* Quaternions */
+
+static inline void q_identity( v4f q )
+{
+ q[0] = 0.0f; q[1] = 0.0f; q[2] = 0.0f; q[3] = 1.0f;
+}
+
+static inline void q_axis_angle( v4f q, v3f axis, float angle )
+{
+ float a = angle*0.5f,
+ c = cosf(a),
+ s = sinf(a);
+
+ q[0] = s*axis[0];
+ q[1] = s*axis[1];
+ q[2] = s*axis[2];
+ q[3] = c;
+}
+
+static inline void q_mul( v4f q, v4f q1, v4f d )
+{
+ v4f t;
+ t[0] = q[3]*q1[0] + q[0]*q1[3] + q[1]*q1[2] - q[2]*q1[1];
+ t[1] = q[3]*q1[1] - q[0]*q1[2] + q[1]*q1[3] + q[2]*q1[0];
+ t[2] = q[3]*q1[2] + q[0]*q1[1] - q[1]*q1[0] + q[2]*q1[3];
+ t[3] = q[3]*q1[3] - q[0]*q1[0] - q[1]*q1[1] - q[2]*q1[2];
+ v4_copy( t, d );
+}
+
+static inline void q_normalize( v4f q )
+{
+ float s = 1.0f/ sqrtf(v4_dot(q,q));
+ q[0] *= s;
+ q[1] *= s;
+ q[2] *= s;
+ q[3] *= s;
+}
+
+static inline void q_inv( v4f q, v4f d )
+{
+ float s = 1.0f / v4_dot(q,q);
+ d[0] = -q[0]*s;
+ d[1] = -q[1]*s;
+ d[2] = -q[2]*s;
+ d[3] = q[3]*s;
+}
+
+static inline void q_m3x3( v4f q, m3x3f d )
+{
+ float
+ l = v4_length(q),
+ s = l > 0.0f? 2.0f/l: 0.0f,
+
+ xx = s*q[0]*q[0], xy = s*q[0]*q[1], wx = s*q[3]*q[0],
+ yy = s*q[1]*q[1], yz = s*q[1]*q[2], wy = s*q[3]*q[1],
+ zz = s*q[2]*q[2], xz = s*q[0]*q[2], wz = s*q[3]*q[2];
+
+ d[0][0] = 1.0f - yy - zz;
+ d[1][1] = 1.0f - xx - zz;
+ d[2][2] = 1.0f - xx - yy;
+ d[0][1] = xy + wz;
+ d[1][2] = yz + wx;
+ d[2][0] = xz + wy;
+ d[1][0] = xy - wz;
+ d[2][1] = yz - wx;
+ d[0][2] = xz - wy;
+}