qm3x3,boxf debug, bugfixes
authorhgn <hgodden00@gmail.com>
Sun, 12 Jun 2022 22:42:07 +0000 (23:42 +0100)
committerhgn <hgodden00@gmail.com>
Sun, 12 Jun 2022 22:42:07 +0000 (23:42 +0100)
src/vg/vg.h
src/vg/vg_input.h
src/vg/vg_lines.h
src/vg/vg_m.h
src/vg/vg_platform.h

index 22351ea0253249b92f54218ba841802c34669ab6..a36697b516a77ce3e7ea8298bb2c271438aeb7a9 100644 (file)
@@ -59,11 +59,11 @@ double vg_time,
 
 #include "vg/vg_audio.h"
 #include "vg/vg_shader.h"
-#include "vg/vg_lines.h"
 #include "vg/vg_tex.h"
 #include "vg/vg_input.h"
 #include "vg/vg_ui.h"
 #include "vg/vg_console.h"
+#include "vg/vg_lines.h"
 #include "vg/vg_debug.h"
 
 #ifdef VG_STEAM
index bbc2f84dad13c5dc43e86586044eb1eaa7d7c169..2506ba412d7686cde0b8fba74c863aea89e6eccf 100644 (file)
@@ -45,6 +45,7 @@ static struct button_binding
 {
        const char *name;
        int bind;
+   int controller;
        
        int value; int prev;
 }
@@ -126,22 +127,17 @@ void vg_update_inputs(void)
    {
       vg_gamepad_ready = 0;
    }
-   
 
        /* Update button inputs */
        for( int i = 0; i < vg_list_size( vg_button_binds ); i ++ )
        {
                struct button_binding *binding = vg_button_binds + i;
                binding->prev = binding->value;
-       
-               if( vg_input_mode == k_EInputMode_pc )
-               {
-                       binding->value = get_button_cross_device( binding->bind );
-               }
-               else
-               {
-                       binding->value = vg_gamepad.buttons[ binding->bind ];
-               }
+          
+      if( binding->controller )
+         binding->value = vg_gamepad.buttons[ binding->controller ];
+      else
+         binding->value = get_button_cross_device( binding->bind );
        }
        
        /* Update axis inputs */
index 0263722818b592fa05f4a3db73c1f2f2d80c511c..704d9bde796b28ae16da8005e6d95d20fb376ddc 100644 (file)
@@ -1,5 +1,7 @@
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
+static int debug_lines_enable = 1;
+
 #ifdef VG_3D
  typedef v3f line_co;
 #else
@@ -63,6 +65,14 @@ vg_lines;
 
 static void vg_lines_init(void)
 {
+   vg_convar_push( (struct vg_convar){
+      .name = "debug_lines",
+      .data = &debug_lines_enable,
+      .data_type = k_convar_dtype_i32,
+      .opt_i32 = { .min=0, .max=1, .clamp=1 },
+      .persistent = 1
+   });
+
        SHADER_INIT( vg_line_shader );
        
        glGenVertexArrays( 1, &vg_lines.vao );
@@ -127,7 +137,9 @@ static void vg_lines_drawall( float* projection )
        glEnable( GL_BLEND );
        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
        glBlendEquation( GL_FUNC_ADD );
-       glDrawArrays( GL_LINES, 0, vg_lines.draw_idx );
+
+   if( debug_lines_enable )
+      glDrawArrays( GL_LINES, 0, vg_lines.draw_idx );
        
        glDisable( GL_BLEND );
        vg_lines.draw_idx = 0;
@@ -167,3 +179,44 @@ static void vg_line_box( line_co min, line_co max, u32 colour )
        vg_line( (v2f){max[0],min[1]}, min, colour );
 #endif
 }
+
+static void vg_line_boxf( boxf box, u32 colour )
+{
+   v3f p000, p001, p010, p011, p100, p101, p110, p111;
+
+   p000[0]=box[0][0];p000[1]=box[0][1];p000[2]=box[0][2];
+   p001[0]=box[0][0];p001[1]=box[0][1];p001[2]=box[1][2];
+   p010[0]=box[0][0];p010[1]=box[1][1];p010[2]=box[0][2];
+   p011[0]=box[0][0];p011[1]=box[1][1];p011[2]=box[1][2];
+
+   p100[0]=box[1][0];p100[1]=box[0][1];p100[2]=box[0][2];
+   p101[0]=box[1][0];p101[1]=box[0][1];p101[2]=box[1][2];
+   p110[0]=box[1][0];p110[1]=box[1][1];p110[2]=box[0][2];
+   p111[0]=box[1][0];p111[1]=box[1][1];p111[2]=box[1][2];
+   
+   vg_line( p000, p001, colour );
+   vg_line( p001, p011, colour );
+   vg_line( p011, p010, colour );
+   vg_line( p010, p000, colour );
+
+   vg_line( p100, p101, colour );
+   vg_line( p101, p111, colour );
+   vg_line( p111, p110, colour );
+   vg_line( p110, p100, colour );
+
+   vg_line( p100, p000, colour );
+   vg_line( p101, p001, colour );
+   vg_line( p110, p010, colour );
+   vg_line( p111, p011, colour );
+}
+
+static void vg_line_pt3( v3f pt, float size, u32 colour )
+{
+   boxf box =
+   {
+      { pt[0]-size, pt[1]-size, pt[2]-size },
+      { pt[0]+size, pt[1]+size, pt[2]+size }
+   };
+
+   vg_line_boxf( box, colour );
+}
index 9f934163e42bd7c5204566eeb11eb637f93e4206..c71f3abd5af081f077c6547855d55de8a1841208 100644 (file)
@@ -103,7 +103,7 @@ static inline float v2_dot( v2f a, v2f b )
 
 static inline float v2_cross( v2f a, v2f b )
 {
-       return a[0] * b[1] - a[1] * b[0];
+       return a[0]*b[1] - a[1]*b[0];
 }
 
 static inline void v2_add( v2f a, v2f b, v2f d )
