uniform sampler2D Texture;
uniform sampler2D NormalMap;

uniform int NormalMapExists;

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

void main() 
{
	vec3 norm;
	
	vec4 diffuse;
	vec4 specular;
	
	vec3 cVec = normalize(camVec);
	vec3 lVec = normalize(lightVec);
	
	vec3 TexRGB = texture2D(Texture, texCoord.st).rgb;
	
	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);
	
	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * max( dot(lVec,norm),0.0);
	specular = gl_LightSource[0].specular * shineFactor;
	
	gl_FragColor = vec4(TexRGB,1.0)*gl_LightSource[0].ambient+(vec4(TexRGB,1.0)*diffuse+specular);
	
}