//
// Common lighting functions
//

void LightingSun
(
  // Inputs
  float3 Ke,
  float3 Ka,
  float3 Kd,
  float3 Ks,
  float  shininess,
  float3 lightDirection,
  float3 lightColor,
  float3 lightAmbient,
  float3 P,               // Position (world)
  float3 N,               // Normal
  float3 eyePosW,
  
  // Outputs
  out float3 ambient,
  out float3 diffuse,
  out float3 specular
)
// Calculate mostly standard OpenGL lighting
// Use the results to modify your base texture color:
// outColor=textureCol*diffuse+ambient+emissive+specular
// The light is assumed a sun; all rays are parallel.
{
  // Ambient
  // Add a bit of sky influence, assuming down=dark and up=light
  float normalInfluence;
  //normalInfluence=(N.y+1.0)*0.5;
  normalInfluence=(N.y+4.0)*0.2;
  ambient=Ka*lightAmbient*normalInfluence;

  // Note that 'normal' is interpolated across the triangle, so it may not be normalized
  N=normalize(N);
  
  // Diffuse
  float3 L=lightDirection; //normalize(lightPosition)
  float  diffuseLight=max(dot(N,L),0);
  diffuse=Kd*lightColor*diffuseLight;

  // Specular
  float3 V=normalize(eyePosW-P);
  float3 H=normalize(L+V);          // Half vector
  float  specularLight=pow(max(dot(N,H),0),shininess);
  if(diffuseLight<=0)               // Convert 'if' to 'max' to avoid branch?
    specularLight=0;
  specular=Ks*lightColor*specularLight;
  // Clamp specular since Cg output values can not be too large
  //specular=min(specular,100);
//specular=float3(0,1,0);
//specular=lightColor;
}
