Module 4 Computer Graphics
Module 4 Computer Graphics
VIEWING
we have completed our discussion of the first half of the synthetic camera model—
specifying objects in three dimensions. We now investigate the multitude of ways in
which we can describe our virtual camera. Along the way, we examine related topics,
such as the relationship between classical viewing techniques and computer viewing
and how projection is implemented using projective transformations.
There are three parts to our approach. First, we look at the types of views that
we can create and why we need more than one type of view. Then we examine how
an application program can specify a particular view within OpenGL. We will see
that the viewing process has two parts. In the first, we use the model-view matrix to
switch vertex representations from the object frame in which we defined our objects
to their representation in the eye frame, in which the camera is at the origin. This
representation of the geometry will allow us to use canonical viewing procedures.
The second part of the process deals with the type of projection we prefer (parallel
or perspective) and the part of the world we wish to image (the clipping or view
volume). These specifications will allow us to form a projection matrix that is
concatenated with the model-view matrix. Once more, we use a simple example
program to demonstrate how the OpenGL API handles viewing. Finally, we derive
the projection matrices that describe the most important parallel and perspective
views.
235
236 Chapter 5 Viewing
Objec t
Projector
Projection plane
COP
Objec t
Projector
DOP
Projection plane
When we introduced the synthetic-camera model in Chapter 1, we pointed out the similarities between classical and
computer viewing. The basic elements in both cases are the same. We have objects, a viewer, projectors, and a projection
plane (Figure 5.1). The projectors meet at the center of projection (COP). The COP corresponds to the center of the
lens in the camera or in the eye, and in a computergraphics system, it is the origin of the camera frame for perspective
views. All standard graphics systems follow the model that we described in Chapter 1, which is based on geometric
optics. The projection surface is a plane, and the projectors are straight lines. This situation is the one we usually
encounter and is straightforward to implement, especially with our pipeline model.
Both classical and computer graphics allow the viewer to be an infinite distance from the objects. Note that as we
move the COP to infinity, the projectors become parallel and the COP can be replaced by a direction of projection
(DOP), as shown in Figure 5.2. Note also that as the COP moves to infinity, we can leave the projection plane fixed and
the size of the image remains about the same, even though the COP is infinitely far from the objects. Views with a finite
COP are called perspective views; views with a COP at infinity are called parallel views. For parallel views, the origin
of the camera frame usually lies in the projection plane.
Color Plates 9 and 10 show a parallel and a perspective rendering, respectively. These plates illustrate the
importance of having both types of view available in applications such as architecture; in an API that supports both types
of viewing, the user can switch easily between various viewing modes. Most modern APIs support both parallel and
perspective viewing. The class of projections produced by these systems is known as planar geometric projections
because the projection surface is a plane and the projectors are lines. Both perspective and parallel projections preserve
lines; they do not, in general, preserve angles. Although the parallel views are the limiting case of perspective viewing,
5.1 Classical and Computer Viewing 237
both classical and computer viewing usually treat them as separate cases. For classical views, the techniques that people
use to construct the two types by hand are different, as anyone who has taken a drafting class surely knows. From the
computer perspective, there are differences in how we specify the two types of views. Rather than looking at a parallel
view as the limit of the perspective view, we derive the limiting equations and use those equations directly to form the
corresponding projection matrix. In modern pipeline architectures, the projection matrix corresponding to either type of
view can be loaded into the pipeline.
Although computer-graphics systems have two fundamental types of viewing (parallel and perspective), classical
graphics appears to permit a host of different views, ranging from multiview orthographic projections to one-, two-, and
threepoint perspectives. This seeming discrepancy arises in classical graphics as a result of the desire to show a specific
relationship among an object, the viewer, and the projection plane, as opposed to the computer-graphics approach of
complete independence of all specifications.
each case with the projection plane parallel to one of the principal faces of the object. Usually, we use three views—
such as the front, top, and right—to display the object. The reason that we produce multiple views should be clear from
Figure 5.5. For a box-like object, only the faces parallel to the projection plane appear in the image. A viewer usually
5.1 Classical and Computer Viewing 239
needs more than two views to visualize what an object looks like from its multiview orthographic projections.
Visualization from these images can require skill on the part of the viewer. The importance of this type of view is that
it preserves both distances and angles, and because there is no distortion of either distance or shape, multiview
orthographic projections are well suited for working drawings.
FIGURE 5.6 Axonometric projections. (a) Construction of trimetric-view projections. (b) Top view. (c) Side view.
240 Chapter 5 Viewing
orthogonal to the projection plane, as shown in Figure 5.6, but the projection plane can have any orientation with respect
to the object. If the projection plane is placed symmetrically with respect to the three principal faces that meet at a corner
of our rectangular object, then we have an isometric view. If the projection plane is placed symmetrically with respect
to two of the principal faces, then the view is dimetric. The general case is a trimetric view. These views are shown in
Figure 5.7. Note that in an isometric view, a line segment’s length in the image space is shorter than its length measured
in the object space. This foreshortening of distances is the same
FIGURE 5.8 Oblique view. (a) Construction. (b) Top view. (c) Side view.
in the three principal directions, so we can still make distance measurements. In the dimetric view, however, there are
two different foreshortening ratios; in the trimetric view, there are three. Also, although parallel lines are preserved in
the image, angles are not. A circle is projected into an ellipse. This distortion is the price we pay for the ability to see
more than one principal face in a view that can be produced easily either by hand or by computer. Axonometric views
are used extensively in architectural and mechanical design.
see more than one principal face of the object. Oblique views are the most difficult to construct by hand. They are also
somewhat unnatural. Most physical viewing devices, including the human visual system, have a lens that is in a fixed
relationship with the image plane—usually, the lens is parallel to the plane. Although these devices produce perspective
views, if the viewer is far from the object, the views are approximately parallel, but orthogonal, because the projection
plane is parallel to the lens. The bellows camera that we used to develop the synthetic-camera model in Section 1.6 has
the flexibility to produce approximations to parallel oblique views. One use of such a camera is to create images of
buildings in which the sides of the building are parallel rather than converging as they would in an image created with
a orthogonal view with the the camera on the ground.
From the application programmer’s point of view, there is no significant difference among the different parallel
views. The application programmer specifies a type of view—parallel or perspective—and a set of parameters that
describe the camera. The problem for the application programmer is how to specify these parameters in the viewing
procedures so as best to view an object or to produce a specific classical view.
pyramid. This symmetry is caused by the fixed relationship between the back ( retina ) and lens of the eye for human
viewing, or between the back and lens of a camera for standard cameras, and by similar fixed relationships in most
physical situations. Some cameras, such as the bellows camera, have movable film backs and can produce general
perspective views. The model used in computer graphics includes this general case.
The classical perspective views are usually known as one-, two-, and three-point perspectives. The differences
among the three cases are based on how many of the three principal directions in the object are parallel to the projection
plane. Consider the three perspective projections of the building shown in Figure 5.10. Any corner of the building
includes the three principal directions. In the most general case— the three-point perspective—parallel lines in each of
the three principal directions converges to a finite vanishingpoint (Figure 5.10(a)). If we allow one of the principal
directions to become parallel to the projection plane, we have a two-point projection (Figure 5.10(b)), in which lines in
only two of the principal directions converge. Finally, in the one-point perspective (Figure 5.10(c)), two of the principal
directions are parallel to the projection plane, and we have only a single vanishing point. As with parallel viewing, it
should be apparent from the programmer’s point of view that the three situations are merely special cases of general
perspective viewing, which we implement in Section 5.4.
ticular type of view, the application programmer may well have to determine where to place the
camera.
In terms of the pipeline architecture, viewing consists of two fundamental operations. First,
we must position and orient the camera. This operation is the job of the model-view
transformation. After vertices pass through this transformation, they are represented in eye or
camera coordinates. The second step is the application of the projection transformation. This step
applies the specified projection—orthographic or perspective—to the vertices and puts objects
within the specified clipping volume in a normalized clipping volume. We will examine these
steps in detail in the next few sections, but at this point it would help to review the default camera,
that is, the camera that OpenGL uses if we do not specify any viewing functions.
OpenGL starts with the camera at the origin of the object frame, pointing in the negative z-
direction. This camera is set up for orthogonal views and has a viewing volume that is a cube,
centered at the origin and with sides of length 2. The default projection plane is the plane z = 0 and
the direction of the projection is along the zaxis. Thus, objects within this box are visible and
projected as shown in Figure 5.11. Until now, we were able to ignore any complex viewing
procedures by exploiting our knowledge of this camera. Thus, we were able to define objects in
the application programs that fit inside this cube and we knew that they would be visible. In this
approach, both the model-view and projection matrices were left as the default identity matrices.
Subsequently, we altered the model-view matrix, initially an identity matrix, by rotations and
translations, so as to place the camera where we desired. The parameters that we set in glOrtho
alter the projection matrix, also initially an identity matrix, so as to allow us to see objects inside
an arbitrary right parallelepiped. In this chapter, we will generate a wider variety of views by using
the model-view matrix to position the camera and the projection matrix to produce both
orthographic and perspective views.
Projection plane
z
We conclude the chapter with a short discussion of the two most important
methods for handling global lighting effects: ray tracing and radiosity.
focus on a simpler model, based on the Phong lighting model, that provides a compromise between
physical correctness and efficient calculation. We will consider the rendering equation, radiosity,
and ray tracing in greater detail in Chapter 12.
Rather than looking at a global energy balance, we follow rays of light from light-emitting (or
self-luminous) surfaces that we call light sources. We then model what happens to these rays as
they interact with reflecting surfaces in the scene. This approach is similar to ray tracing, but we
consider only single interactions between light sources and surfaces and do not consider the
possibility that light from a source may be blocked from reaching the surface by another surface.
There are two independent parts of the problem. First, we must model the light sources in the
scene. Then we must build a reflection model that describes the interactions between materials and
light.
To get an overview of the process, we can start following rays of light from a point source, as
shown in Figure 6.2. As we noted in Chapter 1, our viewer sees only the light that leaves the source
and reaches her eyes—perhaps through a complex path and multiple interactions with objects in
the scene. If a ray of light enters her eye directly from the source, she sees the color of the source.
If the ray of light hits a surface visible to our viewer, the color she sees is based on the interaction
between the source and the surface material: She sees the color of the light reflected from the
surface toward her eyes.
In terms of computer graphics, we can place the projection plane between the center of
projection and the objects, as shown in Figure 6.3. Conceptually, the clipping window in this plane
is mapped to the display; thus, we can think of the projection plane as ruled into rectangles, each
corresponding to a pixel on the display.
292 Chapter 6 Lighting and Shading
Because we only need to consider light that enters the camera by passing through the center of projection, we can start
at the center of projection and follow a projector though each pixel in the clipping window. If we assume that all our
surfaces are opaque, then the color of the first surface intersected along each projector determines the color of the
corresponding pixel in the frame buffer.
Note that most rays leaving a light source do not contribute to the image and are thus of no interest to us. Hence, by
starting at the center of projection and casting rays we have the start of an efficient ray tracing method, one that we
explore further in Section 6.10 and Chapter 13.
Figure 6.2 shows both single and multiple interactions between rays and objects. It is the nature of these interactions
that determines whether an object appears red or brown, light or dark, dull or shiny. When light strikes a surface, some
of it is absorbed and some of it is reflected. If the surface is opaque, reflection and absorption account for all the light
striking the surface. If the surface is translucent, some of the light is transmitted through the material and emerges to
interact with other objects. These interactions depend on the wavelength of the light. An object illuminated by white
light appears red because it absorbs most of the incident light but reflects light in the red range of frequencies. A shiny
object appears so because its surface is smooth. Conversely, a dull object has a rough surface. The shading of objects
also depends on the orientation of their surfaces, a factor that we will see is characterized by the normal vector at each
point. These interactions between light and materials can be classified into the three groups depicted in Figure 6.4.
1. Specular surfaces appear shiny because most of the light that is reflected or scattered is
in a narrow range of angles close to the angle of reflection. Mirrors are perfectly specular
surfaces; the light from an incoming light ray may be partially absorbed, but all reflected
light emerges at a single angle, obeying the rule that the angle of incidence is equal to the
angle of reflection.
6.1 Light and Matter
293
FIGURE 6.4 Light–material interactions. (a) Specular surface. (b) Diffuse surface. (c) Translucent surface.
2. Diffuse surfaces are characterized by reflected light being scattered in all directions. Walls
painted with matte or flat paint are diffuse reflectors, as are many natural materials, such
as terrain viewed from an airplane or a satellite. Perfectly diffuse surfaces scatter light
equally in all directions and thus appear the same to all viewers.
3. Translucent surfaces allow some light to penetrate the surface and to emerge from
another location on the object. This process of refraction characterizes glass and water.
Some incident light may also be reflected at the surface.
From a physical perspective, the reflection, absorption, and transmission of light at the surface
of a material is described by a single function called the Bidirectional Reflection Distribution
Function (BRDF). Consider a point on a surface. Light energy potentially can arrive at this
location from any direction and be reflected and leave in any direction. Thus, for every pair of
input and output directions, there will be a value that is the fraction of the incoming light that is
reflected in the output direction. The BDRF is a function of five variables: the frequency of light,
the two angles required to describe the direction of the input vector, and the two angles required
to describe the direction of the output vector. For a perfectly diffuse surface, the BDRF is
simplified because it will have the same value for all possible output vectors. For a perfect
reflector, the BRDF will only be nonzero when the angle of incidence is equal to the angle of
reflection.
A real physical surface is never perfectly diffuse nor perfectly specular. Although we could
approach light–material interactions by modeling the BDRF for materials of interest, the subject
is not only complex but does not lead to efficient computational models. Instead, we use models
that lead to fast computations, especially when we employ them in a pipeline architecture. These
models are based on adding together the contributions of simple models of diffuse, specular, and
ambient reflections and, if necessary, accounting for transmission. We develop models for these
idealized interactions in Section 6.3 and Chapter 9.
First, we consider modeling of light sources.
y bulb, can also reflect some light that is incident on it from the surrounding environment.
I We neglect x the emissive term in our simple models. When we discuss OpenGL lighting
in Section 6.7, we will see that we can add a self-emission term.
If we consider a source such as the one shown in Figure 6.5, we can look at it as an
object with a surface. Each point (x, y, z) on the surface can emit light
p zthat is characterized by the direction of emission (θ, φ) and the intensity of energy
FIGURE 6.5 Light source. emitted at each wavelength λ. Thus, a general light source can be
characterized by a six-variable illumination function I(x, y, z, θ, φ, λ). Note that we
need two angles to specify a direction, and we are assuming that each frequency can
be considered independently. From the perspective of a surface illuminated by this
source, we can obtain the total contribution of the source (Figure 6.6) by integrating
over its surface, a process that accounts for the emission angles that reach this surface
and must also account for the distance between the source and the surface. For a
distributed light source, such as a light bulb, the evaluation of this integral is difficult,
whether we use analytic or numerical methods. Often, it is easier to model the
distributed source with polygons, each of which is a simple source, or with an
approximating set of point sources.
We consider four basic types of sources: ambient lighting, point sources,
spotlights, and distant light. These four lighting types are sufficient for rendering most
simple scenes.
p1
I (x 1 , y1 , z 1 , 1, 1, ) p2
I (x 2 , y 2 , z 2 , 2, 2, )
as having three components—red, green, and blue—and can use each of the three color sources to
obtain the corresponding color component that a human observer sees. Thus, we can describe a
color source through a three-component intensity or illumination function L = (Lr, Lg, Lb),
each of whose components is the intensity of the independent red, green, and blue components.
Thus, we use the red component of a light source for the calculation of the red component of the
image. Because light–material computations involve three similar but independent calculations,
we tend to present a single scalar equation, with the understanding that it can represent any of the
three color components. Rather than write what will turn out to be identical expressions for each
component of L, we will use the the scalar L to denote any of the its components. That is, L ∈ {Lr,
Lg, Lb}.
1
. Measuring all the light information at each point in an environment determines the light field, which can be used
to create new images. Its capture, which is a complex and expensive data-rich process, is the subject of much present
research. See Lev [96].
296 Chapter 6 Lighting and Shading
We use L(p0) to refer to any of the components. The intensity of illumination received
from a point source located at p0 at a point p is proportional to the inverse square of
the distance from the source. Hence, at a point p (Figure 6.7), any component of the
intensity of light received from the point source is given by function of the form
1 L(p, p0)
= |p − p0 |2L(p0).
φ, where the exponent e sun strikes all objects that are in close proximity to one another at the same angle.
(Figure 6.11) determines Figure 6.12 illustrates that we are effectively replacing a point source of light with a
how rapidly the light source that illuminates objects with parallel rays of light—a parallel source. In
intensity drops off. practice, the calculations for distant light sources are similar to the calculations for
As we will see parallel projections; they replace the location of the light source with the direction
of the light source. Hence, in homogeneous coordinates, the location of a point light
throughout this chapter,
source at p0 is represented internally as a four-dimensional column matrix:
cosines are convenient
functions for lighting
p ,
calculations. If u and v
are any unit-length
vectors, we can compute ps
the cosine of the angle θ
between them with the
dot product cos θ = u . v,
Is
a calculation that requires
only three multiplications FIGURE 6.9 Spotlight.
and two additions.
Intensity
6.2.5 Distant Light
Sources
Most shading
calculations require the
direction from the point
on the surface to the light
source position. As we FIGURE 6.10 Attenuation of a spotlight.
move across a surface,
calculating the intensity Intensity
at each point, we should
recompute this vector
repeatedly—a
computation that is a
significant part of the
lighting calculation.
However, if the light FIGURE 6.11 Spotlight exponent.
source is far from the
surface, the vector does
not change much as we
move from point to point,
just as the light from the
298 Chapter 6 Lighting and Shading
source.
x
⎡ ⎤
y p0
= ⎢⎢⎢⎣
⎥⎥⎥⎦ .
z 0
The graphics system can carry out rendering calculations more efficiently for distant
light sources than for near ones. Of course, a scene rendered with distant light
sources looks different than a scene rendered with near light sources. Fortunately,
OpenGL allows both types of sources.
n
v 6.3 THE PHONG LIGHTING MODEL
The lighting model that we present was introduced by Phong and later modified by r
I
Blinn. It has proved to be efficient and to be a close enough approximation to physical
reality to produce good renderings under a variety of lighting conditions and material
p properties. The Phong-Blinn or (modified Phong) model is the basis for lighting and
shading in graphics APIs and is implemented on virtually all graphics cards.
FIGURE 6.13 Vectors used by The Phong lighting model uses the four vectors shown in Figure 6.13 to calculate
the Phong model. a color for an arbitrary point p on a surface. If the surface is curved, all four vectors can change as we
move from point to point. The vector n is the normal at p; we discuss its calculation
in Section 6.4. The vector v is in the direction from p to the viewer or COP. The
vector l is in the direction of a line from p to an arbitrary point on the source for a
distributed light source or, as we are assuming for now, to the point-light source.
Finally, the vector r is in the direction that a perfectly reflected ray from l would
take. Note that r is determined by n and l. We calculate it in Section 6.4.
The Phong model supports the three types of light–material interactions—
ambient, diffuse, and specular—that we introduced in Section 6.1. Suppose that we
299
have a set of point sources. Each source can have separate ambient, diffuse, and
specular components for each of the three primary colors.
From a physical perspective it may appear somewhat strange that a light source
has three independent colors for each of the three types of lighting. However,
remember that our goal is to create realistic shading effects in as close to real time
as possible, rather than trying to model the BRDF as accurately as possible. Hence,
we use tricks with a local model to simulate effects that can be global in nature.
The ambient source color represents the the interaction of a light source with the
surfaces in the environment whereas the the specular source color is designed to
produce the desired color of a specular highlight.
Thus, if our light-source model has separate ambient, diffuse, and specular
terms, we need nine coefficients to characterize a light source at any point p on the
surface. We can place these nine coefficients in a 3 × 3 illumination array for the ith
light source:
300 Chapter 6 Lighting and Shading
We obtain the total intensity by adding the contributions of all sources and, possibly, a global ambient term.
Thus, the red term is
Ir Iar,
i where Iar is the red component of the global ambient light.
We can simplify our notation by noting that the necessary computations are the same for each source and
for each primary color. They differ depending on whether we are considering the ambient, diffuse, or specular
terms. Hence, we can omit the subscripts i, r, g, and b. We write
with the understanding that the computation will be done for each of the primaries and each source; a global
ambient term can be added at the end.
6.3 The Phong Lighting Model 301
and thus
Ia = kaLa.
FIGURE 6.14 Rough surface.
Here La can be any of the individual light sources, or it can be a global ambient term.
A surface has, of course, three ambient coefficients—kar, kag, and kab—and they
can differ. Hence, for example, a sphere appears yellow under white ambient light if
its blue ambient coefficient is small and its red and green coefficients are large.
d n
(a) d/cos
(b )
FIGURE 6.16 Vertical contributions by Lambert’s law. (a) At noon. (b) In the afternoon.
cos θ = l . n.
If we add in a reflection coefficient kd representing the fraction of incoming diffuse light that is reflected, we have
the diffuse reflection term:
Id = kd(l . n)Ld.
If we wish to incorporate a distance term, to account for attenuation as the light travels a distance d from the
source to the surface, we can again use the quadratic attenuation term:
= + k d+ . n)Ld.
Id 2(l a bd cd
There is a potential problem with this expression because (l . n)Ld will be negative
if the light source is below the horizon. In this case, we want to use zero rather than a
negative value. Hence, in practice we use max((l . n)Ld, 0).
Whereas a diffuse surface is rough, a specular surface is smooth. The smoother the
surface is, the more it resembles a mirror. Figure 6.18 shows that as the surface gets
smoother, the reflected light is concentrated in a smaller range of angles centered face.
=2
=5
about the angle of a perfect reflector—a mirror or a perfectly specular surface. Modeling specular surfaces realistically
can be complex because the pattern by which the light is scattered is not symmetric. It depends on the wavelength of
the incident light, and it changes with the reflection angle.
Phong proposed an approximate model that can be computed with only a slight increase over the work done for
diffuse surfaces. The model adds a term for specular reflection. Hence, we consider the surface as being rough for the
diffuse term and smooth for the specular term. The amount of light that the viewer sees depends on the angle φ between
r, the direction of a perfect reflector, and v, the direction of the viewer. The Phong model uses the equation
Is = ksLs cosα φ.
The coefficient ks (0 ≤ ks ≤ 1) is the fraction of the incoming specular light that is reflected. The exponent α is a shininess
coefficient. Figure 6.19 shows how, as α is increased, the reflected light is concentrated in a narrower region centered
on the angle of a perfect reflector. In the limit, as α goes to infinity, we get a mirror; values in the range 100 to 500
correspond to most metallic surfaces, and smaller values (< 100) correspond to materials that show broad highlights.
The computational advantage of the Phong model is that if we have normalized r and n to unit length, we can again
use the dot product, and the specular term becomes Is = ksLsmax((r . v)α, 0).
We can add a distance term, as we did with diffuse reflections. What is referred to as the Phong model, including the
distance term, is written
= + 1+ 2
. n, 0) + ksLsmax((r . v)α, 0)) + kaLa.
I (kdLdmax(l
a bd cd
This formula is computed for each light source and for each primary.
304 Chapter 6 Lighting and Shading
It might seem counter intuitive to have a single light source characterized by different amounts of red, green, and
blue light for the ambient, diffuse and specular
terms in the lighting model. However, the white highlight we might see on a red ball vectors. Here we have
is the distorted reflection of a light source or perhaps some other bright white object defined ψ as the angle
in the environment. To calculate this highlight correctly would require a global rather between n and h, the halfway
than local lighting calculation. Because we cannot solve the full rendering equation, angle. When v lies in the
we instead use various tricks in an attempt to obtain realistic renderings, one of which same plane as do l, n, and r,
is to allow different ambient, specular, and diffuse lighting colors. we can show (see Exercise
Consider, for example, an environment with many objects. When we turn on a n
h
light, some of that light hits a surface directly. These contributions to the image can
ψ
be modeled with specular and diffuse components of the source. However, much of r
the rest of the light from the source is scattered from multiple reflections from other
objects and makes a contribution to the light received at the surface under
consideration. We can approximate this term by having an ambient component
associated with the source. The shade that we should assign to this term depends on
both the color of the source and the color of the objects in the room—an unfortunate
consequence of our use of approximate models. To some extent, the same analysis
FIGURE 6.20 Determination
holds for diffuse light. Diffuse light reflects among the surfaces, and the color that we 6.7) that
see on a particular surface depends on other surfaces in the environment. Again, by
using carefully chosen diffuse and specular components with our light sources, we 2ψ = φ.
can approximate a global effect with local calculations. l
We have developed the Phong lighting model in object space. The actual lighting v
calculation, however, can be done in a variety of ways within the pipeline. In OpenGL,
the default is to, do the calculations for each vertex in eye coordinates and then
interpolate the shades for each fragment later in the pipeline. We must be careful of
the effect of the model-view and projection transformations on the vectors used in the
model because these transformations can affect the cosine terms in the model (see
Exercise 6.20). Consequently, to make a correct shading calculation, we must either of the halfway vector.
preserve spatial relationships as vertices and vectors pass through the pipeline, If we replace r . v with n . h,
perhaps by sending additional information through the pipeline from object space, or we avoid calculation of r.
go backward through the pipeline to obtain the required shading information. However, the halfway angle
ψ is smaller than φ, and if we
use the same exponent e in (n
6.3.4 The Modified Phong Model
. h)e that we used in (r . v)e,
If we use the Phong model with specular reflections in our rendering, the dot product then the size of the specular
r . v should be recalculated at every point on the surface. We can obtain a different highlights will be smaller. We
approximation for the specular term by using the unit vector halfway between the can mitigate this problem by
view vector and the light-source vector, the halfway vector l v h = | ++ |. l v replacing the value of the
exponent e with a value e so
that (n . h)e is closer to (r . v)e.
It is clear that avoiding
Note that if the normal is in the direction of the halfway vector, then the maximum recalculation of r is desirable.
reflection from the surface is in the direction of the viewer. Figure 6.20 shows all five However, to appreciate fully
where savings can be made,
6.3 The Phong Lighting Model 305
you should consider all the cases of flat and curved surfaces, near and far light sources,
and near and far viewers (see Exercise 6.8).
When we use the halfway vector in the calculation of the specular term, we are
using the Blinn-Phong, or modifiedPhong, shading model. This model is the default
in OpenGL and is the one carried out on each vertex as it passes down the pipeline. It
is important to keep in mind that both the Phong and Blinn-Phong models were created
as computationally feasible approximations to the BRDF rather than as the best
physical models. The availability of the programmable pipelines that we study in
Chapter 9 has opened the door to better approximations of the BRDF that can be
implemented on present hardware.
Color Plate 25 shows a group of Utah teapots (Section 12.10) that have been
rendered in OpenGL using the modified Phong model. Note that it is only our ability
to control material properties that makes the teapots appear different from one another.
The various teapots demonstrate how the modified Phong model can create a variety
of surface effects, ranging from dull surfaces to highly reflective surfaces that look
like metal.
f (p) = ax + by + cz + d = 0 ,
6.5 Polygonal Shading 309
r = αl + βn.
n . r = αl . n + β = l . n.
We can get a second condition between α and β from our requirement that r also be of
unit length; thus,
1= r . r = α2 + 2αβl . n + β2.
r = 2(l . n)n − l.
Although the fixed-function OpenGL pipeline uses the modified Phong model
and thus avoids having to calculate the reflection vector, in Chapter 9 we introduce
programmable shaders that can use the reflection vector. Methods such as
environment maps will use the reflected-view vector (see Exercise 6.24) that is used
to determine what a viewer would see if she looked at a reflecting surface such as a
highly polished sphere.
pipeline. Shades are then assigned to fragments by interpolating these vertex shades
as part of fragment processing. When programmable processors became available,
shading could be done on a per-fragment basis. A shade could be computed rather
than interpolated for each pixel, thus permitting real-time shading by methods such
as Phong shading, which until then could only be done by off-line software renderers.
l v
2
. We can make this assumption in OpenGL by setting the local-viewer flag to false.
6.5 Polygonal Shading 311
If flat shading is in effect, OpenGL uses the normal associated with the first vertex
of a single polygon for the shading calculation. For primitives such as a triangle strip,
OpenGL uses the normal of the third vertex for the first triangle, the normal of the
fourth for the second, and so on. Similar rules hold for other primitives, such as
quadrilateral strips.
Flat shading will show differences in shading for the polygons in our mesh. If the
light sources and viewer are near the polygon, the vectors l and v will be different for
each polygon. However, if our polygonal mesh has been designed to model a smooth
surface, flat shading will almost always be disappointing because we can see even small
differences in shading between adjacent polygons, as shown in Figure 6.25. The human
visual system has a remarkable sensitivity to small differences in light intensity, due to
a property known as lateral inhibition. If we see an increasing sequence of intensities,
as shown in Figure 6.26, we perceive the increases in brightness as overshooting on FIGURE 6.26 Step chart.
one side of an intensity step and undershooting on the other, as shown in Figure 6.27.
We see stripes, known as Mach bands, along the edges. This phenomenon is a consequence of how the cones in the eye
are connected to the optic nerve, and there is little that we can do to avoid it, other than to look for smoother shading
techniques that do not produce large differences in shades at the edges of polygons.
Suppose that we have enabled both smooth shading and lighting and that we assign
to each vertex the normal of the polygon being shaded. The lighting calculation is
made at each vertex using the material properties and the vectors v and l computed
for each vertex. Note that if the light source is distant, and either the viewer is distant
312 Chapter 6 Lighting and Shading
Perceived intensity
Actual intensity
n2 n4
n1 n3 or there are no specular reflections, then smooth (or interpolative) shading shades a
polygon in a constant color.
If we consider our mesh, the idea of a normal existing at a vertex should cause
concern to anyone worried about mathematical correctness. Because multiple polygons
meet at interior vertices of the mesh, each of which has its own normal, the normal at the
vertex is discontinuous. Although this situation might complicate the mathematics,
FIGURE 6.28 Normals near
Gouraud realized that the normal at the vertex could be defined in such a way as to
achieve smoother shading through interpolation. Consider an interior vertex, as shown in Figure 6.28, where four
polygons meet. Each has its own normal. In
interior vertex. Gouraud shading, we define the normal at a vertex to be the normalized average of the normals of the
polygons that share the vertex. For our example, the vertexnormal is given by
n = |n 1 ++ n2 ++ n3 ++ n4|.
n1 n2 n3 n4
computed shades for the vertices and has interpolated these shades over the faces of the
polygons.
Color Plate 21 contains another illustration of the smooth shading provided by
OpenGL. We used this color cube as an example in Chapters 2 and 3, and the
programs are in Appendix A. The eight vertices are colored black, white, red, green,
blue, cyan, magenta, and yellow. Once smooth shading is enabled, OpenGL
interpolates the colors across the faces of the polygons automatically.
We can do a similar interpolation on all the edges. The normal at any interior point
can be obtained from points on the edges by n(α, β) = (1− β)nC + βnD.
nA
nB
n
A Once we have the normal at each point, we can make an independent shading
calculation. Usually, this process can be combined with rasterization of the polygon.
Until recently, Phong shading could only be carried out off-line because it requires
the interpolation of normals across each polygon. In terms of the pipeline, Phong
shading requires that the lighting model be applied to each fragment; hence, the name
nC nD
perfragmentshading. The latest graphics cards allow the programmer to write
programs that operate on each fragment as it is generated by the rasterizer.
Consequently, we
nB can now do per-fragment operations and thus implement Phong shading in real time.
We will discuss this topic in detail in Chapter 9.
FIGURE 6.31 Interpolation of normals in
Phong shading.
6. The regular icosahedron is composed of 20 equilateral triangles; it makes a nice starting point for
generating spheres. See [ Ope 05].