Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
48 views30 pages

Mod 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views30 pages

Mod 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

MODULE 4

VIEWING
C H AP T E R 5
W e have completed our discussion of the first half of the synthetic camera
model—specifying objects in three dimensions. We now investigate the multi-
tude 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 com-
puter 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 represen-
tation 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 perspec-
tive) 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.

5.1 CLASSICAL AND COMPUTER VIEWING


Before looking at the interface between computer-graphics systems and application
programs for three-dimensional viewing, we take a slight diversion to consider classi-
cal viewing. There are two reasons for examining classical viewing. First, many of the
jobs that were formerly done by hand drawing—such as animation in movies, archi-
tectural rendering, drafting, and mechanical-parts design—are now routinely done
with the aid of computer graphics. Practitioners of these fields need to be able to pro-
duce classical views—such as isometrics, elevations, and various perspectives—and
thus must be able to use the computer system to produce such renderings. Second,
the relationships between classical and computer viewing show many advantages of,
and a few difficulties with, the approach used by most APIs.
235
236 Chapter 5 Viewing

Object

Projector

Projection plane

COP

FIGURE 5.1 Viewing.

Object

Projector

DOP
Projection plane

FIGURE 5.2 Movement of the center of projection (COP) to infinity.

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 cor-
responds to the center of the lens in the camera or in the eye, and in a computer-
graphics system, it is the origin of the camera frame for perspective views. All stan-
dard 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 imple-
ment, 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.
5.1 Classical and Computer Viewing 237

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 appli-
cations 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, 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 corre-
sponding 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 three-
point 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 projec-
tion plane, as opposed to the computer-graphics approach of complete independence
of all specifications.

5.1.1 Classical Viewing


When an architect draws an image of a building, she knows which side she wishes to
display and thus where she should place the viewer in relationship to the building.
Each classical view is determined by a specific relationship between the objects and
the viewer.
In classical viewing, there is the underlying notion of a principal face. The
types of objects viewed in real-world applications, such as architecture, tend to be
composed of a number of planar faces, each of which can be thought of as a principal
face. For a rectangular object, such as a building, there are natural notions of the
front, back, top, bottom, right, and left faces. In addition, many real-world objects
have faces that meet at right angles; thus, such objects often have three orthogonal
directions associated with them.
Figure 5.3 shows some of the main types of views. We start with the most re-
strictive view for each of the parallel and perspective types, and then move to the less
restrictive conditions.

5.1.2 Orthographic Projections


Our first classical view is the orthographic projection shown in Figure 5.4. In all or-
thographic (or orthogonal) views, the projectors are perpendicular to the projection
plane. In a multiview orthographic projection, we make multiple projections, in
238 Chapter 5 Viewing

Front elevation Elevation oblique Plan oblique

Isometric One-point perspective Three-point perspective

FIGURE 5.3 Classical views.

FIGURE 5.4 Orthographic projections.

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 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.

5.1.3 Axonometric Projections


If we want to see more principal faces of our box-like object in a single view, we
must remove one of our restrictions. In axonometric views, the projectors are still
5.1 Classical and Computer Viewing 239

FIGURE 5.5 Temple and three multiview orthographic projections.

Projection plane Projection plane Projection plane


(a) (b) (c)

FIGURE 5.6 Axonometric projections. (a) Construction of trimetric-view


projections. (b) Top view. (c) Side view.

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
240 Chapter 5 Viewing

Dimetric Trimetric Isometric

FIGURE 5.7 Axonometric views.

Projection plane

Projection plane Projection plane


(a) (b) (c)

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.

5.1.4 Oblique Projections


The oblique views are the most general parallel views. We obtain an oblique projec-
tion by allowing the projectors to make an arbitrary angle with the projection plane,
as shown in Figure 5.8. Consequently, angles in planes parallel to the projection plane
are preserved. A circle in a plane parallel to the projection plane is projected into a cir-
cle, yet we can 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 physi-
5.1 Classical and Computer Viewing 241

cal 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 differ-
ence 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.

5.1.5 Perspective Viewing