@@ -237,11 +237,13 @@ static inline float v3_dot( v3f a, v3f b )
        return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
 }
 
-static inline void v3_cross( v3f a, v3f b, v3f d )
+static inline void v3_cross( v3f a, v3f b, v3f dest )
 {
-  d[0] = a[1] * b[2] - a[2] * b[1];
-  d[1] = a[2] * b[0] - a[0] * b[2];
-  d[2] = a[0] * b[1] - a[1] * b[0];
+   v3f d;
+   d[0] = a[1]*b[2] - a[2]*b[1];
+   d[1] = a[2]*b[0] - a[0]*b[2];
+   d[2] = a[0]*b[1] - a[1]*b[0];
+   v3_copy( d, dest );
 }
 
 static inline float v3_length2( v3f a )
@@ -743,6 +745,12 @@ static inline void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point )
        v3_maxv( box[1], v, box[1] );
 }
 
+static inline void box_addpt( boxf a, v3f pt )
+{
+       v3_minv( a[0], pt, a[0] );
+       v3_maxv( a[1], pt, a[1] );
+}
+
 static inline void box_concat( boxf a, boxf b )
 {
        v3_minv( a[0], b[0], a[0] );
@@ -755,6 +763,12 @@ static inline void box_copy( boxf a, boxf b )
        v3_copy( a[1], b[1] );
 }
 
+static inline void box_init_inf( boxf box )
+{
+       v3_fill( box[0],  INFINITY );
+       v3_fill( box[1], -INFINITY );
+}
+
 static inline void m4x3_transform_aabb( m4x3f m, boxf box )
 {
        v3f a; v3f b;
@@ -780,7 +794,7 @@ static inline void m4x3_lookat( m4x3f m, v3f pos, v3f target, v3f up )
    v3_sub( target, pos, dir );
    v3_normalize( dir );
 
-   v3_negate( dir, m[2] );
+   v3_copy( dir, m[2] );
 
    v3_cross( up, m[2], m[0] );
    v3_normalize( m[0] );
@@ -1016,3 +1030,46 @@ static inline void q_m3x3( v4f q, m3x3f d )
    d[2][1] = yz - wx;
    d[0][2] = xz - wy;
 }
+
+static void m3x3_q( m3x3f m, v4f q )
+{
+   float diag, r, rinv;
+
+   diag = m[0][0] + m[1][1] + m[2][2];
+   if( diag >= 0.0f )
+   {
+      r    = sqrtf( 1.0f + diag );
+      rinv = 0.5f / r;
+      q[0] = rinv * (m[1][2] - m[2][1]);
+      q[1] = rinv * (m[2][0] - m[0][2]);
+      q[2] = rinv * (m[0][1] - m[1][0]);
+      q[3] = r    * 0.5f;
+   } 
+   else if( m[0][0] >= m[1][1] && m[0][0] >= m[2][2] )
+   {
+      r    = sqrtf( 1.0f - m[1][1] - m[2][2] + m[0][0] );
+      rinv = 0.5f / r;
+      q[0] = r    * 0.5f;
+      q[1] = rinv * (m[0][1] + m[1][0]);
+      q[2] = rinv * (m[0][2] + m[2][0]);
+      q[3] = rinv * (m[1][2] - m[2][1]);
+   } 
+   else if( m[1][1] >= m[2][2] )
+   {
+      r    = sqrtf( 1.0f - m[0][0] - m[2][2] + m[1][1] );
+      rinv = 0.5f / r;
+      q[0] = rinv * (m[0][1] + m[1][0]);
+      q[1] = r    * 0.5f;
+      q[2] = rinv * (m[1][2] + m[2][1]);
+      q[3] = rinv * (m[2][0] - m[0][2]);
+   } 
+   else 
+   {
+      r    = sqrtf( 1.0f - m[0][0] - m[1][1] + m[2][2] );
+      rinv = 0.5f / r;
+      q[0] = rinv * (m[0][2] + m[2][0]);
+      q[1] = rinv * (m[1][2] + m[2][1]);
+      q[2] = r    * 0.5f;
+      q[3] = rinv * (m[0][1] - m[1][0]);
+   }
+}
index 7e70d81178a9cdb8599165d0249645e3563ac8f8..b0794baa7d955ea0b161e9011a0b1d46baf3eb80 100644 (file)
@@ -22,7 +22,7 @@ typedef float         v3f[3];
 typedef float          v4f[4];
 typedef v2f                    m2x2f[2];
 typedef v3f                    m3x3f[3];
-typedef v3f                    m4x3f[4];   /* TODO why this is 4x4 too? */
+typedef v3f                    m4x3f[4];
 typedef v4f       m4x4f[4];
 typedef v3f                    boxf[2];