
#version 130


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

out     vec4 		outText;	// output sampler

uniform float rangeX, rangeY, rangeW, rangeH;
uniform  float strength; // effect strength, blend with source, 0 -- 1, default is 1

const vec3 c1 = vec3(0.992,0.137,0.314);
const vec3 c2 = vec3(0.204,0.98,0.725);

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 src, dst;
     float alpha;    
            
    src.rgb = texture2D(SamplerBGRA, v_texCoord).rgb;
    dst = src;
    
    //vignette    
	 vec2 v_texCoordVignette = vec2(v_texCoord.x*rangeW+rangeX, v_texCoord.y*rangeH+rangeY);
     vec2 textureCoordinateToUse = vec2(v_texCoordVignette.x, (v_texCoordVignette.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;

    vec3 tmp = dst * alpha;

	dst.r = 1.0 - (1.0 - tmp.r*c1.r) * (1.0 - tmp.g*c2.r);
	dst.g = 1.0 - (1.0 - tmp.r*c1.g) * (1.0 - tmp.g*c2.g);
	dst.b = 1.0 - (1.0 - tmp.r*c1.b) * (1.0 - tmp.g*c2.b);
    
    outText = vec4(mix(src, dst, strength),1.0);
}