All perspective views are characterized by diminution of size. When objects are
moved farther from the viewer, their images become smaller. This size change gives
perspective views their natural appearance; however, because the amount by which a
line is foreshortened depends on how far the line is from the viewer, we cannot make
measurements from a perspective view. Hence, the major use of perspective views is
in applications such as architecture and animation, where it is important to achieve
natural-looking images.
In the classical perspective views, the viewer is located symmetrically with respect
to the projection plane, as shown in Figure 5.9. Thus, the pyramid determined by the
window in the projection plane and the center of projection is a symmetric or right

FIGURE 5.9 Perspective viewing.


242 Chapter 5 Viewing

(a) (b) (c)

FIGURE 5.10 Classical perspective views. (a) Three-point. (b) Two-point.


(c) One-point.

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 vanishing point (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.

5.2 VIEWING WITH A COMPUTER


We can now return to three-dimensional graphics from a computer perspective. Be-
cause viewing in computer graphics is based on the synthetic-camera model, we
should be able to construct any of the classical views. However, there is a fundamen-
tal difference. All the classical views were based on a particular relationship among
the objects, the viewer, and the projectors. In computer graphics, we stress the inde-
pendence of the object specifications and camera parameters. In OpenGL, we have
the choice of a perspective camera or an orthogonal camera. Whether a perspective
view is a one-, two-, or three-point perspective is not something that is understood
by OpenGL, as it would require knowing the relationships between objects and the
camera. On balance, we prefer this independence, but if an application needs a par-
5.2 Viewing with a Computer 243

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 oper-
ations. 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 z-
axis. 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 ap-
proach, 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 param-
eters 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 ma-
trix to position the camera and the projection matrix to produce both orthographic
and perspective views.

Projection plane
z

FIGURE 5.11 Imaging using the default camera.


290 Chapter 6 Lighting and Shading

We conclude the chapter with a short discussion of the two most important
methods for handling global lighting effects: ray tracing and radiosity.

6.1 LIGHT AND MATTER


In Chapters 1 and 2, we presented the rudiments of human color vision, delaying
until now any discussion of the interaction between light and surfaces. Perhaps the
most general approach to rendering is based on physics, where we use principles such
as conservation of energy to derive equations that describe how light interacts with
materials.
From a physical perspective, a surface can either emit light by self-emission, as a
light bulb does, or reflect light from other surfaces that illuminate it. Some surfaces
may both reflect light and emit light from internal physical processes. When we look
at a point on an object, the color that we see is determined by multiple interactions
among light sources and reflective surfaces. These interactions can be viewed as a re-
cursive process. Consider the simple scene illustrated in Figure 6.1. Some light from
the source that reaches surface A is scattered. Some of this reflected light reaches sur-
face B, and some of it is then scattered back to A, where some of it is again reflected
back to B, and so on. This recursive scattering of light between surfaces accounts
for subtle shading effects, such as the bleeding of colors between adjacent surfaces.
Mathematically, this recursive process results in an integral equation, the rendering
equation, which in principle we could use to find the shading of all surfaces in a
scene. Unfortunately, this equation generally cannot be solved analytically. Numerical
methods are not fast enough for real-time rendering. There are various approximate
approaches, such as radiosity and ray tracing, each of which is an excellent approx-
imation to the rendering equation for particular types of surfaces. Unfortunately,
neither ray tracing nor radiosity can yet be used to render scenes at the rate at which
we can pass polygons through the modeling-projection pipeline. Consequently, we

FIGURE 6.1 Reflecting surfaces.


6.1 Light and Matter 291

FIGURE 6.2 Light and surfaces.

focus on a simpler model, based on the Phong lighting model, that provides a com-
promise 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 clip-
ping window in this plane is mapped to the display; thus, we can think of the pro-
jection plane as ruled into rectangles, each corresponding to a pixel on the display.
292 Chapter 6 Lighting and Shading

COP

FIGURE 6.3 Light, surfaces, and computer imaging.

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 projec-
tor 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

(a) (b) (c)

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.
294 Chapter 6 Lighting and Shading

y
6.2 LIGHT SOURCES
I
Light can leave a surface through two fundamental processes: self-emission and re-
 flection. We usually think of a light source as an object that emits light only through

internal energy sources. However, a light source, such as a light bulb, can also re-
flect some light that is incident on it from the surrounding environment. We neglect
p the emissive term in our simple models. When we discuss OpenGL lighting in Sec-
x
tion 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
z that 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, spot-
lights, and distant light. These four lighting types are sufficient for rendering most
simple scenes.

6.2.1 Color Sources


Not only do light sources emit different amounts of light at different frequencies,
but their directional properties can vary with frequency. Consequently, a physically
correct model can be complex. However, our model of the human visual system is
based on three-color theory that tells us we perceive three tristimulus values, rather
than a full color distribution. For most applications, we can thus model light sources

p1

I(x1, y1, z1, 1, 1,  ) p2

I (x 2, y2, z 2,  2,  2,  )

FIGURE 6.6 Adding the contribution from a source.


6.2 Light Sources 295

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}.

