From 0aea2ef68a5ed32fc940673402a1b4b67f38d4d3 Mon Sep 17 00:00:00 2001 From: hgn Date: Wed, 1 Mar 2023 05:25:58 +0000 Subject: [PATCH] heebie --- vg.h | 6 ++-- vg_build.h | 2 +- vg_build_utils_shader.h | 16 +++++++-- vg_console.h | 2 +- vg_input.h | 22 +++++++++++- vg_lines.h | 35 +++++++++++++++++++ vg_log.h | 4 +++ vg_m.h | 37 ++++++++++++++++++++ vg_shader.h | 75 +++++++++++++++++++---------------------- 9 files changed, 148 insertions(+), 51 deletions(-) diff --git a/vg.h b/vg.h index 25dc65e..b7475ad 100644 --- 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 ))) { diff --git a/vg_build.h b/vg_build.h index 53de6c3..fbfbf95 100644 --- a/vg_build.h +++ b/vg_build.h @@ -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" ); diff --git a/vg_build_utils_shader.h b/vg_build_utils_shader.h index 77ce897..1c81c41 100644 --- a/vg_build_utils_shader.h +++ b/vg_build_utils_shader.h @@ -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; iarray ) continue; + if( uf->array ) + continue; for( int j=0; jaxis.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 ) { diff --git a/vg_lines.h b/vg_lines.h index 12e8d96..9300445 100644 --- a/vg_lines.h +++ b/vg_lines.h @@ -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; diff --git a/vg_log.h b/vg_log.h index 9f3ae9e..d30e7ba 100644 --- a/vg_log.h +++ b/vg_log.h @@ -16,6 +16,10 @@ #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 4844eeb..3c78466 100644 --- 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 ); diff --git a/vg_shader.h b/vg_shader.h index 2ee1ef4..50e8bd0 100644 --- a/vg_shader.h +++ b/vg_shader.h @@ -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; -- 2.25.1