simplify gitignore
[vg.git] / src / vg / vg_m.h
index 8ecadbdbdf1a19eddaa336f5c6f82efc2a4fa1d9..8e49fe55e330f4adb99d2291ae552c56fbf55274 100644 (file)
@@ -1,4 +1,11 @@
-/* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+
+#ifndef VG_M_H
+#define VG_M_H
+
+#include "vg_platform.h"
+#include <math.h>
+#include <stdlib.h>
 
 #define VG_PIf  3.14159265358979323846264338327950288f
 #define VG_TAUf 6.28318530717958647692528676655900576f
@@ -23,14 +30,9 @@ 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 int vg_randint(int max)
+static inline float vg_fractf( float a )
 {
-   return rand()%max;
+   return a - floorf( a );
 }
 
 static float stable_force( float current, float diff )
@@ -43,9 +45,6 @@ static float stable_force( float current, float diff )
    return fnew;
 }
 
-#define VG_MIN( A, B ) ((A)<(B)?(A):(B))
-#define VG_MAX( A, B ) ((A)>(B)?(A):(B))
-
 static inline int vg_min( int a, int b )
 {
        return a < b? a: b;
@@ -69,6 +68,11 @@ static inline void v2_copy( v2f a, v2f b )
        b[0] = a[0]; b[1] = a[1];
 }
 
+static inline void v2_zero( v2f a )
+{
+       a[0] = 0.f; a[1] = 0.f;
+}
+
 static inline void v2i_copy( v2i a, v2i b )
 {
        b[0] = a[0]; b[1] = a[1];
@@ -184,7 +188,14 @@ static inline void v2_lerp( v2f a, v2f b, float t, v2f d )
 
 static inline void v2_normalize( v2f a )
 {
-       v2_muls( a, 1.f / v2_length( a ), a );
+       v2_muls( a, 1.0f / v2_length( a ), a );
+}
+
+static void v2_normalize_clamp( v2f a )
+{
+   float l2 = v2_length2( a );
+   if( l2 > 1.0f )
+      v2_muls( a, 1.0f/sqrtf(l2), a );
 }
 
 static inline void v2_floor( v2f a, v2f b )
@@ -294,6 +305,19 @@ static inline float vg_lerpf( float a, float b, float t )
        return a + t*(b-a);
 }
 
+static inline double vg_lerp( double a, double b, double t )
+{
+       return a + t*(b-a);
+}
+
+/* correctly lerp around circular period -pi -> pi */
+static float vg_alerpf( float a, float b, float t )
+{
+   float d = fmodf( b-a, VG_TAUf ),
+         s = fmodf( 2.0f*d, VG_TAUf ) - d;
+   return a + s*t;
+}
+
 static inline void v3_lerp( v3f a, v3f b, float t, v3f d )
 {
        d[0] = a[0] + t*(b[0]-a[0]);
@@ -631,6 +655,45 @@ static inline void m4x3_invert_affine( m4x3f a, m4x3f b )
    v3_negate( b[3], b[3] );
 }
 
