









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Lecture 21: Shading
put your trust in my shadow. Judges 9:
1. Polygonal Models
Polygonal models are one of the most common representations for geometry in Computer
Graphics. Polygonal models are popular because the underlying surface of each polygon is a plane
and planes are the easiest surfaces to analyze: the normal vector along a plane is constant, and the
points on a plane satisfy a linear equation. Thus rendering algorithms are particularly simple for
polygonal models.
But models consisting solely of polygons also have severe limitations because planar polygons
cannot model smooth curved surfaces. To overcome this deficiency, many small polygons are
needed to simulate curved shapes. But even with lots of small polygons, the human eye is adept at
seeing the edges between adjacent polygons. Thus surfaces modeled by polygons do not look
smooth. The purpose of shading algorithms is to smooth the appearance of polygonal models by
reducing the impact of sharp edges in order to give the eye the impression of a smooth curved
surface.
We shall study three different shading algorithms: uniform shading, Gouraud shading, and
Phong shading. As in ray tracing, we will consider three illumination models: ambient reflection,
diffuse reflection, and specular reflection. Since a computer model of a complicated curved shape
may contain millions of polygons, the emphasis in shading algorithms is on speed. A good
shading algorithm must be fast and make the model look smooth; shading algorithms are not
necessarily intended to be physically accurate models of the behavior of light.
Uniform shading is fast, but makes no attempt to smooth the appearance of the model. Thus
we introduce uniform shading only to compare the results with Gouraud and Phong shading.
Gouraud shading smooths the appearance of the model by linearly interpolating intensities along
scan lines; Phong shading simulates curved surfaces by linearly interpolating normal vectors along
scan lines.
To speed up our computations, we shall make several simplifying assumptions. In addition to
assuming that curved surfaces are approximated by planar polygons, we shall assume that all light
sources are point light sources and that both the light sources and the eye are located far from the
model (essentially at infinity). These assumptions mean that the vectors to each of the light sources
as well as the vector to the eye are constant along each polygon. Together with the fact that the
normal vector is constant along a polygon, these assumptions help to simplify the computation of
diffuse and specular reflections.
1.1 Newell’s Formula for the Normal to a Polygon. Intensity calculations require normal
vectors. Polygons are typically represented by their vertices, so to calculate the intensity of light on
a polygon, we need to compute the normal vector from the vertices. Newell’s formula is a
numerically robust way to compute the normal vector of a polygon from its vertices.
Naively, we could compute the normal vector N to a polygon from its vertices
0
n
by
selecting two adjacent edge vectors
i + 1
i
i + 2
i + 1
and setting
i + 1
i
i + 2
i + 1
since this cross product is perpendicular to the plane of the polygon. The problem with this
approach is that two of the vertices might be very close together so one of the edge vectors
i + 1
i
i + 2
i + 1
might be very short; worse yet the three points
i
i + 1
i + 2
might be
collinear or almost collinear. In each of these cases, the computation of the unit normal
N / | N | is
numerically unstable.
Newell’s formula overcomes this difficulty by using all of the vertices to calculate the normal to
the polygon. (For a derivation of Newell’s formula, see Lecture 11, Section 4.2.2.)
Newell’s Formula
k
k + 1
k = 0
n
n + 1
0
Recall from Lecture 11 that the magnitude of the normal vector in Newell’s formula is related
to the area of the associated polygon. In fact,
| N |= 2 × Area ( Polygon ).
Thus Newell’s formula avoids degeneracies due to collinear vertices.
2. Uniform Shading
In uniform shading or flat shading no attempt is made to smooth the appearance of a polygonal
model. Each individual polygon is displayed using the intensity formula
uniform
a
k
a
ambient
p
k
d
diffuse
p
k
s
n
specular
where
R = 2 ( L • N ) N − L is the direction of the light vector L after it bounces off the polygon.
Scan Line
Edge
Edge
Edge
Edge
Scan Line
Scan Line €
Scan Line
Vertex
Vertex
Vertex
Vertex
Vertex
Edge
Vertex
Figure 2: Gouraud Shading. First compute the intensity at each vertex. Then interpolate these
vertex intensities along each edge. Finally interpolate these edge intensities to the pixels in the
interior of the polygon by interpolating edge intensities along scan lines.
Once we have the intensities at the vertices, we can apply linear interpolation to compute
intensities first along edges and then along scan lines in the following fashion. Suppose that we
know the intensities
1
2
at two points
1
2
. The parametric equation of the line through
1
2
is
L ( t ) = ( 1 − t ) P
1
2
1
2
1
Linear interpolation means that we compute the intensity I at a parameter t by the analogous
formula
I ( t ) = ( 1 − t ) I
1
2
1
2
1
To speed up the computation of intensity, we shall reduce this computation to one addition per
pixel by computing
I ( t ) incrementally. For the line
L ( t + Δ t ) = P
1
2
1
1
2
1
) + Δ t ( P
2
1
L ( t ) = P
1
2
1
so
Δ L = Δ t ( P
2
1
L ( t + Δ t ) = L ( t ) + Δ L.
Thus
next
current
In terms of coordinates, Equation (3.2) for
Δ L is really three equations, one for each coordinate:
Δ x = Δ t ( x
2
− x
1
Δ y = Δ t ( y
2
− y
1
Δ z = Δ t ( z
2
− z
1
Similarly, for intensity
I ( t + Δ t ) = I
1
2
1
1
2
1
) + Δ t ( I
2
1
I ( t ) = I
1
2
1
so
Δ I = Δ t ( I
2
1
I ( t + Δ t ) = I ( t ) + Δ I.
Thus
next
current
Since we know
1
and
2
, if we know
Δ t , then we can compute
Δ I and we can update
next
by a
single addition per pixel.
Now consider what happens in two cases: along a scan line and along an edge. Along a scan
line, when we move to the next pixel
Δ x = 1 (see Figure 3). Since by Equation (3.4)
Δ x = Δ t ( x
2
− x
1
), it follows that along a scan line
Δ t = 1 / ( x
2
− x
1
2
1
)Δ t = ( I
2
1
) / ( x
2
− x
1
€
€
€
( x + Δ x , y , z )
€
€
1
1
1
1
€
P
2
= ( x
2
, y
2
, z
2
)
€
I
1
2
Figure 3: Moving to the next pixel along a scan line:
Δ x = 1.
To get to the next scan line, we move along an edge. When we move to the next scan line
Δ y = 1
(see Figure 4). Since by Equation (3.4)
Δ y = Δ t ( y
2
− y
1
), it follows that along an edge
Δ t = 1 / ( y
2
− y
1
2
1
)Δ t = ( I
2
1
) / ( y
2
− y
1
Thus in each case, we can reduce the computation of intensity to one addition per pixel.
€
I
1
€
I
2
€
P
1
= ( x
1
, y
1
, z
1
)
€
P
2
= ( x
2
, y
2
, z
2
)
€
( x , y , z )
€
( x , y + Δ y , z )
€
Δ y = 1
Figure 4: Moving to the next scan line:
Δ y = 1.
P = ( 1 − u )( 1 − t ) + u ( 1 − s ) ( )
β
1
1
β
2
2
β
3
3
But linear interpolations of intensities is performed in exactly the same way. Thus
I = ( 1 − u )( 1 − t ) + u ( 1 − s ) ( )
β
1
1
β
2
2
β
3
3
Since the barycentric coordinates
β
1
, β
2
, β
3
are unique, this result is independent of the orientation
of the triangle.
1
2
1
2
3
( 1 − s ) I
1
3
1
2
1
2
3
1
3
€
Figure 6: Linear interpolation for triangles: for points along edges (left) and for intensities (right).
Nevertheless, even though for triangles the pixel intensities are independent of the orientation of
the triangles relative to the coordinate system, the pixel intensities for polygons still depend on
exactly how we subdivide the polygons into triangles; different subdivisions will generate different
intensities for the same pixels.
4. Phong Shading
Gouraud shading reduces the visibility of polygonal edges by blurring the intensity across the
boundaries of adjacent polygons. But blurring these intensities also blurs specular reflections. To
reproduce sharp specular reflections, we need a better way to simulate curved surfaces.
Phong shading simulates curved surfaces by interpolating normal vectors instead of intensities.
Normal vectors vary along a curved surface, so Phong shading provides a better approximation to
the intensity for curved surfaces modeled by planar polygons. We begin by presenting a naive
version of Phong shading; more sophisticated enhancements will be presented in subsequent
subsections.
4.1 Naive Phong Shading. The underlying strategy of Phong shading is similar to the
underlying strategy for Gouraud shading, except that intensities are replaced by normal vectors.
The normal vectors for each polygon are computed in three steps:
i. First, calculate the normals at the individual vertices of the polygon.
ii. Next, interpolate these normals along the edges of the polygon.
iii. Finally, interpolate these edge normals along scan lines to the pixels in the interior of the
polygon.
Once the normal vectors are computed, the intensities are calculated in the usual manner using
Equation (2.1).
As in Gouraud shading, we calculate the unit normal vector at a vertex by Equation (3.1),
averaging the unit normals of all the polygons to which the vertex belongs. The unit normal for
each polygon is computed by Newell’s formula (Equation (1.1)).
Once we have the unit normals at the vertices, we can apply linear interpolation to compute
normals first along edges and then along scan lines just as we calculated intensities for Gouraud
shading. If
1
2
are the unit normals at the end points
1
2
of the line
P ( t ) = P
1
2
1
then using linear interpolation the normal
N ( t ) at the point
P ( t ) is calculated by
N ( t ) = ( 1 − t ) N
1
2
1
2
1
To speed up the computation of normal vectors, we reduce this computation to three additions
(one for each coordinate) per pixel by computing normals incrementally, using the same approach
we used to compute intensities incrementally in Gouraud shading. Replacing intensities by normals
in Equation (3.5) leads to
Δ N = Δ t ( N
2
1
N ( t + Δ t ) = N ( t ) + Δ N.
Thus
next
current
Since we know
1
and
2
, if we know
Δ t , then we can compute
Δ N , and we can update
next
by
three additions (one for each coordinate) per pixel. However, to calculate intensities with Equation
(2.1), we need not just a normal, but a unit normal. Thus after we calculate
next
current
we need to renormalize -- that is, we must calculate
next
unit
current
current
We compute the normals for Phong shading, just like we computed the intensities for Gouraud
shading, in two stages: along scan lines and along polygonal edges. Recall that when we move to
the next pixel along a scan line,
Δ x = 1 (see Figure 3). Since by Equation (3.4)
Δ x = Δ t ( x
2
− x
1
), it
follows that along scan lines
Δ t = 1 / ( x
2
− x
1
2
1
)Δ t = ( N
2
1
) / ( x
2
− x
1
We begin with the numerator. Let
J ( t ) = L • N ( t ). Then the numerator is
J ( t ) = ( 1 − t )( L • N
1
) + t ( L • N
2
) = ( 1 − t ) J
1
2
where
1
1
and
2
2
. Equation (4.3) for
J ( t ) is just linear interpolation of the scalar
valued function
J ( t ) = L • N ( t ). We can calculate this scalar valued function at each pixel in exactly
the same way that we calculate the intensities for Gouraud shading: first calculate
J ( t ) = L • N ( t ) at
the vertices, then interpolate along edges, and finally interpolate along scan lines. This computation
is line for line the exact same computation as Gouraud shading: simply apply the computation to
the linear function
J ( t ) instead of the intensity
I ( t ).
Now consider the denominator. Let
N ( t ) = ( 1 − t ) N
1
2
Then the denominator is given by
d ( t ) = N ( t ) = N ( t )• N ( t ).
From naive Phong shading (Equation (4.1)), we know that
Δ N = Δ t ( N
2
1
Therefore by Equation (4.2)
next
current
hence
d
next
next
next
We can calculate
Δ N and
next
quickly using the scan line approach to linear interpolation
presented in Section 4.1; what slows us down here is the square root in Equation (4.4).
To find
d
next
quickly, we can apply Newton’s method (see Lecture 7, Section 3.1) to calculate
this square root. Let
F ( x ) = x
2
− d
next
2
= x
2
next
next
Newton’s method starts with an initial guess
x
0
for a root of
F ( x ) -- the value of
d
next
-- and
computes subsequent guesses
x
n + 1
from previous guesses
x
n
by the formula
x
n + 1
= x
n
F ( x
n
F ( x
n
= x
n
x
n
2
− d
next
2
2 x
n
Since N changes only by a small amount from pixel to pixel,
Δ N is typically small, so we can start
the iteration with the initial guess
x
0
= d
current
. Because our initial guess is already close to
d
next
Newton’s method will typically converge to a good approximation to the required square root after
only a few iterations. Notice that in this implementation, we need to compute both
N and
d for
each pixel. The scan line algorithm is used to update N and
d
2
= N • N ; Newton’s method is used
only to compute
d = d
2
4.3 Fast Phong Shading and Specular Reflection. Recall that the formula for specular
reflection is
specular
p
k
s
n
where
Substituting this expression for R into the formula for specular reflection yields
specular
p
k
s
( )
n
Now recall again that for Phong shading the normal vector N is calculated by linear interpolation, so
N ( t ) = ( 1 − t ) N
1
2
and
unit
( t ) =
( 1 − t ) N
1
2
( 1 − t ) N
1
2
Substituting this formula for the unit normal into Equation (4.5) for specular reflection yields
specular
p
k
s
2 ( 1 − t )( L • N
1
) + t ( L • N
2
{ }
( 1 − t )( V • N
1
) + t ( V • N
2
{ }
( 1 − t ) N
1
2
2
n
To analyze this formula, let
J ( t ) = L • N ( t ) and
K ( t ) = V • N ( t ). Then the first term in the
numerator on the right hand side of Equation (4.6) is the product of the expressions
J ( t ) = ( 1 − t )( L • N
1
) + t ( L • N
2
) = ( 1 − t ) J
1
2
K ( t ) = ( 1 − t )( V • N
1
) + t ( V • N
2
) = ( 1 − t ) K
1
2
where
1
1
2
2
1
1
and
2
2
. These expressions are just linear
interpolation for the scalar valued functions
J ( t ) = L • N ( t ) and
K ( t ) = V • N ( t ). Once again we
can calculate these scalar valued functions at each pixel in exactly the same way that we calculate the
intensities for Gouraud shading: first calculate
J ( t ) and
K ( t ) at the vertices, then interpolate along
edges, and finally interpolate along scan lines. This computation is line for line the exact same
computation as Gouraud shading, with
J ( t ) and
K ( t ) in place of the intensity
I ( t ). Moreover, the
term
L • V is a constant, so we need to compute this value only once. Thus, we have
specular
p
k
s
2 ( 1 − t )( L • N
1
) + t ( L • N
2
{ }
Similar to Gouraud
( 1 − t )( V • N
1
) + t ( V • N
2
{ }
Similar to Gouraud
( 1 − t ) N
1
2
2
Constant
n
We are left to compute only the denominator of the first term on the right hand side. This
denominator is given by
D ( t ) = N ( t )
2
= N ( t ) • N ( t ).
N ( t + Δ t ) =
sin ( 1 − t − Δ t ) φ ( )
sin(φ)
1
sin ( t + Δ t ) φ ( )
sin(φ)
2
First notice that the denominator,
sin(φ), is a constant. Moreover, since
1
2
are unit vectors,
cos( φ) = N
1
2
so
sin(φ) = 1 − cos
2
( φ) = 1 − ( N
1
2
2
The square root here is not a problem, since this square root is computed only once. We do not
need to repeat this computation for each pixel; rather we compute
sin(φ) once and store the result.
Next observe that by the trigonometric identities for the sine of the sum or difference of two
angles:
sin ( t + Δ t ) φ ( )
= sin( t φ) cos(Δ t φ) + cos( t φ)sin(Δ t φ) (4.9)
sin ( 1 − t − Δ t ) φ ( )
= sin(( 1 − t ) φ)cos(Δ t φ) − cos(( 1 − t ) φ)sin(Δ t φ). (4.10)
Since
Δ t , φ are constants, the values
cos(Δ t φ), sin(Δ t φ) are also constants, so we can also compute
these values once and store them for subsequent use. Furthermore, we can assume that we have
already calculated
sin( t φ), sin(( 1 − t ) φ) for the current pixel, so we do not need to recalculate these
values for the next pixel. But we still need
cos( t φ), cos(( 1 − t ) φ). Fortunately, these values can
also be computed incrementally from the trigonometric identities for the cosine of the sum or
difference of two angles:
cos ( t + Δ t ) φ ( )
= cos( t φ)cos(Δ t φ) − sin( t φ)sin(Δ t φ) (4.11)
cos ( 1 − t − Δ t ) φ ( )
= cos(( 1 − t ) φ)cos(Δ t φ) + sin(( 1 − t ) φ) sin(Δ t φ). (4.12)
Using equations (4.9)-(4.12), after computing the constants
cos(Δ t φ), sin(Δ t φ), we can compute all
the necessary sines and cosines incrementally with just four additions/subtractions and eight
multiplications. No trigonometric evaluations are necessary, nor are any square roots required.
Now we can proceed in the usual fashion for Phong shading:
i. First, calculate the unit normals at the individual vertices of each polygon.
ii. Next, apply spherical linear interpolation, using equations (4.9)-(4.12) with
Δ t = 1 / ( y
2
− y
1
) to calculate the unit normals incrementally along the edges of each
polygon.
iii. Next, apply spherical linear interpolation to the unit normals along the edges, using
equations (4.9)-(4.12) with
Δ t = 1 / ( x
2
− x
1
) to calculate the unit normals incrementally
along scan lines for the pixels in the interior of each polygon.
iv. Finally, use these unit normals to calculate the intensity at each pixel using Equation (2.1).
The preceding algorithm corresponds to naive Phong shading with spherical linear interpolation
replacing standard linear interpolation. This procedure eliminates square roots, but if we were to
follow this approach, in step iv we would need to compute two dot products --
L • N and
for every pixel. To eliminate these dot products, we can proceed as in fast Phong shading by
setting
J ( t ) = L • N ( t ) and
K ( t ) = V • N ( t ). Substituting the right hand side of Equation (4.8) for
N ( t ) into these expression for
J ( t ) and
K ( t ) yields the scalar valued functions:
J ( t ) =
sin ( 1 − t ) φ ( )
sin(φ)
1
sin t φ ( )
sin(φ)
2
K ( t ) =
sin ( 1 − t ) φ ( )
sin(φ)
1
sin t φ ( )
sin(φ)
2
Now we can proceed as in the previous algorithm, but instead of updating the unit normal vector
N ( t ) incrementally along scan lines using Equations (4.9)-(4.12), we use these equations to
incrementally update the scalar valued functions
J ( t ) and
K ( t ) along scan lines. This approach
eliminates the need to compute two dot products at every pixel, but notice that we still need to
update the normal vector
N ( t ) incrementally along each edge, since we need the angle
φ between
normal vectors at opposite edges along each scan line in order to calculate the functions
J ( t ) and
K ( t ) along the scan lines.
5. Summary
The purpose of shading algorithms is to smooth the appearance of polygonal models by
reducing the impact of sharp edges in order to give the eye the impression of a smooth curved
surface. Gouraud shading blurs these sharp edges by applying linear interpolation to the intensity
for each polygon along scan lines. But Gouraud shading also blurs specular reflections. Phong
shading provides a better model for curved surfaces by performing linear interpolation of normal
vectors along scan lines. Phong shading is slower than Gouraud shading, but Phong shading is
considerably better for specular reflections.
We investigated several ways to speed up Phong shading, including calculating diffuse and
specular reflections independently by linear interpolation and using Newton’s method to compute
the square root needed to find the magnitude of the normal vector for diffuse reflection. We also
examined spherical linear interpolation of unit normals in order to avoid the square root altogether.
To speed up these algorithms, the interpolation steps in both Gouraud and Phong shading are
performed incrementally. Below we summarize these incremental formulas for both Gouraud and
Phong shading.
c. Spherical Linear Interpolation
next
= N ( t + Δ t ) =
sin ( 1 − t − Δ t ) φ ( )
sin( φ)
1
sin ( t + Δ t ) φ ( )
sin(φ)
2
{along edges only}
next
= J ( t + Δ t ) =
sin ( 1 − t − Δ t ) φ ( )
sin( φ)
1
sin ( t + Δ t ) φ ( )
sin(φ)
2
next
= K ( t + Δ t ) =
sin ( 1 − t − Δ t ) φ ( )
sin( φ)
1
sin ( t + Δ t ) φ ( )
sin(φ)
2
sin ( t + Δ t ) φ ( )
= sin( t φ) cos(Δ t φ) + cos( t φ)sin(Δ t φ)
sin ( 1 − t − Δ t ) φ ( )
= sin(( 1 − t ) φ)cos(Δ t φ) − cos(( 1 − t ) φ)sin(Δ t φ)
cos ( t + Δ t ) φ ( )
= cos( t φ)cos(Δ t φ) − sin( t φ)sin(Δ t φ)
cos ( 1 − t − Δ t ) φ ( )
= cos(( 1 − t ) φ)cos(Δ t φ) + sin(( 1 − t ) φ) sin(Δ t φ)
Δ t = 1 / ( x
2
− x
1
) {along scan lines}
Δ t = 1 / ( y
2
− y
1
) {along edges}
Exercises:
o
) = 0.40674 and sin(
o
) = 0.42262. Use linear
interpolation to estimate sin(24.
o
e
t
incrementally using only one multiplication per pixel.
Programming Project:
your favorite API.
a. Use your implementation to shade several large polygonal models.
b. Compare the relative speeds of the following algorithms for each of your models:
i. Gouraud Shading
ii. Naive Phong Shading
iii. Fast Phong Shading
iv. Phong Shading using Spherical Linear Interpolation