struct VertexOutputPosNorm {
  float4 position : TEXCOORD0;
  float3 normal	  : TEXCOORD1;
};


struct PixelOutput {
  float4 color : COLOR;
};

uniform float3 lightColor;
uniform float3 lightPosition;
uniform float3 eyePosition;
uniform float3 Ke;
uniform float3 Ka;
uniform float3 Kd;
uniform float3 Ks;
uniform float  shininess;

PixelOutput PerPixelLight(in VertexOutputPosNorm IN)
{
  PixelOutput OUT;
  float3 P = IN.position.xyz;
  float3 N = normalize(IN.normal);

  // Compute emissive term
  float3 emissive = Ke;

  // Compute ambient term
  float3 ambient = Ka;

  // Compute the diffuse term
  float3 L = normalize(lightPosition - P);
  float diffuseLight = max(dot(L, N), 0);
  float3 diffuse = Kd * lightColor * diffuseLight;

  // Compute the specular term
  float3 V = normalize(eyePosition - P);
  float3 H = normalize(L + V);
  float specularLight = pow(max(dot(H, N), 0), shininess);
  if (diffuseLight <= 0) specularLight = 0;
  float3 specular = Ks * lightColor * specularLight;

  OUT.color.xyz = emissive + ambient + diffuse + specular;
  OUT.color.w = 1;
  
  return OUT;
}

