#extension GL_ARB_texture_rectangle : enable

varying vec2 texcoord;
uniform sampler2DRect tex1;
uniform sampler2DRect mask;
uniform sampler2DRect bg;
uniform float amount;
uniform float angle;
uniform float alpha;
uniform float maskheight;
uniform float contrast;
uniform vec2 camerarange;

float readDepth( in vec2 coord ) {
	return camerarange.y * camerarange.x / ( camerarange.x - texture2DRect( mask, coord ).x * (camerarange.y - camerarange.x) );
}


void main (void)
{
	vec2 texcoord0 = texcoord;
	float theta = 0.0;//radians(angle);

	vec2 amount1,amount2;//,amount3,amount4,amount5,amount6,amount7,amount8;


	amount1 = vec2(2,2) * amount;
	amount2 = amount1 *2.0;
	
	
	//amount3 = amount1 *3.0;
	//amount4 = amount1 *4.0;
	//amount5 = -amount1;
	//amount6 = amount5 * 2.0;
	//amount7 = amount5 * 3.0;
	//amount8 = amount5 * 4.0;


	// sample our textures
	vec4 sample0 = texture2DRect(tex1, texcoord0);
	vec4 sample1 = texture2DRect(tex1, texcoord0 + amount1);
	
	vec4 bgc = texture2DRect(bg, vec2(texcoord0.s,maskheight-texcoord0.t));
	//vec4 maskc2 = texture2DRect(mask, vec2(texcoord0.s+amount1.s,maskheight-texcoord0.t+amount1.t));
	
	
//	vec4 sample2 = texture2DRect(tex1, texcoord0 + amount2);
//	vec4 sample3 = texture2DRect(tex1, texcoord0 + amount3);
//	vec4 sample4 = texture2DRect(tex1, texcoord0 + amount4);
//	vec4 sample5 = texture2DRect(tex1, texcoord0 + amount5);
//	vec4 sample6 = texture2DRect(tex1, texcoord0 + amount6);
//	vec4 sample7 = texture2DRect(tex1, texcoord0 + amount7);
//	vec4 sample8 = texture2DRect(tex1, texcoord0 + amount8);

//	vec4 col=(sample0 + sample1 + sample2 + sample3 + sample4 + sample5 + sample6 + sample7 + sample8) / 9.0;
	vec4 col=(sample0 + sample1 ) / 2.0;
	//col=col*0.3;
	
	//col*=vec4(contrast,contrast,contrast,1);
	
	col.r=smoothstep(contrast,1.0,col.r);
	col.g=col.r;
	col.b=col.r;

	col.a=alpha;
	vec4 fc=bgc*texture2DRect(tex1,texcoord0)*col;
	
	
	float d=readDepth(texcoord0);
	
	//if(d==1.0 || d==0.0)
	if(bgc.g>0.95 && bgc.r<0.1 && bgc.b<0.1)
	{
		fc.a=0.0;
	}
	else
	{

	}

//	maskc.r=smoothstep(0.3,1.0,maskc.r);
//	if(maskc.r==0.0)fc.a=0.0;
//	else fc=vec4(1,0,0,1);

	gl_FragColor =fc; 
}
