Transformations and Viewing This chapter discusses the mathematics of linear, affine, and perspective transformations and their uses in OpenGL.. There are special transformations, called
Trang 1I.2 Coordinates, Points, Lines, and Polygons 13
Figure I.12 Three triangles The triangles are turned obliquely to the viewer so that the top portion of each triangle is in front of the base portion of another
It is also important to clear the depthbuffer eachtime you render an image This is typically done witha command suchas
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
which both clears the color (i.e., initializes the entire image to the default color) and clears the depthvalues
The SimpleDraw program illustrates the use of depth buffering for hidden surfaces It shows three triangles, each of which partially hides another, as in Figure I.12 This example shows why ordering polygons from back to front is not a reliable means of performing hidden surface computation
Polygon Face Orientations
OpenGL keeps track of whether polygons are facing toward or away from the viewer, that is, OpenGL assigns eachpolygon a front face and a back face In some situations, it is desirable for only the front faces of polygons to be viewable, whereas at other times you may want both the front and back faces to be visible If we set the back faces to be invisible, then any polygon whose back face would ordinarily be seen is not drawn at all and, in effect, becomes transparent (By default, bothfaces are visible.)
OpenGL determines which face of a polygon is the front face by the default convention that vertices on a polygon are specified in counterclockwise order (with some exceptions for triangle strips and quadrilateral strips) The polygons in Figures I.8, I.9, and I.10 are all shown withtheir front faces visible
You can change the convention for which face is the front face by using the glFrontFace command This command has the format
glFrontFace(
GL_CW GL_CCW
);
where “CW” and “CCW” stand for clockwise and counterclockwise; GL_CCW is the default Using GL_CW causes the conventions for front and back faces to be reversed on subsequent polygons
To make front or back faces invisible, or to do both, you must use the commands
glCullFace(
GL_FRONT GL_BACK GL_FRONT_AND_BACK
);
glEnable( GL_CULL_FACE );
Trang 2(a) Torus as multiple quad strips.
(b) Torus as a single quad strip
Figure I.13 Two wireframe tori withback faces culled Compare withFigure I.11
You must explicitly turn on the face culling with the call to glEnable Face culling can be turned off withthe corresponding glDisable command If bothfront and back faces are culled, then other objects such as points and lines are still drawn
The two wireframe tori of Figure I.11 are shown again in Figure I.13 with back faces culled Note that hidden surfaces are not being removed in either figure; only back faces have been culled
Toggling Wireframe Mode
By default, OpenGL draws polygons as solid and filled in It is possible to change this by using the glPolygonMode function, which determines whether to draw solid polygons, wireframe polygons, or just the vertices of polygons (Here, “polygon” means also triangles and quadri-laterals.) This makes it easy for a program to switch between the wireframe and nonwireframe mode The syntax for the glPolygonMode command is
glPolygonMode(
GL_FRONT GL_BACK GL_FRONT_AND_BACK
,
GL_FILL GL_LINE GL_POINT
);
The first parameter to glPolygonMode specifies whether the mode applies to front or back faces or to both The second parameter sets whether polygons are drawn filled in, as lines, or
as just vertices
Exercise I.5 Write an OpenGL program that renders a cube with six faces of different
colors.Form the cube from six quadrilaterals, making sure that the front faces are facing
Trang 3I.3 Double Buffering for Animation 15
outwards.If you already know how to perform rotations, let your program include the ability to spin the cube around.(Refer to Chapter II and see the WrapTorus program for code that does this.)
If you rendered the cube using triangles instead, how many triangles would be needed?
Exercise I.6 Repeat Exercise I.5 but render the cube using two quad strips, each containing
three quadrilaterals.
Exercise I.7 Repeat Exercise I.5 but render the cube using two triangle fans.
I.3 Double Buffering for Animation
The term “animation” refers to drawing moving objects or scenes The movement is only a visual illusion, however; in practice, animation is achieved by drawing a succession of still scenes, called frames, each showing a static snapshot at an instant in time The illusion of motion is obtained by rapidly displaying successive frames This technique is used for movies, television, and computer displays Movies typically have a frame rate of 24 frames per second The frame rates in computer graphics can vary with the power of the computer and the complexity of the graphics rendering, but typically one attempts to get close to 30 frames per second and more ideally 60 frames per second These frame rates are quite adequate to give smooth motion on
a screen For head-mounted displays, where the view changes with the position of the viewer’s head, much higher frame rates are needed to obtain good effects
Double buffering can be used to generate successive frames cleanly While one image is displayed on the screen, the next frame is being created in another part of the memory When the next frame is ready to be displayed, the new frame replaces the old frame on the screen instantaneously (or rather, the next time the screen is redrawn, the new image is used) A region of memory where an image is being created or stored is called a buffer The image
being displayed is stored in the front buffer, and the back buffer holds the next frame as it is
being created When the buffers are swapped, the new image replaces the old one on the screen Note that swapping buffers does not generally require copying from one buffer to the other; instead, one can just update pointers to switch the identities of the front and back buffers
A simple example of animation using double buffering in OpenGL is shown in the program SimpleAnimthat accompanies this book To use double buffering, you should include the following items in your OpenGL program: First, you need to have a graphics context that supports double buffering This is obtained by initializing your graphics window by a function call suchas
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
In SimpleAnim, the function updateScene is used to draw a single frame It works by drawing into the back buffer and at the very end gives the following commands to complete the drawing and swap the front and back buffers:
glFlush();
glutSwapBuffers();
It is also necessary to make sure that updateScene is called repeatedly to draw the next frame There are two ways to do this The first way is to have the updateScene routine call glutPostRedisplay() This will tell the operating system that the current window needs rerendering, and this will in turn cause the operating system to call the routine speci-fied by glutDisplayFunc The second method, which is used in SimpleAnim, is to use glutIdleFuncto request the operating system to call updateScene whenever the CPU is
Trang 4idle If the computer system is not heavily loaded, this will cause the operating system to call updateScenerepeatedly
You should see the GLUT documentation for more information about how to set up call-backs, not only for redisplay functions and idle functions but also for capturing keystrokes, mouse button events, mouse movements, and so on The OpenGL programs supplied with this book provide examples of capturing keystrokes; in addition, ConnectDots shows how to capture mouse clicks
Trang 5Transformations and Viewing
This chapter discusses the mathematics of linear, affine, and perspective transformations and their uses in OpenGL The basic purpose of these transformations is to provide methods of changing the shape and position of objects, but the use of these transformations is pervasive throughout computer graphics In fact, affine transformations are arguably the most fundamen-tal mathematical tool for computer graphics
An obvious use of transformations is to help simplify the task of geometric modeling For example, suppose an artist is designing a computerized geometric model of a Ferris wheel
A Ferris wheel has considerable symmetry and includes many repeated elements such as multiple cars and struts The artist could design a single model of the car and then place multiple instances of the car around the Ferris wheel attached at the proper points Similarly, the artist could build the main structure of the Ferris wheel by designing one radial “slice” of the wheel and using multiple rotated copies of this slice to form the entire structure Affine transformations are used to describe how the parts are placed and oriented
A second important use of transformations is to describe animation Continuing withthe Ferris wheel example, if the Ferris wheel is animated, then the positions and orientations of its individual geometric components are constantly changing Thus, for animation, it is necessary
to compute time-varying affine transformations to simulate the motion of the Ferris wheel
A third, more hidden, use of transformations in computer graphics is for rendering After a 3-D geometric model has been created, it is necessary to render it on a two-dimensional surface
called the viewport Some common examples of viewports are a window on a video screen, a
frame of a movie, and a hard-copy image There are special transformations, called perspective transformations, that are used to map points from a 3-D model to points on a 2-D viewport
To properly appreciate the uses of transformations, it is important to understand the ren-dering pipeline, that is, the steps by which a 3-D scene is modeled and rendered A high-level
description of the rendering pipeline used by OpenGL is shown in Figure II.1 The stages of the pipeline illustrate the conceptual steps involved in going from a polygonal model to an on-screen image The stages of the pipeline are as follows:
Modeling In this stage, a 3-D model of the scene to be displayed is created This stage is
generally the main portion of an OpenGL program The program draws images by spec-ifying their positions in 3-space At its most fundamental level, the modeling in 3-space consists of describing vertices, lines, and polygons (usually triangles and quadrilaterals)
by giving the x-, y-, z-coordinates of the vertices OpenGL provides a flexible set of tools
for positioning vertices, including methods for rotating, scaling, and reshaping objects
Trang 6Modeling View
Selection
Perspective
Figure II.1 The four stages of the rendering pipeline in OpenGL
These tools are called “affine transformations” and are discussed in detail in the next sections OpenGL uses a 4× 4 matrix called the “model view matrix” to describe affine transformations
View Selection This stage is typically used to control the view of the 3-D model In this
stage, a camera or viewpoint position and direction are set In addition, the range and the field of view are determined The mathematical tools used here include “orthographic projections” and “perspective transformations.” OpenGL uses another 4× 4 matrix called the “projection matrix” to specify these transformations
Perspective Division The previous two stages use a method of representing points in
3-space by means of homogeneous coordinates Homogeneous coordinates use vectors with four components to represent points in 3-space
The perspective division stage merely converts from homogeneous coordinates back
into the usual three x-, y-, z-coordinates The x- and y-coordinates determine the position
of a vertex in the final graphics image The z-coordinates measure the distance to the
object, although they can represent a “pseudo-distance,” or “fake” distance, rather than
a true distance
Homogeneous coordinates are described later in this chapter As we will see, perspec-tive division consists merely of dividing through by aw value.
Displaying In this stage, the scene is rendered onto the computer screen or other display
medium suchas a printed page or a film A window on a computer screen consists of a rectangular array of pixels Eachpixel can be independently set to an individual color and brightness For most 3-D graphics applications, it is desirable to not render parts of the scene that are not visible owing to obstructions of view OpenGL and most other graphics display systems perform this hidden surface removal with the aid of depth (or distance) information stored witheachpixel During this fourthstage, pixels are given color and depth information, and interpolation methods are used to fill in the interior of polygons This fourth stage is the only stage dependent on the physical characteristics of the output device The first three stages usually work in a device-independent fashion
The discussion in this chapter emphasizes the mathematical aspects of the transformations used by computer graphics but also sketches their use in OpenGL The geometric tools used
in computer graphics are mathematically very elegant Even more important, the techniques discussed in this chapter have the advantage of being fairly easy for an artist or programmer to use and lend themselves to efficient software and hardware implementation In fact, modern-day PCs typically include specialized graphics chips that carry out many of the transformations and interpolations discussed in this chapter
II.1 Transformations in 2-Space
We start by discussing linear and affine transformations on a fairly abstract level and then see examples of how to use transformations in OpenGL We begin by considering affine transformations in 2-space since they are much simpler than transformations in 3-space Most
of the important properties of affine transformations already apply in 2-space
Trang 7II.1 Transformations in 2-Space 19
The x y-plane, denotedR2= R × R, is the usual Cartesian plane consisting of points x, y.
To avoid writing too many coordinates, we often use the vector notation x for a point inR2, with
the usual convention being that x= x1, x2, where x1, x2∈ R This notation is convenient but potentially confusing because we will use the same notation for vectors as for points.1
We write 0 for the origin, or zero vector, and thus 0= 0, 0 We write x + y and x − y for
the componentwise sum and difference of x and y A real numberα ∈ R is called a scalar, and
the product of a scalar and a vector is defined byαx = αx1, αx2.2
II.1.1 Basic Definitions
A transformation onR2 is any mapping A :R2 2 That is, each point x∈ R2is mapped
to a unique point, A(x), also inR2
Definition Let A be a transformation A is a linear transformation provided the following two
conditions hold:
1 For allα ∈ R and all x ∈ R2, A(αx) = α A(x).
2 For all x, y ∈ R2, A(x + y) = A(x) + A(y).
Note that A(0) = 0 for any linear transformation A This follows from condition 1 with α = 0.
Examples: Here are five examples of linear transformations:
1 A1:
2 A2:
3 A3:
4 A4:
5 A5:
Exercise II.1 Verify that the preceding five transformations are linear.Draw pictures of
how they transform the F shown in Figure II.2.
We defined transformations as acting on a single point at a time, but of course, a transfor-mation also acts on arbitrary geometric objects since the geometric object can be viewed as a collection of points and, when the transformation is used to map all the points to new locations, this changes the form and position of the geometric object For example, Exercise II.1 asked you to calculate how transformations acted on theF shape
1 Points and vectors in 2-space both consist of a pair of real numbers The difference is that a point specifies a particular location, whereas a vector specifies a particular displacement, or change in location That is, a vector is the difference of two points Rather than adopting a confusing and nonstandard notation that clearly distinguishes between points and vectors, we will instead fol-low the more common, but ambiguous, convention of using the same notation for points as for vectors
2 In view of the distinction between points and vectors, it can be useful to form the sums and differences
of two vectors, or of a point and a vector, or the difference of two points, but it is not generally useful
to form the sum of two points The sum or difference of two vectors is a vector The sum or difference
of a point and a vector is a point The difference of two points is a vector Likewise, a vector may be multiplied by a scalar, but it is less frequently appropriate to multiply a scalar and point However, we gloss over these issues and define the sums and products on all combinations of points and vectors
In any event, we frequently blur the distinction between points and vectors
Trang 81, 0
1, 1
0, −1
0, 0
0, 1 y
x
Figure II.2 AnF shape
One simple, but important, kind of transformation is a “translation,” which changes the position of objects by a fixed amount but does not change the orientation or shape of geometric objects
Definition A transformation A is a translation provided that there is a fixed u∈ R2
suchthat
A(x)= x + u for all x ∈ R2
The notation Tuis used to denote this translation, thus Tu (x) = x + u.
The composition of two transformations A and B is the transformation computed by first applying B and then applying A This transformation is denoted A ◦ B, or just AB, and satisfies ( A ◦ B)(x) = A(B(x)).
The identity transformation maps every point to itself The inverse of a transformation A is the transformation A−1 suchthat A ◦ A−1 and A−1◦ A are boththe identity transformation Not every transformation has an inverse, but when A is one-to-one and onto, the inverse transformation A−1always exists
Note that the inverse of Tuis T−u
Definition A transformation A is affine provided it can be written as the composition of a
translation and a linear transformation That is, provided it can be written in the form A = TuB
for some u∈ R2and some linear transformation B.
In other words, a transformation A is affine if it equals
with B a linear transformation and u a point.
Because it is permitted that u = 0, every linear transformation is affine However, not every affine transformation is linear In particular, if u = 0, then transformation II.1 is not linear since it does not map 0 to 0.
Proposition II.1 Let A be an affine transformation.The translation vector u and the linear
transformation B are uniquely determined by A.
Proof First, we see how to determine u from A We claim that in fact u = A(0) This is proved
by the following equalities:
A(0) = Tu(B(0)) = Tu (0) = 0 + u = u.
Then B = T−1
u A = T−uA, and so B is also uniquely determined.
II.1.2 Matrix Representation of Linear Transformations
The preceding mathematical definition of linear transformations is stated rather abstractly
However, there is a very concrete way to represent a linear transformation A – namely, as a
2× 2 matrix
Trang 9II.1 Transformations in 2-Space 21
Define i= 1, 0 and j = 0, 1 The two vectors i and j are the unit vectors aligned with the x-axis and y-axis, respectively Any vector x = x1, x2 can be uniquely expressed as a linear
combination of i and j, namely, as x= x1i+ x2j.
Let A be a linear transformation Let u = u1, u2 = A(i) and v = v1, v2 = A(j) Then,
by linearity, for any x∈ R2,
A(x) = A(x1i+ x2j) = x1A(i) + x2A(j) = x1u+ x2v
= u1x1+ v1x2, u2x1+ v2x2.
Let M be the matrix u1
u2v1
v2 Then,
M
x1
x2
=
u1 v1
u2 v2
x1
x2
=
u1x1+ v1x2
u2x1+ v2x2
,
and so the matrix M computes the same thing as the transformation A We call M the matrix representation of A.
We have just shown that every linear transformation A is represented by some matrix.
Conversely, it is easy to check that every matrix represents a linear transformation Thus, it
is reasonable to think henceforth of linear transformations onR2as being the same as 2× 2 matrices
One notational complication is that a linear transformation A operates on points x = x1, x2,
whereas a matrix M acts on column vectors It would be convenient, however, to use both of
the notations A(x) and Mx To make both notations be correct, we adopt the following rather
special conventions about the meaning of angle brackets and the representation of points as column vectors:
Notation The point or vectorx1, x2 is identical to the column vector x1
x2 So “point,”
“vector,” and “column vector” all mean the same thing A column vector is the same as
a single column matrix A row vector is a vector of the form (x1, x2), that is, a matrix witha single row
A superscript T denotes the matrix transpose operator In particular, the transpose of
a row vector is a column vector and vice versa Thus, xTequals the row vector (x1, x2)
It is a simple, but important, fact that the columns of a matrix M are the images of i and j under M That is to say, the first column of M is equal to Mi and the second column of M is equal to Mj This gives an intuitive method of constructing a matrix for a linear transformation,
as shown in the next example
Example: Let M= 1
1 0
2 Consider the action of M on theF shown in Figure II.3 To find the
matrix representation of its inverse M−1, it is enoughto determine M−1i and M−1j It is not hard to see that
M−1
1
0
=
1
−1/2
and M−1
0 1
=
0 1/2
.
Hint: Bothfacts follow from M 0
1/2 = 0
1 and M 1
0 = 1
1
Therefore, M−1is equal to 1
−1/210/2 .
Trang 101, 0
1, 1
0, −1
0, 0
0, 1 y
1, 3
1, 1
0, −2
0, 0
0, 2
y
x
Figure II.3 AnF shape transformed by a linear transformation
The example shows a rather intuitive way to find the inverse of a matrix, but it depends on
being able to find preimages of i and j One can also compute the inverse of a 2× 2 matrix by the well-known formula
a b
c d
−1
det(M)
d −b
−c a
, where det(M ) = ad − bc is the determinant of M.
Exercise II.2 Figure II.4 shows an affine transformation acting on an F.(a) Is this a linear transformation? Why or why not? (b) Express this affine transformation in the form
x
A rotation is a transformation that rotates the points inR2by a fixed angle around the origin Figure II.5 shows the effect of a rotation ofθ degrees in the counterclockwise (CCW) direction.
As shown in Figure II.5, the images of i and j under a rotation ofθ degrees are cos θ, sin θ
and−sin θ, cos θ Therefore, a counterclockwise rotation through an angle θ is represented
by the matrix
R θ =
cosθ −sin θ
sinθ cos θ
Exercise II.3 Prove the angle sum formulas for sin and cos:
sin(θ + ϕ) = sin θ cos ϕ + cos θ sin ϕ
cos(θ + ϕ) = cos θ cos ϕ − sin θ sin ϕ,
by considering what the rotation R θ does to the point x = cos ϕ, sin ϕ.
1, 0
1, 1
0, 1
0, 0
0, 1 y
1, −1
1, 0
1, 1
0, 0
0, 1
y
x
−
Figure II.4 An affine transformation acting on anF
... . Trang 101, 0
1, 1
0, −1
0, 0
0,... able to find preimages of i and j One can also compute the inverse of a 2× matrix by the well-known formula
a b
c d
−1
det(M)