Tải bản đầy đủ (.pdf) (490 trang)

Ebook Interactive computer graphics (5th edition) Part 1

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (21.01 MB, 490 trang )

Interactive

Graphics

Computer
A

Top-Down

Approach

FIFTH

Using

EDITION

OpenGL\302\256


To

Rose

Mary


CONTENTS

x xv


Preface

1.1

A pp l i c a t i o n s of

1.1.1

D is

1.1.2

D

1.1.3
1.1.4
1.2

Si m u la t io n

1.2.1 /

P ix e ls and the Frame
7
O u t p u t Devices
I n p u t Devices
10

1.21


1.2.3

U

p la y of

e s ig

A

2

n3
3

and Animation

5

Interfaces

ser

Computer Graphics

Information

System

Graphics


5

Buffer

1.3.1

I m a g e s : Physical
Ob j e c t s and Viewers

1.3.2

L ig

1.3.3

Im

a g e Formation

1.4

Im

a g i n g Systems

1.4.1

Th e


Pinhole

Camera

1.4.2
1.5

Th e
Th e

Human

Visual System

1.6
1.6.1

Th e
Th e

1.6.2
1.6.3

T h r e e -D i m e n s io n a lAPIs

1.6.4

Th e

1.7


Gr a p h i c s Architectures

1.7.1

D is

la y

1.7.2
1.7.3
1.7.4

Th e

Graphics

1.3

ht

and

12

and Images

14

Models


16
16

of

19

21

Interface

Programmer's

ASequence

18
Model

Synthetic-Camera

Pen-Plotter

10

Synthetic
11

23
24


Model

27

Images

Paradigm

Modeling-Rendering

28

29

Processors
30
p
Pip e l i n e Architectures
30
V

e r t ex

31

Pipeline

Processing


32

vu


viii

C o n t e nts

1.7.5

Cl i p p i n g

1.7.6

Ra st e r iz a t io n3

1.7.7

F

1.8

Pr o g r a m

1.9

P

and


Primitive

33

r a g m e n t Processing
m a b le

e rfo r m a n c e

35

Notes

Suggested Readings

36

36

Exercises

2.1

Th e

2.2

P


2.2.1

C o o r d i n a te

2.3

T

2.3.1

G ra p h ic s Functions

2.3.2

T

2.3.3

Th e

2.4

Pr i m i t iv e s and

2.4.1

P o ly g o n

Basics


2.4.2

P o ly g o n

Types

2.4.3

A p p

2.4.4
2.4.5

g ra m

h e

Two-Dimensional

m ing

O pen

Systems
G LA P I

40
4 6

48


Machines

5 0

Attributes

52
in OpenGL

ro x i m a t i n g

Tex t5

Applications

45

47
Graphics Pipeline and State
49
Interface
OpenGL

he

aSphere

53
55


6

2.4.6

Cu r v e d Objects
A t t r i b u t e s5
8

2.5

C o lo r

2.5.1

RGB Color

2.5.2

In d

58
60

ex ed

62

63


Color

2.5.3 Se

t t i n g of

Attributes

Color

65

66

2.6

V ie

2.6.1
2.6.2

Th e Orthographic View
Tw o - D i m e n s io n a l Viewing

2.6.3

M

2.7
2.7.1


Con t r o l

2.7.2

As p e c t

2.7.3

Th e

w in g

Functions
e ra ct io n with the
Ratio

66
69

70

a t r ix Modes

In t

39

Gasket


Sierpinski

ro

33
34

Pipelines

Characteristics

and

Summary

32

Assembly

3

and

System

Viewports

2.7.4

main, display,

P r o g ra m Structure

2.8

Th e

2.9

Pol y g o n s

Gasket

70
Window

and

myinit

72
Functions

74

75

Program
and

71


Recursion

76
77


Contents

The

2.10.1

U

se

s e of

of

2.10.2

U

H idde

2.11

P l o t t i n g Implicit


2.11.1

M

Suggested

Functions
86

Notes

92

Readings

94

Exercises

9

3.1

In

t e r a ct i o n
p ut

4


99

100

Devices

3.2

In

3.2.1

Ph y s ic a l

10 1

Devices

Input

104

3.2.2

Lo

3.2.3

In p


3.3

Cl i e n t s and Servers

3.4
3.4.1

3.4.2

3.4.3
3.5

g ic a l Devices

D is

105

Modes

107

109

p l a y Lists

Execution of Display
Tex t and Display Lists 112
F o n t sinGLUT

115
D e f i n i t i o n and

Di s p la y

and

Lists

3.6

Pr o g ra m

m in g

3.6.1

U s in g

the

Pointing

3.6.2

Win d ow

3.6.3

K


3.6.4

Th e

3.6.5

W

121

122

Events

Display and Idle
i n d o w Management

1 24

124

P i c k in g
P ic k i n g and Selection Mode

3.9

A

3.10


Bu i l d i n g Interactive

e nus

Simple

CAD

n im a tin g

s ing

aTimer

126
127

133

Program

Models

Interactive

3.11.1 The Rotating
Square
3.11.2 D o u ble Buffering
U


1 23

Callbacks

M

3.11.3

117

Input

1 17

Device

3.8
3.8.1

A

1 10

116

Event-Driven

Events


e y b o a rd

Lists

Modeling

3.7

3.11

85

AND

INPUT

ut

82

83

Removal

Squares
and

Summary

80

80

Dimensions

in Three

Polygons

n - Su rface

a rc h i n g

3

Points

Three-Dimensional

2.10.3

CHAPTER

Gasket

Three-Dimensional

2.10

140


142

Programs
14 3

144

146

De s i g n of Interactive
3.12
3.12.1 Toolkit s , W idgets, and

Programs
the

Frame

Buffer

147
148

IX


Contents

3.13
3.13.1


L

o g i c Operations

D

r a w ing

3.13.2

XOR

3.13.3

Cur s o rs and

Erasable

Lines

Color

15 3

and

15 0

1 53


Planes

Overlay

Summary and Notes

154

SuggestedReadings

155

155

Exercises

TC

14 9

HAPTE R

OBJECTS

^GEOMETRIC

4

4.1


Scal a r s ,

4.1.1

G e o m e t r ic Objects

AND TRANSFORMATIONS

160

and Vectors

Points,

16 0

4.1.5
4.1.6

