// This is C6E1v_bulge from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 145-6.

// From page 145
float3 computeLighting(float3 lightPosition, 
                       float3 lightColor,
                       float3 Kd,
                       float  shininess,
                       float3 P,
                       float3 N,
                       float3 eyePosition) 
{
  // Compute the diffuse lighting
  float3 L = normalize(lightPosition - P);
  float diffuseLight = max(dot(N, L), 0);
  float3 diffuseResult = Kd * lightColor * diffuseLight;
  // Compute the specular lighting
  float3 V = normalize(eyePosition - P);
  float3 H = normalize(L + V);
  float3 specularLight = lightColor * pow(max(dot(N, H), 0), 
                                          shininess);
  if (diffuseLight <= 0) specularLight = 0;
  float3 specularResult = lightColor * specularLight;
  return diffuseResult + specularResult;
}

// From page 146
void C6E1v_bulge(float4 position  : POSITION,
                 float3 normal    : NORMAL,

             out float4 oPosition : POSITION,
             out float4 color     : COLOR,

         uniform float4x4 modelViewProj,
         uniform float    time,
         uniform float    frequency,
         uniform float    scaleFactor,
         uniform float3   Kd,
         uniform float    shininess,
         uniform float3   eyePosition,
         uniform float3   lightPosition,
         uniform float3   lightColor)
{
  float displacement = scaleFactor * 0.5 *
                       sin(position.y * frequency * time)+1;
  float4 displacementDirection = float4(normal.x, normal.y, 
                                        normal.z, 0);
  float4 newPosition = position + 
                       displacement * displacementDirection;
  oPosition = mul(modelViewProj, newPosition);
  color.xyz = computeLighting(lightPosition, lightColor,
                              Kd, shininess,
                              newPosition.xyz, normal, 
                              eyePosition);
  color.w = 1;
}
