
#version 130


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

out     vec4 		outText;	// output sampler

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

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

     vec3 src, dst, txt;
            
    src.rgb = texture2D(SamplerBGRA, v_texCoord).rgb;
    dst = src;

     vec2 smp_coord;
    smp_coord.x = v_texCoord.x*rangeW+rangeX;
    smp_coord.y = (v_texCoord.y*rangeH+rangeY) * kx/ky;
    txt = texture2D(SamplerBackTex1, smp_coord).rgb;

    vec3 tmp2_pic;
       
        if(dst.r < 0.5)
        {
            tmp2_pic.r = 2.0*(dst.r * txt.r);
        }
        else
        {
            tmp2_pic.r = 2.0*(dst.r + txt.r) - dst.r * txt.r * 2.0 - 1.0;
        }
        if(dst.g < 0.5)
        {
            tmp2_pic.g = dst.g * txt.g * 2.0;
        }
        else
        {
            tmp2_pic.g = 2.0*(dst.g + txt.g) - dst.g * txt.g * 2.0 - 1.0;
        }
        if(dst.b < 0.5)
        {
            tmp2_pic.b = dst.b * txt.b * 2.0;
        }
        else
        {
            tmp2_pic.b = 2.0*(dst.b + txt.b) - dst.b * txt.b * 2.0 - 1.0;
        }
        
        dst = dst*0 + tmp2_pic*1;
            
    outText = vec4(mix(src, dst, strength),1.0);
}