161
Coor d i n a t e -F r e e Geometry
Th e Mathematical View: Vector and Affine
Th e Computer Science View
1 63
1 63
Geom e t r i cADTs
L i n e s1
65


4.1.7

A f f i n eSums

4.1.2

4.1.3
4.1.4

4.1.8

4.1.9
4.1.10

4.2

E

x a m p l e Change

4.3.4

H

o m og e n e o u s

4.3.6
4.4

E


x a m p le

W

o r k in g

F

r a m e s in

M

o d

4.5.1

M

o d e lin g

4.5.3
4.5.4

Representation

175

Coordinates


183

OpenGL

e l i n g a Colored
the Faces

188

a rd -an d Outward-Pointing
Faces
D a t a Structures for Object
Representation
Th e Color Cube
19 0
B il i n e a r Interpolation
191
V e r t ex Arrays
19 2

4.5.5
4.5.6
4.6
4.7

Tr a n s l a t i o n ,

4.7.1

T


4.7.2

R ot a t io n

Rotation,
19 8

19 8

189
18 9

195

e Transformations

r a n s l a t io n

187

Cube

In w

A ff in

170

176

in Frames
179
Change
181
with Representations

4.5

4.5.2

of

168

Primitives

4.3.3
4.3.5

162

166

4.3.2

4.3.1

Spaces

16 5


Con v e x it y1
66
D o t and Cross Products
Pla n e s1 67
T h r e e -D i m e n s io n a l

C o or d i n a t e Systems and Frames
R e p re sen t a t io n s and N-Tuples
172
172
Ch a n g e of Coordinate Systems

4.3

T59

and Scaling

197


Contents

4.7.3

S c a l i n g2

4.8


Tr a n s f o r m a t i o n s
Tr a n s l a t io n2
03
Scali n g2 04

4.8.1

4.8.2
4.8.3
4.8.4

4.9
4.9.1

o t at io

R

00
in

R

o t at io n

About a

4.9.4

R


About an

4.10
O pe n G L
4.10.1 The Current
4.10.2

Ro tati

4.10.3

Rotatio n
Ord e r of

4.10.4
4.10.5
4.10.6

o n,

Translation, and Scaling
a Fixed Point in

About

4.11.2

AVirtual


Trackball

221

4.11 .3

Smooth

Rotations

224

In c r

Qu a t e r n i o n s

4.12.1
4.12r2

Com

225

and

Quaternions

Notes

230

231

231

235

JEWING

5.1

C la s s i c a l and

5.1.1

237
C l a s s ic a l Viewing
Or t h o g r a p h ic Projections

Computer

5.1.2
5.1.3

A

5.1.4

Obl i q u e Projections
P


x o n o m e t r i c Projections

e r s p e c t iv e

Viewing

w in g

a

V ie

2 26

228

Rotation

Readings

Exercises

5.1.5
5.2

220

22 1

Screen


226

Quat e r n io n s and

|

2 19

Three-DimensionalApplications
the

ple x Numbers

Suggested

Matrices

Popping

e m e n tal Rotation

Summary and

2 17

2 18
a nd

4.11.1


4.11.4

2 16
OpenGL

2 17

Transformations

4.12

215
2 15

Matrix

Transformation

Loading , Pushing,
I n t e r f a c e s to
U s ing Areas of

4.11

2 12

Matrices

Transformation


n goftheCube

Spinni

211
Axis

Arbitrary

207

208

Point

Fixed

209
G e n e r a l Rotation
Th e Instance Transformation
o t at io n

202

05

n2

Sh e a r2 06

Con c a t e n a t i o n of Transformations

4.9.3

4.9.2

Coordinates

Homogeneous

with

Viewing

235

237
238

240
241

Computer

242

XI


xii


Con t e n t s

5.3.2

Pos i t i o n in g of the Camera
P o s i t io n i n g of the Camera Frame
T w o Viewing APIs
249

5.3.3

T

5.3.4

Ot h e r Viewing APIs

5.4

Simple Projections

5.4.1

5.3
5.3.1

he

Look-At


253
2 5

5.4.2

Pr o j e c t i o n s

5.5.1
5.5.2
5.6
5.6.1

Per s p e ct iv e in OpenGL
259
Par a l le l Viewing in OpenGL
261

5.7
5.7.1

in

257
2 58

OpenGL

2 62


Removal

d e n - S u rf a c e

64

In

t e r a ct i v e Mesh

M

e s he

s2

6

26 4

Displays

4

266

5.7.3

W a l k i n g Through a Scene
P o ly g o n Offset

268

5.8

P a r a l l e l- P r o j e c t i o n Matrices

5.7.2

5.8.1
5.8.2
5.8.3

P

r o j e c t io n

O

r t h o g o n a l - P r o j e c t io n

5.9

P

e r s p e ct i v e - P roj e c t i o n

P

e r s p e c t iv e


O b l iq

ue

Projections

O p

5.10

P r o j e c t i o n s and
and

L ig

ht

L i g h t Sources

6.2.1

Colo r

6.2.2

A

280

281


2 84
285

290
294

Sources

294

6.2.3

m b i e n t Light
P o i n t Sources

296

6.2.4

Sp o t l ig h t s2

6.2.5

D i s t a n t Light Sources

6.3

T


6.3.1

A

he

276

and Matter

6.2

2 76

Matrices

Transformations

Notes

271

273

Shadows

Suggested Readings
Exercises 285

6.1


Matrices

Normalization

e n G L Perspective

26 9

2 70

Normalization

5.9.1
5.9.2

Summary

4

254

5.5

Cu l l i n g2

44

2 52


Function

Pe r s p e ct iv e Projections
O r t h o g o n a l Projections

H id

2

244

|

2 95

9

7

297
Model
Lighting

Phong
m b i e n t Reflection

300

298



Contents

6.4.2

3 00
3 01
Th e Modified Phong Model
C o m p u t a t i o n of Vectors
N o r m a l Vectors
3 04
A n g l e of Reflection
3 08

6.5

Pol y g o n a l

6.5.1

F la

6.5.2

S

6.3.2

D iff u s e Reflection


6.3.3 Sp e c u la r
6.3.4

6.4
6.4.1

Reflection

Ph o n g Shading

6.6

A

6.8

6.9
6.10

3 04

3 09

Shading

t Shading
310
m o o t h and
Gouraud


6.5.3
6.7

303

311

Shading

313

p p r o x i m a t i o n of

a Sphere

