Interactive Computer Graphics
A Top-Down Approach Using OpenGL®
FIFTH EDITION
EDWARD ANGEL
UNIVERSITY OF NEW MEXICOPEARSON
Addison
Wesley
Boston San Francisco New YorkLondon Toronto Sydney Tokyo Singapore Madrid Mexico City Munich Paris Cape Töwn Hong Kong Montreal
Preface xxv
CHAPTER 1 GRAPHICS SYSTEMS A N D MODELS
1.1 Applications of Computer Graphics
1.1.1 Display of Information 2 1.1.2 Design 3
1.1.3 Simulation and Animation 3 1.1.4 User Interfaces 5
1.2 A Graphics System
1.2.1 Pixels and the Frame Buffer 5 1.2.2 Output Devices 7
1.2.3 Input Devices 10
1.3 Images: Physical and Synthetic
1.3.1 Objects and Viewers 11 1.3.2 Light and Images 12 1.3.3 Image Formation Models 14
1.4 Imaging Systems
1.4.1 The Pinhole Camera 16 1.4.2 The Human Visual System 18
1.5 The Synthetic-Camera Model 1.6 The Programmer's Interface
1.6.1 The Pen-Plotter Model 23 1.6.2 Three-Dimensional APIs 24 1.6.3 A Sequence of Images 27
1.6.4 The Modeling-Rendering Paradigm 28
1.7 Graphics Architectures
1.7.1 Display Processors 30 1.7.2 Pipeline Architectures 30 1.7.3 The Graphics Pipeline 31 1.7.4 Vertex Processing 32
viii C o n t e n t s
1.7.5 Clipping and Primitive Assembly 32 1.7.6 Rasterization 33
1.7.7 Fragment Processing 33
1.8 P r o g r a m m a b l e Pipelines 3 3 1.9 P e r f o r m a n c e Characteristics 3 4
Summary and Notes 35 Suggested Readings 36 Exercises 36 C H A P T E R 2 G R A P H I C S P R O G R A M M I N G 3 9 2.1 T h e Sierpinski G a s k e t 3 9 2 . 2 P r o g r a m m i n g T w o - D i m e n s i o n a l A p p l i c a t i o n s 4 0 2.2.1 Coordinate Systems 45 2 . 3 T h e O p e n G L A P I 4 6 2.3.1 Graphics Functions 47
2.3.2 The Graphics Pipeline and State Machines 48 2.3.3 The OpenGL Interface 49
2 . 4 Primitives a n d A t t r i b u t e s 5 0
2.4.1 Polygon Basics 52
2.4.2 Polygon Types in OpenGL 53 2.4.3 Approximating a Sphere 55 2.4.4 Text 56 2.4.5 Curved Objects 58 2.4.6 Attributes 58 2 . 5 C o l o r 6 0 2.5.1 RGB Color 62 2.5.2 Indexed Color 63
2.5.3 Setting of Color Attributes 65
2 . 6 V i e w i n g 6 6
2.6.1 The Orthographie View 66 2.6.2 Two-Dimensional Viewing 69 2.6.3 Matrix Modes 70
2 . 7 C o n t r o l Functions 7 0
2.7.1 Interaction with the W i n d o w System 71 2.7.2 Aspect Ratio and Viewports 72
2.7.3 The main, d i s p l a y , and m y i n i t Functions 74 2.7.4 Program Structure 75
2 . 8 T h e G a s k e t P r o g r a m 7 6 2 . 9 Polygons a n d Recursion 7 7
C o n t e n t s
2 . 1 0 T h e T h r e e - D i m e n s i o n a l G a s k e t 8 0
2.10.1 Use of Three-Dimensional Points 80 2.10.2 Use of Polygons in Three Dimensions 82 2.10.3 Hidden-Surface Removal 83
2 . 1 1 P l o t t i n g Implicit Functions 8 5
2.11.1 Marching Squares 86 Summary and Notes 92 Suggested Readings 94 Exercises 94
CHAPTF.R3 I N P U T A N D I N T E R A C T I O N 9 9
3.1 I n t e r a c t i o n 9 9 3 . 2 I n p u t Devices 1 0 0
3.2.1 Physical Input Devices 101 3.2.2 Logical Devices 104 3.2.3 Input Modes 105
3 . 3 Clients a n d Servers 1 0 7 3 . 4 Display Lists 1 0 9
3.4.1 Definition and Execution of Display Lists 110 3.4.2 Text and Display Lists 112
3.4.3 Fonts in GLUT 115
3 . 5 Display Lists a n d M o d e l i n g 1 1 6 3 . 6 P r o g r a m m i n g E v e n t - D r i v e n I n p u t 1 1 7
3.6.1 Using the Pointing Device 117 3.6.2 W i n d o w Events 121
3.6.3 Keyboard Events 122
3.6.4 The Display and Idle Callbacks 123 3.6.5 W i n d o w Management 124
3 . 7 M e n ü s 1 2 4 3 . 8 Picking 1 2 6
3.8.1 Picking and Selection M o d e 127
3 . 9 A S i m p l e C A D P r o g r a m 1 3 3 3 . 1 0 Building I n t e r a c t i v e M o d e l s 1 4 0 3 . 1 1 A n i m a t i n g I n t e r a c t i v e P r o g r a m s 1 4 2
3.11.1 The Rotating Square 143 3.11.2 Double Buffering 144 3.11.3 Using a Timer 146
3 . 1 2 D e s i g n o f I n t e r a c t i v e P r o g r a m s 1 4 7
Contents 3.13 3.13.1 3.13.2 3.13.3 Logic Operations
Drawing Erasable Lines 150 XORandColor 153
Cursors and Overlay Planes 153 Summary and Notes 154 Suggested Readings 155 Exercises 155
CHAPTER 4. GEOMETRIC OBJECTS A N D TRANSFORMATIONS
4.1 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.1.10 4.2 4.3 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.4 4.5 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.6 4.7 4.7.1 4.7.2
Sealars, Points, and Vectors
Geometrie Objects 160 Coordinate-Free Geometry 161
The Mathematical View: Vector and Affine Spaces 162 The Computer Science View 163
Geometrie ADTs 163 Lines 165
Affine Sums 165 Convexity 166
Dot and Cross Products 166 Planes 167
Three-Dimensional Primitives Coordinate Systems and Frames
Representations and N-Tuples 172 Change of Coordinate Systems 172 Example Change of Representation 175 Homogeneous Coordinates 176 Example Change in Frames 179 Working with Representations 181
Frames in OpenGL
Modeling a Colored Cube
Modeling the Faces 188
Inward- and Outward-Pointing Faces 189 Data Structures for Object Representation 189 The Color Cube 190
Bilinear Interpolation 191 Vertex Arrays 192
Affine Transformations
Translation, Rotation, and Scaling
Translation 198 Rotation 198 149 159 160 168 170 183 187 195 197
4.7.3 Scaling 200 4 . 8 T r a n s f o r m a t i o n s in H o m o g e n e o u s C o o r d i n a t e s 2 0 2 4.8.1 Translation 203 4.8.2 Scaling 204 4.8.3 Rotation 205 4.8.4 Shear 206 4 . 9 C o n c a t e n a t i o n of T r a n s f o r m a t i o n s 2 0 7
4.9.1 Rotation A b o u t a Fixed Point 208 4.9.2 General Rotation 209
4.9.3 The Instance Transformation 211 4.9.4 Rotation A b o u t an Arbitrary Axis 212
4 . 1 0 O p e n G L T r a n s f o r m a t i o n M a t r i c e s 2 1 5
4.10.1 The Current Transformation Matrix 215 4.10.2 Rotation, Translation, and Scaling 216 4.10.3 Rotation A b o u t a Fixed Point in OpenGL 217 4.10.4 Order of Transformations 217
4.10.5 Spinning of the Cube 218
4.10.6 Loading, Pushing, and Popping Matrices 219
4 . 1 1 I n t e r f a c e s t o T h r e e - D i m e n s i o n a l A p p l i c a t i o n s 2 2 0
4.11.1 Using Areas of the Screen 221 4.11.2 A Virtual Trackball 221 4.11.3 Smooth Rotations 224 4.11.4 Incremental Rotation 225
4 . 1 2 Q u a t e r n i o n s 2 2 6
4.12.1 Complex Numbers and Quaternions 226 4.12.2 Quaternions and Rotation 228
Summary and Notes 230 Suggested Readings 231 Exercises 231 5 V I E W I N G 2 3 5 5.1 Classical a n d C o m p u t e r V i e w i n g 2 3 5 5.1.1 Classical Viewing 237 5.1.2 Orthographie Projections 237 5.1.3 Axonometrie Projections 238 5.1.4 Oblique Projections 240 5.1.5 Perspective Viewing 241 5 . 2 V i e w i n g w i t h a C o m p u t e r 2 4 2
x i i Contents
5.3 Positioning of the Camera 244
5.3.1 Positioning of the Camera Frame 244 5.3.2 Two Viewing APIs 249
5.3.3 The Look-At Function 252 5.3.4 Other Viewing APIs 253
5.4 Simple Projections 254
5.4.1 Perspective Projections 254 5.4.2 Orthogonal Projections 257
5.5 Projections in OpenGL 258
5.5.1 Perspective in OpenGL 259 5.5.2 Parallel Viewing in OpenGL 261
5.6 Hidden-Surface Removal 262
5.6.1 Culling 264
5.7 Interactive Mesh Displays 264
5.7.1 Meshes 264
5.7.2 Walking Through a Scene 266 5.7.3 Polygon Offset 268 5.8 Parallel-Projection Matrices 269 5.8.1 Projection Normalization 270 5.8.2 Orthogonal-Projection Matrices 271 5.8.3 Oblique Projections 273 5.9 Perspective-Projection Matrices 276 5.9.1 Perspective Normalization 276
5.9.2 OpenGL Perspective Transformations 280
5.10 Projections and Shadows 281
Summary and Notes 284 Suggested Readings 285 Exercises 285
CHAPTER6 LIGHTING A N D SHADING 289
6.1 Light and Matter 290 6.2 Light Sources 294
6.2.1 Color Sources 294 6.2.2 Ambient Light 295 6.2.3 Point Sources 296 6.2.4 Spotlights 297
6.2.5 Distant Light Sources 297
6.3 The Phong Lighting Model 298
Contents xiii
6.3.2 Diffuse Reflection 300 6.3.3 Specular Reflection 301 6.3.4 The Modified Phong Model 303
6.4 Computation of Vectors 304
6.4.1 Normal Vectors 304 6.4.2 Angle of Reflection 308
6.5 Polygonal Shading 309
6.5.1 Fiat Shading 310
6.5.2 Smooth and Gouraud Shading 311 6.5.3 Phong Shading 313
6.6 Approximation of a Sphere by Recursive Subdivision 314
6.7 Light Sources in OpenGL 317 6.8 Specification of Materials in OpenGL 320
6.9 Shading of the Sphere Model 322
6.10 Global Illumination 323 Summary and Notes 325
Suggested Readings 326 Exercises 326
CHAPTER 7 FROM VERTICES TO FRAGMENTS 329 7.1 Basic Implementation Strategies 330
7.2 Four Major Tasks 332
7.2.1 Modeling 332 7.2.2 Geometry Processing 333 7.2.3 Rasterization 334 7.2.4 Fragment Processing 335 7.3 Clipping 336 7.4 Line-Segment Clipping 336 7.4.1 Cohen-Sutherland Clipping 337 7.4.2 Liang-Barsky Clipping 339 7.5 Polygon Clipping 341 7.6 Clipping of Other Primitives 343
7.6.1 Bounding Boxes and Volumes 344 7.6.2 Curves, Surfaces, and Text 345 7.6.3 Clipping in the Frame Buffer 345
7.7 Clipping in Three Dimensions 346
7.8 Rasterization 349 7.9 Bresenham's Algorithm 352
x i v C o n t e n t s
7 . 1 0 P o l y g o n Rasterization 3 5 4
7.10.1 Inside-Outside Testing 354
7.10.2 OpenGL and Concave Polygons 355 7.10.3 Fill and Sort 356
7.10.4 Flood Fill 357 7.10.5 Singularities 357
7 . 1 1 H i d d e n - S u r f a c e R e m o v a l 3 5 8
7.11.1 Object-Space and Image-Space Approaches 358 7.11.2 Sorting and Hidden-Surface Removal 360 7.11.3 ScanLine Algorithms 360
7.11.4 Back-Face Removal 361 7.11.5 The z-Buffer Algorithm 362
7.11.6 Scan Conversion with the z-Buffer 365 7.11.7 Depth Sort and the Painter's Algorithm 367
7 . 1 2 Antialiasing 3 6 9 7 . 1 3 Display C o n s i d e r a t i o n s 3 7 1
7.13.1 Color Systems 372 7.13.2 The Color Matrix 375 7.13.3 Gamma Correction 376 7.13.4 Dithering and Halftoning 376
Summary and Notes 377 Suggested Readings 379 Exercises 379 8 DISCRETE T E C H N I Q U E S 3 3 3 8.1 Buffers 3 8 3 8.2 Digital I m a g e s 3 8 5 8.3 W r i t i n g into Buffers 3 8 8 8.3.1 Writing Modes 389 8.3.2 Writes with XOR 391
8.4 Bit a n d Pixel O p e r a t i o n s in O p e n G L 3 9 2
8.4.1 OpenGL Buffers and the Pixel Pipeline 392 8.4.2 Bitmaps 394
8.4.3 Raster Fonts 395 8.4.4 Pixels and Images 396 8.4.5 Lookup Tables 397
8.5 E x a m p l e s 3 9 9
8.5.1 Displaying a Color Gamut 400 8.5.2 Testing Algorithms 400 8.5.3 Buffers for Picking 401
8.6 Mapping Methods 401 8.7 Texture Mapping 403
8.7.1 Two-Dimensional Texture Mapping 404
8.8 Texture Mapping in OpenGL 410
8.8.1 Two-Dimensional Texture Mapping 411 8.8.2 Texture Sampling 414
8.8.3 Working with Texture Coordinates 417 8.8.4 Texture Objects 419
8.8.5 Multitexturing 420
8.9 Texture Generation 421 8.10 Environment Maps 422 8.11 Compositing Techniques 427
8.11.1 Opacity and Blending 428 8.11.2 Image Compositing 429
8.11.3 Blending and Compositing in OpenGL 429 8.11.4 Antialiasing Revisited 431
8.11.5 Back-to-Front and Front-to-Back Rendering 432 8.11.6 Depth Cueing and Fog 433
8.12 Multirendering and the Accumulation Buffer 434
8.12.1 Scene Antialiasing 435
8.12.2 Bump Mapping and Embossing 436 8.12.3 Image Processing 436
8.12.4 Imaging Extensions 438 8.12.5 Other Multipass Methods 438
8.13 Sampling and Aliasing 439
8.13.1 Sampling Theory 440 8.13.2 Reconstruction 444 8.13.3 Quantization 446
Summary and Notes 447 Suggested Readings 448 Exercises 448
cnArr-:f<9 PROGRAMMABLE SHADERS 451
9.1 Programmable Pipelines 451 9.2 Shading Languages 453
9.2.1 ShadeTrees 453
9.3 Extending OpenGL 454
9.3.1 OpenGL Versions and Extensions 455 9.3.2 GLSL and Cg 456
XVI Contents 9.4 9.4.1 9.4.2 9.5 9.5.1 9.5.2 9.5.3 9.6 9.7 9.7.1 9.7.2 9.7.3 9.8 9.8.1 9.8.2 9.9 9.10 9.11 9.12 9.12.1 9.12.2 9.12.3 9.13 9.13.1 9.13.2
The OpenGL Shading Language
Vertex Shaders 457 Fragment Shaders 459
The OpenGL Shading Language
GLSL Execution 461
Data Types and Qualifiers 461 Operators and Functions 464
Linking Shaders with OpenGL Programs Moving Vertices
Scaling Vertex Positions 470 Morphing 472
Particle Systems 474
Vertex Lighting with Shaders
Phong Lighting 475
Nonphotorealistic Shading 478
Fragment Shaders
Per-Vertex Versus Per-Fragment Lighting Samplers Cube Maps Reflection Maps 486 Refraction 487 Normalization Maps 490 Bump Mapping
Finding Bump Maps 492 Examples 495
456
460
465
470
475
479
480
483
485
492
Summary and Notes 499 Suggested Readings 499 Exercises 500
.- 10 MODELING AMD rü=RARCHY 503
10.1 Symbols and Instances 504 10.2 Hierarchical Models 505 10.3 A Robot Arm 507 10.4 Trees and Traversal 509
10.4.1 A Stack-Based Traversal 511
10.5 Use of Tree Data Structures 513
Contents xvii
10.7 Graphical Objects 519
10.7.1 M e t h o d s , Attributes, and Messages 5 1 9 10.7.2 ACubeObject 521
10.7.3 I m p l e m e n t i n g t h e C u b e O b j e c t 523 10.7.4 Objects and Hierarchy 5 2 4
10.7.5 Geometrie Objects 5 2 5
10.8 Scene Graphs 526
10.9 A Simple Scene Graph API 528
10.9.1 TheNodeClass 528 10.9.2 G e o m e t r y N o d e s 5 3 0 10.9.3 Camera Class 532 10.9.4 Lights a n d Materials 533 10.9.5 Transformations 535 10.9.6 The Robot Figure 535 10.9.7 I m p l e m e n t i n g the Viewer 537 10.9.8 I m p l e m e n t i n g a N o d e 541
10.10 Open Scene Graph 544 10.11 Graphics and the Internet 546
10.11.1 Networks a n d Protocols 5 4 6 10.11.2 Hypermedia a n d H T M L 547 10.11.3 Databases and VRML 548 10.11.4 Java and A p p l e t s 5 4 9
10.12 Other Tree Structures 549
10.12.1 CSGTrees 5 5 0 10.12.2 BSPTrees 551
10.12.3 Quadtrees and Octrees 5 5 4
Summary a n d Notes 555 Suggested Readings 5 5 6 Exercises 5 5 6
••• : iPTER 11 PROCEDURAL METHODS 559
11.1 Algorithmic Models 559 11.2 Physically-Based Models and Particle Systems 561
11.3 Newtonian Particles 562
11.3.1 I n d e p e n d e n t Particles 5 6 4 11.3.2 Spring Forces 5 6 5
11.3.3 Attractive and Repulsive Forces 566
XVU1 Contents 11.5 11.5.1 11.5.2 11.6 11.6.1 11.6.2 11.6.3 11.6.4 11.6.5 11.6.6 11.7 11.8 11.8.1 11.8.2 11.8.3 11.8.4 11.8.5 11.9 Constraints Collisions 571 Soft Constraints 573
A Simple Partial System
Displaying the Particles 574 Updating Particle Positions 575 Initialization 575
Collisions 576 Forces 577 Flocking 577
Language-Based Models Recursive Methods and Fractals
Rulers and Length 582 Fractal Dimension 583
Midpoint Division and Brownian Motion Fractal Mountains 585
The Mandelbrot Set 586
Procedural Noise
Summary and Notes 594 Suggested Readings 594 Exercises 595 570 574 578 582 584 590 C H A P 1 1 : ? 1 2 CURVES A N D SUSPACES 597
12.1 Representation of Curves and Surfaces 597
12.1.1 Explicit Representation 597 12.1.2 Implicit Representations 599 12.1.3 Parametric Form 600
12.1.4 Parametric Polynomial Curves 601 12.1.5 Parametric Polynomial Surfaces 602
12.2 Design Criteria 603 12.3 Parametric Cubic Polynomial Curves 604
12.4 Interpolation 605
12.4.1 Blending Functions 607
12.4.2 The Cubic Interpolating Patch 609
12.5 Hermite Curves and Surfaces 611
12.5.1 The Hermite Form 611
12.5.2 Geometrie and Parametric Continuity 613
12.6 Bezier Curves and Surfaces 614
Contents
xix
12.6.2 Bezier Surface Patches 617 12.7 Cubic B-Splines
12.7.1 The Cubic B-Spline Curve 618 12.7.2 B-Splines and Basis 621 12.7.3 Spline Surfaces 623 12.8 General B-Splines
12.8.1 Recursively Defined B-Splines 624 12.8.2 Uniform Splines 625
12.8.3 Nonuniform B-Splines 626 12.8.4 NURBS 626
12.8.5 Catmull-Rom Splines 628 12.9 Rendering Curves and Surfaces 12.9.1 Polynomial Evaluation Methods 629
12.9.2 Recursive Subdivision of Bezier Polynomials 630 12.9.3 Rendering Other Polynomial Curves by Subdivision 12.9.4 Subdivision of Bezier Surfaces 634
12.10 The Utah Teapot 12.11 Algebraic Surfaces 12.11.1 Quadrics 638
12.11.2 Rendering of Surfaces by Ray Casting 639 12.11.3 Subdivision Curves and Surfaces 640 12.11.4 Mesh Subdivision 641
12.12 Curves and Surfaces in OpenGL 12.12.1 Bezier Curves 644
12.12.2 Bezier Surfaces 645 12.12.3 Displaying the Teapot 646 12.12.4 NURBS Functions 648 12.12.5 Quadrics 648
Summary and Notes 649 Suggested Readings 650 Exercises 650 618 623 629 633 636 638 644
CHARTER 13 ADVANCED RENDERING 13.1 Going Beyond Pipeline Rendering 13.2 Ray Tracing
13.3 Building a Simple Ray Tracer 13.3.1 Recursive Ray Tracing 658 13.3.2 Calculating Intersections 660 13.3.3 Ray-Tracing Variations 663 653 653 654 658
x x Contents
13.4 The Rendering Equation 664
13.5 Radiosity 666 13.5.1 The Radiosity Equation 666
13.5.2 Solving the Radiosity Equation 668 13.5.3 Computing Form Factors 669 13.5.4 Carrying Out Radiosity 672
13.6 RenderMan 673 13.7 Parallel Rendering 674 13.7.1 Sort-Middle Rendering 675 13.7.2 Sort-Last Rendering 677 13.7.3 Sort-First Rendering 680 13.8 Image-Based Rendering 682 13.8.1 A Simple Example 682
Summary and Notes 684 Suggested Readings 685 Exercises 686
APPENDIX A SAMPLE PROGRAMS 689 A.1 Sierpinski Gasket Program 690 A.2 Recursive Generation of Sierpinski Gasket 692
A.3 Recursive Three-Dimensional Sierpinski Gasket 693
A.4 Marching Squares 696 A.5 Polygon M o d e l i n g Program 701
A.6 Double-Buffering program 707 A.7 Selection-Mode Picking Program 710
A.8 Rotating-Cube Program 712 A.9 Rotating Cube Using Vertex Arrays 715
A.10 Rotating Cube w i t h a Virtual Trackball 717
A.11 Moving Viewer 721 A.12 Sphere Program 724 A.13 M a n d e l b r o t Set Program 727
A.14 Bresenham's A l g o r i t h m 730 A.15 Rotating Cube w i t h Texture 733
A.16 GLSL Example 735 A.17 Scene Graph Program 741
A.18 Particle System Program 746 A.19 Program f o r Drawing Bezier Curves 751
Contents xxi APPENDIX B SPACES B.1 Sealars B.2 Vector Spaces B.3 Affine Spaces B.4 Euclidean Spaces B.5 Projections B.6 Gram-Schmidt Orthogonalization Suggested Readings 762 Exercises 762 755 755 756 758 759 760 761 APPENDIX C MATRICES C.1 Definitions C.2 M a t r i x Operations
C.3 Row and Column Matrices C.4 Rank
C.5 Change of Representation C.6 The Cross Product
C.7 Eigenvalues and Eigenvectors Suggested Readings 773 Exercises 774 765 765 766 767 768 769 771 772
APPENDIX D SYNOPSIS OF OPENGL FUNCTIONS D.1 Specifying Simple Geometry
D.2 A t t r i b u t e s D.3 W o r k i n g w i t h t h e W i n d o w System D.4 Interaction D.5 Enabling Features D.6 Transformations D.7 V i e w i n g
D.8 Defining Discrete Primitives D.9 Display Lists
D.10 Picking D.11 Lighting D.12 Texture M a p p i n g
D.13 State and Buffer Manipulation D.14 Vertex Arrays 775 775 776 777 779 780 781 782 783 784 785 786 786 788 788
x x i i Contents
D.15 Blending Functions 789 D.16 Query Functions 789 D.17 Curve and Surface Functions 790
D.18 GLUQuadrics 791 D.19 GLSL Functions 791
References 795
OpenGL Function Index 805 Subject Index 807