From bd3188f0fe52c9231f79af85d6cfaef2576b9f83 Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 6 Sep 2021 21:12:09 +0100 Subject: [PATCH] refactor & vg_m --- .gitmodules | 3 - cglm | 1 - fishladder.c | 275 +++++---------------- vg/vg.h | 32 ++- vg/vg_lines.h | 19 +- vg/vg_m.h | 622 +++++++++++++++++++++++++++++++++++++++++++++++ vg/vg_platform.h | 12 +- 7 files changed, 722 insertions(+), 242 deletions(-) delete mode 160000 cglm create mode 100644 vg/vg_m.h diff --git a/.gitmodules b/.gitmodules index 40f448a..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "cglm"] - path = cglm - url = https://github.com/recp/cglm.git diff --git a/cglm b/cglm deleted file mode 160000 index d0d1e46..0000000 --- a/cglm +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d0d1e4653399a5d7b3a72c01903275590adf935e diff --git a/fishladder.c b/fishladder.c index 89bc1d8..5ad95d6 100644 --- a/fishladder.c +++ b/fishladder.c @@ -10,13 +10,12 @@ SHADER_DEFINE( colour_shader, // VERTEX "layout (location=0) in vec2 a_co;" - "uniform mat4 uPv;" - "uniform mat4 uMdl;" + "uniform mat3 uPv;" + "uniform mat3 uMdl;" "" "void main()" "{" - " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );" - " gl_Position = vert_pos;" + " gl_Position = vec4( uPv * uMdl * vec3( a_co, 1.0 ), 1.0 );" "}", // FRAGMENT @@ -37,7 +36,7 @@ SHADER_DEFINE( tilemap_shader, "layout (location=0) in vec2 a_co;" // XY "layout (location=1) in vec2 a_offset;" // XY offset "layout (location=2) in vec4 a_data;" // atlas-uv, amt, other - "uniform mat4 uPv;" + "uniform mat3 uPv;" "uniform vec2 uOrigin;" "" "out vec4 aCoords;" @@ -45,8 +44,8 @@ SHADER_DEFINE( tilemap_shader, "void main()" "{" "vec2 world_coord = a_co+a_offset+uOrigin;" - "gl_Position = uPv * vec4( world_coord.x, 0.0, world_coord.y, 1.0 );" - "aCoords = vec4((a_co*0.98+0.01 + vec2(a_data.x,0.0)) * 0.0625, a_data.zw);" + "gl_Position = vec4( uPv * vec3( world_coord, 1.0 ), 1.0 );" + "aCoords = vec4( (a_co*0.98+0.01 + vec2(a_data.x,0.0)) * 0.0625, a_data.zw );" "}", // FRAGMENT @@ -63,7 +62,10 @@ SHADER_DEFINE( tilemap_shader, "vec4 ripple = texture( uTexRipples, vec2( glyph.x - uTime, glyph.y ));" "float wstatus = -((aCoords.z/128.0)-1.0);" // -1 := None in, 0.0 := Hold middle, 1.0 := All out "float dev = step( 0.0, glyph.x+wstatus ) * step( glyph.x+wstatus, 1.0 );" //+ abs(glyph.y-0.5) );" - "vec3 composite = mix( vec3(0.5,0.5,0.5), vec3(0.3,0.6,1.0) + ripple.xyz * 0.05, step( 0.75, glyph.a )*dev ) * glyph.z;" + + // TODO: Fix texture instead of doing this.. + "float shadow = mix( 1.0, glyph.z, step(0.25, glyph.w) );" + "vec3 composite = mix( vec3(0.5,0.5,0.5), vec3(0.3,0.6,1.0) + ripple.xyz * 0.05, step( 0.75, glyph.a )*dev ) * shadow;" "FragColor = vec4( composite, 1.0 );" "}" , @@ -74,7 +76,7 @@ SHADER_DEFINE( fish_shader, // VERTEX "layout (location=0) in vec2 a_co;" // XY UV - "uniform mat4 uPv;" + "uniform mat3 uPv;" "uniform vec3 uPosition;" "out vec2 aTexCoords;" @@ -82,7 +84,7 @@ SHADER_DEFINE( fish_shader, "void main()" "{" "vec2 world_coord = a_co + uPosition.xy;" - "gl_Position = uPv * vec4( world_coord.x, 0.0, world_coord.y, 1.0 );" + "gl_Position = vec4( uPv * vec3( world_coord, 1.0 ), 1.0 );" "aTexCoords = vec2( (a_co.x + uPosition.z) * 0.125, a_co.y );" "}", // FRAGMENT @@ -101,9 +103,9 @@ SHADER_DEFINE( fish_shader, UNIFORMS({ "uPv", "uTexMain", "uColour", "uPosition" }) ) -mat4 m_projection; -mat4 m_view; -mat4 m_mdl; +m3x3f m_projection; +m3x3f m_view; +m3x3f m_mdl; int main( int argc, char *argv[] ) { @@ -151,7 +153,7 @@ static struct fishes[ 20 ]; int num_fishes; - vec3 origin; + v2f origin; struct cell *selected; int select_valid; int playing; @@ -196,21 +198,21 @@ static void map_free(void) map.io = NULL; } -static struct cell *map_tile( int pos[2] ) +static struct cell *ptile( int pos[2] ) { return map.cells + pos[1]*map.x + pos[0]; } -static struct cell *map_tile_at( int pos[2] ) +static struct cell *gettile( int pos[2] ) { if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y ) return map.cells + pos[1]*map.x + pos[0]; return NULL; } -static struct cell *map_tile_at_cond( int pos[2], u32 flags ) +static struct cell *getiftile( int pos[2], u32 flags ) { - struct cell *cell = map_tile_at( pos ); + struct cell *cell = gettile( pos ); if( cell && (cell->flags & flags) ) return cell; @@ -250,7 +252,7 @@ static struct cell *map_stack_next(void) if( frame->i < 3 ) { int *dir = output_dirs[ frame->i ]; - tile = map_tile_at_cond( (int[2]){frame->x+dir[0], frame->y+dir[1]}, CELL_FLAG_WALKABLE ); + tile = getiftile( (int[2]){frame->x+dir[0], frame->y+dir[1]}, CELL_FLAG_WALKABLE ); if( tile && !(tile->flags & CELL_FLAG_VISITED) ) { @@ -318,10 +320,10 @@ static void map_update_visual(void) { struct cell *a, *b, *c, *d; - a = map_tile_at_cond( (int[2]){ x,y+1 }, CELL_FLAG_WALKABLE ); - b = map_tile_at_cond( (int[2]){ x+1,y }, CELL_FLAG_WALKABLE ); - c = map_tile_at_cond( (int[2]){ x,y-1 }, CELL_FLAG_WALKABLE ); - d = map_tile_at_cond( (int[2]){ x-1,y }, CELL_FLAG_WALKABLE ); + a = getiftile( (int[2]){ x,y+1 }, CELL_FLAG_WALKABLE ); + b = getiftile( (int[2]){ x+1,y }, CELL_FLAG_WALKABLE ); + c = getiftile( (int[2]){ x,y-1 }, CELL_FLAG_WALKABLE ); + d = getiftile( (int[2]){ x-1,y }, CELL_FLAG_WALKABLE ); u32 config = (a?0x1:0x0) | (b?0x2:0x0) | (c?0x4:0x0) | (d?0x8:0x0); cellbytes[ 0 ] = config; @@ -347,7 +349,7 @@ static void map_update_visual(void) { map_tile_coords_from_index( map.io[i], inputcoord ); map_stack_init( inputcoord ); - struct cell *cell = map_tile_at( inputcoord ); + struct cell *cell = gettile( inputcoord ); int cr[2]; @@ -362,9 +364,9 @@ static void map_update_visual(void) int outrate = outflow == 128? k_rate_flow: 0; struct cell *a, *b, *d; - a = map_tile( (int[2]){ cr[0], cr[1]+1 } ); - b = map_tile( (int[2]){ cr[0]+1, cr[1] } ); - d = map_tile( (int[2]){ cr[0]-1, cr[1] } ); + a = gettile( (int[2]){ cr[0], cr[1]+1 } ); + b = gettile( (int[2]){ cr[0]+1, cr[1] } ); + d = gettile( (int[2]){ cr[0]-1, cr[1] } ); int compute_l = 0, compute_r = 0; @@ -423,7 +425,7 @@ static int map_load( const char *str ) { map_free(); - char *c = str; + char const *c = str; // Scan for width for(;; map.x ++) @@ -538,7 +540,7 @@ static int map_load( const char *str ) // Origin top left corner map.origin[0] = -((float)map.x) * 0.5f; - map.origin[2] = -((float)map.y) * 0.5f; + map.origin[1] = -((float)map.y) * 0.5f; float *offset_array = (float *)malloc( map.x*map.y*2*sizeof(float) ); @@ -560,34 +562,24 @@ static int map_load( const char *str ) return 1; } +// Determines if tile at position co is contentious static int map_tile_availible( int co[2] ) { - // Extract 5x5 grid surrounding tile + // Extract tiles area of influence as a 5x5 grid in bitfield format u32 blob = 0x1000; for( int y = vg_max( co[1]-2, 0 ); y < vg_min( map.y, co[1]+3 ); y ++ ) for( int x = vg_max( co[0]-2, 0 ); x < vg_min( map.x, co[0]+3 ); x ++ ) - { - struct cell *cell = map_tile( (int[2]){ x, y } ); - - if( cell && (cell->flags & CELL_FLAG_WALKABLE) ) + if( getiftile( (int[2]){ x, y }, CELL_FLAG_WALKABLE ) ) blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2)); - } // Run filter over center 3x3 grid to check for invalid configurations + // The kernel codes both the offsets for reaching each tile inside it, as well as the offsets + // to move the kernel as a whole. int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 }; for( int i = 0; i < vg_list_size(kernel); i ++ ) - { if( blob & (0x1 << (6+kernel[i])) ) { - // (reference window: 0x1CE7) Illegal moves - // 0100011100010 ; - // 0000001100011 ; - // 0000011000110 ; - // 0110001100000 ; - // 1100011000000 ; - // 0100001100010 ; - // 0100011000010 ; - + // Illegal moves list in bitfield format u32 invalid[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 }; u32 window = blob >> kernel[i]; @@ -595,7 +587,6 @@ static int map_tile_availible( int co[2] ) if((window & invalid[j]) == invalid[j]) return 0; } - } return 1; } @@ -605,13 +596,11 @@ void vg_update(void) // Update camera float ratio = (float)vg_window_y / (float)vg_window_x; float const size = 7.5f; - glm_ortho( -size, size, -size*ratio, size*ratio, 0.01f, 150.f, m_projection ); - - glm_mat4_identity( m_view ); - glm_translate_z( m_view, -10.f ); - glm_rotate_x( m_view, 1.5708f, m_view ); - glm_mat4_mul( m_projection, m_view, vg_pv ); + m3x3_projection( m_projection, -size, size, size*ratio, -size*ratio ); + m3x3_identity( m_view ); + m3x3_mul( m_projection, m_view, vg_pv ); + vg_projection_update(); // Compute map update for( int y = 0; y < map.y; y ++ ) @@ -619,18 +608,18 @@ void vg_update(void) for( int x = 0; x < map.x; x ++ ) { struct cell *tile, *upper, *lower, *l, *r; - tile = map_tile_at( (int [2]){ x, y } ); + tile = gettile( (int [2]){ x, y } ); tile->flags &= ~(CELL_FLAG_SPLIT|CELL_FLAG_MERGE|CELL_FLAG_UPLVL); if( tile->flags & CELL_FLAG_WALKABLE ) { - r = map_tile_at_cond( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE ); - l = map_tile_at_cond( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE ); + r = getiftile( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE ); + l = getiftile( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE ); if( r && l ) { - upper = map_tile_at_cond( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE ); - lower = map_tile_at_cond( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE ); + upper = getiftile( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE ); + lower = getiftile( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE ); if( upper ) { @@ -647,170 +636,21 @@ void vg_update(void) } } } - - // Compute classification - /* - map_stack_refresh(); - - for( int i = 0; i < arrlen( map.io ); i ++ ) - { - struct *cell cell = &map.cells[ map.io ]; - int coords[2]; - if( cell->flags & CELL_FLAG_INPUT ) - { - map_tile_coords_from_index( map.io, coords ); - map_stack_init( coords ); - - do - { - if( cell->flags & CELL_FLAG_CONNECTOR ) - { - - } - } - while( (cell = map_stack_next()) ); - } - }*/ - - // Get mouse ray - vec3 ray_origin; - vec3 ray_dir; - - mat4 pv_inverse; - vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y }; - glm_mat4_inv( vg_pv, pv_inverse ); - glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir ); - glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin ); - glm_vec3_sub( ray_dir, ray_origin, ray_dir ); - - // Get floor tile intersection - float ray_t = -ray_origin[1] / ray_dir[1]; - - vec3 tile_pos; - glm_vec3_copy( ray_origin, tile_pos ); - glm_vec3_muladds( ray_dir, ray_t, tile_pos ); - glm_vec3_sub( tile_pos, map.origin, tile_pos ); + v2f tile_pos; + v2_copy( vg_mouse_ws, tile_pos ); + v2_sub( tile_pos, map.origin, tile_pos ); int tile_x = floorf( tile_pos[0] ); - int tile_y = floorf( tile_pos[2] ); + int tile_y = floorf( tile_pos[1] ); - map.selected = map_tile_at( (int [2]){tile_x, tile_y} ); + map.selected = ptile( (int [2]){tile_x, tile_y} ); if( map.playing ) { - static int fish_counter = 0; - fish_counter ++; - - if( fish_counter > 20 ) - { - fish_counter = 0; - - // Advance characters - for( int i = 0; i < map.num_fishes; i ++ ) - { - struct fish *fish = map.fishes + i; - - if( !fish->alive ) - continue; - - struct cell *tile, *next; - tile = map_tile_at( fish->co ); - - if( tile->flags & CELL_FLAG_OUTPUT ) - { - vg_info( "Fish got zucced (%d)\n", i ); - fish->alive = 0; - continue; - } - - int die = 0; - if( tile->flags & CELL_FLAG_SPLIT ) - { - die = 1; - int new_dir[][2] = { {0,-1},{1,0},{-1,0} }; - int *test_dir; - - for( int j = 0; j < 3; j ++ ) - { - test_dir = new_dir[ tile->state ]; - tile->state = (tile->state+1)%3; - - next = map_tile_at( (int[2]){ fish->co[0]+test_dir[0], fish->co[1]+test_dir[1] } ); - if( next && (next->flags & (CELL_FLAG_WALKABLE)) ) - { - fish->dir[0] = test_dir[0]; - fish->dir[1] = test_dir[1]; - die = 0; - break; - } - } - } - - next = map_tile_at( (int[2]){ fish->co[0]+fish->dir[0], fish->co[1]+fish->dir[1] } ); - if( !next || (next && !(next->flags & CELL_FLAG_WALKABLE)) ) - { - // Try UP - die = 1; - } - - if( die ) - { - vg_info( "Fish died! (%d)\n", i ); - fish->alive = 0; - continue; - } - - - fish->co[0] += fish->dir[0]; - fish->co[1] += fish->dir[1]; - } - - // Try spawn fish - for( int i = 0; i < arrlen( map.io ); i ++ ) - { - struct cell *input = &map.cells[ map.io[i] ]; - - if( input->flags & CELL_FLAG_INPUT ) - { - if( input->state < arrlen( input->conditions ) ) - { - struct fish *fish = &map.fishes[ map.num_fishes ]; - map_tile_coords_from_index( map.io[i], fish->co ); - - int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} }; - int can_spawn = 0; - - for( int i = 0; i < vg_list_size( output_dirs ); i ++ ) - { - int *dir = output_dirs[i]; - struct cell *next = map_tile_at( (int[2]){ fish->co[0]+dir[0], fish->co[1]+dir[1] } ); - if( next && next->flags & CELL_FLAG_CANAL ) - { - fish->dir[0] = dir[0]; - fish->dir[1] = dir[1]; - can_spawn = 1; - } - } - - if( can_spawn ) - { - fish->alive = 1; - input->state ++; - map.num_fishes ++; - } - } - } - } - - vg_info( "There are now %u active fish\n", map.num_fishes ); - } - if( vg_get_button_down( "go" ) ) { map.playing = 0; - map.num_fishes = 0; - vg_info( "Ending!\n" ); } } @@ -819,11 +659,6 @@ void vg_update(void) if( vg_get_button_down( "go" ) ) { map.playing = 1; - - // Reset everything - for( int i = 0; i < map.x*map.y; i ++ ) - map.cells[ i ].state = 0; - vg_info( "Starting!\n" ); } @@ -866,7 +701,7 @@ void vg_render(void) map_update_visual(); SHADER_USE( tilemap_shader ); - glUniformMatrix4fv( SHADER_UNIFORM( tilemap_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv ); + glUniformMatrix3fv( SHADER_UNIFORM( tilemap_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv ); glUniform1i( SHADER_UNIFORM( tilemap_shader, "uTexTiles" ), 0 ); glActiveTexture( GL_TEXTURE0 ); @@ -876,13 +711,14 @@ void vg_render(void) glActiveTexture( GL_TEXTURE1 ); glBindTexture( GL_TEXTURE_2D, map.flow_texture ); - glUniform2f( SHADER_UNIFORM( tilemap_shader, "uOrigin" ), map.origin[0], map.origin[2] ); + glUniform2fv( SHADER_UNIFORM( tilemap_shader, "uOrigin" ), 1, map.origin ); glUniform1f( SHADER_UNIFORM( tilemap_shader, "uTime" ), vg_time * 0.5f ); glDrawArraysInstanced( GL_TRIANGLES, 0, 6, map.x*map.y ); + /* SHADER_USE( fish_shader ); - glUniformMatrix4fv( SHADER_UNIFORM( fish_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv ); + glUniformMatrix3fv( SHADER_UNIFORM( fish_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv ); glUniform1i( SHADER_UNIFORM( fish_shader, "uTexMain" ), 0 ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, fish_texture ); @@ -896,6 +732,7 @@ void vg_render(void) glDrawArrays( GL_TRIANGLES, 0, 6 ); glDisable( GL_BLEND ); + */ } void vg_register(void) diff --git a/vg/vg.h b/vg/vg.h index c6b8fb9..b35091b 100644 --- a/vg/vg.h +++ b/vg/vg.h @@ -15,14 +15,14 @@ #include "stb/stb_ds.h" #include "stb/stb_image.h" -#include "cglm/include/cglm/cglm.h" +#include "vg/vg_platform.h" void vg_register_exit( void( *funcptr )(void), const char *name ); void vg_exiterr( const char *strErr ); -mat4 vg_pv; +m3x3f vg_pv; -#include "vg/vg_platform.h" +#include "vg/vg_m.h" #include "vg/vg_io.h" #include "vg/vg_audio.h" #include "vg/vg_shader.h" @@ -41,8 +41,8 @@ GLFWwindow* vg_window; int vg_window_x = 1280; int vg_window_y = 720; -float vg_mouse_x; -float vg_mouse_y; +v2f vg_mouse; +v3f vg_mouse_ws; float vg_time; float vg_time_last; @@ -240,8 +240,8 @@ void vg_exiterr( const char *strErr ) void vg_mouse_callback( GLFWwindow* ptrW, double xpos, double ypos ) { - vg_mouse_x = xpos; - vg_mouse_y = ypos; + vg_mouse[0] = xpos; + vg_mouse[1] = ypos; } void vg_scroll_callback( GLFWwindow* ptrW, double xoffset, double yoffset ) @@ -380,5 +380,23 @@ static void vg_init( int argc, char *argv[], const char *window_name ) vg_exit(); } +// Screen projections +// ============================================================================================ + +void vg_projection_update(void) +{ + // Do transform local->world + vg_mouse_ws[0] = vg_mouse[0]; + vg_mouse_ws[1] = vg_mouse[1]; + vg_mouse_ws[2] = 1.0f; + + vg_mouse_ws[0] = (2.0f * vg_mouse_ws[0]) / ((float)vg_window_x) - 1.0f; + vg_mouse_ws[1] = -((2.0f * vg_mouse_ws[1]) / ((float)vg_window_y) - 1.0f); + + m3x3f inverse; + m3x3_inv( vg_pv, inverse ); + m3x3_mulv( inverse, vg_mouse_ws, vg_mouse_ws ); +} + u32 NvOptimusEnablement = 0x00000001; int AmdPowerXpressRequestHighPerformance = 1; diff --git a/vg/vg_lines.h b/vg/vg_lines.h index 81b0091..18a0a7c 100644 --- a/vg/vg_lines.h +++ b/vg/vg_lines.h @@ -3,15 +3,15 @@ SHADER_DEFINE( vg_line_shader, // VERTEX - "layout (location=0) in vec3 a_co;" + "layout (location=0) in vec2 a_co;" "layout (location=1) in vec4 a_colour;" - "uniform mat4 uPv;" + "uniform mat3 uPv;" "" "out vec4 s_colour;" "" "void main()" "{" - " vec4 vert_pos = uPv * vec4( a_co, 1.0 );" + " vec4 vert_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );" " s_colour = a_colour;" " gl_Position = vert_pos;" "}", @@ -33,14 +33,13 @@ struct { struct vg_lines_vert { - vec3 co; + v2f co; u32 colour; } *buffer; GLuint vao, vbo; u32 draw_idx, cap, buffer_size; - } vg_lines; @@ -93,7 +92,7 @@ static void vg_lines_free(void) static void vg_lines_drawall(void) { SHADER_USE( vg_line_shader ); - glUniformMatrix4fv( SHADER_UNIFORM( vg_line_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv ); + glUniformMatrix3fv( SHADER_UNIFORM( vg_line_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv ); glBindVertexArray( vg_lines.vao ); glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); @@ -109,18 +108,18 @@ static void vg_lines_drawall(void) vg_lines.draw_idx = 0; } -static void vg_line2( vec3 from, vec3 to, u32 fc, u32 tc ) +static void vg_line2( v2f from, v2f to, u32 fc, u32 tc ) { struct vg_lines_vert *v = vg_lines.buffer + vg_lines.draw_idx; - glm_vec3_copy( from, v[0].co ); - glm_vec3_copy( to, v[1].co ); + v2_copy( from, v[0].co ); + v2_copy( to, v[1].co ); v[0].colour = fc; v[1].colour = tc; vg_lines.draw_idx += 2; } -static void vg_line( vec3 from, vec3 to, u32 colour ) +static void vg_line( v2f from, v2f to, u32 colour ) { vg_line2( from, to, colour, colour ); } diff --git a/vg/vg_m.h b/vg/vg_m.h new file mode 100644 index 0000000..de9cb6d --- /dev/null +++ b/vg/vg_m.h @@ -0,0 +1,622 @@ +// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved + +// Util +// ================================================================================================================== + +#define VG_PIf 3.14159265358979323846264338327950288f + +// Simple min/max replacements +static inline float vg_minf( float a, float b ) +{ + return a < b? a: b; +} + +static inline float vg_maxf( float a, float b ) +{ + return a > b? a: b; +} + +static inline int vg_min( int a, int b ) +{ + return a < b? a: b; +} + +static inline int vg_max( int a, int b ) +{ + return a > b? a: b; +} + +// Convert degrees to radians +static inline float vg_rad( float deg ) +{ + return deg * VG_PIf / 180.0f; +} + +// Vector 2 +// ================================================================================================================== + +static inline void v2_copy( v2f a, v2f b ) +{ + b[0] = a[0]; b[1] = a[1]; +} + +static inline void v2_minv( v2f a, v2f b, v2f dest ) +{ + dest[0] = vg_minf(a[0], b[0]); + dest[1] = vg_minf(a[1], b[1]); +} + +static inline void v2_maxv( v2f a, v2f b, v2f dest ) +{ + dest[0] = vg_maxf(a[0], b[0]); + dest[1] = vg_maxf(a[1], b[1]); +} + +static inline void v2_sub( v2f a, v2f b, v2f d ) +{ + d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; +} + +static inline float v2_cross( v2f a, v2f b ) +{ + return a[0] * b[1] - a[1] * b[0]; +} + +static inline void v2_add( v2f a, v2f b, v2f d ) +{ + d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; +} + +static inline void v2_muls( v2f a, float s, v2f d ) +{ + d[0] = a[0]*s; d[1] = a[1]*s; +} + +static inline void v2_mul( v2f a, v2f b, v2f d ) +{ + d[0] = a[0]*b[0]; d[1] = a[1]*b[1]; +} + +static inline void v2_div( v2f a, v2f b, v2f d ) +{ + d[0] = a[0]/b[0]; d[1] = a[1]/b[1]; +} + +// Vector 3 +// ================================================================================================================== + +static inline void v3_zero( v3f a ) +{ + a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; +} + +static inline void v3_copy( v3f a, v3f b ) +{ + b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; +} + +static inline void v3_add( v3f a, v3f b, v3f d ) +{ + d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; d[2] = a[2]+b[2]; +} + +static inline void v3_sub( v3f a, v3f b, v3f d ) +{ + d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; d[2] = a[2]-b[2]; +} + +static inline void v3_mul( v3f a, v3f b, v3f d ) +{ + d[0] = a[0]*b[0]; d[1] = a[1]*b[1]; d[2] = a[2]*b[2]; +} + +static inline void v3_div( v3f a, v3f b, v3f d ) +{ + d[0] = a[0]/b[0]; d[1] = a[1]/b[1]; d[2] = a[2]/b[2]; +} + +static inline void v3_muls( v3f a, float s, v3f d ) +{ + d[0] = a[0]*s; d[1] = a[1]*s; d[2] = a[2]*s; +} + +static inline void v3_divs( v3f a, float s, v3f d ) +{ + d[0] = a[0]/s; d[1] = a[1]/s; d[2] = a[2]/s; +} + +static inline void v3_muladds( v3f a, v3f b, float s, v3f d ) +{ + d[0] = a[0]+b[0]*s; d[1] = a[1]+b[1]*s; d[2] = a[2]+b[2]*s; +} + +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 ) +{ + 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]; +} + +static inline float v3_length2( v3f a ) +{ + return v3_dot( a, a ); +} + +static inline float v3_length( v3f a ) +{ + return sqrtf( v3_length2( a ) ); +} + +static inline float v3_dist2( v3f a, v3f b ) +{ + v3f delta; + v3_sub( a, b, delta ); + return v3_length2( delta ); +} + +static inline float v3_dist( v3f a, v3f b ) +{ + return sqrtf( v3_dist2( a, b ) ); +} + +static inline void v3_normalize( v3f a ) +{ + v3_muls( a, 1.f / v3_length( a ), a ); +} + +static inline float csr_lerpf( float a, float b, float t ) +{ + return a + t*(b-a); +} + +static inline void v3_lerp( v3f a, v3f b, float t, v3f d ) +{ + d[0] = a[0] + t*(b[0]-a[0]); + d[1] = a[1] + t*(b[1]-a[1]); + d[2] = a[2] + t*(b[2]-a[2]); +} + +static inline void v3_minv( v3f a, v3f b, v3f dest ) +{ + dest[0] = vg_minf(a[0], b[0]); + dest[1] = vg_minf(a[1], b[1]); + dest[2] = vg_minf(a[2], b[2]); +} + +static inline void v3_maxv( v3f a, v3f b, v3f dest ) +{ + dest[0] = vg_maxf(a[0], b[0]); + dest[1] = vg_maxf(a[1], b[1]); + dest[2] = vg_maxf(a[2], b[2]); +} + +static inline float v3_minf( v3f a ) +{ + return vg_minf( vg_minf( a[0], a[1] ), a[2] ); +} + +static inline float v3_maxf( v3f a ) +{ + return vg_maxf( vg_maxf( a[0], a[1] ), a[2] ); +} + +static inline void v3_fill( v3f a, float v ) +{ + a[0] = v; + a[1] = v; + a[2] = v; +} + +// Vector 4 +// ================================================================================================================== + +static inline void v4_copy( v4f a, v4f b ) +{ + b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3]; +} + +static inline void v4_zero( v4f a ) +{ + a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; a[3] = 0.f; +} + +// Matrix 3x3 +//====================================================================================================== + +#define M3X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\ + { 0.0f, 1.0f, 0.0f, },\ + { 0.0f, 0.0f, 1.0f, }} + +#define M3X3_ZERO {{0.0f, 0.0f, 0.0f, },\ + { 0.0f, 0.0f, 0.0f, },\ + { 0.0f, 0.0f, 0.0f, }} + + +static inline void m3x3_copy( m3x3f a, m3x3f b ) +{ + v3_copy( a[0], b[0] ); + v3_copy( a[1], b[1] ); + v3_copy( a[2], b[2] ); +} + +static inline void m3x3_identity( m3x3f a ) +{ + m3x3f id = M3X3_IDENTITY; + m3x3_copy( id, a ); +} + +static inline void m3x3_zero( m3x3f a ) +{ + m3x3f z = M3X3_ZERO; + m3x3_copy( z, a ); +} + +static inline void m3x3_inv( m3x3f src, m3x3f dest ) +{ + float a = src[0][0], b = src[0][1], c = src[0][2], + d = src[1][0], e = src[1][1], f = src[1][2], + g = src[2][0], h = src[2][1], i = src[2][2]; + + float det = 1.f / + (+a*(e*i-h*f) + -b*(d*i-f*g) + +c*(d*h-e*g)); + + dest[0][0] = (e*i-h*f)*det; + dest[0][1] = -(b*i-c*h)*det; + dest[0][2] = (b*f-c*e)*det; + dest[1][0] = -(d*i-f*g)*det; + dest[1][1] = (a*i-c*g)*det; + dest[1][2] = -(a*f-d*c)*det; + dest[2][0] = (d*h-g*e)*det; + dest[2][1] = -(a*h-g*b)*det; + dest[2][2] = (a*e-d*b)*det; +} + +static inline void m3x3_transpose( m3x3f src, m3x3f dest ) +{ + float a = src[0][0], b = src[0][1], c = src[0][2], + d = src[1][0], e = src[1][1], f = src[1][2], + g = src[2][0], h = src[2][1], i = src[2][2]; + + dest[0][0] = a; + dest[0][1] = d; + dest[0][2] = g; + dest[1][0] = b; + dest[1][1] = e; + dest[1][2] = h; + dest[2][0] = c; + dest[2][1] = f; + dest[2][2] = i; +} + +static inline void m3x3_mul( m3x3f a, m3x3f b, m3x3f d ) +{ + float a00 = a[0][0], a01 = a[0][1], a02 = a[0][2], + a10 = a[1][0], a11 = a[1][1], a12 = a[1][2], + a20 = a[2][0], a21 = a[2][1], a22 = a[2][2], + + b00 = b[0][0], b01 = b[0][1], b02 = b[0][2], + b10 = b[1][0], b11 = b[1][1], b12 = b[1][2], + b20 = b[2][0], b21 = b[2][1], b22 = b[2][2]; + + d[0][0] = a00*b00 + a10*b01 + a20*b02; + d[0][1] = a01*b00 + a11*b01 + a21*b02; + d[0][2] = a02*b00 + a12*b01 + a22*b02; + d[1][0] = a00*b10 + a10*b11 + a20*b12; + d[1][1] = a01*b10 + a11*b11 + a21*b12; + d[1][2] = a02*b10 + a12*b11 + a22*b12; + d[2][0] = a00*b20 + a10*b21 + a20*b22; + d[2][1] = a01*b20 + a11*b21 + a21*b22; + d[2][2] = a02*b20 + a12*b21 + a22*b22; +} + +static inline void m3x3_mulv( m3x3f m, v3f v, v3f d ) +{ + v3f res; + + res[0] = m[0][0]*v[0] + m[1][0]*v[1] + m[2][0]*v[2]; + res[1] = m[0][1]*v[0] + m[1][1]*v[1] + m[2][1]*v[2]; + res[2] = m[0][2]*v[0] + m[1][2]*v[1] + m[2][2]*v[2]; + + v3_copy( res, d ); +} + +static inline void m3x3_projection( m3x3f dst, float const left, float const right, float const bottom, float const top ) +{ + float rl, tb; + + m3x3_zero( dst ); + + rl = 1.0f / (right - left); + tb = 1.0f / (top - bottom); + + dst[0][0] = 2.0f * rl; + dst[1][1] = 2.0f * tb; + dst[2][2] = 1.0f; +} + +static inline void m3x3_translate( m3x3f m, v3f v ) +{ + m[2][0] = m[0][0] * v[0] + m[1][0] * v[1] + m[2][0]; + m[2][1] = m[0][1] * v[0] + m[1][1] * v[1] + m[2][1]; + m[2][2] = m[0][2] * v[0] + m[1][2] * v[1] + m[2][2]; +} + +static inline void m3x3_scale( m3x3f m, v3f v ) +{ + m[0][0] = m[0][0] * v[0]; + m[0][1] = m[0][1] * v[0]; + m[0][2] = m[0][2] * v[0]; + + m[1][0] = m[1][0] * v[1]; + m[1][1] = m[1][1] * v[1]; + m[1][2] = m[1][2] * v[1]; +} + +static inline void m3x3_rotate( m3x3f m, float angle ) +{ + float m00 = m[0][0], m10 = m[1][0], + m01 = m[0][1], m11 = m[1][1], + m02 = m[0][2], m12 = m[1][2]; + float c, s; + + s = sinf( angle ); + c = cosf( angle ); + + m[0][0] = m00 * c + m10 * s; + m[0][1] = m01 * c + m11 * s; + m[0][2] = m02 * c + m12 * s; + + m[1][0] = m00 * -s + m10 * c; + m[1][1] = m01 * -s + m11 * c; + m[1][2] = m02 * -s + m12 * c; +} + +// Matrix 4x3 +// ================================================================================================================== + +#define M4X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\ + { 0.0f, 1.0f, 0.0f, },\ + { 0.0f, 0.0f, 1.0f, },\ + { 0.0f, 0.0f, 0.0f }} + +static inline void m4x3_to_3x3( m4x3f a, m3x3f b ) +{ + v3_copy( a[0], b[0] ); + v3_copy( a[1], b[1] ); + v3_copy( a[2], b[2] ); +} + +static inline void m4x3_copy( m4x3f a, m4x3f b ) +{ + v3_copy( a[0], b[0] ); + v3_copy( a[1], b[1] ); + v3_copy( a[2], b[2] ); + v3_copy( a[3], b[3] ); +} + +static inline void m4x3_identity( m4x3f a ) +{ + m4x3f id = M4X3_IDENTITY; + m4x3_copy( id, a ); +} + +static inline void m4x3_mul( m4x3f a, m4x3f b, m4x3f d ) +{ + float + a00 = a[0][0], a01 = a[0][1], a02 = a[0][2], + a10 = a[1][0], a11 = a[1][1], a12 = a[1][2], + a20 = a[2][0], a21 = a[2][1], a22 = a[2][2], + a30 = a[3][0], a31 = a[3][1], a32 = a[3][2], + b00 = b[0][0], b01 = b[0][1], b02 = b[0][2], + b10 = b[1][0], b11 = b[1][1], b12 = b[1][2], + b20 = b[2][0], b21 = b[2][1], b22 = b[2][2], + b30 = b[3][0], b31 = b[3][1], b32 = b[3][2]; + + d[0][0] = a00*b00 + a10*b01 + a20*b02; + d[0][1] = a01*b00 + a11*b01 + a21*b02; + d[0][2] = a02*b00 + a12*b01 + a22*b02; + d[1][0] = a00*b10 + a10*b11 + a20*b12; + d[1][1] = a01*b10 + a11*b11 + a21*b12; + d[1][2] = a02*b10 + a12*b11 + a22*b12; + d[2][0] = a00*b20 + a10*b21 + a20*b22; + d[2][1] = a01*b20 + a11*b21 + a21*b22; + d[2][2] = a02*b20 + a12*b21 + a22*b22; + d[3][0] = a00*b30 + a10*b31 + a20*b32 + a30; + d[3][1] = a01*b30 + a11*b31 + a21*b32 + a31; + d[3][2] = a02*b30 + a12*b31 + a22*b32 + a32; +} + +static inline void m4x3_mulv( m4x3f m, v3f v, v3f d ) +{ + v3f res; + + res[0] = m[0][0]*v[0] + m[1][0]*v[1] + m[2][0]*v[2] + m[3][0]; + res[1] = m[0][1]*v[0] + m[1][1]*v[1] + m[2][1]*v[2] + m[3][1]; + res[2] = m[0][2]*v[0] + m[1][2]*v[1] + m[2][2]*v[2] + m[3][2]; + + v3_copy( res, d ); +} + +// Affine transforms +// ==================================================================================================================== + +static inline void m4x3_translate( m4x3f m, v3f v ) +{ + v3_muladds( m[3], m[0], v[0], m[3] ); + v3_muladds( m[3], m[1], v[1], m[3] ); + v3_muladds( m[3], m[2], v[2], m[3] ); +} + +static inline void m4x3_scale( m4x3f m, float s ) +{ + v3_muls( m[0], s, m[0] ); + v3_muls( m[1], s, m[1] ); + v3_muls( m[2], s, m[2] ); +} + +static inline void m4x3_rotate_x( m4x3f m, float angle ) +{ + m4x3f t = M4X3_IDENTITY; + float c, s; + + c = cosf( angle ); + s = sinf( angle ); + + t[1][1] = c; + t[1][2] = s; + t[2][1] = -s; + t[2][2] = c; + + m4x3_mul( m, t, m ); +} + +static inline void m4x3_rotate_y( m4x3f m, float angle ) +{ + m4x3f t = M4X3_IDENTITY; + float c, s; + + c = cosf( angle ); + s = sinf( angle ); + + t[0][0] = c; + t[0][2] = -s; + t[2][0] = s; + t[2][2] = c; + + m4x3_mul( m, t, m ); +} + +static inline void m4x3_rotate_z( m4x3f m, float angle ) +{ + m4x3f t = M4X3_IDENTITY; + float c, s; + + c = cosf( angle ); + s = sinf( angle ); + + t[0][0] = c; + t[0][1] = s; + t[1][0] = -s; + t[1][1] = c; + + m4x3_mul( m, t, m ); +} + +// Warning: These functions are unoptimized.. +static inline void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point ) +{ + v3f v; + m4x3_mulv( m, point, v ); + + v3_minv( box[0], v, box[0] ); + v3_maxv( box[1], v, box[1] ); +} + +static inline void box_concat( boxf a, boxf b ) +{ + v3_minv( a[0], b[0], a[0] ); + v3_maxv( a[1], b[1], a[1] ); +} + +static inline void box_copy( boxf a, boxf b ) +{ + v3_copy( a[0], b[0] ); + v3_copy( a[1], b[1] ); +} + +static inline void m4x3_transform_aabb( m4x3f m, boxf box ) +{ + v3f a; v3f b; + + v3_copy( box[0], a ); + v3_copy( box[1], b ); + v3_fill( box[0], INFINITY ); + v3_fill( box[1], -INFINITY ); + + m4x3_expand_aabb_point( m, box, a ); + m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], a[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], a[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], a[2] } ); + m4x3_expand_aabb_point( m, box, b ); + m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], b[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], b[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } ); +} + +// Planes (double precision) +// ================================================================================================================== + +static inline void tri_to_plane( double a[3], double b[3], double c[3], double p[4] ) +{ + double edge0[3]; + double edge1[3]; + double l; + + edge0[0] = b[0] - a[0]; + edge0[1] = b[1] - a[1]; + edge0[2] = b[2] - a[2]; + + edge1[0] = c[0] - a[0]; + edge1[1] = c[1] - a[1]; + edge1[2] = c[2] - a[2]; + + p[0] = edge0[1] * edge1[2] - edge0[2] * edge1[1]; + p[1] = edge0[2] * edge1[0] - edge0[0] * edge1[2]; + p[2] = edge0[0] * edge1[1] - edge0[1] * edge1[0]; + + l = sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]); + p[3] = (p[0] * a[0] + p[1] * a[1] + p[2] * a[2]) / l; + + p[0] = p[0] / l; + p[1] = p[1] / l; + p[2] = p[2] / l; +} + +static inline int plane_intersect( double a[4], double b[4], double c[4], double p[4] ) +{ + double const epsilon = 1e-8f; + + double x[3]; + double d; + + x[0] = a[1] * b[2] - a[2] * b[1]; + x[1] = a[2] * b[0] - a[0] * b[2]; + x[2] = a[0] * b[1] - a[1] * b[0]; + + d = x[0] * c[0] + x[1] * c[1] + x[2] * c[2]; + + if( d < epsilon && d > -epsilon ) return 0; + + p[0] = (b[1] * c[2] - b[2] * c[1]) * -a[3]; + p[1] = (b[2] * c[0] - b[0] * c[2]) * -a[3]; + p[2] = (b[0] * c[1] - b[1] * c[0]) * -a[3]; + + p[0] += (c[1] * a[2] - c[2] * a[1]) * -b[3]; + p[1] += (c[2] * a[0] - c[0] * a[2]) * -b[3]; + p[2] += (c[0] * a[1] - c[1] * a[0]) * -b[3]; + + p[0] += (a[1] * b[2] - a[2] * b[1]) * -c[3]; + p[1] += (a[2] * b[0] - a[0] * b[2]) * -c[3]; + p[2] += (a[0] * b[1] - a[1] * b[0]) * -c[3]; + + p[0] = -p[0] / d; + p[1] = -p[1] / d; + p[2] = -p[2] / d; + + return 1; +} + +static inline double plane_polarity( double p[4], double a[3] ) +{ + return + (a[0] * p[0] + a[1] * p[1] + a[2] * p[2]) + -(p[0]*p[3] * p[0] + p[1]*p[3] * p[1] + p[2]*p[3] * p[2]) + ; +} diff --git a/vg/vg_platform.h b/vg/vg_platform.h index da9a6ad..2800519 100644 --- a/vg/vg_platform.h +++ b/vg/vg_platform.h @@ -11,8 +11,16 @@ typedef int64_t i64; typedef unsigned int uint; -#define vg_min( A, B ) ((A)<(B)?(A):(B)) -#define vg_max( A, B ) ((A)>(B)?(A):(B)) +typedef int v2i[2]; +typedef int v3i[3]; +typedef int v4i[4]; +typedef float v2f[2]; +typedef float v3f[3]; +typedef float v4f[4]; +typedef v3f m3x3f[3]; +typedef v3f m4x3f[4]; +typedef v3f boxf[2]; + #define vg_list_size( A ) (sizeof(A)/sizeof(A[0])) // THREADING -- 2.25.1