audio rework pt 1
[vg.git] / src / vg / vg_m.h
index a97101e6d0ac94b5012cd69f93f8dcf67d7a3eba..5bbb46733bbb0482d532d36d885d35ad4effecad 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
@@ -28,16 +35,6 @@ static inline float vg_fractf( float a )
    return a - floorf( a );
 }
 
-static inline float vg_randf(void)
-{
-   return (float)rand()/(float)(RAND_MAX);
-}
-
-static inline int vg_randint(int max)
-{
-   return rand()%max;
-}
-
 static float stable_force( float current, float diff )
 {
    float fnew = current + diff;
@@ -1174,6 +1171,12 @@ 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 )
+{
+   v4_lerp( a, b, t, d );
+   q_normalize( d );
+}
+
 static inline void q_m3x3( v4f q, m3x3f d )
 {
    float
@@ -1282,3 +1285,33 @@ 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;
+}
+
+#endif /* VG_M_H */