varying vec2 		texcoord;
uniform sampler2D 	tex;
uniform sampler2D 	lum;
uniform vec4		params;

void main()
{
	/*params.x=0.4;
	params.y=0.4;
	params.z=0.4;
	params.w=0.5;
*/
	//float fWhiteCutoff=0.9;

/*	vec4 color=texture2D(tex, texcoord);
	float lumColor=texture2D(lum, texcoord).r;
	
	// saturate(color.xyz - params.xyz) * params.w;
	//float grey=clamp(dot(vec3(0.27, 0.67, 0.06),color.rgb),0.0,1.0);
	
	// Determine what the pixel's value will be after tone mapping occurs
	vec4 outcolor;
	outcolor.rgb = color.rgb * (1.0/(lumColor + 0.001));
	//color.rgb *= ( 1.0f + ( color.rgb / ( fWhiteCutoff * fWhiteCutoff ) ) );
 
	// Subtract out dark pixels
	outcolor.rgb -= params.xyz;

	// Clamp to 0
	outcolor = max(outcolor, 0.0);

	// Map the resulting value into the 0 to 1 range. Higher values for
	// BRIGHT_PASS_OFFSET will isolate lights from illuminated scene 
	// objects.
	outcolor.rgb /= (vec3(params.w)+outcolor.rgb); 
	//float grey=clamp(dot(vec3(0.27, 0.67, 0.06),outcolor.rgb),0.0,1.0);
	/*color.rgb -= 2.0;
	color = max(color,0.0);
	color.rgb = color.rgb * 10.0;*/
	
	vec4 color=texture2D(tex, texcoord);
	vec3 outcolor;
	
	// computes color lum
	float lum=dot(vec3(0.27, 0.67, 0.06),color.rgb);

	// remove color below white point
	/*if(lum<params.x)
		outcolor=vec3(0.0,0.0,0.0);
	else
		outcolor.rgb = color.rgb/(vec3(params.w)+color.rgb); // scale color upon white point (params.w=OFFSET)
*/
	// PROVA 
	//vec3 color=texture2D(tex, texcoord).rgb;
	float lum2=max(lum-params.x,0.0);	// cut off below white point
	outcolor=color.rgb*(lum2/lum);		// scales upon white point
	/*********/
	gl_FragColor.xyz=outcolor;
	
	//gl_FragColor.xyz = outcolor.rgb;//mix(outcolor.rgb,grey,0.5);//max(color.rgb,outcolor.rgb);//outcolor.rgb;//max(color.rgb,outcolor.rgb);
	//vec3 final=(color.xyz - params.xyz) * params.w;
	//gl_FragColor.xyz = clamp(final.xyz,0.0,1.0);
}
