// $LastChangedDate: 2010-11-12 11:27:22 -0500 (Fri, 12 Nov 2010) $

/*****************************************************************************
 * Compute diffuse and specular factors.
 * Returns total light intensity. 
 *****************************************************************************/
float
ComputeDiffuseSpecular( const vec3  ecPosition3,
                        const float diffuseScale,
                        const float specularScale,
                        const float specularExponent )
{
    vec3 ecNormal  = normalize( gl_NormalMatrix * gl_Normal );              // surface normal
    vec3 ecLight   = normalize( uni_light0_ecPosition.xyz - ecPosition3 );  // light vector
    vec3 ecReflect = reflect( -ecLight, ecNormal );                         // reflected light vector from surface
    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;
    }
}
