uniform sampler2DRect uOutgoingTexture;
uniform sampler2DRect uIncomingTexture;
uniform float uAlpha;
uniform vec2 uIntensity;

void main()
{
	// Get the current pixel color (by mixing outgoing and incoming textures)...
	
	vec2 xy = gl_TexCoord[0].xy;
	vec4 outgoingColor = texture2DRect(uOutgoingTexture,xy);
	vec4 incomingColor = texture2DRect(uIncomingTexture,xy); 
	vec4 color = mix(outgoingColor,incomingColor,uAlpha);
	
	// Calculate the brightness of the pixel...
	
	vec3 rgb = vec3(color);
	vec3 gray = vec3(0.299,0.587,0.114);
	float brightness = dot(rgb,gray);
	
	// If it is brighter than the intensity, then mix with overexposed (slightly bluish) color instead...
	
	float factor = smoothstep(uIntensity.x,uIntensity.y,brightness);
	
	vec4 overexposed = color + vec4(0.3,0.3,0.9,0.0);
	overexposed *= 1.0+factor;
	gl_FragColor = mix(color,overexposed,factor);
}
