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

//shader to draw dynamic object shadows

#version 150

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

uniform sampler2D		uGBuffer; //geometry buffer storing depth + normals
uniform float			uCamFarPlane; //scene camera far plane

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

in vec3					vViewPos; //view space position			

// ---------------------------------------------------------------------------
// function declarations

float calcShadow( in vec3 viewSpacePos );

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

out vec4				out_Color;

void main()
{	
	vec4 nrmDepth = texelFetch( uGBuffer, ivec2( gl_FragCoord.xy ), 0 );
	
	//----------------------------------------------------------------
	//calculate shadow occlusion

	//reconstruct pixel position from depth: frustum ray * depth
	//frustum ray = viewPos * ( camFarPlane/viewPos.z )

	float shadow = calcShadow( vViewPos * ( uCamFarPlane/-vViewPos.z * nrmDepth.a ) ); //takes as input the reconstructed pixel position

	if ( shadow > 0.9 )
		discard;

	out_Color = vec4( shadow, shadow, shadow, 0.0 );
}
