Tài liệu 3D - Computer Graphics P2 - Pdf 10

I.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 depth buffer each time you render an image. This is typically
done with a command such as
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
depth values.
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 each polygon 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, both faces 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
with their 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

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 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
SimpleAnim that 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 such as
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,istouse
glutIdleFunc to request the operating system to call updateScene whenever the CPU is
Team LRN
16 Introduction
idle. If the computer system is not heavily loaded, this will cause the operating system to call

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.
17
Team LRN
18 Transformations and Viewing
Modeling
View
Selection
Perspective
Division
Displaying
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

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.
Team LRN
II.1 Transformations in 2-Space 19
The xy-plane, denoted R
2
= 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 in R
2
, with
the usual convention being that x =x
1
, x
2
, where x
1
, x
2
∈ 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 =αx
1
,αx
2
.
2

3
: x, y→x + y, y.
4. A
4
: x, y→x, −y.
5. A
5
: x, y→−x, −y.
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 the F 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.

such that A ◦ A
−1
and A
−1
◦ A are both the identity transformation.
Not every transformation has an inverse, but when A is one-to-one and onto, the inverse
transformation A
−1
always exists.
Note that the inverse of T
u
is 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 = T
u
B
for some u ∈ R
2
and some linear transformation B.
In other words, a transformation A is affine if it equals
A(x) = B(x) + u, II.1
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

1
, u
2
=A(i) and v =v
1
,v
2
=A(j). Then,
by linearity, for any x ∈ R
2
,
A(x) = A(x
1
i + x
2
j) = x
1
A(i) + x
2
A(j) = x
1
u + x
2
v
=u
1
x
1
+ v
1

=

u
1
v
1
u
2
v
2

x
1
x
2

=

u
1
x
1
+ v
1
x
2
u
2
x
1

1
x
2

. 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 (x
1
, x
2
), that is, a matrix
with a 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, x
T
equals the row vector (x
1
, x
2
).
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

1/2

.
Hint: Both facts follow from M

0
1/2

=

0
1

and M

1
0

=

1
1

.
Therefore, M
−1
is equal to

1
−1/2

=
1
det(M)

d −b
−ca

,
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 → Mx +u by explicitly giving M and u.
A rotation is a transformation that rotates the points in R
2
by 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 θ

. II.2
Exercise II.3 Prove the angle sum formulas for sin and cos:
sin(θ +ϕ) = sin θ cos ϕ +cos θ sin ϕ
cos(θ +ϕ) = cos θ cos ϕ −sin θ sin ϕ,

θ
 sin , cos θ

θ
θ

Figure II.5. Effect of a rotation through angle θ . The origin 0 is held fixed by the rotation.
Conventions on Row and Column Vectors and Transposes. The conventions adopted in
this book are that points in space are represented by column vectors, and linear transfor-
mations with matrix representation M are computed as Mx. Thus, our matrices multiply
on the left. Unfortunately, this convention is not universally followed, and it is also com-
mon in computer graphics applications to use row vectors for points and vectors and
to use matrix representations that act on the right. That is, many workers in computer
graphics use a row vector to represent a point: instead of using x, they use the row vec-
tor x
T
. Then, instead of multiplying on the left with M, they multiply on the right with
its transpose M
T
. Because x
T
M
T
equals (Mx)
T
, this has the same meaning. Similarly,
when multiplying matrices to compose transformations, one has to reverse the order of
the multiplications when working with transposed matrices because (MN)
T
= N

is either a translation or
a generalized rotation.
Team LRN
24 Transformations and Viewing
x
y
0, 0
a, b
b, a

Figure II.6. A rigid, orientation-preserving, linear transformation acting on the unit vectors i and j.
For linear transformations, an equivalent definition of rigid transformation is that a linear
transformation A is rigid if and only if it preserves dot products. That is to say, if and only if, for
all x, y ∈ R
2
, x · y = A(x) · A(y). To see that this preserves distances, recall that ||x||
2
= x · x
is the square of the magnitude of x or the square of x’s distance from the origin.
3
Thus, ||x||
2
=
x · x = A(x) · A(x) =||A(x)||
2
. From the definition of the dot product as x · y =||x|| ·
||y||cos θ, where θ is the angle between x and y, the transformation A must also preserve
angles between lines.
Exercise II.4 Which of the five linear transformations in Exercise II.1 on page 19 are
rigid? Which ones are both rigid and orientation-preserving?

