CG Module4 On
CG Module4 On
Module 4
Viewing
4.1 CLASSICAL AND COMPUTER 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, architectural 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 produce 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.
We have objects, a viewer, projectors, and a projection plane (Figure 5.1). The projectors meet at the center of
projection (COP). The COP corresponds to the center of the lens in the camera or in the eye, and in a computer
graphics system, it is the origin of the camera frame for perspective views. All standard graphics systems follow
the model that we described in Chapter 1, which is based on geometric optics. The projection surface is a plane,
and the projectors are straight lines. This situation is the one we usually encounter and is straightforward to
implement, especially with our pipeline model.
Both classical and computer graphics allow the viewer to be an infinite distance from the objects. Note that as we
move the COP to infinity, the projectors become parallel and the COP can be replaced by a direction of
projection (DOP), as shown in Figure 5.2. Note also that as the COP moves to infinity, we can leave the
projection plane fixed and the size of the image remains about the same, even though the COP is infinitely far
from the objects. Views with a finite COP are called perspective views; views with a COP at infinity are called
parallel views. For parallel views, the origin of the camera frame usually lies in the projection plane.
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.
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 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
s shorter than its length measured in
the object space. This foreshortening of distances is the same 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.
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 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.
In terms of the pipeline architecture, viewing consists of two fundamental operations. First, we must position and
orient the camera. This operation is the job of the model-view transformation. After vertices pass through this
transformation, they are represented in eye or camera coordinates. The second step is the application of the
projection transformation. This step applies the specified projection orthographic or perspective to the vertices
and puts objects within the specified clipping volume in a normalized clipping volume.
OpenGL starts with the camera at the origin of the object frame, pointing in the negative z-direction. This camera
is set up for orthogonal views and has a viewing volume that is a cube, centered at the origin and with sides of
length 2. The default projection plane is the plane z = 0 and the direction of the projection is along the zaxis.
Thus, objects within this box are visible and projected as shown in Figure 5.11. Until now, we were able to ignore
any complex viewing procedures by exploiting our knowledge of this camera. Thus, we were able to define
objects in the application programs that fit inside this cube and we knew that they would be visible. In this
approach, both the model-view and projection matrices were left as the default identity matrices.
Subsequently, we altered the model-view matrix, initially an identity matrix, by rotations and translations, so as
to place the camera where we desired. The parameters that we set in glOrtho alter the projection matrix, also
initially an identity matrix, so as to allow us to see objects inside an arbitrary right parallelepiped.
in 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 approximation to the rendering equation for particular types of surfaces.
Rather than looking at a global energy balance, we follow rays of light from light-emitting (or self-luminous)
surfaces that we call light sources. We then model what happens to these rays as they interact with reflecting
surfaces in the scene.
This approach is similar to ray tracing, but we consider only single interactions between light sources and surfaces
and do not consider the possibility that light from a source may be blocked from reaching the surface by another
surface. There are two independent parts of the problem. First, we must model the light sources in the scene. Then
we must build a reflection model that describes the interactions between materials and light.
To get an overview of the process, we can start following rays of light from a point source, as shown in Figure
6.2. As we noted in Chapter 1, our viewer sees only the light that leaves the source and reaches her eyes perhaps
through a complex path and multiple interactions with objects in the scene. If a ray of light enters her eye directly
from the source, she sees the color of the source. If the ray of light hits a surface visible to our viewer, the color
she sees is based on the interaction between the source and the surface material: She sees the color of the light
reflected from the surface toward her eyes.
In terms of computer graphics, we can place the projection plane between the center of projection and the objects,
as shown in Figure 6.3. Conceptually, the clipping window in this plane is mapped to the display; thus, we can
think of the projection plane as ruled into rectangles, each corresponding to a pixel on the display.
Because we only need to consider light that enters the camera by passing through the center of projection, we can
start at the center of projection and follow a projector though each pixel in the clipping window. If we assume
that all our surfaces are opaque, then the color of the first surface intersected along each projector determines the
color of the corresponding pixel in the frame buffer.
The 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.
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.
The four basic types of sources: ambient lighting, point sources, spotlights, and distant light. These four lighting
types are sufficient for rendering most simple scenes.
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
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 we
can see from Figure 6.8, which shows the shadows created by a source of finite 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 + cd2) 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 of the light from the source is sufficiently uniform
that the distance term is almost constant over the surfaces.
4.4.4 Spotlights
Spotlights are characterized by a narrow range of angles through which light is emitted. 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 points in the direction ls, and whose width is determined by an an
otlight becomes a point source. More realistic spotlights are characterized by the
distribution of light within the cone usually with most of the light concentrated in the center of the cone. Thus,
the inten tion of the source and a vector s to a point on the surface
(as long as this angle is less this function could be defined in many ways, it is
usual 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
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:
In contrast, the distant light source is described by a direction vector whose representation in homogeneous
coordinates is the matrix
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.
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.
The Phong model supports the three types of light material interactions ambient, diffuse, and specular. Suppose
that we have a set of point sources. Each source can have separate ambient, diffuse, and specular components for
each of the three primary colors.
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:
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 contains 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
at p is RirdLird. 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:
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:
We obtain the total intensity by adding the contributions of all sources and, possibly, a global ambient term. Thus,
the red term is
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
and thus
Ia = kaLa.
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.
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, according
of the incoming light.
One way to understand this law is to consider a small parallel light source striking a plane, as shown in Figure
6.16. As the source is lowered in the (artificial) sky, the same amount of light is spread over a larger area, and the
surface appears dimmer. Returning to the point source of Figure 6.15, we can characterize diffuse reflections
Rd cos ,
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, then
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:
Whereas a diffuse surface is rough, a specular surface is smooth. The smoother the surface is, the more it
resembles a mirror. Figure 6.18 shows that as the surface gets smoother, the reflected light is concentrated in a
smaller range of angles centered about the angle of a perfect reflector a mirror or a perfectly specular surface.
Modeling specular surfaces realistically can be complex because the pattern by which the light is scattered is not
symmetric. It depends on the wavelength of the incident light, and it changes with the reflection angle.
Phong proposed an approximate model that can be computed with only a slight increase over the work done for
diffuse surfaces. The model adds a term for specular reflection. Hence, we consider the surface as being rough
for the diffuse term and smooth for the specular term. The amount of light that the viewer sees depends on the
or, and v, the direction of the viewer. The Phong model uses
the equation Is
incoming specular light that is
shininess coefficient. increased, the reflected light is concentrated in a narrower
region centered on the ity, 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
This formula is computed for each light source and for each primary.
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
and h, the halfway angle.
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.
If we replace r . v with n . h, we avoid calculation of
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.
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.
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.
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);
Flat shading will show differences in shading for the polygons in our mesh. If the light sources and viewer are
near the polygon, the vectors l and v will be different for each polygon. However, if our polygonal mesh has been
designed to model a smooth surface, flat shading will almost always be disappointing because we can see even
small differences in shading between adjacent polygons, as shown in Figure 6.25. The human visual system has
a remarkable sensitivity to small differences in light intensity, due to a property known as lateral inhibition. If we
see an increasing sequence of intensities, as shown in Figure 6.26, we perceive the increases in brightness as
overshooting on 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.
is distant, and either the viewer is distant or there are no specular reflections, then smooth (or
interpolative) shading shades a polygon in a constant color.
If we consider our mesh, the idea of a normal existing at a vertex should cause concern to anyone
worried about mathematical correctness. Because multiple polygons meet at interior vertices of
the mesh, each of which has its own normal, the normal at the vertex is discontinuous. Although
this situation might complicate the mathematics, Gouraud realized that the normal at the vertex
could be defined in such a way as to achieve smoother shading through interpolation. Consider
an interior vertex, as shown in Figure 6.28, where four polygons meet. Each has its own normal.
In 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
What we need, of course, is a data structure for representing the mesh that contains 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.
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
Once we have the normal at each point, we can make an independent shading calculation. Usually, this process
can be combined with rasterization of the polygon. Until recently, Phong shading could only be carried out off-
line because it requires the interpolation of normals across each polygon. In terms of the pipeline, Phong shading
requires that the lighting model be applied to each fragment; hence, the name per fragment shading.