
#version 130
precision mediump float;

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

out     vec4 		outText;	// output sampler

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 highp float vignetteStart;  // The normalized distance from the center where the vignette effect starts. Default of 0.
uniform highp float vignetteEnd;    // The normalized distance from the center where the vignette effect ends. Default of 0.5.
uniform highp float aspectRatio;
 
void main()
{
    vec2 textureCoordinate = gl_TexCoord[0].st;
    vec3 dst;
    vec3 dst_a;
    vec3 dst_b;
    
    dst = texture2D(SamplerBGRA, textureCoordinate).rgb;

    dst_a.r = texture2D(SamplerTableTex1, vec2(dst.r,0.0)).r;
    dst_a.g = texture2D(SamplerTableTex1, vec2(dst.g,0.0)).g;
    dst_a.b = texture2D(SamplerTableTex1, vec2(dst.b,0.0)).b;
    
    dst_b.r = texture2D(SamplerTableTex2, vec2(dst.r,0.0)).r;
    dst_b.g = texture2D(SamplerTableTex2, vec2(dst.g,0.0)).g;
    dst_b.b = texture2D(SamplerTableTex2, vec2(dst.b,0.0)).b;

    //vignette    
    highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * aspectRatio + 0.5 - 0.5 * aspectRatio));
    highp float d = distance(textureCoordinateToUse, vec2(vignetteCenter.x, vignetteCenter.y));
    highp float percent = smoothstep(vignetteStart, vignetteEnd, d);
    //highp float percent = clamp((d-vignetteStart)/(vignetteEnd-vignetteStart), 0, 1);
    //percent = percent*percent;

    dst = dst_a * percent + dst_b * (1.0 - percent);
    outText = vec4(dst,1.0);
}