February 5, 2015 firstname.lastname@example.org
University of Kufa
February 5, 2015 2
Why we need shading
Suppose we build a model of a
sphere using many polygons and
color it. We get something like
But we want
February 5, 2015 3
Shading refers to the application of a reflection model over the surface of
To use the lighting and reﬂectance model to
shade facets of a polygonal mesh — that is, to
assign intensities to pixels to give the
impression of opaque surfaces rather than
Shading is a way to paint the object with light
What is Shading
February 5, 2015 4
In creating and interpreting images, we need to understand two things:
• Geometry –Where scene points appear in the image (image locations)
• Radiometry –How “bright” they are (image Computer Graphics values)
Geometric enables us to know something about the scene location of a point
imaged at pixel (u, v)
Radiometric enables us to know what a pixel value implies about surface
lightness and illumination
Geometry and Radiometry
February 5, 2015 5
• For “matte” objects with no shininess
• Diffuse/matte objects are called Lambertian
• In general, shading does not vary with viewpoint
ƒExample: a piece of paper
• Reflected light is scattered equally in all directions
February 5, 2015 6
Lambertian Surface Obeys Lambert’s Law (from physics): The color, c, of a
surface is proportional to the cosine of the angle between the surface normal
and the direction of the light source
February 5, 2015 7
February 5, 2015 email@example.com 8
Diffuse Lighting Model
February 5, 2015 firstname.lastname@example.org 9
February 5, 2015 10
• Want to add highlights for shiny surfaces
• Highlight moves with eye location
February 5, 2015 11
• The exponent p controls the “sharpness” of the highlight: larger p
produces sharper highlight
Values of p between 100 and
200 correspond to metals
Values between 5 and 10 give
surface that look like plastic
The Shininess Coefficient
February 5, 2015 12
Phong Illumination: Computing r
• How can we compute the reflection vector r?
February 5, 2015 email@example.com 16
Shadows & Multiple Lights
February 5, 2015 17
• Illumination of a point on a surface:
• Technical detail: clamp all colors at 1
• Illumination of an entire surface = SHADING! ƒ
• Very expensive to compute at every visible point. We have 3 typical options
- Per polygon (Flat shading): entire polygon gets same color
- Per vertex (Gourad Shading): compute illumination at each vertex and
interpolate vertex colors
- Per pixel (Phong Shading): interpolate vertex normals and compute
illumination for each pixel
Illumination vs. Shading
February 5, 2015 19
One illumination calculation per polygon, assign all pixels inside each
polygon the same color
February 5, 2015 20
The simplest shading algorithm, called flat shading, consists of using an
illumination model to determine the corresponding intensity value for the
incident light, then shade the entire polygon according to this value. Flat
shading is also known as constant shading or constant intensity shading. Its
main advantage is that it is easy it implement.
Flat shading produces satisfactory results under the following conditions:
1. The subject is illuminated by ambient light and there are no surface textures or
2. In the case of curved objects, when the surface changes gradually and the
light source and viewer are far from the surface.
3. In general, when there are large numbers of plane surfaces.
Figure shows three cases of flat shading of a conical surface. The more polygons,
the better the rendering.
With ﬂat shading, each triangle of a mesh is ﬁlled with a single color.
February 5, 2015 21
• each entire polygon is drawn with the same color.
• need to know one normal for the entire polygon.
• fast .
• lighting equation used once per polygon.
Given a single normal to the plane the lighting equations and the material
properties are used to generate a single color. The polygon is filled with that
February 5, 2015 firstname.lastname@example.org
February 5, 2015 email@example.com
February 5, 2015 24
February 5, 2015 25
The major limitation of flat shading is that each polygon is rendered in a
Very often the only way of improving the rendering is by increasing the
number of polygons, as shown in the previous Figure. An alternative scheme
is based on using more than one shade in each polygon, which is
accomplished by interpolating the values calculated for the vertices to the
polygon's interior points.
This type of manipulation, called interpolative or incremental shading,
under some circumstances is capable of producing a more satisfactory
shade rendering with a smaller number of polygons. Two incremental
shading methods, called Gouraud and Phong shading, are almost in 3D
Interpolative (incremental) Shading
February 5, 2015 26
This shading algorithm was first described by H. Gouraud in 1971. It is also
called bilinear intensity interpolation. Gouraud shading is easier to understand in
the context of the scan-line algorithm used in hidden surface removal, For now,
assume that each pixel is examined according to its horizontal (scan-line)
placement, usually left to right. Following figure shows a triangular polygon with
vertices at A, B, and C.
February 5, 2015 firstname.lastname@example.org 27
February 5, 2015 email@example.com 28
February 5, 2015 29
February 5, 2015 30
The intensity value at each of these vertices is
based on the reflection model. As scan-line
processing proceeds, the intensity of pixel p1
is determined by interpolating the intensities at
vertices A and B, according to the formula In
the next slide, the intensity of p1 is closer to
the intensity of vertex A than that of vertex B.
The intensity of p2 is determined similarly, by
interpolating the intensities of vertices A and C.
February 5, 2015 31
To find the intensity of Ip, we need to know the intensity of Ia and Ib. To
find the intensity of Ia we need to know the intensity of I1 and I2. To find
the intensity of Ib we need to know the intensity of I1 and I3.
Ia = (Ys - Y2) / (Y1 - Y2) * I1 + (Y1 - Ys) / (Y1 - Y2) * I2
Ib = (Ys - Y3) / (Y1 - Y3) * I1 + (Y1 - Ys) / (Y1 - Y3) * I3
Ip = (Xb - Xp) / (Xb - Xa) * Ia + (Xp - Xa) / (Xb - Xa) * Ib
Whether we are interpolating normal's or
colors the procedure is the same:
The process is continued for each pixel in the polygon, and for each
polygon in the scene.
February 5, 2015 32
Gouraud shading also has limitations.
• One of the most important ones is the loss of highlights on surfaces and
highlights that are displayed with unusual shapes. Following figure shows a
polygon with an interior highlight. Since Gouraud shading is based on the
intensity of the pixels located at the polygon edges, this highlight is missed.
In this case pixel p3 is rendered by interpolating the values of p1 and p2,
which produces a darker color than the one required.
• Another error associated with Gouraud shading is the appearance of bright or
dark streaks, called Mach bands.
February 5, 2015 33
• colors are interpolated across the polygon.
• need to know a normal for each vertex of the polygon.
• slower than flat shading.
• lighting equation used at each vertex.
Given a normal at each vertex of the polygon, the color at each vertex is
determined from the lighting equations and the material properties. Linear
interpolation of the color values at each vertex are used to generate color
values for each pixel on the edges. Linear interpolation across each scan line
is used to then fill in the color of the polygon.
Gouraud Shading Properties
February 5, 2015 34
- The intensity value is calculated once for each vertex of a polygon.
- The intensity values for the inside of the polygon are obtained by
interpolating the vertex values.
- Eliminates the intensity discontinuity problem.
- Still not model the specular reflection correctly.
- The interpolation of color values can cause bright or dark intensity streaks,
called the Mach- bands, to appear on the surface.
February 5, 2015 35
Gouraud Shading Limitations - Mach Bands
The rate of change of pixel
intensity is even across any
polygon, but changes as
boundaries are crossed
This ‘discontinuity’ is
accentuated by the human
visual system, so that we see
either light or dark lines at the
polygon edges - known as
February 5, 2015 firstname.lastname@example.org 36
The individual bands should appear as gradients,
and they may even appear to be curved. In fact,
they are all solid colors. Now look at this one:
If you look closely at the area above the center
two arrows, you should see a thin bright line
(left-middle arrow) and a thin dark line (right-
middle arrow). Once again, this is despite the
fact that each of the three areas (dark, light,
and in between) are solid colors.
February 5, 2015 email@example.com 37
In the graph on the bottom, the
black line represents the actual
luminance of the figure. The red
line reflects the perceived
luminance. The red line’s
deviation from the black line
represents the Mach Band
phenomena, and the little spikes
over and above the black line
represent the bright and dark
lines you see in the second
February 5, 2015 38
February 5, 2015 firstname.lastname@example.org 39
Gouraud ShadingFlat Shading
February 5, 2015 40
Phong shading is the most popular shading algorithm in use today. This
method was developed by Phong Bui-Toung.
Pong shading, also called normal-vector interpolation, is based on
calculating pixel intensities by means of the approximated normal vector
at the point in the polygon.
Although more calculation expensive, Phong shading improves the
rendering of bright points and highlights that are mis-rendered in Gouraud
February 5, 2015 41
One lighting calculation per pixel
February 5, 2015 email@example.com 42
February 5, 2015 firstname.lastname@example.org 43
February 5, 2015 44
- Instead of interpolating the intensity values, the normal vectors are being
interpolated between the vertices.
- The intensity value is then calculated at each pixel using the interpolated
- This method greatly reduces the Mach-band problem but it requires more
February 5, 2015 45
• Compute normal, ni, at each vertex (if not already given)
• Interpolate normals during scan conversion
• Compute color with the interpolated normals ƒ
- Expensive: compute illumination for every visible point on a surface ƒ
- Captures highlights in the middle of a polygon ƒ
- Looks smoother across edges
February 5, 2015 46
•normals are interpolated across the polygon.
•need to know a normal for each vertex of the polygon.
•better at dealing with highlights than Goraud shading.
•slower than Goraud shading.
•lighting equation used at each pixel.
Where Goraud shading uses normals at the vertices and then interpolates the
resulting colors across the polygon, Phong shading goes further and
interpolates than normals. Linear interpolation of the normal values at each
vertex are used to generate normal values for the pixels on the edges. Linear
interpolation across each scan line is used to then generate normals at each
pixel across the scan line.
February 5, 2015 47
February 5, 2015 email@example.com 48
Phong Shading Gouraud Shading
February 5, 2015 49
Fog, or atmospheric attenuation allows us to simulate this affect.
Fog is implemented by blending the calculated color of a pixel with a given
background color ( usually grey or black ), in a mixing ratio that is somehow
proportional to the distance between the camera and the object. Objects that are
farther away get a greater fraction of the background color relative to the object's
color, and hence "fade away" into the background. In this sense, fog can ( sort of )
be thought of as a shading effect.
Fog is typically given a starting distance, an ending distance, and a color. The fog
begins at the starting distance and all the colors slowly transition to the fog color
towards the ending distance. At the ending distance all colors are the fog color.
Distance fog is a technique used in 3D computer graphics to enhance the
perception of distance by simulating fog.
February 5, 2015 50
Here is a scene from battalion
without fog. The monster sees a
very sharp edge to the world
Here is the same scene with fog.
The monster sees a much softer
horizon as objects further away tend
towards the black color of the sky
February 5, 2015 firstname.lastname@example.org 51
Along a particular ray from some object to your eye, fog particles suspended in
the air "scatter" some of the light that would otherwise reach your eye.
Pollutants, smoke, and other airborne particles can also absorb light, rather than
scattering the light, thereby reducing the intensity of the light that reaches your
February 5, 2015 email@example.com 52
If light travels a unit of distance along
some ray through a uniformly foggy
Cleave = Center - Creduction + Cincrease
Cleave is the color intensity of the light leaving a unit segment along a ray,
Center is the color intensity of the light entering a unit segment along a ray,
Creduction is the color intensity of all the light absorbed or scattered away along the
unit segment, and
Cincrease is the color intensity of additional light redirected by the fog in the direction
of the ray along the unit segment.
February 5, 2015 firstname.lastname@example.org 53
The intensity of light absorbed or scattered away cannot be greater than the
intensity of incoming light. Assuming a uniform fog density, there must be a fixed
ratio between the color of incoming light and the color of light scattered away. So
h is a constant scale factor dependent on the fog
February 5, 2015 email@example.com 54
Assuming a uniform fog color, the color of additional light scattered by the fog
in the direction of the ray is:
Cincrease = h x Cfog
Cfog is the constant fog color.
Now you can express the color of the light leaving the fogged unit distance in
terms of the color of the entering light, a constant percentage of light scattering
and extinction, and a constant fog color. This relationship is:
Cleave = (1 - h) x Center + h x Cfog