Li g h t Sources in OpenGL
S pe ci f i c a t i o n of Materials in
Sh a d i n g of the Sphere Model
Gl o b a l Illumination
and Notes
3 25
Summary

SuggestedReadings
Exercises 326

by

Recursive


Subdivision

3 14
3 17

OpenGL

3 20
3 2

2

3 23

3 26

'T ^M\302\273TER 7
7.1

7.2
7.2.1

B a s i c Implementation
F

o ur

Strategies

3 32


Major Tasks

7.2.3
7.2.4

2
g33
Geom e try Processing
R a s t e r iz a t io n3
34
F r a g m e n t Processing

7.3

C

7.4

L in

7.2.2

7.4.1

o d e lin

M

lip


333
3 35

3 36
336

p in g

e - S e g m e n t Clipping
Coh e n -S u t h e rla n d

337

Clipping

7.4.2 L ia n g -Bar s ky Clipping 339
Pol y g o n Clipping
7.5
7.6
C l i p p i n g of Other Primitives
7.6.1
7.6.2

b ou n d i n g Boxes and Volumes
Cu rv e s , Surfaces, and Text

7.6.3

C lip


7.7

Cl i p p i n g

7.8
7.9

R

330

as

p

in

g in the Frame
in

Three

34

3

344

345


Buffer

Dimensions

345

346
3 49

te r iz a t io n

Br e s e n h a m 's

341

Algorithm

3 5

2

xiii


53V

Contents

7.10


P

7.10.1

I n s id

7.10.2

Ope

7.10.3

F il landSort

7.10.4

Fl

o ly g o n

354

Rasterization

354

e - Ou t s i d e Testing

and Concave


n GL

3 55

Polygons

356

357

o o dFill

7.10.5 Si n gu la r it ie s3 57
7.11 H i d d e n - S u r f a c e Removal
-Spaceand Image-Space
7.11.1 Object
7.11.2

So r ting

7.11.3
7.11.4

Scan L in e

7.11.5
7.11 .6
7.11 .7


The z-Buffer

7.12

B

and Hidden-Surface

a c k- Fa

Scan
A

Di s

7.13.1

C o lo

and the

Painter's Algorithm

and

Dith e r ing

371

3 72


r Systems

3 75

3 76
3 76

Halftoning

Summary and Notes

377

SuggestedReadings
Exercises 379

3 79

PtSCRETf TECHNIQUES

_

B uf fe rs

8.2

Di g i t a l

8.3


W r i t i n g into

8.3.1

W

Images

Buffers
389
391

Modes
r it e swithXOR
r it i n g

W

8.4.2

B i t and
Pixel Operations
O p e n G L Buffers and the Pixel
Bit m a p s3
94

8.4.3

Rast e r Fonts


8.4.4
8.4.5

Pix e ls

8.4.1

3 67

p l a y Considerations

CHAPTER 8

8.3.2
8.4

3 65

z-Buffer

369

7.13.2 Th e Color Matrix
7.13.3 Gam m a Correction

8.1

362


with the

n t i a l i a s in g

7.13

7.13.4

360

3 61

Algorithm

Sort

Depth

358

Approaches

Removal

360

Algorithms

ce Removal


Conversion

358

Lo

and

in

396

Images

8.5

E

8.5.1

D is

8.5.2

Test i n g Algorithms

8.5.3

B


397

x a m p le s

u ff e rs

for

392

3 95

o ku p Tables

p la y i n g

392

OpenGL
Pipeline

399
a Color Gamut

Picking

400
401

4 00



Contents

8.6

8.7
8.7.1
8.8
8.8.1

8.8.2

8.8.3
8.8.4
8.8.5
8.9
8.10

M a p p in
T

e xt u re

T

w o - D i m e n s io n a l Texture

T


e xt u re

Mapping
Mapping

in

Text u r e

Ble

8.11.4

An tiali a s ing

8.11.5

B

-

Depth
M

Cueing

and
and

u l t ir e n d e r i n g


8.12.1 Scen e
8.12.2 B u m p

Antialiasing

Front-to-Back

Fog

and the
4 35

8.13

S a m p lin g

8.13.1

Sam pling
R

Qua

n tiz

and

a tio n


4 44
446

SuggestedReadings

4 48

448

SHADERS

PROGRAMMABLE

Pr o g ra m

9

4 40

m a b le

GL S LandCg

4 56

U

p

451


451
4 53

Pipelines

9.2
Sh a d in g Languages
9.2.1 Sh a d e Trees 453
E x t e n d i n g OpenGL
9.3
9.3.1 O p e n G L Versions and
9.3.2

4 3

Aliasing

447

Exercises

9.1

4 36

438

Summary and Notes


9

4 34

a ge

Theory
e c o n s t ru ct io n

8.13.3

432

Buffer

Accumulation

and Embossing

Mapping

8.12.5

8.13.2

Rendering

433

436

Processing
I m a ging Extensions
438
Othe r Multipass Methods
Im

429

OpenGL

43 1

Revisited

a c k - to F ro n t

8.12

422
427

Maps

n ding

8.11.6

417

421


429
Compositing
and Compositing in

Im

a ge

4 11

s i t i n g Techniques
4 28
and Blending

po

8.11.3

CHAPTER

410

OpenGL

Generation

n v ir o n m e n t

E


8.11.2

8.12.4

4 04

Mapping

Tw o - Di m e n s i o n a l Texture Mapping
Tex t u re Sampling 414
W o r k i n g with Texture Coordinates
T e x t u r e Objects
419
M u l t i t e x t u r i n g4
20

8.11 Com
8.11.1 Opacity

8.12.3

401
4 03

g Methods

454
Extensions


455

xv


xvi

Con t e nt s
9.4

9.4.1
9.4.2

Th e
V
F

Shading

OpenGL

e rt e x

r a g m e n t Shaders

9.5

9.5.1

G


OpenGL
Shading
LS L Execution
4 61

D

a ta

9.5.3
9.6

9.7

456

Language

4 60

4 59

Th e

9.5.2

Language

457


Shaders

Types and

461

Qualifiers

Oper a t ors and Functions
Li n k i n g Shaders with
M o v i n g Vertices

4 64
OpenGL

Programs

4 65

470

9.7.1 Sca lin g Vertex Positions 470
9.7.2 M o r p h in g 472
9.7.3

9.8
9.8.1
9.8.2


9.9
9.10
9.11