6.2.2 Ambient Light


In many situations, such as in classrooms or kitchens, the lights have been designed
and positioned to provide uniform illumination throughout the room. Often such il-
lumination is achieved through large sources that have diffusers whose purpose is to
scatter light in all directions. We could create an accurate simulation of such illumina-
tion, at least in principle, by modeling all the distributed sources and then integrating
the illumination from these sources at each point on a reflecting surface. Making such
a model and rendering a scene with it would be a daunting task for a graphics system,
especially one for which real-time performance is desirable.2 Alternatively, we can
look at the desired effect of the sources: to achieve a uniform light level in the room.
This uniform lighting is called ambient light. If we follow this second approach, we
can postulate an ambient intensity at each point in the environment. Thus, ambient
illumination is characterized by an intensity, Ia , that is identical at every point in the
scene.
Our ambient source has red, green, and blue color components, Lar , Lag , and Lab .
We will use the scalar La to denote any one of the three components. Although every
point in our scene receives the same illumination from La , each surface can reflect
this light differently based on the surface’s properties.
Ambient light depends on the color of the the light sources in the environment.
For example, a red light bulb in a white room creates red ambient light. Hence, if
we turn off the light, the ambient contribution disappears. OpenGL permits us to
add a global ambient term, which does not depend on any of the light sources and is
reflected from surfaces. The advantage of adding such a term is that there will always

2. 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

p0

FIGURE 6.7 Point source illuminating a surface.

be some light in the environment so that objects in the viewing volume that are not
blocked by other objects will always appear in the image.

6.2.3 Point Sources


An ideal point source emits light equally in all directions. We can characterize a point
source located at a point p0 by a three-component color function

L(p0) = (Lr (p0), Lg (p0), Lb(p0)).

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) = L(p0).
|p − p0|2

The use of point sources in most applications is determined more by their ease
of use than by their resemblance to physical reality. Scenes rendered with only point
sources tend to have high contrast; objects appear either bright or dark. In the real
world, it is the large size of most light sources that contributes to softer scenes, as
FIGURE 6.8 Shadows created we can see from Figure 6.8, which shows the shadows created by a source of finite
by finite-size light source. size. Some areas are fully in shadow, or in the umbra, whereas others are in partial
shadow, or in the penumbra. We can mitigate the high-contrast effect from point-
source illumination by adding ambient light to a scene.
The distance term also contributes to the harsh renderings with point sources.
Although the inverse-square distance term is correct for point sources, in practice it
is usually replaced by a term of the form (a + bd + cd 2)−1, where d is the distance
between p and p0. The constants a, b, and c can be chosen to soften the lighting. In
addition, a small amount of ambient light also softens the effect of point sources.
Note that if the light source is far from the surfaces in the scene, then the intensity
6.2 Light Sources 297

of the light from the source is sufficiently uniform that the distance term is almost ps
constant over the surfaces.


6.2.4 Spotlights
Spotlights are characterized by a narrow range of angles through which light is emit-
Is
ted. We can construct a simple spotlight from a point source by limiting the angles at
which light from the source can be seen. We can use a cone whose apex is at ps , which FIGURE 6.9 Spotlight.
points in the direction ls , and whose width is determined by an angle θ, as shown in
Figure 6.9. If θ = 180, the spotlight becomes a point source.
More realistic spotlights are characterized by the distribution of light within the Intensity
cone—usually with most of the light concentrated in the center of the cone. Thus, the
intensity is a function of the angle φ between the direction of the source and a vector
s to a point on the surface (as long as this angle is less than θ; Figure 6.10). Although
this function could be defined in many ways, it is usually defined by cose φ, where the
exponent e (Figure 6.11) determines how rapidly the light intensity drops off.  
As we will see throughout this chapter, cosines are convenient functions for 
lighting calculations. If u and v are any unit-length vectors, we can compute the FIGURE 6.10 Attenuation of
cosine of the angle θ between them with the dot product a spotlight.

