//$c12 OZ  02/22/07 tweaked threshold for shadow on incident light
//$c11 OZ  02/21/07 reworked the ambient light contribution that was making thin object shadow invisible
//$c10 FKP  11/03/06 clamp alpha so that is works with hdr (spr 344609)
//$c9 OZ  10/31/06 Scene Floor edge fading is now correctly computed
//$c8 OZ  10/30/06 now takes advantage of NVIDIA hardware shadow filtering
//$c7 OZ  10/20/06 mapscale is now a uniform
//$c6 OZ  10/19/06 fade shadow at low angle
//$c5 OZ  10/19/06 Shadow made slightly less dark on transparent floor
//$c4 OZ  10/17/06 and back to 1024 because of the FX1400..
//$c3 OZ  10/13/06 shadowmap in 2048
//$c2 FKP 10/09/06 envmap cleaning
//$c1 OZ  10/06/06 texture matrix cleaning
//$c0 OZ 10/05/06 created

// this line is needed to get the associated xml files
#define SW_PARAMETER_FILES "global.xml, commonlighting.xml"
#define SW_INCLUDE "utils.glsl,common.glsl"
#define SW_SHADERFORMATERIAL






// TEMPLATE description
//
// Lean and Mean shadow only for totally transparent floor:
//
// This is used for default scene without a material floor and Only when shadow is ON (otherwise nothing to render)
// The reflection is done separately as a full screen quad to avoid geometry floor clipping

/////////////////////////////////////////////////////////////////////////////////
// varying
// this parameters are passed from the vertex to the pixel shader
// note that they are not always used, depeding on the configuration

// ShadowMap
varying vec4 v_projCoordSM;


// Shadow fading at low angle
varying vec3 v_Normal;

// floor radius fading
varying vec3 v_Vertex;
varying vec4 v_FloorPoint;
/////////////////////////////////////////////////////////////////////////////////

#ifdef _VERTEX_PROGRAM
void main()
{
	// vertex position in clip space
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	
	v_Normal = gl_NormalMatrix * gl_Normal;

	// vertex position in eye space
	vec4 tVertex = gl_ModelViewMatrix * gl_Vertex;
	v_projCoordSM = gl_TextureMatrix[1] * tVertex;

	v_Vertex = tVertex.xyz;
	v_FloorPoint = gl_ModelViewMatrix * vec4(floorPoint.xyz, 1.0);
}
#endif //_VERTEX_PROGRAM
















#ifdef _FRAGMENT_PROGRAM
void main()
{
	// ShadowMap
	float shadowColor = GetShadowColor(shadowMap2D, v_projCoordSM.xyz, shadowMapScale);

	// radial blur to avoid brutal clipping of shadow and/or reflection
	float radialDist = 1.0 - distance(v_Vertex, v_FloorPoint.xyz)/floorPoint.w;

	float dpLight = clamp(abs(dot(normalize(lightDir0.xyz), v_Normal)*10.0)-0.5, 0.0, 1.0);
	float brightness = clamp(1.0 - (ambientLight.r*0.3 + ambientLight.g*0.59 + ambientLight.b*0.11), 0.0, 1.0);

	// need a clamp so that it does not introduce negative value with hdr mode
	float fAlpha = clamp(radialDist*(1.0-shadowColor), 0.0, 1.0);
	fAlpha = mix(0.0, fAlpha, dpLight);

	gl_FragColor = vec4(0.0,0.0,0.0,fAlpha*brightness);
}
#endif //_FRAGMENT_PROGRAM