9.12
9.12.1

P a r t ic le

4 74

Systems

V

e r t e x Lighting

with

P

h o n g Lighting

4 75

N

o n p h ot o r e a lis t ic Shading

F


ra

Pe

g men

r - Ve

S am

Versus Per-Fragment Lighting

9.12.2

Ref

r a ctio

9.12.3

N

9.13

Bu m p
Ex

48 6


87

n4

o r m a liz a tio n

Finding

49 0

Maps

4 92

Mapping
4 92

Maps

Bump

4 95

a m ples

Suggested

499

Notes


and

Summary

499

Readings

500

Exercises

AND
MODELING

CHARTER1 0

10.1

Sy m b o l s

10.2
10.3

ARobot

10.4

Tr e e s and Traversal


H ie

and

r a rc h ic a l

Us e

10.6

A

HIERARCHY

at io n

|

5 05

Models

5 07

Traversal

of Tree Data

n im


5 0

5 04

Instances

Arm

10.4.1 AStack-Based
10.5

0

4 85

Maps

e c tio n Maps

9.13.2

4 8

4 83

Refl

9.13.1


479

t Shaders

rt e x

475

4 78

p le r s

u be

C

Shaders

Structures

509
5 11
5 13

5 17


Contents

10.7


G

10.7 .1

Meth o ds , Attributes,

10.7 .2

ACube Object

5

10.7 .3

Imple m e n ting the

Cube Object

r a p h i c a l Objects

519
and

5 19

Messages

21
5


23

524
Objects and Hierarchy
10.7.5 Geom e tri c Objects
525
10.8
Scen e Graphs
10.9
ASimple Scene Graph API

10.7 .4

The Node

Class

10.9.2 Geom e try

Nodes

10.9.3
10.9.4

Materials

10.9.1

m e ra


Ca

Lights

528
530
532

Class

and

5 33

35

10.9.5 Tr a n sform a tio n s5
10.9 .6

The Robot

10.9.7

Im

535

Figure


ple m e n ti n g the Viewer

10.9.8 Imple m e n ting a Node
10.10 Open SceneGraph
10.11

10.11.1
10.11.2
10.11.3

Networks

10.11.4 Java and
10.12

54 7
5 48

VRML
549

Applets

549

Other Tree Structures

10.12.1

550


Trees

CSG

10.12.2

546

5 46

HTML

and
and

Databases

544

Protocols

and

Hypermedia

537
541

Internet


and the

Graphics

526

528

BSPTrees

10.12.3Quadtrees

551
and

Octrees

554

Summary and Notes

555

Suggested Readings

556

556


Exercises

11.1

A lg

11.2

P h y s ic a l l y - B a s ed

11.3

N

o r i t h m i c Models

e w

ton

ia

Models and

11.3.1
11.3.2

Spri

11.3.3


Attr

11.4

S o l v i n g Particle Systems

Ind

e pe n de n t Particles

Forces
a c tiv e and
ng

Particle

Systems

n Particles
5 64

565
Repulsive

Forces

566

568


xvu


xviii

Con t e n t s
11.5

11.5.1
11.5.2

Con s t r a i n t s

5 70

71

s5

Colli

s io n

Soft

Constraints

5 73


11.6
ASimple Partial
System
11.6.1 D isplaying the Particles
574

11.6.2

Updati

11.6.3

In

11.6 .4

Colli

11.6.5

F

s io n

o rc e

575

76


s5

77

s5

11.6.6 Flocki

77

n g5

11.7

L a n g u a g e - Ba s ed

11.8

R

11.8 .1

R u le rs and Length

11.8.2

F

11.8.3
11.8.4


e c u r s iv e

ra c tal

F

r a c tal

583

11.8.5

The

11.9

P r o c edu r a l Noise

12.1
12.1.1
12.1.2

5 90
594

Notes

5 94


Readings

595

CURVES A N

12

CHAPTER

D SURFACES

R

e p re s

en t a t io n

E

x plicit

Representation

Im

and Surfaces
597
599


12.1.3 Par a m e tri cForm
600
12.1 .4 Par a m e tri c Polynomial Curves
12.1.5 Par a m etr ic Polynomial Surfaces
12.2
Des i g n Criteria
In

12.4.1

B

12.4.2

Th

e Cubic

12.5

H

e r m it e

P

a r a m e t r i c Cubic

Polynomial


6 03
Curves

Interpolating
Form

e tri c

12.6

B ez i e

r Curves

12.6.1

B

Patch

609

Continuity

and Surfaces
6 15

611

6 11


and Parametric

e z ie r Curves

6 04
6 05

Curves and Surfaces

Geo m

597
597

607

Functions

Hermite

.

601
602

t e r p ol a t i o n

le n ding


12.5.1 The
12.5.2

g\"

of Curves

plicit Representations

12.3
12.4

584

Motion

586

Set

Mandelbrot

Suggested
Exercises

Brownian

585

Mountains


and

582

and Fractals
582

Division and

Summary

5 78

Models

Methods

Dimension
nt

Midpoi

575

Particle Positions

ng

itiali z atio n


5 74

6 13

6 14


Contents

12.6 .2

B e zier Surface

12.7

Cu b i c

12.7.1

The

12.7.2

B

-

6 17


Patches

6 18

B-Splines

Cubic

B-Spline

Splin e s

62 1

Basis

and

618

Curve

623

12.7.3

Splin e Surfaces

12.8


G

e ne

12.8.1

R

e c u rs iv e ly

Defined B-Splines

rm

625

12.8.2
12.8.3
12.8.4

Unifo
No

ra l

623

B-Splines

Splines


n u n ifo r m

624

626

B-Splines

6 26

NURBS

12.8.5

Cat m

12.9
12.9.1

R e n d e r i n g Curves and Surfaces
P o lyn o m ial Evaluation Methods
629

12.9.2
12.9.3

Re

R


u ll-R o m Splines

628

e c u rs iv e Subdivision

Other

n de r ing

12.9.4 Subdivi

s io n of

Bezier

of

Polynomials

633

6 34

Surfaces

Bezier

12.10 The Utah Teapot

12.11 Algebraic Surfaces
12.11 .1 Quadrics 638
12.11.2

Surfaces

of

Rendering

29

6 30

by Subdivision

Curves

Polynomial

6

636
6

by Ray Casting

12.11.3Subdivision
Curves
and Surfaces

