// Vertex Shader Program for IDL example "shader_lightsurf_doc.pro".
// Most of this program was generated by 3Dlabs' ShaderGen.
// Lines not generated by ShaderGen are surrounded by //***.

// File-scope globals
// Light Intensity Accumulators
vec4 Ambient;
vec4 Diffuse;
vec4 Specular;

//***
// Uniforms passed from shader_lightsurf_doc.pro
uniform float Time;
uniform int DirectionalLightIndex;

// Displacement map associated with the surface in shader_lightsurf_doc.pro
attribute float Displacement;
//***

// Perform Point light source lighting calcs
void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3)
{
   float nDotVP;       // normal . light direction
   float nDotHV;       // normal . light half vector
   float pf;           // power factor
   float attenuation;  // computed attenuation factor
   float d;            // distance from surface to light source
   vec3  VP;           // direction from surface to light position
   vec3  halfVector;   // direction of maximum highlights

   // Compute vector from surface to light position
   VP = vec3 (gl_LightSource[i].position) - ecPosition3;

   // Compute distance between surface and light position
   d = length(VP);

   // Normalize the vector from surface to light position
   VP = normalize(VP);

   // Compute attenuation
   attenuation = 1.0 / (gl_LightSource[i].constantAttenuation +
       gl_LightSource[i].linearAttenuation * d +
       gl_LightSource[i].quadraticAttenuation * d * d);

   halfVector = normalize(VP + eye);

   nDotVP = max(0.0, dot(normal, VP));
   nDotHV = max(0.0, dot(normal, halfVector));

   if (nDotVP == 0.0)
   {
       pf = 0.0;
   }
   else
   {
       pf = pow(nDotHV, gl_FrontMaterial.shininess);

   }
   Ambient  += gl_LightSource[i].ambient * attenuation;
   Diffuse  += gl_LightSource[i].diffuse * nDotVP * attenuation;
   Specular += gl_LightSource[i].specular * pf * attenuation;
}

vec3 fnormal(void)
{
    //Compute the normal
    vec3 normal = gl_NormalMatrix * gl_Normal;
    normal = normalize(normal);
    return normal;
}

void flight(in vec3 normal, in vec4 ecPosition, float alphaFade)
{
    vec4 color;
    vec3 ecPosition3;
    vec3 eye;

    ecPosition3 = (vec3 (ecPosition)) / ecPosition.w;
    eye = vec3 (0.0, 0.0, 1.0);

    // Clear the light intensity accumulators
    //***
    Ambient = gl_LightModel.ambient / 2.0;  // Use IDL's ambient light
    //***
    Diffuse  = vec4 (0.0);
    Specular = vec4 (0.0);

    //***
    pointLight(DirectionalLightIndex, normal, eye, ecPosition3);
    //***
    
    color = gl_FrontLightModelProduct.sceneColor +
      Ambient  * gl_FrontMaterial.ambient +
      Diffuse  * gl_FrontMaterial.diffuse;
    color += Specular * gl_FrontMaterial.specular;
    color = clamp( color, 0.0, 1.0 );
    gl_FrontColor = color;

    gl_FrontColor.a *= alphaFade;
}



void main (void)
{
    vec3  transformedNormal;
    float alphaFade = 1.0;

    // Eye-coordinate position of vertex, needed in various calculations
    vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;

    //***
    // Get vertex and displace by its corresponding value in
    // the displacement map. adjusted by time.
    vec4 vert = gl_Vertex;
    vert.z += Displacement * Time;

    gl_Position = gl_ModelViewProjectionMatrix * vert;
    //***
    
    transformedNormal = fnormal();
    flight(transformedNormal, ecPosition, alphaFade);
    
    //***
    // Make the displaced part use more red.
    gl_FrontColor.r = Displacement ? 1.0 : gl_FrontColor.r;
    //***
}

