#version 430 core
in vec2 vTexPos;
out vec4 fColor;

uniform vec2 winSize;
uniform sampler2D screenTexture;

const float hOff = 2.0 / 600;
const float vOff = 2.0 / 600;
const float frac = 1.0 / 9;
const vec2 off[9] = vec2[]
    ( vec2( vOff,hOff )
    , vec2( vOff,0.0 )
    , vec2( vOff,-hOff )
    , vec2( 0.0,hOff )
    , vec2( 0.0,0.0 )
    , vec2( 0.0,-hOff )
    , vec2( -vOff,hOff )
    , vec2( -vOff,0.0 )
    , vec2( -vOff,-hOff )
    );
const vec2 fiveOff[5] = vec2[]
    ( vec2( vOff,0.0 )
    , vec2( 0.0,hOff )
    , vec2( 0.0,0.0 )
    , vec2( 0.0,-hOff )
    , vec2( -vOff,0.0 )
    );
vec4 combinef (vec4 ac, vec4 bc)
//{ return vec4 (max(ac.x,bc.x),max(ac.y,bc.y),max(ac.z,bc.z),ac.w + bc.w -
//        ac.w*bc.w);
//{ return vec4 (max(ac.x,bc.x),max(ac.y,bc.y),max(ac.z,bc.z),ac.w + bc.w);
{ return vec4 (ac.rgb + bc.rgb * bc.a , ac.a + bc.a );
}
void main()
{
    vec4 sampleTex[9];
    for(int i=0; i<9; i++)
    {
        sampleTex[i] = vec4(texture(screenTexture, vTexPos + off[i]));
    }
    vec4 col = vec4(0,0,0,0);
    for (int i = 0; i < 9; i++)
        col = combinef(col,sampleTex[i]);
        //col += sampleTex[i] * frac;
    float alph = 0.0;
    for (int i = 0; i < 9; i++)
        alph += sampleTex[i].a;
    alph = min(sampleTex[4].a, alph/9.0);
        //
    //fColor = col;
    fColor = vec4(col.rgb/col.a,alph);
    //if (col.a == 0.0)
    //{ fColor = vec4(0,0,0,0); }
    //else
    //{ fColor = vec4( col.r/col.a , col.g/col.a , col.b/col.a, col.a/9.0); }
}