cos θ = u . v, Intensity

a calculation that requires only three multiplications and two additions.

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 move across a surface, calculating the intensity at FIGURE 6.11 Spotlight expo-
each point, we should recompute this vector repeatedly—a computation that is a nent.
significant part of the lighting calculation. However, if the light 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 sun strikes all objects that are in close proximity to one another
at the same angle. Figure 6.12 illustrates that we are effectively replacing a point
source of light with a source that illuminates objects with parallel rays of light—a
parallel source. In practice, the calculations for distant light sources are similar to the
calculations for 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 source at p0 is represented internally as a four-dimensional column
matrix:
FIGURE 6.12 Parallel light
⎡ ⎤ source.
x
⎢y⎥
⎢ ⎥
p0 = ⎢ ⎥ ,
⎣z⎦
1
298 Chapter 6 Lighting and Shading

In contrast, the distant light source is described by a direction vector whose represen-
tation in homogeneous coordinates is the matrix
⎡ ⎤
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 6.3 THE PHONG LIGHTING MODEL


v
I The lighting model that we present was introduced by Phong and later modified by
r 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
have a set of point sources. Each source can have separate ambient, diffuse, and spec-
ular 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, remem-
ber 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:
6.3 The Phong Lighting Model 299

⎡ ⎤
Lira Liga Liba

Li = Lird Ligd Libd ⎦ .
Lirs Ligs Libs

The first row of the matrix contains the ambient intensities for the red, green, and
blue terms from source i. The second row contains the diffuse terms; the third con-
tains the specular terms. We assume that any distance-attenuation terms have not yet
been applied.
We build our lighting model by summing the contributions for all the light
sources at each point we wish to light. For each light source, we have to compute
the amount of light reflected for each of the nine terms in the illumination array. For
example, for the red diffuse term from source i, Lird , we can compute a reflection
term Rird , and the latter’s contribution to the intensity at p is Rird Lird . The value of
Rird depends on the material properties, the orientation of the surface, the direction
of the light source, and the distance between the light source and the viewer. Thus,
for each point, we have nine coefficients that we can place in an array of reflection
terms:
⎡ ⎤
Rira Riga Riba
Ri = ⎣ Rird Rigd Ribd ⎦ .
Rirs Rigs Ribs

We can then compute the contribution for each color source by adding the ambient,
diffuse, and specular components. For example, the red intensity that we see at p from
source i is the sum of red ambient, red diffuse, and red specular intensities from this
source:
Iir = Rira Lira + Rird Lird + RirsLirs
= Iira + Iird + Iirs .

We obtain the total intensity by adding the contributions of all sources and, possibly,
a global ambient term. Thus, the red term is

Ir = (Iira + Iird + Iirs) + 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

I = I a + Id + Is = La Ra + Ld Rd + Ls Rs ,

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.
300 Chapter 6 Lighting and Shading

6.3.1 Ambient Reflection


The intensity of ambient light Ia is the same at every point on the surface. Some of this
light is absorbed and some is reflected. The amount reflected is given by the ambient
reflection coefficient, Ra = ka . Because only a positive fraction of the light is reflected,
we must have

0 ≤ ka ≤ 1,

and thus

Ia = ka La .

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.

6.3.2 Diffuse Reflection