12.11.4 Mesh Subdivision
64 1
12.12 Curves and Surfaces in OpenGL

38

639

640
644

12.12.1 Bezier Curves
6 44
12.12.2 BezierSurfaces 645
12.12.3

646

the Teapot

Displaying

12.12.4 NURBS

648

Functions

648


12.12.5 Quadrics
and

Summary

Suggested

649

Notes

650

Readings

>

650

Exercises

\342\200\242 r;i
'\"%^fr-'
i';\342\200\242\342\200\242\342\200\242'
;Jfer*
\302\245f:;,\342\200\242

13.1

G o i n g Beyond


13.2
13.3

R

ay

B

u i l d in g

R

e c u rs iv e Ray Tracing

13.3.1
13.3.2

Cal

13.3.3

Ray

Pipeline Rendering

Tracing

a Simple


Ray

c u lati n g Intersections
-Tr a c

ing

Variations

Tracer

658
660
663

653
6 54
6 58

xix


xx

Con t e n t s

13.4

Th e


13.5

R

6 66

13.5.1 The Radiosity
13.5.2 Solv ing the

6 66

Equation

668
Radiosity Equation
Form Factors
669
r rying Out Radiosity
6 72
e n der M a n

13.5.3 Co
13.5.4 Ca
13.6

6 64

Equation


Rendering

a d io s i t y

m puting

R

6 73

13.7 Par a l le l Rendering
13.7.1 Sort - Middle Rendering 67 5
13.7.2 Sor t-L a st Rendering 677
13.7.3 Sor t-Fi rs t Rendering 680
13.8 I m a g e -Based Rendering
13.8.1

ASimple

Example

Summary

a nd

682

6 82

684


Notes

SuggestedReadings
Exercises

6 74

685

686

689

A.1

A.2
A.3
A.4
A.5
A.6
A.7
A.8

A.9
A.10

A.11

S


ie

r p i n s k i Gasket

6 90

Program

R

ec

u rs i v e Generation

of Sierpinski

R

ec

u r s i v e Three-Dimensional

Sierpinski Gasket

h i n g Squares
Pol y g o n Modeling
M

a rc


Program
Bu f f e r i n g program
Sel e c t i o n - M o d e Picking
Program
R o t a t i n g - C u b e Program
R o t a t i n g Cube
Using Vertex Arrays
R o t a t i n g Cube with
a Virtual
Trackball
M o v i n g Viewer
D

o u b le -

A.12 Sp h e r e Program
A.13 M a n d e l b r o t Set Program
A.14 B r e s e n h a m 's Algorithm
A.15

R

o tatin g

Cube

A.16 G L S L Example
A.17 Scen e Graph
A.18


Gasket

for

7 10

712
7 15

7 17

7 21
7 24
7 27
7 3

0

733

with Texture

7 35
7 41

Program

Par t i c le System Program


A.19 Pr o g r a m

692
693
6 96
7 01
7 07

Drawing

Bezier

7 46
Curves

751


Contents

B.1

Sc

B.2

V

rs


755

e c t o r Spaces

756

B.3

A f f i n e Spaces

758

B.4

Eu

B.5

P roj e c t i o n s

759
760

B.6

Gr a m - S c h m

a la

c l id e a n


Suggested

Spaces

id t

Orthogonalization

Readings

761

7 62

7 62

Exercises

C.1

D

e f i n it i o n s

765

C.2

M


a t r ix

766

C.3

R

ow

767

C.4

R

a nk

Operations
and Column Matrices

768

C.5

Ch a n g e of Representation

C.6


T

C.7

Ei g e n v a l u e s and Eigenvectors

Cross

he

769

Product

SuggestedReadings
Exercises 774

771

D.1

Speci f y i n g Simple Geometry

D.2
D.3

A

t t r ib


W

o r k i n g with

D.4

In

t e ra ct i o n

D.5

E n a b li n g Features

D.6

T

D.7

V ie

D.8

De f i n i n g Discrete Primitives

ra

772


7 73

utes

775

776
the

Window

System

777

779

780

n s f o r m a t io n s

781

w in g

782

D.9

D is


D.10

P i c k in g

D.11
D.12

Li gh

t in g

Textu

re

D.13

S t a t e and Buffer Manipulation

D.14

V

p l a y Lists

Mapping

e r t e x Arrays


783
784
785
786

786
788
788

xxi


xxii

C o nte n t s

D.15

B l e n d i n g Functions

D.16 Quer y
D.17

Cu r v e and Surface Functions

D.18

GLUQuad

D.19


GL S L

Function

Subject Index

7 90

s

7 91

Functions

7 91

r ic

References 795
OpenGL

7 89
7 89

Functions

Index
807


8 05


PREFACE

is an

book

introduction

Thiscations programming.

In

the

to computer graphics,
first editi on, which

with
was

an emphasis

published
thefieldh
ad

on appli-


in 1997,1 noted

years since my previousgraphicstext,
experiencedenorof growth that exceeded most people'sexpectations,
including
since, we have seen even more changes.
my ow n. In the 11 years (and four editions)
Feature-length computer-animated movieshave p roved to be commercialand artistic successes. The use of com puter effects in movies is standard,
and it is often almost
live action from computer-generated effects. Recent hardimpossibleto distinguish
ware
has blurred
the distinction between
and game b oxes.Programmable
computers
in commodity
graphics
processors
provide a levelof flexibility
systems that was not
in even the most expensiveworkstations
available
a
few
just
years ago.
Not
costs
have been reduced for

only have graphics capabilities increased,but
both high- and low-end workstations.
Within
the last few years, th e cost of a graphics system th at can generate over 100million
three-dimensional
polygons per second
with
and texture mapping has gone from
over $100,000
to less tha n $1000.
lighting
The availability
ofc o mmo ditygraphicsboardsfor
personal computershasbeenespeThese
boards
for
three-dimensional
cially
significant.
provide support
sophisticated
at about $100. On the software
side, OpenGL remains the stanapplications, starting
in theseven

that

growth\342\200\224arate

mous


dard

programmer's interface both for writing application programs and developing
products for multiplatform
applications.
OpenGL
supports applications
from
large scientific visualizations to cellphon e games.

high-level
ranging

A Top-Down

Theserecent

Approach

and the success of the first four editions continue to reinforce
top-down, programming-oriented approach to introductory
computer
science and engineering departments now
graphics. Although many
computer
support more than one course in the subject, most students will take only a single course.
Such
a course
is placed in the curriculum

