uniform sampler2D tonemappedTexture;
uniform sampler2D bloomTexture;
uniform sampler2D paperTexture;
uniform int paper;
uniform float paperInvSize;
uniform float invGamma;



void main()
{	
	vec2 p = gl_TexCoord[0].xy;
	
	vec3 main = texture2D(tonemappedTexture, p).rgb;
	vec3 bloom = texture2D(bloomTexture, p).rgb;
	
	vec3 paperf = vec3(float(paper) * 1.8) * texture2D(paperTexture, gl_FragCoord.xy * paperInvSize).rgb 
	           + vec3(1.0 - float(paper));
	
	vec3 finalColor = paperf * mix(main, bloom, 0.2);
	vec3 gammaCorrect = vec3(pow(finalColor.r, invGamma), pow(finalColor.g, invGamma), pow(finalColor.b, invGamma));
	gl_FragColor = vec4(gammaCorrect, 1.0);
}