#version 400

// Input texture that will get tonemapped
uniform sampler2D inputTex;

// Should we actually apply the tonemapping?
uniform float useToneMap;

// Should we use gamma correction?
uniform float useGamma;
uniform float gamma;

in vec2 fragTexCoord;
out vec4 result;

// This is the actual code that is executed.
void main( void )
{
	// Find the input pixel color
	vec4 fragColor = texture( inputTex, fragTexCoord );
	
	// Convert the color into luminance to apply the Reinhard operator
	float pixLuminance = 0.2126 * fragColor.x + 0.7152 * fragColor.y + 0.0722 * fragColor.z;
	fragColor = (useToneMap > 0) ? (fragColor / (1.0 + pixLuminance)) : fragColor;
	
	// Apply gamma correction
	fragColor = (useGamma > 0) ? vec4( pow( fragColor.x, float(1.0/gamma) ),
	                                   pow( fragColor.y, float(1.0/gamma) ),
	                                   pow( fragColor.z, float(1.0/gamma) ), 1.0f )
	                           : fragColor;
	
	// Output the result
	result = fragColor;
}