uniform float pixel_size;
uniform vec2 resolution;
varying vec2 v_texCoord;
uniform sampler2D u_texture;

/*void main(){

	sum += texture2D(u_texture, vec2(v_texCoord.x, v_texCoord.y - 6.0*pixel_size)) * 0.045;

	gl_FragColor = sum;
}

*/
#define saturate(i) ( clamp( i, 0.0, 1.0 ) )
const float pi = 3.14159265;

float GaussianWeight( float x, float mean, float deviation )
{
	float c = deviation;
	float a = c * sqrt( 2.0 * pi );
	float b = x - mean;
	float v = a * exp( -( ( b * b ) / ( 2.0*c*c ) ) );
	return v / a;
}

const int samples = 10;
const float fSamples = float( samples * samples * 2 * 2 );
//const float blurscale = 1.0 / 16.0;

void main(void)
{
	float aspect = resolution.x / resolution.y;

    float blurscale = 0.025;//1.0 / ( ( 0.5 + sin( iGlobalTime * 0.5 ) * 0.5 ) * 24.0 );
    
	vec2 uv = v_texCoord;//gl_FragCoord.xy / iResolution.xy;
	vec4 org_color = texture2D( u_texture, uv);

    float sample_r = 0.0;
    float sample_g = 0.0;
    float sample_b = 0.0;
    float sample_a = 0.0;

    for( int x = -samples; x < samples; x++ )
    {
    	for( int y = -samples; y < samples; y++ )
        {
            vec2 subSampleOffset = ( vec2( float( x ), float( y ) ) / float( samples ) ) 
                / ( resolution.xy * blurscale );
            
            float fx = float( x ); float fy = float( y );
            float gaussianLength = sqrt( fx*fx + fy*fy ) / sqrt( fSamples * 0.5 );
            float sampleWeight = GaussianWeight( gaussianLength, 0.0, 0.3 );
            
            float subsample_g = pow( texture2D( u_texture, uv + subSampleOffset ).g, 2.2 );
            float subsample_r = pow( texture2D( u_texture, uv + subSampleOffset ).r, 2.2 );
            float subsample_b = pow( texture2D( u_texture, uv + subSampleOffset ).b, 2.2 );
            float subsample_a = pow( texture2D( u_texture, uv + subSampleOffset ).a, 2.2 );

    		sample_g += saturate( subsample_g ) * sampleWeight;
            sample_r += saturate( subsample_r ) * sampleWeight;
    		sample_b += saturate( subsample_b ) * sampleWeight;
    		sample_a += saturate( subsample_a ) * sampleWeight;

        }
    }
    sample_r /= fSamples;
    sample_g /= fSamples;
    sample_b /= fSamples;
    sample_a /= fSamples;

	float sample_factor = 5.0;
    sample_r *= sample_factor;
    sample_g *= sample_factor;
    sample_b *= sample_factor;
    sample_a *= sample_factor;

    	gl_FragColor = vec4( 
           pow( sample_r, 1.0 / 2.2 ),
           pow( sample_g, 1.0 / 2.2 ),
           pow( sample_b, 1.0 / 2.2 ),
            sample_a ) * 0.8;
}
