
#version 130


uniform sampler2D SamplerBGRA;
//uniform sampler2D SamplerTableTex1;
//uniform sampler2D SamplerTableTex2;
//uniform sampler2D SamplerBackTex1;
//uniform sampler2D SamplerBackTex2;

out     vec4 		outText;	// output sampler

uniform  float saturate;
uniform  float bound_l;

const vec2 vignetteCenter = vec2(0.5, 0.5);   // the center for the vignette in tex coords (defaults to 0.5, 0.5)
//uniform vec3 vignetteColor = vec3(1.0, 1.0, 1.0); // The color to use for the Vignette (defaults to black)
uniform  float vignetteStart;  // The normalized distance from the center where the vignette effect starts. Default of 0.
uniform  float vignetteRange;  // The normalized range from the vignette effect start and ends. Default of 0.5.
uniform  float aspectRatio;

void main(void)
{
	vec2 v_texCoord = gl_TexCoord[0].st;

     vec3 dst;
     vec3 tmp_s;
     float alpha;    
     float lum;
        
    dst.rgb = texture2D(SamplerBGRA, v_texCoord).rgb;
    
    //vignette    
     vec2 textureCoordinateToUse = vec2(v_texCoord.x, (v_texCoord.y * aspectRatio + 0.5 - 0.5 * aspectRatio));
     float d = distance(textureCoordinateToUse, vec2(vignetteCenter.x, vignetteCenter.y));
    // float percent = smoothstep(vignetteStart, vignetteStart+vignetteRange, d);
     float percent = clamp((d-vignetteStart)/vignetteRange, 0, 1);
    alpha = 1-percent;// * percent;
        
    lum = dst.r*0.299 + dst.g*0.587 + dst.b*0.114;
    tmp_s = lum*(1.0-saturate) + saturate*dst;
    tmp_s = (tmp_s - bound_l)/saturate;    
    dst = tmp_s * alpha;
        
    outText = vec4(dst,1.0);
}