after students have already
studied
prosoftware engineering, and basic mathematics.
gramming, data structures,
algorithms,
A class in computer graphics
allows
the instructor to build
on these topics in a way
that can be both informative
and fun. I want these students
to be programming
threedimensional
as soon as possible.Low-levelalgorithms,
such as those that
applications
draw
lines orfillp oly g o n s , can bedealtwithlater,
after students
are creatinggraphics.
in
John
a
used
a
familiar
automobile
education,
Kemeny,
pioneer

computer
what's
under the hood to be literate,
but unless
analogy: You don't have to know
my

belief

advances

in a

XXUl


xxiv

Pr e f a c e

you

how to

know

same

algorithmic


program,

applies

analogy

to the

approach\342\200\224is

of driving.
That
sitting in the back seat instead
teach computer graphics. One approach\342\200\224the
everything about what makes a car function: the

you'll

be

way

we

to teach

the transmission,
the
approach\342\200\224is to hire a chauffeur,


A second approach\342\200\224the survey
process.
and see the world as a spectator. The third
that I have adopted here\342\200\224isto teach you
approach\342\200\224the
programming
approach
howto drive and howto take yourselfwherever you want to go. As the old auto-rental
commercial used to say, \"Let us put you in the driver's seat.\"
engine,

Programming

combustion

sit back,

with OpenGL and
teaching computer graphics

C

25 years ago, t he greatest impediment to
a textbook
for that
course, and to writing
course, was the lack of a widely
accepted
graphics
library or application

programmer's interface (API). Difficulties
included
lack of g e n high cost, limi t ed availability,
and
The
of
resolved
most
of the diferality,
high complexity.
development
OpenGL
ficulties
of
us
had
with
other
APIs
as
GKS
and
and
(such
PHIGS)
many
experienced
with the alternative
of using home-brewed
software. OpenGLtoday is supported

on
all platforms.
It is bundled with
Microsoft
Windows
and the Apple
Macintosh
Opfor virtually
all graphics cards. Thereis also an
erating System. Drivers are available
distributions.
Versions of
OpenGL API called Mesathat is included with most Linux
now
exist
for
embedded
and
are
to
its
use
for
hand-held
OpenGL
systems
leading
I began

When


a programming-oriented

implementing

devices.

class teachesfarmore than theuse
of a particular API, but a good API
three-dimensional
graphics topics, including
graphics,
client-server
lighting and shading,
graphics,
modeling, and implementation
algoI believe that OpenGL's
rithms.
extensive
architecture
capabilities and well-defined
lead to a stronger
foundation
for teaching both theoretical
and practical
aspects of
thefielda n d for teaching advanced concepts,including
texture
mapping,
compositshaders.

ing, and programmable
I switched my classes t o OpenGL about
15 years ago, and the results
astounded
me. By the middleofthesemester, every student
was ableto
write a moderately comthat required understanding of three-dimensional
plex three-dimensional
program
and event-driven
viewing
input. In my previous
years of teaching computer graphics, I had never comeeven closeto this result. That class ledme to rewrite
my previous
book from scratch.
This book is a textbook
on computer
graphics; it is not an OpenGL manual.
I
API but rather explain
do
not
cover
all
of
the
Consequendy,
aspects
OpenGL
only

what is necessary for mastering
this book's contents. I present OpenGLat a level
that should permit users of other APIs t o have litd e difficulty with the material.For
students
who want more detailon OpenGL,my recentbookOpenGL:A
Third
Primer,
should be a valuable
Edition, (Addison-Wesley
2008)
supplement.
In this edition, I useboth
C and C++, with
C as the dominant language.
There
are two reasons for this decision. First, OpenGL is not object-oriented,
so using C++
or Java would not add significandy to the basic presentation,
unless I were to insert
Agraphics

makes

it

easier

to teach key



Preface

geometriclibrary between OpenGL and the user.I have n ot taken
this step, despite its appealing
because it would detract
from
the graphics
features,
and would
make
the book less accessibleto students
who
are good programmers
but
unfamiliar
with object-oriented
has been that
languages. Second,my experience
object-oriented approaches shield the user from what is going on inside (as they
I want readers to be aware
to computer graphics,
should), whereas in an introduction
of what is happening at the lowest levels. Although the use of computer graphics is a
in my view, an
wonderful
introduce
studentsto object-oriented programming,
wayto
object-oriented approach is not the most effective way to teach graphics to computer
I introduce

scienceand engineering
students.
The exception to thisviewiswhen
scene
from
the use of C++. My undergraduate
graphs that are object oriented and benefit
students
use Java and C++ in their beginning coursesbut have no difficulty in using
the
code in this book with
either
C or C++.
an object-oriented

Intended
This
in

book
computer

Audience
is suitable

for advanced

science

and engineering


andfirst-yearg

undergraduates
and

for

students

in other

r a du a t e
disciplines

students

who

have

I have
good programming skills. Th e bookalso will be useful to many professionals.
courses
for professionals; my experiences
with these nontrataught over 100short
in the
ditional
students
have had a great

influence
on what I have chosento include
book.
skills in C or C++, an underPrerequisites for the book are good programming
(linked lists, trees), and a rudimentary
standing ofbas ic data structures
knowledge of
I have found that the mathematical backgrounds of
linear
and trigonometry.
algebra
science
whether undergraduates or graduates,
students,
computer
vary considerably.
I
have
chosento
into the text much of the linear
Hence,
integrate
algebra and geomeis re qui red for fundamental
this
try that
computer
graphics. I have also summarized
material in Appendices B and C.

Organization


of the

Book

Thebookis organized
as follows. Chapter 1 provides an overview
of thefielda n d introduces image formation
devices; thus, we start with three-dimensional
by optical
concepts immediately. Chapter 2 introduces
programming
using OpenGL. Although
thefirste xam p l e program that we develop\342\200\224each chapter has one or more complete
it is embedded in a three -dime nsional
programming
examples\342\200\224is two-dimensional,
and leads to a three-dimensional extension. In Chapter
3, we discuss intersetting
active
and develop
event-driven graphics
graphics in a modern client-server
setting
4
on three-dimensional concepts; Chapter
programs. Chapters 4 and 5 concentrate
is concerned with
and
three-dimensional

