// All content © 2010-2011 DigiPen (USA) Corporation, all rights reserved.

//Fragment shader for projective textures.

#version 150
#extension GL_EXT_gpu_shader4:enable

// ---------------------------------------------------------------------------
//uniforms

//textures
uniform float			uCamFarPlane; //camera far plane
uniform float			uAlpha; //alpha of the texture map
uniform sampler2D		uDiffuse; //diffuse map
uniform sampler2D		uNormalDepth; //diffuse map
uniform sampler2D		uLightBuffer; //diffuse map
uniform mat4			uMatrix; //texture bias * projection * frustumLookAt * viewToWorld

// ---------------------------------------------------------------------------
// from vertex shader

in vec3					vViewPos; //camera space position

// ---------------------------------------------------------------------------
// output

out vec4				out_Color;

// ---------------------------------------------------------------------------

void main()
{	
	ivec2 tc = ivec2( gl_FragCoord.xy );

	//reconstruct pixel position from depth and get the surface-to-light ray: frustum ray * depth
	//frustum ray = viewPos * ( camFarPlane/viewPos.z )
	vec3 pixelPos = vViewPos * ( uCamFarPlane/-vViewPos.z * texelFetch( uNormalDepth, tc, 0 ).a ); //in view space

	//convert view space pixel position to homogenous texture coordinates and then project
	vec4 color = textureProj( uDiffuse, uMatrix * vec4( pixelPos, 1.0 ) );
	color.a *= uAlpha;

	out_Color = color * vec4( texelFetch( uLightBuffer, tc, 0 ).rgb, 1.0 );
}





