/*
Bump mapping shader without parallax mapping but with shadows for single directional light source
*/

uniform sampler2D Texture;
uniform sampler2D NormalMap;
uniform int NormalMapExists;

varying vec2 texCoord;
varying vec3 camVec;
varying vec3 lightVec;

//For shadows
uniform sampler2D ShadowMap;
uniform float ShadowDepth;

varying vec4 vPos;
varying vec4 gPos;

float shadowCoef()
{

	vec4 shadow_coord = gl_TextureMatrix[2]*vPos;

	shadow_coord.w = shadow_coord.z;
		
	// get the stored depth
	float shadow_d = texture2D(ShadowMap, shadow_coord.xy).x;
	
	// get the difference of the stored depth and the distance of this fragment to the light
	float diff = min(shadow_d - shadow_coord.z,0.0);

	float shadow_coef = clamp( 20*ShadowDepth*diff + 1.0, 0.0, 1.0);	//[0..1]
	
	return shadow_coef;
}

void main() 
{

	vec3 norm;
	
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	
	vec3 TexRGB = texture2D(Texture, texCoord.st).rgb;
	
	vec3 cVec = normalize(camVec);
	
	if (NormalMapExists == 0)
	{
		norm = vec3(0.0,0.0,1.0);
	}
	else
	{
		norm = (texture2D(NormalMap, texCoord.st).rgb - 0.5) * 2.0;
	}
	
	float shineFactor = 15.0 * pow(max(dot(cVec, norm),0.0),gl_FrontMaterial.shininess) / (129.0 - gl_FrontMaterial.shininess);
	
	float shadow_coef = shadowCoef();
	
	ambient = gl_LightSource[0].ambient;
	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * min( max(dot(lightVec,norm),0.0),shadow_coef);
	specular = gl_LightSource[0].specular * min(shineFactor,shadow_coef);
	
	
	gl_FragColor = vec4(TexRGB,1.0)*max(ambient,diffuse)+specular;
	
	
}