whereas
defining
manipulating
objects,
in5 is concerned with
them. Chapter 6 introduces
Chapter
viewing
light-material
teractions and shading.
These
should be coveredin order and can be taught
chapters
in about
10 weeks of a 15-week
semester.
The next seven chapters can be read in almost
any order. All seven are somewhat
ended
and
can
be
covered
at
a
or individual
can be
level,
open
survey

topics

x xv


xxvi

Pr e f a c e

in depth.
rasterization.
It gives one or two major
Chapter 7 surveys
algorithms for each oft h e basic steps, including
line generation, and polygon
fill.
clipping,
8
introduces
of
the
new
discrete
that
are
now
Chapter
many
capabilities
supported in

hardware
and by OpenGL. All these techniques
involve working with vargraphics
in computer
ious buffers. It concludes with
a short discussion of aliasing
problems
pursued

to programmable
shaders using the OpenGL
graphics. Chapter 9 is an introduction
w
hich
is
now
of
Shading Language (GLSL),
part
OpenGL.We use programmable
shaders to developtechniques,
such as bump mapping, that
can now be done in real
time. Chapters 8 and 9 conclude the discussion of the standard
viewing pipeline used
by all interactive
graphics systems.
a number
of topics that fit loosely under the heading
of

Chapter 10 contains
hierarchical
from
models that encapsulate the
modeling. The topics range
building
between the parts of a model,
to high-level
over
relationships
approaches to graphics
the Internet. It includes a simple
scene graph API and an introduction
to Open Scene
12 int roduce s a number
of procedural
methods, including particle
Graph. Chapter
and procedural
noise. Curves and surfaces,
subdivision
systems, fractals,
including
12. Chapter 13 surveys
alternate
to
surfaces, are discussedin Chapter
approaches
It
includes

discussions
of
and
and
an
rendering.
expanded
ray
tracing
radiosity,
introduction
to image-based rendering and parallel
rendering.
in AppendixA.
a rt of thebook, areincluded
Programs, primarilyfromt h efirstp
online
(see Support Materials). Appendices B and C contain
They are also available
a review of the background
mathematics.
a synopsis
of the
Appendix D contains
in
functions
used
the
book.
OpenGL


Changes

from the

Edition

Fourth

reaction of readers of the first four editions of this book was overwhelmingly
But there are
positive, especially to the use of OpenGL and the top-down
approach.
to
be
made
and
recent
advances
to
be
covered.
The advent
always
improvements
of programmable
one that I believe
advance,
graphics processors has been a major
should

have
a major impact on how
we teach computer
graphics. In the fourth
In
shaders and the OpenGLShading
edition, I introduced
programmable
Language.
addition to forcing
a rearrangement
of material from earlier editions, techniques
that
in a
were not possible to implement in most
systems, and thus were covered only
and are covered at a level appropriate for a first
perfunctory manner, are now routine
in computer graphics. An additional
course
benefit of the rearrangement
of topics
in previous
is that the chapters
now follow the OpenGL pipeline more closely
than
The

editions.
in a rewritten

9 using the OpenGL
Chapter
includes
more
than
the
edition.
Shading
Language.
examples
previous
can now be implemented in real
Using programmable shaders, many techniques
time. Hence, some of the material
from the first three editions
has been moved
in the third
around. Bump mapping,
which
was covered with
texture
edimapping
h
is
now
covered
a
d
er
s

.
some
instructors
find
this
tion,
withfragments
Although
may

Programmable

shaders

are

This chapter

covered


Preface

a bit odd at first, I think they will see the advantages when they st a rt teaching
change
vertex and fragment
shaders material.
For th e fourth edition, I eliminated
the separate
visualizachapters on scientific

tion and procedural methods. Someof the material
on scientific visualization, such
as the marching-squares
method, is spread over the other chapters. However, the
methods
led me to reintroduce
an expanded
importance of procedural
chapter on
t h at includes particle systems, fractals,
and procedural
noise.
procedural graphics
Readers of previous
editions
will notice that some of the example
programs
I replaced the paint
in Chapter 3 with
have been changed. In particular,
program
I believe illustrates interaction and
a polygon
manipulation program, which
design
more clearly. Chapter 12includes the code for a basic particle system. Chapter 9 has
better
of p ro grammable shaders
than the fourth edition.
examples

Given the positive feedback I've received on the core material in Chapters 1-6,
I'vetried to keep the changes to those chapters minor, seeking primarily to improve
I believe that these changes should
the
have
pedagogy.
only minor effects on the
in the following
of a one-semester introductory
course
but the new material
teaching
make
it easier for instructors
to use the book for a two-semester
chapters should

course.

Materials

Support

Thesupport

.unm.edu/~angel
is available

and


rial that

\342\200\242

Web,

readers of this

on how

\342\200\242
Instructions

the

Addison-Wesley's

to all

of information

\342\200\242
Sources

both

through the

site www.aw
book


author's

Web

.com/cssupport.

site www.es

Support

mate-

includes

on OpenGL

to get started

with

OpenGL

on the

most popular

systems

code


Program

to selected

\342\200\242
Solutions
\342\200\242
PowerPoint

Additional

is on

the book

for

exercises

lectures

solutions
to all the nonprogramming
support materials, including
to instructors
this
textbook for classroomuse.
adopting
for information

on obyour school's Addison-Wesley representative
to this material.

are available only

exercises,
Please

contact

taining

access

Acknowledgments

I have

over the past few years to have worked with wonderful
students
in OpenGL,
thefirstt o get me interested
and I have learned much
from
them.
Ye Cong, Pat Crossno,
(Walker),
They include Hue Bumgarner-Kirby
Tommie Daniel,Chris Davis, Lisa Desjarlais, Kim Edlund,
Lee Ann Fisk, Maria

Jordan, Max Hazelrigg,
Gallegos,Brian Jones, Christopher
Sheryl
Hurley, Thomas
been

at UNM.

fortunate

They

we re

Keller, Ge Li,Pat
Munich, Jim

McCormick,

Pinkerton,

Jim

Al McPherson,
Prewett,

Ken Moreland,

Martin


Muller,

Dave Rogers, Hal Smyer, Takeshi

David

Hakamata,

xx v

ii


xxviii

Pr e f a c e
DaveVick,
createdby

Brian
these

Wylie, and Jin
students.

Xiong. Many

of t h e

examplesin


the

color plates

were

