frosting ui style
[vg.git] / vg_imgui.h
index 750ebfb866a58f5f3e411c3b52de411ba4b93bd6..c6553e61c0b24352636de4c3ba6619ee3f61a724 100644 (file)
@@ -200,7 +200,8 @@ struct{
        }
    modal;
        
-       GLuint tex_glyphs, vao, vbo, ebo;
+       GLuint tex_glyphs, vao, vbo, ebo, tex_bg;
+   v2f bg_inverse_ratio;
 
        ui_px mouse[2], mouse_click[2];
    u32 mouse_state[2];
@@ -209,6 +210,7 @@ struct{
 
    ui_rect click_fader, click_fader_end;
    float click_fade_opacity;
+   f32 frosting;
 
    ui_scheme scheme;
    const ui_font *font;
@@ -222,7 +224,6 @@ struct{
    cursor;
 
    SDL_Cursor *cursor_map[ k_ui_cursor_max ];
-
    v4f colour;
 } 
 static vg_ui = {
@@ -263,59 +264,76 @@ static vg_ui = {
       [ k_ui_gray   + k_ui_brighter ] = UI_RGB( 0xa89984 ),
    },
    .font = &vg_ui_font_small,
-   .colour = {1.0f,1.0f,1.0f,1.0f}
+   .colour = {1.0f,1.0f,1.0f,1.0f},
+   .bg_inverse_ratio = {1,1}
 };
 
-static struct vg_shader _shader_ui =
-{
-   .name = "[vg] ui",
+static struct vg_shader _shader_ui ={
+   .name = "[vg] ui - transparent",
    .link = NULL,
-   .vs = 
-   {
+   .vs = {
       .orig_file = NULL,
       .static_src = 
        "layout (location=0) in vec2 a_co;"
        "layout (location=1) in vec2 a_uv;"
        "layout (location=2) in vec4 a_colour;"
        "uniform mat3 uPv;"
+   "uniform vec2 uBGInverseRatio;"
        ""
-       "out vec2 aTexCoords;"
+       "out vec4 aTexCoords;"
        "out vec4 aColour;"
-       "out vec2 aWsp;"
        ""
-       "void main()"
-       "{"
-               "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
-               "aTexCoords = a_uv * 0.00390625;" /* TODO: Uniform */
+       "void main(){"
+      "vec4 proj_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
+               "gl_Position = proj_pos;"
+               "aTexCoords = vec4( a_uv * 0.00390625, "
+                        " (proj_pos.xy*0.5+0.5) * uBGInverseRatio );"
                "aColour = a_colour;"
-               
-               "aWsp = a_co;"
        "}",
    },
-   .fs = 
-   {
+   .fs = {
       .orig_file = NULL,
       .static_src = 
-       "uniform sampler2D uTexGlyphs;"
+   "uniform sampler2D uTexGlyphs;"
+       "uniform sampler2D uTexBG;"
    "uniform vec4 uColour;"
+   "uniform float uSpread;"
        "out vec4 FragColor;"
        ""
-       "in vec2 aTexCoords;"
+       "in vec4 aTexCoords;"
        "in vec4 aColour;"
        ""
-       "in vec2 aWsp;"
-
+   "vec2 rand_hash22( vec2 p ){"
+      "vec3 p3 = fract(vec3(p.xyx) * 213.8976123);"
+      "p3 += dot(p3, p3.yzx+19.19);"
+      "return fract(vec2((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y));"
+   "}"
        ""
-       "void main()"
-       "{"
+       "void main(){"
                "vec4 diffuse = aColour;"
                
-               "if( diffuse.a == 0.0 )"
-               "{"
-                       "diffuse.a = texture( uTexGlyphs, aTexCoords ).r;"
+      "vec4 avg = vec4(0.0);"
+
+               "if( aColour.a == 0.0 ){"
+         "avg = aColour;"
+                       "avg.a = texture( uTexGlyphs, aTexCoords.xy ).r;"
                "}"
-               
-               "FragColor = diffuse * uColour;"
+      "else{"
+         "if( uSpread > 0.0001 ){"
+            "for( int i=0; i<4; i ++ ){"
+               "vec2 spread = rand_hash22(aTexCoords.zw+vec2(float(i)));"
+               "avg += texture( uTexBG, aTexCoords.zw + (spread-0.5)*uSpread );"
+            "}"
+            "avg *= 0.25;"
+            "avg.a = 1.0;"
+            "avg.rgb = mix( avg.rgb, aColour.rgb, aColour.a );"
+         "}"
+         "else{"
+            "avg = aColour;"
+         "}"
+      "}"
+
+               "FragColor = avg * uColour;"
        "}"
    }
 };
@@ -367,8 +385,9 @@ static struct vg_shader _shader_ui_image = {
 
 static void _vg_ui_init(void){
    if( !vg_shader_compile( &_shader_ui ) ||
-       !vg_shader_compile( &_shader_ui_image ) ) 
+       !vg_shader_compile( &_shader_ui_image ) ){
       vg_fatal_error( "Failed to compile ui shader" );
+   }
 
    /*
     * Vertex buffer
@@ -525,6 +544,14 @@ static void ui_flush( enum ui_shader shader, f32 w, f32 h ){
       glActiveTexture( GL_TEXTURE0 );
       glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs );
       glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexGlyphs" ), 0 );
+
+      glActiveTexture( GL_TEXTURE1 );
+      glBindTexture( GL_TEXTURE_2D, vg_ui.tex_bg );
+      glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexBG" ), 1 );
+      glUniform1f( glGetUniformLocation( _shader_ui.id, "uSpread" ), 
+                   vg_ui.frosting );
+      glUniform2fv( glGetUniformLocation( _shader_ui.id, "uBGInverseRatio" ),
+                     1, vg_ui.bg_inverse_ratio );
    }
    else if( shader == k_ui_shader_image ){
       glUseProgram( _shader_ui_image.id );