new tileset
authorhgn <hgodden00@gmail.com>
Sat, 16 Oct 2021 19:40:39 +0000 (20:40 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 16 Oct 2021 19:40:39 +0000 (20:40 +0100)
fishladder.c
textures/tile_overlays.png [new file with mode: 0644]
textures/tileset.png [new file with mode: 0644]
textures/wood.png [new file with mode: 0644]
vg/vg_tex.h

index 7c026532f66178d5496bc86ddf5830e14e110909..21fe2ff088fb87c5af4d6ebe642d48670f3c02a8 100644 (file)
@@ -27,6 +27,53 @@ SHADER_DEFINE( shader_tile_colour,
        UNIFORMS({ "uPv", "uOffset", "uColour" })
 )
 
+SHADER_DEFINE( shader_tile_main,
+       // VERTEX
+       "layout (location=0) in vec2 a_co;"
+       "uniform vec4 uOffset;" // Tile x/y, uv x/y
+       "uniform mat3 uPv;"
+       ""
+               "out vec4 aTexCoords;"
+       ""
+       "vec2 hash22(vec2 p)"
+       "{"
+               "vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));"
+               "p3 += dot(p3, p3.yzx+33.33);"
+               "return fract((p3.xx+p3.yz)*p3.zy);"
+       "}"
+       ""
+       "void main()"
+       "{"
+               "vec2 random_offset = floor(hash22(uOffset.xy) * 4.0) * 0.25;"
+               "vec2 edge_safe_coords = a_co * 0.98 + 0.01;"
+               "aTexCoords = vec4((edge_safe_coords + uOffset.zw) * 0.25, edge_safe_coords * 0.25 + random_offset );"
+               "gl_Position = vec4( uPv * vec3( a_co + uOffset.xy, 1.0 ), 1.0 );"
+       "}",
+       
+       // FRAGMENT
+       "out vec4 FragColor;"
+       ""
+       "uniform sampler2D uTexGlyphs;"
+       "uniform sampler2D uTexWood;"
+       ""
+       "in vec4 aTexCoords;"
+       ""
+       "void main()"
+       "{"
+               "vec3 shadowing_colour = vec3( 0.93, 0.88536, 0.8184 );"
+               "vec4 glyph = texture( uTexGlyphs, aTexCoords.xy );"
+               "vec4 wood = texture( uTexWood, aTexCoords.zw );"
+               "vec4 wood_secondary = texture( uTexWood, aTexCoords.zw + 0.25 );"
+               "vec3 wood_comp = mix( wood_secondary.rgb * shadowing_colour, wood.rgb, clamp( glyph.b * 2.0 - 1.0, 0.0, 1.0 ) );"
+               
+               "vec3 shadows = mix( vec3( 0.85, 0.7344, 0.561 ), vec3(1.0,1.0,1.0), glyph.r );"
+               
+               "FragColor = vec4( wood_comp * shadows, 1.0 );"
+       "}"
+       ,
+       UNIFORMS({ "uPv", "uOffset", "uTexGlyphs", "uTexWood" })
+)
+
 const char *level_pack[] = 
 {
        // Level 0
@@ -85,6 +132,10 @@ const char *level_pack[] =
        "#############;\n"
 };
 
+GLuint tex_tile_data;
+GLuint tex_tile_detail;
+GLuint tex_wood;
+
 m3x3f m_projection;
 m3x3f m_view;
 m3x3f m_mdl;
@@ -348,6 +399,7 @@ int main( int argc, char *argv[] )
 void vg_register(void)
 {
        SHADER_INIT( shader_tile_colour );
+       SHADER_INIT( shader_tile_main );
 }
 
 void vg_start(void)
@@ -414,6 +466,24 @@ void vg_start(void)
                init_mesh( &world.splitter_r, splitter_r, vg_list_size( splitter_r ) );
        }
        
+       // Textures
+       {
+               tex_tile_detail = vg_tex2d_rgba( "textures/tile_overlays.png" );
+               vg_tex2d_mipmap();
+               vg_tex2d_linear_mipmap();
+               vg_tex2d_repeat();
+               
+               tex_tile_data = vg_tex2d_rgba( "textures/tileset.png" );
+               vg_tex2d_mipmap();
+               vg_tex2d_linear_mipmap();
+               vg_tex2d_repeat();
+               
+               tex_wood = vg_tex2d_rgba( "textures/wood.png" );
+               vg_tex2d_mipmap();
+               vg_tex2d_linear_mipmap();
+               vg_tex2d_repeat();
+       }
+       
        map_load( level_pack[ 0 ] );
 }
 
@@ -425,6 +495,10 @@ void vg_free(void)
        free_mesh( &world.splitter_r );
 
        map_free();
+       
+       glDeleteTextures( 1, &tex_tile_data );
+       glDeleteTextures( 1, &tex_tile_detail );
+       glDeleteTextures( 1, &tex_wood );
 }
 
 static int cell_interactive( v2i co )
@@ -440,10 +514,11 @@ static int cell_interactive( v2i co )
        // List of 3x3 configurations that we do not allow
        static u32 invalid_src[][9] = 
        { 
+               /*
                { 0,1,0,
                  1,1,1,
                  0,1,0 
-               },
+               },*/
                { 0,0,0,
                  0,1,1,
                  0,1,1
@@ -460,6 +535,7 @@ static int cell_interactive( v2i co )
                  1,1,0,
                  0,0,0
                },
+               /*
                { 0,1,0,
                  0,1,1,
                  0,1,0
@@ -467,7 +543,7 @@ static int cell_interactive( v2i co )
                { 0,1,0,
                  1,1,0,
                  0,1,0
-               }
+               }*/
        };
        
        // Statically compile invalid configurations into bitmasks