The first edition of this book was written during my sabbatical;
various
parts
were written infived ifferen t countries. The experience speakswell for the versatility
of portable computers and the universality
of the Internet.
the task
Nevertheless,
would not have been accomplished without the help of a number of peopleand
institutions
that made their facilities
available
to me. I am greatly
indebted
to Jonas
Montilva and Chris Birkbeck oft h e Universidad
de los Andes (Venezuela), to Rodrigo
and Aristides Novoa oft h e Universidad
Gallegos
Tecnologica Equinoccial (Ecuador),
to LongWen Chang of th e National
Hua University (Taiwan), and to Kin Hong
Tsing

ofthe Chinese University
of Hong Kong. Ramiro Jordan
Wong and Pheng AnnHeng
of ISTEC
and the University
of New Mexico made possible many
of these visits.
John Brayer and Jason Stewart at t he University
of New Mexico an d Helen Goldstein
I
at Addison-Wesley somehow managed to get a variety
of items to me wherever
to
be.
Web
site
contains
a
of
adventures
the
happened
My
description
my
writing
edition.

first


This edition

of hundreds

was written

of instructors

me their

comments.Many

requests

and comments.

David

in New Mexico and benefited
greatly from the feedback
and students who used the first four editions
and
sent
of th e changes from
earlier
editions are responses to their

to provide cards for
Kilgard at NVIDIA were kind enough
A

number
of
other
testing
many
algorithms.
people provided significant
help.
I thank Ben Bederson,GonzaloCartagenova,
Tom
Caudell, Kathi Collins, Kathleen
Kirk

and

Mark

of the

Robert Geist, ChuckHansen,
Mark
Bernard Moret,
Henne,
Dave Shieiner, Vicki
Gwen Sylvan, and
Shreiner,
Mason Woo. Mark Kilgard, Brian Paul, and Nate Robins are owed a great debt by the
OpenGL community for creating software that enables OpenGLcodet o be developed
over a variety
of platforms.

At the University
of New Mexico, the Art,
and Science
Research,
Technology,
(ARTS
Lab) and the Center for High Performance
Laboratory
Computing have p r ovided
for many of my projects.
The Computer
Science Department, the
support
Arts Technology
Center in the Collegeof Fine Arts, the National Science FoundaSandia
National
National
have suption,
Laboratories, and Los Alamos
Laboratory
ported many of my students and research projectsthat led to parts of this book.
David Beining and the Lodestar
Planetarium
have provided tremendous support
for the Full Dome Project\342\200\224Sheryl Hurley,
Hue
Jordan, Laurel Ladwig,
Christopher
in
Walker\342\200\224which

some
of
the
the
color
(Bumgarner-Kirby)
provided
images
plat es.
Hue Walker has done the wonderful coversfor the last two editions.
Reviewers
of my manuscript
drafts provided a variety
of viewpoints
on what
I should
I should
include
and what level of presentation
use. These reviewers include Gur Saran Adhar (University of Nor th Carolina at Wilmington), Mario Agrular (Jacksonville
State University), Michael Anderson
of Hartford), C. S.
(University
Bauer
of Central Florida), Marty
Barrett
(East Tennessee State Univer(University
Eric Brown, Robert P. Burton
sity),
(Brigham

Young University), Sam Buss (Uniof California,
San Diego), Kai H. Chang (Auburn University), Ron DiNapoli
versity
Danielson,

Dick

Nordhaus,

Roger Ehrich,
Helena

Saona,


Preface

(Cornell
University), Eric
S. Ebert (Purdue University),
Kilgard
sity),

(NVIDIA
Chung

Technological

lege),Bruce


Alan

Durant
Chenyi

School of Engineering),
David
(Milwaukee
Hu (University of Central Arkansas), Mark

Cor po ratio n) , LisaB. Lancor

(Southern

Connecticut

State Univer-

Lee (CA Polytechnic University,
John L. Lowther (Michigan
Pomona),
Univ ersity), R. Marshall
and Bridgewater State Col(Boston
University
A. Maxwell
James
R.
Miller
of K an s a s ),
(Swathmore College),

(University

Rodrigo

Obando

versity),

Oliver

State University), Han-WeiShen (The Ohio State Uni(Columbus
Staadt (University of California,
Davis),
Stephen L. Stepoway (SouthBill Toll (Taylor University),
Michael
Wainer
(Southern
University),

ern Methodist
Illinois University,
Carbondale),
George Wolberg (City Collegeof New York), Yang
r
n
Methodist
State
Wang (Southe
University), Steve War ren (Kansas State University),
Mike Way (Florida

Southern
College), and Ying Zhu (Georgia State University). In
the following instructors provided valuable
feedback
from the fourth ediaddition,
tion:
Kabekode
V. Bhat (The Pennsylvania
State University),
Richard R. Eckert
(Binghamton
State University, Dominguez
University), Jianchao (Jack) Han (California
Hills), Hugh C. Masterman
(University of Massachusetts, Lowell),Andrea
Salgian
Lori L. Scarlatos (Brooklyn
(The College of New Jersey),
CUNY), Han-Wei
College,
Shen (The Ohio State University),
San
Xiaoyu Zhang (California State
University
State University).
Marcos), and Ye Zhao (Kent
Although the final decision s may not
reflect
their
views\342\200\224which often

differed considerably from one another\342\200\224each reviewer forced me to reflect
on every page of th e manuscript.
I acknowledge the entire production
team at Addison-Wesley. My edit ors , Peter
and Matt Goldstein have been a pleasure
to work with
Gordon, Maite Sujarez-Rivas,
Katherine
Harutunian has
throughfivee dit i o n s ofthisbook and theOpenGLprimer.
the production
helped me through
process on all my books with Addison-Wesley.
five editio n s , Paul
at Windfall Software has always
been
Through
Anagnostopoulos
in assisting
I am especially grateful
more th an helpful
me with TgK problems.
to Lyn
draft of the first
Dupre. I am not a natural writer. If the readerscould seethe original
that Lyn does with
a manuscript.
edition,
they would understand the wonders
did thefiguresf or myfirstg r aph i c s book, many

of
My wife, Rose Mary Molnar,
which form the basis for the figures in this book. Probably
only other authors can
the effort that goes into the book production process and the many
fully
appreciate
contributions
and sacrifices our partners
make to that effort. The dedication
to the
book is a sincerebut inadequate
of all of Rose Mary's
contributions
to
recognition
my work.

x x ix


×