
#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

void main()
{
    mediump vec3 dst;
    mediump vec3 tmp_s;
    mediump vec3 tmp_t;
    mediump vec3 txt;
    
    mediump vec2 dis;
    mediump vec2 coord;
    
    mediump float max_r;
    mediump float lens_fac;
    mediump float lens_facinv;
    mediump float lens_amount;
    mediump float bd;
    mediump float alpha;
    mediump float lumm;
    
    lowp float saturate = 0.9;
    lowp float bound_h = 0.1;
    
    dst = texture2D(SamplerBGRA, gl_TexCoord[0].st).rgb;    
    txt = texture2D(SamplerBackTex1, gl_TexCoord[0].st).rgb;
    
    //calculate alpha
    coord = vec2(gl_TexCoord[0].x * 2.0, gl_TexCoord[0].y * 3.0);
    max_r = 1.5;
    dis.x = 1.0 - coord.x;
    dis.y = 1.5 - coord.y;
    
    lens_fac = 2.25;
    lens_facinv = 1.0/lens_fac;
    lens_amount = 0.3164;
    
    bd = dis.x* dis.x + dis.y* dis.y;
    bd = ((lens_fac + bd) * lens_facinv);
    bd = bd*bd;
    bd = 1.0/bd;
    alpha = 1.0 + lens_amount*(bd - 1.0);
    //calculate alpha end
    
    lumm = dst.r*0.299 + dst.g*0.587 + dst.b*0.114;
    
    tmp_s = lumm*bound_h + saturate*dst;
    tmp_s = tmp_s/saturate;
    
    tmp_t = tmp_s + txt;
    dst = tmp_s * 0.67 + tmp_t * 0.33;
    
    tmp_t = abs(dst - txt);
    
    dst = dst*0.33 + tmp_t*0.67;
    tmp_t = vec3(0.66272,0.486267,0.0039);
    dst = tmp_t * (1.0 - alpha) + dst * alpha;
    
    outText = vec4(dst,1.0);
}