//
// Road specular+fresnel (more specular if looking along the ground)
// Per-fragment lighting
// Expects diffuseMap in first layer (TEXUNIT0) and stripemap in TEXUNIT1 (2nd layer in Racer shader)
//

void main(
  // In
  float2 tc0                : TEXCOORD0,
  float3 normal	            : TEXCOORD1,
  float3 inPosW             : TEXCOORD2,
  float  inReflectionFactor : TEXCOORD3,
  float3 inColor	        : COLOR,
  //float3 R                  : TEXCOORD4,
  // Out
  out float4 outColor : COLOR,
  uniform sampler2D   baseMap : TEXUNIT0,
  //uniform sampler2D   stripeMap : TEXUNIT1,
  //uniform samplerCUBE envMap  : TEXUNIT1,
  uniform float3 lightDirection,
  uniform float3 lightColor,
  uniform float3 eyePosW
)
{
  const float Ka=0.2;
  const float Kd=0.9;
  const float Ks=1.0;
  const float shininess=4.0;
  //const float3 lightColor=float3(1,1,1);

  // Use vertex color
  lightColor*=inColor;
  
  float4 baseCol=tex2D(baseMap,tc0);
  //float4 stripeCol=tex2D(stripeMap,float2(tc0.x/1,tc0.y));
  //baseCol-=stripeCol*1.0;
  
  // 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.xyz*lightColor;			// 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);

  // Use fresnel effect?
  //float3 specular=Ks*lightColor*specularLight*min(baseCol.r*1.0,1)*inReflectionFactor*1.5;
  float3 specular=Ks*lightColor*specularLight*min(baseCol.r*1.0,1);

  // Envmap
  //float3 envColor=texCUBE(envMap,R);
  //float3 rf=inReflectionFactor*baseCol.r;
  
  // Output total color
  outColor.rgb=diffuse+specular;
  //outColor.rgb=specular;
  //outColor.rgb=inColor;
  outColor.a=1.0;
}
