Interactive Computer Graphics A Top-Down Approach Using OpenGL® FIFTH EDITION EDWARD ANGEL UNIVERSITY OF NEW MEXICO PEARSON Addison Wesley Boston San Francisco New York London Toronto Sydney Tokyo Singapore Madrid Mexico City Munich Paris Cape Töwn Hong Kong Montreal
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Interactive Computer Graphics A Top-Down Approach Using OpenGL®
FIFTH EDITION
EDWARD ANGEL UNIVERSITY OF NEW MEXICO
PEARSON
Addison Wesley
Boston San Francisco New York London Toronto Sydney Tokyo Singapore Madrid
Mexico City Munich Paris Cape Töwn Hong Kong Montreal
Preface xxv
CHAPTER 1 GRAPHICS SYSTEMS AND 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
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 Transformations in Homogeneous Coordinates 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 Concatenat ion of Transformations 2 0 7
4.9.1 Rotation About a Fixed Point 208
4.9.2 General Rotation 209
4.9.3 The Instance Transformation 211
4.9.4 Rotation About an Arbitrary Axis 212
4 . 1 0 O p e n G L Transformation Matrices 2 1 5
4.10.1 The Current Transformation Matrix 215
4.10.2 Rotation, Translation, and Scaling 216
4.10.3 Rotation About 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.11 Interfaces to Three-Dimensional Applications 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 Quaternions 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 and Computer V iewing 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 iewing wi th a Computer 2 4 2
xi 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
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 Con ten ts
7.10 Polygon 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.11 Hidden-Surface Removal 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.12 Antialiasing 3 6 9
7 .13 Display Considerations 371
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 TECHNIQUES 3 3 3
8.1 Buffers 3 8 3
8.2 Digital Images 3 8 5
8.3 Wri t ing into Buffers 3 8 8
8.3.1 Writing Modes 389
8.3.2 Writes with XOR 391
8.4 Bit and Pixel Operat ions 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 Examples 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
10.6 Animation 517
Contents xvii
10.7 Graphical Objects 519 10.7.1 Methods , At t r ibutes, and Messages 519 10.7.2 ACubeObject 521 10.7.3 Imp lement ing the Cube Ob jec t 523 10.7.4 Objects and Hierarchy 524
10.7.5 Geometr ie Objects 525
10.8 Scene Graphs 526
10.9 A Simple Scene Graph API 528 10.9.1 TheNodeClass 528
10.9.2 Geomet ry Nodes 530 10.9.3 Camera Class 532 10.9.4 Lights and Materials 533 10.9.5 Transformations 535
10.9.6 The Robot Figure 535 10.9.7 Imp lement ing the Viewer 537 10.9.8 Imp lement ing a N o d e 541
10.10 Open Scene Graph 544
10.11 Graphics and the Internet 546 10.11.1 Networks and Protocols 546 10.11.2 Hypermedia and HTML 547 10.11.3 Databases and VRML 548 10.11.4 Java and App le ts 549
10.12 Other Tree Structures 549 10.12.1 CSGTrees 550 10.12.2 BSPTrees 551
10.12.3 Quadtrees and Octrees 554
Summary and Notes 555
Suggested Readings 556
Exercises 556
••• : 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 Independen t Particles 564 11.3.2 Spring Forces 565 11.3.3 At t ract ive and Repulsive Forces 566
11.4 Solving Particle Systems 568
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 : ? 12 CURVES AND 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 12.6.1 Bezier Curves 615