A perfectly diffuse reflector scatters the light that it reflects equally in all directions.
Hence, such a surface appears the same to all viewers. However, the amount of
light reflected depends both on the material—because some of the incoming light
is absorbed—and on the position of the light source relative to the surface. Diffuse
reflections are characterized by rough surfaces. If we were to magnify a cross section
FIGURE 6.14 Rough surface. of a diffuse surface, we might see an image like that shown in Figure 6.14. Rays of
light that hit the surface at only slightly different angles are reflected back at markedly
different angles. Perfectly diffuse surfaces are so rough that there is no preferred angle
of reflection. Such surfaces, sometimes called Lambertian surfaces, can be modeled
n n
l l mathematically with Lambert’s law.
Consider a diffuse planar surface, as shown in Figure 6.15, illuminated by the
sun. The surface is brightest at noon and dimmest at dawn and dusk because, ac-
cording to Lambert’s law, we see only the vertical component of the incoming light.
(a) (b) One way to understand this law is to consider a small parallel light source striking
FIGURE 6.15 Illumination of a a plane, as shown in Figure 6.16. As the source is lowered in the (artificial) sky, the
diffuse surface. (a) At noon. (b) same amount of light is spread over a larger area, and the surface appears dimmer.
In the afternoon. Returning to the point source of Figure 6.15, we can characterize diffuse reflections
mathematically. Lambert’s law states that

Rd ∝ cos θ ,

where θ is the angle between the normal at the point of interest n and the direction
of the light source l. If both l and n are unit-length vectors,3 then

3. Direction vectors, such as l and n, are used repeatedly in shading calculations through the dot
product. In practice, both the programmer and the graphics software should seek to normalize all
such vectors as soon as possible.
6.3 The Phong Lighting Model 301

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:
kd
Id = (l . n)Ld .
a + bd + cd 2
There is a potential problem with this expression because (l . n)Ld will be neg-
ative 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).

6.3.3 Specular Reflection


If we employ only ambient and diffuse reflections, our images will be shaded and will FIGURE 6.17 Specular high-
appear three-dimensional, but all the surfaces will look dull, somewhat like chalk. lights.
What we are missing are the highlights that we see reflected from shiny objects. These
highlights usually show a color different from the color of the reflected ambient and
diffuse light. For example, a red plastic ball viewed under white light has a white
highlight that is the reflection of some of the light from the source in the direction
of the viewer (Figure 6.17).
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 FIGURE 6.18 Specular sur-
gets smoother, the reflected light is concentrated in a smaller range of angles centered face.
302 Chapter 6 Lighting and Shading

=1

=2

=5

FIGURE 6.19 Effect of shininess coefficient.

about the angle of a perfect reflector—a mirror or a perfectly specular surface. Mod-
eling 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
I= (k L max(l . n, 0) + ksLsmax((r . v)α , 0)) + ka La .
a + bd + cd 2 d d
This formula is computed for each light source and for each primary.
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
6.3 The Phong Lighting Model 303

terms in the lighting model. However, the white highlight we might see on a red ball
is the distorted reflection of a light source or perhaps some other bright white object
in the environment. To calculate this highlight correctly would require a global rather
than local lighting calculation. Because we cannot solve the full rendering equation,
we instead use various tricks in an attempt to obtain realistic renderings, one of which
is to allow different ambient, specular, and diffuse lighting colors.
Consider, for example, an environment with many objects. When we turn on a
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
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 considera-
tion. 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 holds for diffuse
light. Diffuse light reflects among the surfaces, and the color that we see on a partic-
ular surface depends on other surfaces in the environment. Again, by using carefully
chosen diffuse and specular components with our light sources, we can approximate
a global effect with local calculations.
We have developed the Phong lighting model in object space. The actual lighting
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 preserve spatial relationships as vertices and vectors pass through the pipeline,
perhaps by sending additional information through the pipeline from object space,
or go backward through the pipeline to obtain the required shading information.

6.3.4 The Modified Phong Model


If we use the Phong model with specular reflections in our rendering, the dot product
n
r . v should be recalculated at every point on the surface. We can obtain a different h
approximation for the specular term by using the unit vector halfway between the ψ
 r
view vector and the light-source vector, the halfway vector l
 v
l+v
h= .
|l + v|
Note that if the normal is in the direction of the halfway vector, then the maximum
reflection from the surface is in the direction of the viewer. Figure 6.20 shows all five FIGURE 6.20 Determination
vectors. Here we have defined ψ as the angle between n and h, the halfway angle. of the halfway vector.
When v lies in the same plane as do l, n, and r, we can show (see Exercise 6.7) that

2ψ = φ.
304 Chapter 6 Lighting and Shading

