// $LastChangedDate: 2009-05-22 16:47:41 -0500 (Fri, 22 May 2009) $

/*****************************************************************************
 * Compute diffuse and specular factors.
 * Returns total light intensity. 
 *****************************************************************************/
float
ComputeDiffuseSpecular( const vec3  ecPosition3,
                        const float diffuseScale,
                        const float specularScale,
                        const float specularExponent )
{
    const vec3 ecNormal    = normalize( gl_NormalMatrix * gl_Normal );              // surface normal
    const vec3 ecLight     = normalize( uni_light0_ecPosition.xyz - ecPosition3 );  // light vector
    const vec3 ecReflect   = reflect( -ecLight, ecNormal );                         // reflected light vector from surface
    const vec3 ecView      = normalize( -ecPosition3 );                             // viewpoint
    float diffuse = max( dot( ecLight, ecNormal ), 0.0 );
    // No specular if surface normal doesn't face VP.
    if ( diffuse > 0.0 )
    {
        // Facing.
        float specular = max( dot( ecReflect, ecView ), 0.0 );
        specular = pow( specular, specularExponent );
        return diffuse * diffuseScale + specular * specularScale;
    }
    else
    {
        // Not facing.
        return diffuse * diffuseScale;
    }
}
