Graphics Reference
In-Depth Information
Phong Shading
Phong shading is a per-fragment color computation, and is a capability missing
from the fixed-function OpenGL system. In true Phong shading, the vertex
normals are interpolated across a graphics primitive, and the ADS lighting
model is applied separately at each individual pixel. In order to do that, the
lighting model's key variables must be evalu-
ated and set up as out variables during vertex
processing. The vertex shader code below sets
up the normal and position data for the ADS
lighting model function in out variables, so that
a fragment shader can interpolate these vari-
ables and use them in the ADSLightModel( )
function to compute the color. The actual frag-
ment shader that implements this lighting is
shown in Chapter 8. In Figure 6.5, you can see
the smooth specular highlight that you expect
from Phong shading.
Figure 6.5. The familiar teapot with Phong
shading.
out vec3 vNormal;
out vec3 vECpos;
void main( )
{
vNormal = normalize( uNormalMatrix * aNormal );
vECpos = ( uModelViewMatrix * aVertex ).xyz;
gl_Position = uModelViewProjectionMatrix * aVertex;
}
This specular computation uses the unit reflection vector, R , which
changes with each pixel. An alternative approach computes the “half angle”—
the vector H halfway between the light L and the eye E vectors—and uses the
cosine of the angle Φ between H and the normal N . If the angle Φ is zero, the
cosine is 1 and the light is reflected directly to the eye. As the angle increases,
the cosine decreases. Again, a power of that cosine is used to control the size
of the specular highlight. So we could replace the specular term in the model
by the expression
S = L S * M S * ( N H ) SH .
The half angle vector H is computed as the average of the unitized L and
E vectors, which in GLSL is expressed as normalize(L + E) , and the term
( N H ) SH that provides the shiny appearance of specular light is slightly differ-
Search WWH ::




Custom Search