. Let u

be u
rotated counterclockwise 90

. Then M is orientation-preserving if and only if u

· v > 0.]
Theorem II.2 Every rigid, orientation-preserving, linear transformation is a rotation.
The converse to Theorem II.2 holds too: every rotation is obviously a rigid, orientation-
preserving, linear transformation.
Proof Let A be a rigid, orientation-preserving, linear transformation. Let a, b=A(i). By
rigidity, A(i) · A(i) = a
2
+ b
2
= 1. Also, A(j) must be the vector obtained by rotating A(i)
counterclockwise 90

; thus, A(j) =−b, a, as shown in Figure II.6.
Therefore, the matrix M representing A is equal to

a
b
−b
a

. Because a
2
+ b

u
θ
. The center of rotation is u =0, 3. The angle is θ = 45

.
Corollary II.3 Every rigid, orientation-preserving, affine transformation can be (uniquely)
expressed as the composition of a translation and a rotation.
Definition A generalized rotation is a transformation that holds a center point u fixed and
rotates all other points around u through a fixed angle θ . This transformation is denoted R
u
θ
.
An example of a generalized rotation is given in Figure II.7. Clearly, a generalized rotation
is rigid and orientation-preserving.
One way to perform a generalized rotation is first to apply a translation to move the point u
to the origin, then rotate around the origin, and then translate the origin back to u. Thus, the
generalized rotation R
u
θ
can be expressed as
R
u
θ
= T
u
R
θ
T
−u
. II.3


= L, θ is nonzero and is not a multiple of 180

. Let
L
2
be the line perpendicular to L at the point 0, and let L

2
be the line perpendicular to L at the
point u. Note that L
2
and L

2
are parallel. Now let L
3
be the line obtained by rotating L
2
around
Team LRN
26 Transformations and Viewing
0
u = A(0)
A(u)
v
θ
θ
2
θ

3
is mapped to L

3
by A. The two lines L
3
and L

3
are not parallel and intersect in a point v.By
the symmetry of the constructions, v is equidistant from 0 and u. Therefore, again by rigidity,
A(v) = v. It follows that A is the generalized rotation R
v
θ
, which performs a rotation through
an angle θ around the center v.

II.1.4 Homogeneous Coordinates
Homogeneous coordinates provide a method of using a triple of numbers x, y,wto represent
a point in R
2
.
Definition If x, y,w ∈ R and w = 0, then x, y,w is a homogeneous coordinate represen-
tation of the point x/w, y/w∈R
2
.
Note that any given point in R
2
has many representations in homogeneous coordinates.
For example, the point 2, 1 can be represented by any of the following sets of homogeneous

and other conic sections.
Team LRN
II.1 Transformations in 2-Space 27
II.1.5 Matrix Representation of Affine Transformations
Recall that any affine transformation A can be expressed as a linear transformation B followed
by a translation T
u
, that is, A = T
u
◦ B. Let M bea2×2 matrix representing B, and suppose
M =

ab
cd

and u =

e
f

.
Then the mapping A can be defined by

x
1
x
2

→ M


2
+ e
cx
1
+ dx
2
+ f

.
Now define N to be the 3 × 3 matrix
N =


abe
cd f
001


.
Using the homogeneous representation x
1
, x
2
, 1 of x
1
, x
2
, we see that
N


+ bx
2
+ e
cx
1
+ dx
2
+ f
1


.
The effect of N ’s acting on x, y, 1 is identical to the effect of the affine transformation A
acting on x, y. The only difference is that the third coordinate of “1” is being carried around.
More generally, for any other homogeneous representation of the same point, αx
1
,αx
2
,α
with α = 0, the effect of multiplying by N is
N


αx
1
αx
2
α



linear transformation? Why or why not? (b) Give a 3 × 3 matrix that represents the affine
transformation.
[Hint: In this case, the easiest way to find the matrix is to split the transformation into
a linear part and a translation. Then consider what the linear part does to the vectors i
and j.]
For the next exercise, it is not necessary to invert a 3 × 3 matrix. Instead, note that if a
transformation is defined by y = Ax + u, then its inverse is x = A
−1
y − A
−1
u.
Team LRN
28 Transformations and Viewing
1, 0
1, 1
0, −1
0, 0
0, 1
y
x
1, −1
1
2
, 10, 1
0, 2
y
x

