//
// Diffuse/ambient/specular lighting+single texture
// Per-fragment lighting
//

void main(
  // In
  float2 tc0    : TEXCOORD0,
  float3 normal : TEXCOORD1,
  float3 inPosW : TEXCOORD2,
  float  inReflectionFactor : TEXCOORD3,
  // Out
  out float4 outColor : COLOR,
  uniform sampler2D baseMap,
  uniform float3 lightDirection,
  uniform float3 eyePosW
)
{
  const float Ka=0.2;
  const float Kd=0.8;
  const float Ks=1.0;
  const float shininess=80.0;
  const float3 lightColor=float3(1,1,1);
 
  float3 baseCol=tex2D(baseMap,tc0);
  
  // Note that 'normal' is interpolated across the triangle, so it may not be normalized
  float3 N=normalize(normal);
  
  // Compute diffuse term
  float3 L=lightDirection;
  float diffuseLight=max(dot(N,L),0);
  float3 diffuse=(Kd*diffuseLight+Ka)*baseCol;			// Ambient+diffuse

  // Compute the specular term
  float3 V=normalize(eyePosW-inPosW);
  float3 H=normalize(L+V);

  float specularLight;
  if (diffuseLight <= 0)
    specularLight=0;
  else
    specularLight=pow(max(dot(N,H),0),shininess);

  float3 specular=Ks*lightColor*specularLight;

  // Output total color
  outColor.rgb=diffuse+specular;
  //outColor.rgb=specular;
  //outColor.rgb=normalize(eyePosW-inPosW);
  //outColor.rgb=normal;
  outColor.rgb=inReflectionFactor;
  outColor.a=1.0;
}
