uniform sampler2D colorMap;
uniform sampler2D normalMap;

varying mat3 normalMatrix;
varying vec3 v;    
varying vec2 texCoord;

void main(void)  
{  
   vec3 N = normalize(texture2D(normalMap, texCoord).xyz * 2.0 - 1.0);
   N = normalize(normalMatrix * N);
   vec3 L = normalize(gl_LightSource[0].position.xyz - v);   
   vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)  
   vec3 R = normalize(reflect(-L,N));  
 
   vec4 color = texture2D(colorMap, texCoord);	
   //calculate Ambient Term:  
   vec4 Iamb = gl_LightSource[0].ambient + gl_FrontMaterial.emission + gl_LightModel.ambient;    
   Iamb.a = 1.0;
   
   //calculate Diffuse Term:  
	float diff = max(dot(N,L), 0.0);
	vec4 Idiff = vec4(diff, diff, diff, 0.0);   
	
   // calculate Specular Term:
   vec4 Ispec = vec4(0.0, 0.0, 0.0, 0.0);
   if (gl_FrontMaterial.shininess > 0.0)
   {
      Ispec = gl_FrontLightProduct[0].specular * pow(max(dot(R,E),0.0),gl_FrontMaterial.shininess);
      Ispec.a = 0.0;
   }
   
   // write Total Color:  
   gl_FragColor = clamp(color * (Idiff + Iamb + Ispec),0.0,1.0);
}