
#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

uniform mediump float kk;
uniform mediump float which;

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

    mediump vec3 dst;
    mediump vec3 txt;
    mediump vec3 tmp_t;
    
    mediump vec2  coord;
    mediump float lum;
    lowp float saturate = 0.3;
    
    dst.rgb = texture2D(SamplerBGRA, v_texCoord).rgb;    
    //if which == 0 else ...
    coord = vec2(v_texCoord.x*(which+(1.0-which)*kk), v_texCoord.y*(1.0-which+which*kk));
    txt = texture2D(SamplerBackTex1, coord).rgb;
    
    lum = dst.r*0.299 + dst.g*0.587 + dst.b*0.114;
    dst = lum*0.7 + saturate*dst;
    
    tmp_t.r = min(1.0,dst.r+txt.r-2.0*dst.r*txt.r);
    tmp_t.g = min(1.0,dst.g+txt.g-2.0*dst.g*txt.g);
    tmp_t.b = min(1.0,dst.b+txt.b-2.0*dst.b*txt.b);
    
    dst = dst*0.13 + tmp_t*0.87;
    
    lum = (txt.r + txt.g + txt.b)/3.0;
    
    txt.r = min(1.0,dst.r + lum);
    txt.g = min(1.0,dst.g + lum);
    txt.b = min(1.0,dst.b + lum);
    
    dst = dst*0.35 + txt*0.65;
    
    if (dst.r + lum <1.0) 
    {
        txt.r = 0.0;
    }
    else
    {
        txt.r = 1.0;
    }
    if (dst.g + lum <1.0) 
    {
        txt.g = 0.0;
    }
    else
    {
        txt.g = 1.0;
    }
    if (dst.b + lum <1.0) 
    {
        txt.b = 0.0;
    }
    else
    {
        txt.b = 1.0;
    }
    dst = dst * 0.84 + txt * 0.16;
    outText = vec4(dst,1.0);
}