@@ -609,7 +685,6 @@ void vg_update(void)
        // Simulation stuff
        // ========================================================
        
-       // Reclassify world. TODO: Move into own function
        for( int y = 2; y < world.h-2; y ++ )
        {
                for( int x = 2; x < world.w-2; x ++ )
@@ -783,11 +858,8 @@ void vg_render(void)
        glClearColor( 0.8f, 0.8f, 0.8f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        
-       use_mesh( &world.tile );
-       SHADER_USE( shader_tile_colour );
-       glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
-       
        // Shadow layer
+       /*
        glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.5f, 0.5f, 0.5f, 1.0f );
        for( int y = 0; y < world.h; y ++ )
                for( int x = 0; x < world.w; x ++ )
@@ -851,6 +923,78 @@ void vg_render(void)
                        draw_mesh( 0, 2 );
                }
        }
+       */
+       
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       glBlendEquation(GL_FUNC_ADD);
+       
+       use_mesh( &world.tile );
+       SHADER_USE( shader_tile_main );
+       glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+       
+       // Bind textures
+       glActiveTexture( GL_TEXTURE0 );
+       glBindTexture( GL_TEXTURE_2D, tex_tile_data );
+       glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
+       
+       glActiveTexture( GL_TEXTURE1 );
+       glBindTexture( GL_TEXTURE_2D, tex_wood );
+       glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
+       
+       for( int y = 0; y < world.h; y ++ )
+       {
+               for( int x = 0; x < world.w; x ++ )
+               {
+                       struct cell *cell = pcell((v2i){x,y});
+                       int selected = world.selected == y*world.w + x;
+                       
+                       /*
+                               0000 0   | 0001 1   | 0010 2   | 0011 3
+                                                  |          |    |     |    |
+                                       X     |           X=    |    X     |    X=
+                                                  |          |          |     
+                               0100 4   | 0101 5   | 0110 6   | 0111 7
+                                                  |          |    |     |    |
+                                 =X     |   =X=    |   =X     |   =X=
+                                                  |          |          |    
+                               1000 8   | 1001 9   | 1010 10  | 1011 11
+                                             |          |    |     |    |
+                                       X     |    X=    |    X     |    X=
+                                       |               |    |     |    |     |    |
+                               1100 12  | 1101 13  | 1110 14  | 1111 15
+                                             |          |    |     |    |
+                                 =X     |   =X=    |   =X     |   =X=
+                                       |          |    |     |    |     |    |
+                       */
+                       
+                       int tile_offsets[][2] =
+                       {
+                               {2, 0}, {0, 3}, {0, 2}, {2, 2},
+                               {1, 0}, {2, 3}, {3, 2}, {1, 3},
+                               {3, 1}, {0, 1}, {1, 2}, {2, 1},
+                               {1, 1}, {3, 3}, {2, 1}, {2, 1}
+                       };
+                       
+                       int uv[2] = { 3, 0 };
+                       
+                       if( cell->state & FLAG_CANAL )
+                       {
+                               uv[0] = tile_offsets[ cell->config ][0];
+                               uv[1] = tile_offsets[ cell->config ][1];
+                       }
+                       
+                       glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] ); // TODO: PICK GLYPH
+                       draw_mesh( 0, 2 );
+               }
+       }
+       
+       SHADER_USE( shader_tile_colour );
+       glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+       
+       glDisable(GL_BLEND);
+       
+       
        
        use_mesh( &world.circle );
        
@@ -921,7 +1065,7 @@ void vg_render(void)
                        colour_code_v3( fish->payload, dot_colour );
                        glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
                        
-                       glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)fish->pos[0] + 0.5f - (float)fish->dir[0]*lerp, (float)fish->pos[1] + 0.25f - (float)fish->dir[1]*lerp, 0.25f );
+                       glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)fish->pos[0] + 0.5f - (float)fish->dir[0]*lerp, (float)fish->pos[1] + 0.5f - (float)fish->dir[1]*lerp, 0.125f );
                        draw_mesh( 0, 32 );
                }
        }
diff --git a/textures/tile_overlays.png b/textures/tile_overlays.png
new file mode 100644 (file)
index 0000000..6554fac
Binary files /dev/null and b/textures/tile_overlays.png differ
diff --git a/textures/tileset.png b/textures/tileset.png
new file mode 100644 (file)
index 0000000..7b84b6c
Binary files /dev/null and b/textures/tileset.png differ
diff --git a/textures/wood.png b/textures/wood.png
new file mode 100644 (file)
index 0000000..ea25c5c
Binary files /dev/null and b/textures/wood.png differ
index 4ab300cbcb3ee93f3be148da29ef25f29c2c17a1..742493b5720d8bfc4a873f864c9b209deb3ecee8 100644 (file)
@@ -38,7 +38,7 @@ static inline void vg_tex2d_clamp(void)
 static GLuint vg_tex2d_rgba( const char *path )
 {
        int x,y,nc;
-       stbi_set_flip_vertically_on_load( 0 );
+       stbi_set_flip_vertically_on_load( 1 );
 
        i64 length;
        u8 *src_data = vg_asset_read_s( path, &length );