heebie
authorhgn <hgodden00@gmail.com>
Wed, 1 Mar 2023 05:25:58 +0000 (05:25 +0000)
committerhgn <hgodden00@gmail.com>
Wed, 1 Mar 2023 05:25:58 +0000 (05:25 +0000)
vg.h
vg_build.h
vg_build_utils_shader.h
vg_console.h
vg_input.h
vg_lines.h
vg_log.h
vg_m.h
vg_shader.h

diff --git a/vg.h b/vg.h
index 25dc65ecda9fe173b754daa9adcce05a9c76cb55..b7475ad1afc2577d99e16504c6be82da8a0dcc4e 100644 (file)
--- a/vg.h
+++ b/vg.h
@@ -373,13 +373,11 @@ VG_STATIC void vg_bake_shaders(void)
 {
    vg_acquire_thread_sync();
 
-#if 0
    vg_function_push( (struct vg_cmd)
    {
-      .name = "shaders",
+      .name = "reload_shaders",
       .function = vg_shaders_live_recompile
    });
-#endif
 
    vg_shaders_compile();
    vg_release_thread_sync();
@@ -716,7 +714,7 @@ VG_STATIC void _vg_init_window( const char *window_name )
                                      SDL_WINDOWPOS_UNDEFINED,
                                      vg.window_x, vg.window_y,
 
-                                     SDL_WINDOW_FULLSCREEN | 
+                                     SDL_WINDOW_FULLSCREEN_DESKTOP | 
                                      SDL_WINDOW_OPENGL |
                                      SDL_WINDOW_INPUT_GRABBED )))
    {
index 53de6c3edacc3cc3ac8b3415b5223c5dfbe54540..fbfbf95062e56f5c744f36f58685abd4ecfb4895 100644 (file)
@@ -214,7 +214,7 @@ void vg_build(void)
 
    /* Warnings */
    strcat( cmd, 
-      "   -Wall -ferror-limit=0\\\n"
+      "   -Wall\\\n"
       "     -Wno-unused-function -Wno-unused-variable\\\n"
       "     -Wno-unused-command-line-argument -Wno-unused-but-set-variable\\\n"
    );
index 77ce897e4f32297706aa692b2b9ff670b10742c5..1c81c41822dd0532a4273c14bebdae3d97a3ea54 100644 (file)
@@ -35,6 +35,8 @@ static void vg_shader_set_include_dir( char *dir )
 static void parse_uniform_name( char *start, struct uniform *uf )
 {
    uf->array = 0;
+   int type_set = 0;
+
    for( int i=0;; i++ )
    {
       if( start[i] == '\0' )
@@ -56,7 +58,12 @@ static void parse_uniform_name( char *start, struct uniform *uf )
       if( start[i] == ' ' )
       {
          start[i] = '\0';
-         strncpy( uf->type, start, sizeof(uf->type) );
+
+         if( !type_set )
+         {
+            strncpy( uf->type, start, sizeof(uf->type) );
+            type_set = 1;
+         }
          start = start+i+1;
          i=0;
       }
@@ -80,7 +87,9 @@ static int compile_subshader( FILE *header, char *name )
    else
    {
       fprintf( header, "{\n"
-                       ".static_src = \n" );
+                       ".orig_file = \"%s\",\n" 
+                       ".static_src = \n",
+                       name );
 
       char *cur = full, *start = full;
       while( 1 )
@@ -199,7 +208,8 @@ int vg_build_shader( char *src_vert, /* path/to/vert.vs    */
    for( int i=0; i<vg_shaderbuild.uniform_count; i++ )
    {
       struct uniform *uf = &vg_shaderbuild.uniform_buffer[i];
-      if( uf->array ) continue;
+      if( uf->array ) 
+         continue;
 
       for( int j=0; j<vg_list_size(types); j ++ )
       {
index 5a137e3f05222366239791a68866745d474a0fda..26c6a6d20c00414eef330fa18f7500b4caf52dd9 100644 (file)
@@ -76,7 +76,7 @@ struct vg_console
 
                int persistent; /* Should this var be stored to cfg/auto.conf? */
        } 
-       vars[ 64 ];
+       vars[ 128 ];
        
        struct vg_cmd
        {
index 1052c42937f1c61ce535b8199d8857494b08e57d..af0ac6882d9ff285cdbe6e95ba91d71a930553a8 100644 (file)
@@ -59,6 +59,7 @@ struct input_binding
       {
          SDL_GameControllerButton gamepad_id;
          SDL_Keycode              keyboard_id;
+         int                      mouse_id;
          int value, prev;
       }
       button;
@@ -97,6 +98,7 @@ VG_STATIC void vg_create_unnamed_input( struct input_binding *bind,
    bind->axis.keyboard_negative = -1;
    bind->button.gamepad_id = -1;
    bind->button.keyboard_id = -1;
+   bind->button.mouse_id = -1;
 }
 
 VG_STATIC struct input_binding *vg_create_named_input( const char *name,
@@ -115,6 +117,7 @@ VG_STATIC struct input_binding *vg_create_named_input( const char *name,
    bind->axis.keyboard_negative = -1;
    bind->button.gamepad_id = -1;
    bind->button.keyboard_id = -1;
+   bind->button.mouse_id = -1;
 
    return bind;
 }
@@ -179,7 +182,10 @@ vg_all_bindable_inputs[] =
  {k_input_type_gamepad_button, "gp-dpad-left", SDL_CONTROLLER_BUTTON_DPAD_LEFT},
  {k_input_type_gamepad_button,"gp-dpad-right",SDL_CONTROLLER_BUTTON_DPAD_RIGHT},
  {k_input_type_gamepad_button, "gp-dpad-up", SDL_CONTROLLER_BUTTON_DPAD_UP},
- {k_input_type_gamepad_button, "\2gp-menu", SDL_CONTROLLER_BUTTON_BACK}
+ {k_input_type_gamepad_button, "\2gp-menu", SDL_CONTROLLER_BUTTON_BACK},
+
+ {k_input_type_mouse_button,   "mouse1", SDL_BUTTON_LEFT },
+ {k_input_type_mouse_button,   "mouse2", SDL_BUTTON_RIGHT }
 };
 
 VG_STATIC const char *vg_input_to_str( u32 input, enum input_type input_type )
@@ -289,6 +295,9 @@ VG_STATIC void vg_print_binding_info( struct input_binding *bind )
       vg_info( "      keyboard_id: %s\n",
          vg_input_to_str(bind->button.keyboard_id,
                          k_input_type_keyboard_key));
+      vg_info( "      mouse_id: %s\n",
+         vg_input_to_str(bind->button.mouse_id,
+                         k_input_type_mouse_button));
    }
 }
 
@@ -331,6 +340,8 @@ VG_STATIC void vg_apply_bind_str( struct input_binding *bind,
 
       if( type == k_input_type_keyboard_key )
          bind->button.keyboard_id = id;
+      else if( type == k_input_type_mouse_button )
+         bind->button.mouse_id = id;
       else if( type == k_input_type_gamepad_button )
          bind->button.gamepad_id = id;
       else
@@ -486,7 +497,16 @@ VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
                vg_input.controller_buttons[ bind->button.gamepad_id ];
 
          if( bind->button.keyboard_id != -1 )
+         {
             bind->button.value |= vg_getkey( bind->button.keyboard_id );
+         }
+
+         if( bind->button.mouse_id != -1 )
+         {
+            if( SDL_GetMouseState(NULL, NULL) & 
+                                    SDL_BUTTON( bind->button.mouse_id ) )
+               bind->button.value |= 1;
+         }
       }
       else if( bind->type == k_input_type_axis )
       {
index 12e8d969f6df17005a084fe101ee699a9e5092c9..9300445a8b35dc21816a76571ba10256e7d55cdd 100644 (file)
@@ -178,6 +178,41 @@ VG_STATIC void vg_line( line_co from, line_co to, u32 colour )
        vg_line2( from, to, colour, colour );
 }
 
+VG_STATIC void line_tangent_basis( v3f n, v3f tx, v3f ty )
+{
+   /* Compute tangent basis (box2d) */
+   if( fabsf( n[0] ) >= 0.57735027f )
+   {
+      tx[0] =  n[1];
+      tx[1] = -n[0];
+      tx[2] =  0.0f;
+   }
+   else
+   {
+      tx[0] =  0.0f;
+      tx[1] =  n[2];
+      tx[2] = -n[1];
+   }
+
+   v3_normalize( tx );
+   v3_cross( n, tx, ty );
+}
+
+VG_STATIC void vg_line_arrow( line_co co, line_co dir, float size, u32 colour )
+{
+   v3f p1, tx, ty, p2, p3;
+   v3_muladds( co, dir, size, p1 );
+   line_tangent_basis( dir, tx, ty );
+
+   v3_muladds( p1, dir, -size * 0.125f, p2 );
+   v3_muladds( p2, ty,  size * 0.125f, p3 );
+   v3_muladds( p2, ty, -size * 0.125f, p2 );
+
+   vg_line( co, p1, colour );
+   vg_line( p1, p2, colour );
+   vg_line( p1, p3, colour );
+}
+
 VG_STATIC void vg_line_boxf( boxf box, u32 colour )
 {
    v3f p000, p001, p010, p011, p100, p101, p110, p111;
index 9f3ae9e0976c0a4bb62bba7dcb9affe2cefc159a..d30e7ba330bb827d9884a63c1acf05a53bebf477 100644 (file)
--- a/vg_log.h
+++ b/vg_log.h
 #define KCYN  "\x1B[36m"
 #define KWHT  "\x1B[37m"
 
+#define PRINTF_v2f( V2 ) "%.2f %.2f\n",           V2[0], V2[1]
+#define PRINTF_v3f( V3 ) "%.2f %.2f %.2f\n",      V3[0], V3[1], V3[2]
+#define PRINTF_v4f( V4 ) "%.2f %.2f %.2f %.2f\n", V4[0], V4[1], V4[2], V4[3]
+
 #ifdef VG_GAME
 static SDL_SpinLock log_print_sl;
 #endif
diff --git a/vg_m.h b/vg_m.h
index 4844eeb20b2664eb5521a86065f309f92cb2ce5a..3c78466b68ca046f23890a14aaf033bf36f3be55 100644 (file)
--- a/vg_m.h
+++ b/vg_m.h
@@ -1302,6 +1302,22 @@ int plane_intersect2( v4f a, v4f b, v3f p, v3f n )
    return 1;
 }
 
+static int plane_segment( v4f plane, v3f a, v3f b, v3f co )
+{
+   float d0 = v3_dot( a, plane ) - plane[3],
+         d1 = v3_dot( b, plane ) - plane[3];
+
+   if( d0*d1 < 0.0f )
+   {
+      float tot = 1.0f/( fabsf(d0)+fabsf(d1) );
+
+      v3_muls( a, fabsf(d1) * tot, co );
+      v3_muladds( co, b, fabsf(d0) * tot, co );
+      return 1;
+   }
+
+   return 0;
+}
 
 static inline double plane_polarity( double p[4], double a[3] )
 {
@@ -1455,6 +1471,18 @@ static void m3x3_q( m3x3f m, v4f q )
    }
 }
 
+static void q_mulv( v4f q, v3f v, v3f d )
+{
+   v3f v1, v2;
+
+   v3_muls( q, 2.0f*v3_dot(q,v), v1 );
+   v3_muls( v, q[3]*q[3] - v3_dot(q,q), v2 );
+   v3_add( v1, v2, v1 );
+   v3_cross( q, v, v2 );
+   v3_muls( v2, 2.0f*q[3], v2 );
+   v3_add( v1, v2, d );
+}
+
 enum contact_type
 {
    k_contact_type_default,
@@ -1549,6 +1577,15 @@ VG_STATIC float closest_segment_segment( v3f p1, v3f q1, v3f p2, v3f q2,
    return v3_length2( v0 );
 }
 
+VG_STATIC int point_inside_aabb( boxf box, v3f point )
+{
+   if((point[0]<=box[1][0]) && (point[1]<=box[1][1]) && (point[2]<=box[1][2]) &&
+      (point[0]>=box[0][0]) && (point[1]>=box[0][1]) && (point[2]>=box[0][2]) )
+      return 1;
+   else
+      return 0;
+}
+
 VG_STATIC void closest_point_aabb( v3f p, boxf box, v3f dest )
 {
    v3_maxv( p, box[0], dest );
index 2ee1ef4f457143f7acbd73d92787f5639071d679..50e8bd0e291589a17d32b17fe1c11f0038e9234e 100644 (file)
@@ -7,14 +7,12 @@
 #include "vg/vg.h"
 #include "vg/vg_platform.h"
 
-#if 0
 #define STB_INCLUDE_IMPLEMENTATION
 #define STB_INCLUDE_LINE_GLSL
 #define STB_MALLOC  vg_alloc
 #define STB_FREE    vg_free
 #define STB_REALLOC vg_realloc
-#include "stb/stb_include.h"
-#endif
+#include "submodules/stb/stb_include.h"
 
 const char *vg_shader_gl_ver = "#version 330 core\n";
 
@@ -73,71 +71,66 @@ VG_STATIC GLuint vg_shader_subshader( const char *src, GLint gliShaderType )
 
 VG_STATIC int vg_shader_compile( struct vg_shader *shader )
 {
-       vg_info( "Compile shader '%s'\n", shader->name );
-       
        GLuint program, vert, frag;
-   const char *svs, *sfs;
-   char *avs, *afs;
 
-   int static_src = 1;
-   
    /* If we are compiling this again, we obviously need to try to take the src
     * from the disk instead.
     *
     * Only do this if we have filenames set on the shader, so engine shaders 
     * dont have to do it (text.. etc).
     */
+
+   int use_source_files = 0;
    if( shader->compiled )
    {
       if( shader->vs.orig_file && shader->fs.orig_file )
-         static_src = 0;
-      else return 1;
+      {
+         use_source_files = 1;
+      }
+      else 
+      {
+         vg_warn( "No source files for shader '%s'\n", shader->name );
+         return 1;
+      }
    }
 
-   if( static_src )
-   {
-      svs = shader->vs.static_src;
-      sfs = shader->fs.static_src;
-   }
-   else
-   {
-      vg_fatal_exit_loop( "Unimplemented" );
+       vg_info( "Compile shader '%s'\n", shader->name );
 
-#if 0
+   if( use_source_files )
+   {
       char error[260];
       char path[260];
-      strcpy( path, shader->vs.orig_file );
-      avs = stb_include_file( path, "", "../../shaders", error );
 
-      strcpy( path, shader->fs.orig_file );
-      afs = stb_include_file( path, "", "../../shaders", error );
+      strcpy( path, "../../" );
+      strcat( path, shader->vs.orig_file );
+      char *vertex_src = stb_include_file( path, "", "../../shaders", error );
+
+      strcpy( path, "../../" );
+      strcat( path, shader->fs.orig_file );
+      char *fragment_src = stb_include_file( path, "", "../../shaders", error );
    
-      if( !avs || !afs )
+      if( !vertex_src || !fragment_src )
       {
          vg_error( "Could not find shader source files (%s)\n",
-               shader->vs.orig_file );
+                   shader->vs.orig_file );
 
-         vg_free( avs );
-         vg_free( afs );
+         free( vertex_src );
+         free( fragment_src );
          return 0;
       }
 
-      svs = avs;
-      sfs = afs;
-#endif
+      vert = vg_shader_subshader( vertex_src,   GL_VERTEX_SHADER );
+      frag = vg_shader_subshader( fragment_src, GL_FRAGMENT_SHADER );
+
+      free( vertex_src );
+      free( fragment_src );
    }
-       
-       vert = vg_shader_subshader( svs, GL_VERTEX_SHADER );
-       frag = vg_shader_subshader( sfs, GL_FRAGMENT_SHADER );
-       
-#if 0
-   if( !static_src )
+   else
    {
-      free( avs );
-      free( afs );
+      vert = vg_shader_subshader( shader->vs.static_src, GL_VERTEX_SHADER );
+      frag = vg_shader_subshader( shader->fs.static_src, GL_FRAGMENT_SHADER );
    }
-#endif
-
+       
        if( !vert || !frag )
                return 0;