If we replace r . v with n . h, we avoid calculation of r. However, the halfway angle ψ


is smaller than φ, and if we use the same exponent e in (n . h)e that we used in (r . v)e ,
then the size of the specular highlights will be smaller. We can mitigate this problem

by 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 recalculation of r is desirable. However, to appreciate
fully where savings can be made, 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 modified Phong, 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 ren-
dered 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.

6.4 COMPUTATION OF VECTORS


The illumination and reflection models that we have derived are sufficiently general
that they can be applied to either curved or flat surfaces, to parallel or perspective
views, and to distant or near surfaces. Most of the calculations for rendering a scene
involve the determination of the required vectors and dot products. For each special
case, simplifications are possible. For example, if the surface is a flat polygon, the
normal is the same at all points on the surface. If the light source is far from the
surface, the light direction is the same at all points.
In this section, we examine how the vectors are computed for the general case. In
Section 6.5, we see what additional techniques can be applied when our objects are
composed of flat polygons. This case is especially important because most renderers,
including OpenGL, render curved surfaces by approximating those surfaces with
many small, flat polygons.

6.4.1 Normal Vectors


We start our discussion of normal vectors by returning to the equation of the plane.
In Chapter 4, we described a plane by the equation

f (p) = ax + by + cz + d = 0,
6.5 Polygonal Shading 309

r = αl + βn.

Taking the dot product with n, we find that

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 .

Solving these two equations, we find that

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 environ-
ment maps will use the reflected-view vector (see Exercise 6.24) that is used to de-
termine what a viewer would see if she looked at a reflecting surface such as a highly
polished sphere.

6.5 POLYGONAL SHADING


For the most part, interactive computer graphics systems are polygon processors.
From the hardware perspective, systems are optimized for passing polygons down
the pipeline. Performance is measured in terms of polygons per second, a measure-
ment that always includes lighting and shading. From the application perspective, a
large class of CAD applications have the user design polygonal meshes. Even if the
software supports curved surfaces, these surfaces are rendered as polygonal meshes
that approximate the surface.
Because a polygon has the same normal for the entire surface, normals only need
be computed once. Often normals are stored in an application data structure. Further
efficiencies can be obtained for many special conditions such as a distant light source,
and we must also be careful to avoid visual artifacts. We will investigate three methods
for shading a polygonal mesh such as the one shown in Figure 6.23: flat shading,
smooth or interpolative (Gouraud) shading, and Phong shading.
The first two methods are based on per-vertex lighting calculations. That is,
shades are computed at one or more vertices as part of the geometric part of the
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.
310 Chapter 6 Lighting and Shading

FIGURE 6.23 Polygonal mesh.

l v

FIGURE 6.24 Distant source and viewer.

6.5.1 Flat Shading


The three vectors need for shading—l, n, and v—can vary as we move from point
to point on a surface. For a flat polygon, however, n is constant. If we assume a
distant viewer,5 v is constant over the polygon. Finally, if the light source is distant, l
is constant. Here distant could be interpreted in the strict sense of meaning that the
source is at infinity. The necessary adjustments, such as changing the location of the
source to the direction of the source, could then be made to the shading equations
and to their implementation. Distant could also be interpreted in terms of the size of
the polygon relative to how far the polygon is from the source or viewer, as shown in
Figure 6.24. Graphics systems or user programs often exploit this definition.
If the three vectors are constant, then the shading calculation needs to be carried
out only once for each polygon, and each point on the polygon is assigned the same
shade. This technique is known as flat, or constant, shading. In OpenGL, we specify
flat shading as follows:

glShadeModel(GL_FLAT);

5. We can make this assumption in OpenGL by setting the local-viewer flag to false.
6.5 Polygonal Shading 311

FIGURE 6.25 Flat shading of polygonal mesh.

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 differ-
ent 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 Fig-
ure 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 bright- FIGURE 6.26 Step chart.
ness as overshooting on 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.

6.5.2 Smooth and Gouraud Shading


In our rotating-cube example of Section 4.9, we saw that OpenGL interpolates colors
assigned to vertices across a polygon. Smooth shading is the default in OpenGL. We
can also set the mode explicitly as follows:

glShadeModel(GL_SMOOTH);

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

