1// $LastChangedDate: 2010-11-12 11:27:22 -0500 (Fri, 12 Nov 2010) $
2
3/*****************************************************************************
4 * Compute diffuse and specular factors.
5 * Returns total light intensity.
6 *****************************************************************************/
7float
8ComputeDiffuseSpecular( const vec3  ecPosition3,
9                        const float diffuseScale,
10                        const float specularScale,
11                        const float specularExponent )
12{
13    vec3 ecNormal  = normalize( gl_NormalMatrix * gl_Normal );              // surface normal
14    vec3 ecLight   = normalize( uni_light0_ecPosition.xyz - ecPosition3 );  // light vector
15    vec3 ecReflect = reflect( -ecLight, ecNormal );                         // reflected light vector from surface
16    vec3 ecView    = normalize( -ecPosition3 );                             // viewpoint
17    float diffuse  = max( dot( ecLight, ecNormal ), 0.0 );
18    // No specular if surface normal doesn't face VP.
19    if ( diffuse > 0.0 )
20    {
21        // Facing.
22        float specular = max( dot( ecReflect, ecView ), 0.0 );
23        specular = pow( specular, specularExponent );
24        return diffuse * diffuseScale + specular * specularScale;
25    }
26    else
27    {
28        // Not facing.
29        return diffuse * diffuseScale;
30    }
31}
32