Figure II.9. An affine transformation acting on an F.
Exercise II.9 Give the 3 × 3 matrix that represents the inverse of the transformation in

The calls to glBegin and glEnd are used to bracket calls to glVertex2f. The param-
eter
GL_POINTS specifies that individual points are to be drawn, not lines or polygons.
Figure II.10(a) shows the indicated points.
However, OpenGL applies the transformation M before the points are drawn. Thus, the
points will be drawn at the positions shown in Figure II.10(a) if M is the identity matrix. On
Team LRN
II.1 Transformations in 2-Space 29
(a)
0, 1
1, −1

 1, −

1
y
x
2, 3

0, 2
0, 4
(b)
y
x
Figure II.10. Drawing points (a) without transformation by the model view matrix and (b) with trans-
formation by the model view matrix. The matrix is as given in the text and represents a rotation of −90

degrees followed by a translation of 1, 3.
the other hand, for example, if M is the matrix


−90

.
This transformation is applied to the vertices specified in
drawThreePoints, and thus the
vertices are placed as shown in Figure II.10(b). It is important to note the order in which the
two transformations are applied, since this is potentially confusing. The calls to the routines
pglTranslatef and pglRotatef perform multiplications on the right; thus, when the
vertices are transformed by M, the effect is that they are transformed first by the rotation and
4
The prefix
pglstands for “pseudo-GL.” The two pglfunctions would have to be coded as glTrans-
latef(1.0,3.0,0.0)
and
glRotatef(-90.0,0.0,0.0,1.0) to be valid OpenGL function
calls. These perform a translation and a rotation in 3-space (see Section II.2.2).
5
We are continuing to identify affine transformations with homogeneous matrices, and so T
1,3
and
R
−90

can be viewed as 3 × 3 matrices.
Team LRN
30 Transformations and Viewing
θ

r
r

ModelView matrix in a stack. This example shows how the OpenGL matrix manipulation
routines can be used to handle hierarchical models.
If you have never worked with OpenGL transformations before, then the order in which
rotations and translations are applied in the preceding program fragment can be confusing.
Note that the first time
drawThreePoints is called, the model view matrix is equal to
M = R
θ
◦ T
,0
◦ T
0,r+1
.
Team LRN
II.1 Transformations in 2-Space 31
1, 0
1, 1
0, −1
0, 0
0, 1
y
x
3, 01, 0
1, 1
2, 1
y
x

Figure II.12. The affine transformation for Exercise II.11.
The second time drawThreePoints is called

1,3
· R
−90

. The model view matrix was set by the two commands
pglTranslatef(1.0,3.0); // M = M · T
1,3
pglRotatef(-90.0); // M = M · R
−90

,
and our intuition was that these transformations act on the triangle by first rotating it clockwise
90

around the origin and then translating it by the vector 1, 3.
The alternate way of thinking about these transformations is to view them as acting on a
local coordinate system. First, the xy-coordinate system is translated by the vector 1, 3 to
create a new coordinate system with axes x

and y

. Then the rotation acts on the coordinate
system again to define another new local coordinate system with axes x

and y

by rotating
the axes −90

with the center of rotation at the origin of the x


obtained by translating the xy-axes by 1, 3. (b) The
coordinates further transformed by a clockwise rotation of 90

, yielding the local coordinate system with
axes x

and y

. In (b), the triangle’s vertices are drawn according to the local coordinate axes x

and y

.
When transformations are viewed as acting on local coordinate systems, the meanings of
the transformations are to be interpreted within the framework of the local coordinate system.
For instance, the rotation R
−90

has its center of rotation at the origin of the current local
coordinate system, not at the origin of the initial xy-axes. Similarly, a translation must be
carried out relative to the current local coordinate system.
Exercise II.12 Review the transformations used to draw the two triangles shown in Fig-
ure II.11. Understand how this works from the viewpoint that transformations act on local
coordinate systems. Draw a figure showing all the intermediate local coordinate systems
that are implicitly defined by the pseudocode that draws the two triangles.
II.1.8 Two-Dimensional Projective Geometry

Projective geometry provides an elegant mathematical interpretation of the homogeneous co-
ordinates for points in the xy-plane. In this interpretation, the triples x, y,wdo not represent


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status