FIGURE 6.27 Perceived and actual intensities at an edge.

n or there are no specular reflections, then smooth (or interpolative) shading shades a
polygon in a constant color.
n2 n4 If we consider our mesh, the idea of a normal existing at a vertex should cause
n1 n3
concern to anyone worried about mathematical correctness. Because multiple poly-
gons 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, 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 ver-
FIGURE 6.28 Normals near tex, 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 vertex normal
is given by

n1 + n2 + n3 + n4
n= .
|n1 + n2 + n3 + n4|

From an OpenGL perspective, Gouraud shading is deceptively simple. We need only


to set the vertex normals correctly and apply our lighting model. Cosnequently,
the literature often makes no distinction between smooth and Gouraud shading.
However, the lack of a distinction masks a problem: How do we find the normals that
we should average together? If our program is linear, specifying a list of vertices (and
other properties), we do not have the necessary information about which polygons
share a vertex. If we are using a pipeline renderer, polygons are rendered one at a time
independently of other polygons.
What we need, of course, is a data structure for representing the mesh that con-
tains the information about which polygons meet at each vertex. Traversing this data
structure can generate the averaged normals. Such a data structure should contain,
at a minimum, polygons, vertices, normals, and material properties. One possible
structure is shown in Figure 6.29. The structure is a modified vertex list. Each node
on the left points to a list of the polygons that meet at the vertex. This data structure
could be accessed by each polygon or by a data structure that represents the mesh
such as the ones we discussed to represent the cube in Chapter 4.
Color Plates 4 and 5 show the shading effects available in OpenGL. In Color
Plate 4, there is a single light source, but each polygon has been rendered with a
single shade (constant shading), computed using the modified Phong model. In
Color Plate 5, normals have been assigned to all the vertices. OpenGL has then
6.5 Polygonal Shading 313

Polygons
Vertices

FIGURE 6.29 Mesh data structure.

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 pro-
grams 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.

6.5.3 Phong Shading


Even the smoothness introduced by Gouraud shading may not prevent the appear-
ance of Mach bands. Phong proposed that instead of interpolating vertex intensities,
as we do in Gouraud shading, we interpolate normals across each polygon. The light-
ing model can then be applied at every point within the polygon. Note that because
the normals give the local surface orientation, by interpolating the normals across the
polygon, as far as shading is concerned, the surface appears to be curved rather than
flat. This fact accounts for the smooth appearance of Phong-shaded images.
Consider a polygon that shares edges and vertices with other polygons in the
mesh, as shown in Figure 6.30. We can compute vertex normals by interpolating
over the normals of the polygons that share the vertex. Next, we can use bilinear
interpolation, as we did in Chapter 4, to interpolate the normals over the polygon.
Consider Figure 6.31. We can use the interpolated normals at vertices A and B to
interpolate normals along the edge between them:

n(α) = (1 − α)nA + αnB .

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 .


314 Chapter 6 Lighting and Shading

nA

nB

FIGURE 6.30 Edge normals.

nA Once we have the normal at each point, we can make an independent shading calcu-
lation. 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 in-
terpolation 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 per-
fragment shading. The latest graphics cards allow the programmer to write programs
nC nD 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.6 APPROXIMATION OF A SPHERE


BY RECURSIVE SUBDIVISION
We have used the sphere as an example curved surface to illustrate shading calcula-
tions. However, the sphere is not an OpenGL primitive. Both the GL utility library
(GLU) and the GL Utility Toolkit (GLUT) contain spheres, the former by support-
ing quadric surfaces, a topic we will discuss in Chapter 12, and the latter through a
polygonal approximation.
Here we develop our own polygonal approximation to a sphere. It provides a ba-
sis for us to write simple programs that illustrate the interactions between shading
parameters and polygonal approximations to curved surfaces. We introduce recur-
sive subdivision, a powerful technique for generating approximations to curves and
surfaces to any desired level of accuracy.
Our starting point is a tetrahedron, although we could start with any regular
polyhedron whose facets could be divided initially into triangles.6 The regular tetra-
hedron is composed of four equilateral triangles, determined by four vertices. We

6. The regular icosahedron is composed of 20 equilateral triangles; it makes a nice starting point for
generating spheres. See [Ope05].

You might also like