//
// Bump car body fragment shader
//

float3 expand(float3 v) { return (v-0.5)*2; }

void main(float2 normalMapTexCoord : TEXCOORD0,
          float3 lightDir          : TEXCOORD1,
		  float3 R                 : TEXCOORD2,
		  // Texture maps
          uniform sampler2D   baseMap   : TEXUNIT0,
          uniform sampler2D   normalMap : TEXUNIT1,
          uniform samplerCUBE envMap    : TEXUNIT2,

             out float4 outColor : COLOR

//              uniform sampler2D   normalMap : TEXUNIT0,
//            uniform samplerCUBE normalizeCube)
              )
{
  // Normalizes light vector with normalization cube map
//  float3 lightTex = texCUBE(normalizeCube, lightDir).xyz;
//  float3 light = expand(lightTex);
  // Normalize using built-in function (lightDir gets interpolated)
  float3 light = normalize(lightDir);
  // Sample and expand the normal map texture	
  float3 normalTex = tex2D(normalMap, normalMapTexCoord).xyz;
  float3 normal = expand(normalTex);
  
  // Sample the base (diffuse) map
  float4 baseCol=tex2D(baseMap,normalMapTexCoord);

  // Diffuse lighting
  float brightNess=dot(normal,light);
  float4 diffColor = brightNess*baseCol; //*float4(1.0,0.8,0.4,1);

  // Envmap
  float4 reflectivity=0.1;
  float4 reflectedColor=texCUBE(envMap,R);
  
  // Ambient light
  float4 daColor=diffColor+0.1;

  // Alpha  
  outColor.a=1.0;
  
  // Mix reflection and diffuse/ambient
  outColor=lerp(daColor,reflectedColor,reflectivity);
  //outColor=float4(tangent.xyz,1);
  //outColor=baseCol;
}