+static void m4x3_invert_full( m4x3f src, m4x3f dst )
+{
+  float t2, t4, t5,
+        det,
+        a = src[0][0], b = src[0][1], c = src[0][2],
+        e = src[1][0], f = src[1][1], g = src[1][2],
+        i = src[2][0], j = src[2][1], k = src[2][2],
+        m = src[3][0], n = src[3][1], o = src[3][2];
+
+   t2 = j*o - n*k;
+   t4 = i*o - m*k;
+   t5 = i*n - m*j;
+   
+   dst[0][0] =  f*k - g*j;
+   dst[1][0] =-(e*k - g*i);
+   dst[2][0] =  e*j - f*i;
+   dst[3][0] =-(e*t2 - f*t4 + g*t5);
+   
+   dst[0][1] =-(b*k - c*j);
+   dst[1][1] =  a*k - c*i;
+   dst[2][1] =-(a*j - b*i);
+   dst[3][1] =  a*t2 - b*t4 + c*t5;
+   
+   t2 = f*o - n*g;
+   t4 = e*o - m*g; 
+   t5 = e*n - m*f;
+   
+   dst[0][2] =  b*g - c*f ;
+   dst[1][2] =-(a*g - c*e );
+   dst[2][2] =  a*f - b*e ;
+   dst[3][2] =-(a*t2 - b*t4 + c * t5);
+
+   det = 1.0f / (a * dst[0][0] + b * dst[1][0] + c * dst[2][0]);
+   v3_muls( dst[0], det, dst[0] );
+   v3_muls( dst[1], det, dst[1] );
+   v3_muls( dst[2], det, dst[2] );
+   v3_muls( dst[3], det, dst[3] );
+}
+
 static inline void m4x3_copy( m4x3f a, m4x3f b )
 {
        v3_copy( a[0], b[0] );
@@ -892,29 +955,29 @@ static inline void m4x3_lookat( m4x3f m, v3f pos, v3f target, v3f up )
                                                                { 0.0f, 0.0f, 0.0f, 0.0f }}
 
 static void m4x4_projection( m4x4f m, float angle,
-      float ratio, float near, float far )
+      float ratio, float fnear, float ffar )
 {
-   float scale = tanf( angle * 0.5f * VG_PIf / 180.0f ) * near,
+   float scale = tanf( angle * 0.5f * VG_PIf / 180.0f ) * fnear,
          r = ratio * scale,
          l = -r,
          t = scale,
          b = -t;
 
-   m[0][0] =  2.0f * near / (r - l); 
+   m[0][0] =  2.0f * fnear / (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][1] =  2.0f * fnear / (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][2] = -(ffar + fnear) / (ffar - fnear); 
    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][2] = -2.0f * ffar * fnear / (ffar - fnear); 
    m[3][3] =  0.0f;
 } 
 
@@ -1164,6 +1227,19 @@ static inline void q_inv( v4f q, v4f d )
    d[3] =  q[3]*s;
 }
 
+static inline void q_nlerp( v4f a, v4f b, float t, v4f d )
+{
+   if( v4_dot(a,b) < 0.0f )
+   {
+      v4_muls( b, -1.0f, d );
+      v4_lerp( a, d, t, d );
+   }
+   else
+      v4_lerp( a, b, t, d );
+
+   q_normalize( d );
+}
+
 static inline void q_m3x3( v4f q, m3x3f d )
 {
    float
@@ -1272,3 +1348,44 @@ static int ray_tri( v3f tri[3], v3f co, v3f dir, float *dist )
    }
    else return 0;
 }
+
+static inline float vg_randf(void)
+{
+   return (float)rand()/(float)(RAND_MAX);
+}
+
+static inline void vg_rand_dir(v3f dir)
+{
+   dir[0] = vg_randf();
+   dir[1] = vg_randf();
+   dir[2] = vg_randf();
+
+   v3_muls( dir, 2.0f, dir );
+   v3_sub( dir, (v3f){1.0f,1.0f,1.0f}, dir );
+
+   v3_normalize( dir );
+}
+
+static inline void vg_rand_sphere( v3f co )
+{
+   vg_rand_dir(co);
+   v3_muls( co, cbrtf( vg_randf() ), co );
+}
+
+static inline int vg_randint(int max)
+{
+   return rand()%max;
+}
+
+static void eval_bezier_time( v3f p0, v3f p1, v3f h0, v3f h1, float t, v3f p )
+{
+   float tt = t*t,
+         ttt = tt*t;
+
+   v3_muls( p1, ttt, p );
+   v3_muladds( p, h1, 3.0f*tt  -3.0f*ttt, p );
+   v3_muladds( p, h0, 3.0f*ttt -6.0f*tt  +3.0f*t, p );
+   v3_muladds( p, p0, 3.0f*tt  -ttt -3.0f*t +1.0f, p );
+}
+
+#endif /* VG_M_H */