#version 400

// Information from the vertex shader about our fragment location
in vec4 esFragPos;
in vec4 esFragNormal;

// Where to store the colors we compute
layout(location = IGLU_COLOR0) out vec4 outAmbient;
layout(location = IGLU_COLOR2) out vec4 outNorm;
layout(location = IGLU_COLOR1) out vec4 outShadow;
layout(location = IGLU_COLOR3) out vec4 outLambert;

uniform vec4 color;                 // Surface color (we treat as either ambient or diffuse)
uniform vec4 esLightPos;            // Location of the light, relative to the eye
uniform mat4 shadMapMatrix;         // Our shadow map matrix
uniform sampler2DShadow shadowTex;  // Our shadow map texture.  


void main( void )
{	
	// Transform from eye-space to shadow map texture coordinates
	vec4 smTexCoord = shadMapMatrix * esFragPos;
	smTexCoord /= smTexCoord.w;
	
	// Query depth from texture.  
	float isShadowed     = texture(shadowTex, smTexCoord.xyz); 
	
	// Find simple vectors for basic shading
	vec3 toLight = normalize( esLightPos.xyz - esFragPos.xyz);
	vec3 toEye   = normalize( -esFragPos.xyz );
	float NdotL  = max( 0.0f, dot( normalize(esFragNormal.xyz), toLight ) );
	
	// Output our results
	outAmbient = vec4( isShadowed * color.xyz, 1.0f );  
	outShadow  = vec4( vec3(isShadowed), 1.0 );
	outNorm    = vec4( 0.5*normalize(esFragNormal.xyz)+0.5, 1.0 );
	outLambert = vec4( isShadowed * NdotL * color.xyz, 1.0 );
}