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

advanced graphics programming using opengl (morgan kaufmann series in computer graphics and geometric modeling)

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 (16.43 MB, 672 trang )

Advanced Graphics
Programming
Using OpenGL
TOM McREYNOLDS
DAVID BLYTHE
AMSTERDAM • BOSTON • HEIDELBERG • LONDON
NEW YORK • OXFORD • PARIS • SAN DIEGO
SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO
MORGAN KAUFMANN PUBLISHERS IS AN IMPRINT OF ELSEVIER
TEAM LinG
Advanced Graphics Programming Using OpenGL
TEAM LinG
The Morgan Kaufmann Series in Computer Graphics and Geometric Modeling
Series Editor: Brian A. Barsky University of California, Berkeley
This series publishes the finest works for the accomplished and aspiring graphics professional. The series includes intermediate
and advanced textbooks, graphics programming books, surveys of important new areas and methods, and reference works.
Advanced Graphics Programming Using OpenGL
Tom McReynolds and David Blythe
Digital Geometry Geometric Methods for Digital
Picture Analysis
Rienhard Klette and Azriel Rosenfeld
Digital Video and HDTV Algorithms and Interfaces
Charles Poynton
Real-Time Shader Programming
Ron Fosner
Complete Maya Programming:
An Extensive Guide to MEL and the C++ API
David Gould
MEL Scripting for Maya Animators
Mark R. Wilkins and Chris Kazmier
Digital Video and HDTV Algorithms and Interfaces


Charles Poynton
Texturing & Modeling:
A Procedural Approach, Third Edition
David S. Ebert, F. Kenton Musgrave, Darwyn Peachey,
Ken Perlin, and Steven Worley
Geometric Tools for Computer Graphics
Philip Schneider and David Eberly
Understanding Virtual Reality:
Interface, Application, and Design
William Sherman and Alan Craig
Jim Blinn’s Corner: Notation, Notation, Notation
Jim Blinn
Level of Detail for 3D Graphics:
David Luebke, Martin Reddy, Jonathan D. Cohen,
Amitabh Varshney, Benjamin Watson, and
Robert Huebner
Pyramid Algorithms: A Dynamic Programming
Approach to Curves and Surfaces for Geometric
Modeling
Ron Goldman
Non-Photorealistic Computer Graphics:
Modeling, Rendering, and Animation
Thomas Strothotte and Stefan Schlechtweg
Curves and Surfaces for CAGD: A Practical Guide,
Fifth Edition
Gerald Farin
Subdivision Methods for Geometric Design:
A Constructive Approach
Joe Warren and Henrik Weimer
Computer Animation: Algorithms and Techniques

Rick Parent
The Computer Animator’s Technical Handbook
Lynn Pocock and Judson Rosebush
Advanced RenderMan:
Creating CGI for Motion Pictures
Anthony A. Apodaca and Larry Gritz
Curves and Surfaces in Geometric Modeling:
Theory and Algorithms
Jean Gallier
Andrew Glassner’s Notebook:
Recreational Computer Graphics
Andrew S. Glassner
Warping and Morphing of Graphical Objects
Jonas Gomes, Lucia Darsa, Bruno Costa, and Luiz Velho
Jim Blinn’s Corner: Dirty Pixels
Jim Blinn
Rendering with Radiance:
The Art and Science of Lighting Visualization
Greg Ward Larson and Rob Shakespeare
Introduction to Implicit Surfaces
Edited by Jules Bloomenthal
Jim Blinn’s Corner:
A Trip Down the Graphics Pipeline
Jim Blinn
Interactive Curves and Surfaces:
A Multimedia Tutorial on CAGD
Alyn Rockwood and Peter Chambers
Wavelets for Computer Graphics:
Theory and Applications
Eric J. Stollnitz, Tony D. DeRose, and David H. Salesin

Principles of Digital Image Synthesis
Andrew S. Glassner
Radiosity & Global Illumination
François X. Sillion and Claude Puech
Knotty: A B-Spline Visualization Program
Jonathan Yen
User Interface Management Systems:
Models and Algorithms
Dan R. Olsen, Jr.
Making Them Move: Mechanics, Control, and Animation
of Articulated Figures
Edited by Norman I. Badler, Brian A. Barsky, and
David Zeltzer
Geometric and Solid Modeling: An Introduction
Christoph M. Hoffmann
An Introduction to Splines for Use in Computer Graphics
and Geometric Modeling
Richard H. Bartels, John C. Beatty, and Brian A. Barsky
TEAM LinG
Publishing Director: Diane Cerra
Publishing Services Manager: Simon Crump
Project Manager: Brandy Lilly
Editorial Coordinator: Mona Buehler
Cover Design: Dutton & Sherman Design
Text Design: Julio Esperas
Composition: Cepha Imaging Pvt. Ltd.
Illustrations: Dartmouth Publishing, Inc.
Copyeditor: Daril Bentley; Graphic World
Proofreader: Graphic World
Indexer: Graphic World

Interior printer: China Translation & Printing Services, Ltd.
Cover printer: China Tranalation & Printing Services, Ltd.
Morgan Kaufmann Publishers is an imprint of Elsevier.
500 Sansome Street, Suite 400, San Francisco, CA 94111
This book is printed on acid-free paper.
© 2005 by Elsevier Inc. All rights reserved.
Designations used by companies to distinguish their products are often claimed as trademarks or
registered trademarks. In all instances in which Morgan Kaufmann Publishers is aware of a claim,
the product names appear in initial capital or all capital letters. Readers, however, should contact
the appropriate companies for more complete information regarding trademarks and registration.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form
or by any means—electronic, mechanical, photocopying, scanning, or otherwise—without prior written
permission of the publisher.
Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford,
UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, e-mail: You
may also complete your request on-line via the Elsevier homepage () by selecting
“Customer Support” and then “Obtaining Permissions.”
Library of Congress Cataloging-in-Publication Data
Application Submitted
ISBN: 1-55860-659-9
For information on all Morgan Kaufmann publications,
visit our Web site at www.mkp.com or www.books.elsevier.com
Printed in China
10987654321
TEAM LinG
To my friends and colleaguesfrom Silicon Graphics; it was a fabulous time and place to
learn about 3D graphics. –DB
To Ede Forney and HarryMcGinnis; you were there when it counted. – TM
TEAM LinG
Contents

Preface xxiii
Acknowledgments xxvii
Biographies
xxviii
PART I
Concepts 1
CHAPTER 1
Geometry Representation and Modeling 3
1.1 Polygonal Representation 3
1.2 Decomposition and Tessellation 4
1.3 Shading Normals 8
1.3.1 Smooth Shading 9
1.3.2 Vertex Winding Order 11
1.4 Triangle Stripping 12
1.4.1 Greedy Tri-stripping 13
1.5
Vertices and Vertex Arrays 14
1.5.1 Vertex Buffer Objects 15
1.5.2 Triangle Lists 16
1.6 Modeling vs. Rendering Revisited 17
vi
TEAM LinG
Contents vii
CHAPTER 2
3D Transformations 19
2.1
Data Representation 19
2.2 Overview of the Transformation Pipeline
20
2.2.1 Object Space and the Modelview Transform 20

2.2.2 Eye Space and Projection Transform 21
2.2.3 Clip Space and Perspective Divide 22
2.2.4 NDC Space and the Viewport Transform 22
2.2.5 Window Space 23
2.3 Normal Transformation 23
2.4 Texture Coordinate Generation and Transformation
25
2.4.1 Texture Matrix 25
2.4.2 Texture Coordinate Generation 25
2.5 Modeling Transforms
27
2.6 Visualizing Transform Sequences 28
2.7
Projection Transform 30
2.8 The Z Coordinate and Perspective Projection 30
2.8.1 Z Coordinates and Fog 32
2.9 Vertex Programs 32
2.10 Summary
34
CHAPTER 3
Color, Shading, and Lighting 35
3.1 Representing Color 35
3.1.1 Resolution and Dynamic Range 36
3.1.2 Gamma 37
3.1.3 Alpha 39
3.1.4 Color Index 39
3.2 Shading 40
3.3 Lighting 43
3.3.1 Intensities, Colors, and Materials 46
3.3.2 Light Source Properties 47

TEAM LinG
viii Contents
3.3.3 Material Properties 49
3.3.4 Vertex and Fragment Lighting 50
3.4 Fixed-Point and Floating-Point Arithmetic
53
3.4.1 Biased Arithmetic 54
3.5 Summary 56
CHAPTER 4
Digital Images and Image Manipulation 57
4.1
Image Representation 57
4.2
Digital Filtering 60
4.3
Convolution 62
4.4
Images in OpenGL 63
4.5 Positioning Images 65
4.6 Pixel Store Operations
65
4.7 Pixel Transfer Operations 67
4.7.1 Scale and Bias 67
4.7.2 Pixel Mapping Operations 67
4.8 ARB Imaging Subset 68
4.8.1 Convolution 68
4.8.2 Color Matrix Transform 68
4.8.3 Histogram 69
4.8.4 MinMax 70
4.8.5 Color Tables 70

4.8.6 Blend Equation and Constant Color Blending 71
4.9 Off-Screen Processing 72
4.10 Summary 72
CHAPTER 5
Texture Mapping 73
5.1
Loading Texture Images 73
5.1.1 Texture Borders 74
5.1.2 Internal Texture Formats 75
TEAM LinG
Contents ix
5.1.3 Compressed Textures 76
5.1.4 Proxy Textures 77
5.2 Texture Coordinates 77
5.2.1 Texture Coordinate Generation and Transformation 79
5.3 Loading Texture Images from the Frame Buffer
79
5.4 Environment Mapping
80
5.4.1 Generating Environment Map Texture Coordinates 81
5.4.2 Texture Maps Used in Environment Mapping 82
5.4.3 Cube Mapping 83
5.4.4 Sphere Mapping 85
5.5 3D Texture 88
5.5.1 Using 3D Textures to Render Solid Materials 89
5.6 Filtering 90
5.7 Additional Control of Texture Level of Detail 91
5.8 Texture Objects 93
5.9
Multitexture 95

5.9.1 Multitexture Model 96
5.9.2 Multitexture Texture Environments 97
5.10 Texture Environment 98
5.10.1 Advanced Texture Environment Functionality 99
5.10.2 Fragment Programs 100
5.11 Summary 102
CHAPTER 6
Rasterization and Fragment Processing 103
6.1 Rasterization 104
6.1.1 Rasterization Consistency 105
6.1.2 Z-Fighting 105
6.1.3 Bitmaps and Pixel Rectangles 107
6.1.4 Texture, Color, and Depth Interpolation 108
6.1.5 w Buffering 109
6.2 Fragment Operations
110
6.2.1 Multisample Operations 111
6.2.2 Alpha Test 111
6.2.3 Stencil Test 111
TEAM LinG
x Contents
6.2.4 Blending 112
6.2.5 Logic Op 114
6.3 Framebuffer Operations 115
6.3.1 Accumulation Buffer 116
6.4 Summary 117
CHAPTER 7
Window System and Platform Integration 119
7.1 Renderer and Window State 120
7.2 Address Space and Threads 121

7.3 Anatomy of a Window 122
7.3.1 Overlay and Underlay Windows 122
7.3.2 Multiple Displays 123
7.4
Off-Screen Rendering 124
7.4.1 GLX Pbuffers 125
7.4.2 WGL Pbuffers 126
7.5 Rendering to Texture Maps 126
7.6 Direct and Indirect Rendering 127
CHAPTER 8
OpenGL Implementations
129
8.1 OpenGL Versions 129
8.2 OpenGL Extensions 131
8.3 OpenGL ES for Embedded Systems 131
8.3.1 Embedded Profiles 132
8.3.2 Common and Common-Lite Profiles 133
8.3.3 Safety Critical Profile 136
8.3.4 OpenGL ES Revisions 136
8.4 OpenGL Pipeline Evolution 137
8.5 Hardware Implementations of the Pipeline 138
8.5.1 Rasterization Acceleration 138
8.5.2 Primitive Setup Acceleration 141
8.5.3 Transform and Lighting Acceleration 141
8.5.4 Pipeline Balance 142
TEAM LinG
Contents xi
8.5.5 Parallelism Opportunities 142
8.5.6 Reordering the Pipeline 149
8.5.7 Mixed Software and Hardware Implementations 150

8.6 The Future 151
PART II
Basic Techniques 153
CHAPTER 9
Multiple Rendering Passes 155
9.1 Invariance 155
9.2 Multipass Overview 156
9.3 The Multipass Toolbox 159
9.3.1 Arithmetic Operations 159
9.3.2 Arbitrary Functions 160
9.3.3 Conditionals 161
9.3.4 Variables 162
9.3.5 Parameters 163
9.4 Multipass Limitations 165
9.5 Multipass vs. Micropass 165
9.5.1 Multitexture 166
9.6 Deferred Shading 167
9.7 Summary 167
CHAPTER 10
Antialiasing 169
10.1 Full-Scene Antialiasing 170
10.2 Supersampling 171
10.2.1 Supersampling by Overdrawing 172
10.2.2 Supersampling with the Accumulation Buffer 173
10.2.3 Multisample Antialiasing 175
10.2.4 Drawbacks 176
TEAM LinG
xii Contents
10.3
Area Sampling 177

10.4 Line and Point Antialiasing 178
10.5 Antialiasing with Textures 180
10.6 Polygon Antialiasing 181
10.7 Temporal Antialiasing 182
10.7.1 Motion Blur 183
10.8 Summary 184
CHAPTER 11
Compositing, Blending, and Transparency 185
11.1 Combining Two Images 185
11.1.1 Compositing 186
11.1.2 Compositing Multiple Images 187
11.1.3 Alpha Division 190
11.2 Other Compositing Operators 190
11.3
Keying and Matting 192
11.4 Blending Artifacts 192
11.4.1 Arithmetic Errors 192
11.4.2 Blending with the Accumulation Buffer 193
11.4.3 Approximation Errors 193
11.4.4 Gamma Correction Errors 193
11.5 Compositing Images with Depth 194
11.6 Other Blending Operations 195
11.7 Dissolves 196
11.8 Transparency 199
11.9 Alpha-Blended Transparency 200
11.9.1 Dynamic Object Transparency 202
11.9.2 Transparency Mapping 203
11.9.3 Transparency Sorting 204
11.9.4 Depth Peeling 205
11.10 Screen-Door Transparency 205

11.10.1 Multisample Transparency 207
11.11 Summary 208
TEAM LinG
Contents xiii
CHAPTER 12
Image Processing Techniques 211
12.1 OpenGL Imaging Support 211
12.2 Image Storage 212
12.3 Point Operations 213
12.3.1 Color Adjustment 213
12.3.2 Interpolation and Extrapolation 213
12.3.3 Scale and Bias 215
12.3.4 Thresholding 215
12.3.5 Conversion to Luminance 216
12.3.6 Manipulating Saturation 216
12.3.7 Rotating Hue 218
12.3.8 Color Space Conversion 219
12.4 Region-based Operations 223
12.4.1 Contrast Stretching 224
12.4.2 Histogram Equalization 224
12.5 Reduction Operations 225
12.6
Convolution 227
12.6.1 Separable Filters 227
12.6.2 Convolutions Using the Accumulation Buffer 228
12.6.3 Convolution Using Extensions 230
12.6.4 Useful Convolution Filters 230
12.6.5 Correlation and Feature Detection 233
12.7 Geometric Operations 235
12.7.1 Pixel Zoom 235

12.7.2 Scaling Using Texture Mapping 236
12.7.3 Rotation Using Texture Mapping 237
12.7.4 Distortion Correction 237
12.8 Image-Based Depth of Field 238
12.9 High Dynamic Range Imaging 241
12.9.1 Dynamic Range 241
12.9.2 Tone Mapping 242
12.9.3 Modeling Adaptation 245
12.10 Summary 245
TEAM LinG
xiv Contents
CHAPTER 13
Basic Transform Techniques 247
13.1
Computing Inverse Transforms Efficiently 247
13.2 Stereo Viewing 249
13.3 Depth of Field
252
13.4 Image Tiling 254
13.5 Billboarding Geometry 257
13.6 Texture Coordinate vs. Geometric Transformations 261
13.6.1 Direct Vertex to Texture Coordinate Mapping 263
13.6.2 Overlaying an Entire Scene with a Texture 263
13.6.3 Overlaying a Scene with an Independent Texture
Projection 264
13.7 Interpolating Vertex Components through a Perspective
Transformation
265
13.7.1 Transforming Vertices in the Application 265
13.7.2 Interpolating Vertex Components 266

13.7.3 Computing LOD 267
13.8 Summary 268
CHAPTER 14
Texture Mapping Techniques 269
14.1 Loading Texture Images into a Framebuffer 270
14.2
Optimizing Texture Coordinate Assignment 270
14.3 3D Textures 271
14.4 Texture Mosaics 274
14.5 Texture Tiling 277
14.6 Texture Paging 279
14.6.1 Texture Subimage Loading 282
14.6.2 Paging Images in System Memory 285
14.6.3 Hardware Support for Texture Paging 286
14.7 Prefiltered Textures 287
14.7.1 Computing Texel Aspect Ratios 288
TEAM LinG
Contents xv
14.8
Dual-Paraboloid Environment Mapping 291
14.8.1 The Mathematics of Dual-Paraboloid Maps 291
14.8.2 Using Dual-Paraboloid Maps 294
14.8.3 OpenGL Dual-Paraboloid Support 296
14.9 Texture Projection 296
14.10 Texture Color Coding and Contouring
298
14.11 2D Image Warping 300
14.12 Texture Animation 302
14.13 Detail Textures 306
14.13.1 Signed Intensity Detail Textures 309

14.13.2 Creating Detail Textures 311
14.14 Texture Sharpening 312
14.15 Mipmap Generation 313
14.16 Texture Map Limits 315
14.17 Summary
316
CHAPTER 15
Lighting Techniques 317
15.1 Limitations in Vertex Lighting 317
15.1.1 Static and Adaptive Tessellation 319
15.1.2 Local Light and Spotlight Attenuation 320
15.2 Fragment Lighting Using Texture Mapping 321
15.3 Spotlight Effects Using Projective Textures 322
15.4 Specular Lighting Using Environment Maps 325
15.4.1 Multitexture 326
15.5 Light Maps
327
15.5.1 2D Texture Light Maps 327
15.5.2 3D Texture Light Maps 330
15.6 BRDF-based Lighting 332
15.7
Reflectance Maps 332
15.7.1 Gloss Maps 332
15.7.2 Emission Maps 334
TEAM LinG
xvi Contents
15.8
Per-fragment Lighting Computations 334
15.9 Other Lighting Models 335
15.9.1 Fresnel Reflection 335

15.9.2 Gaussian Reflection 336
15.9.3 Anisotropic Lighting 337
15.9.4 Oren-Nayar Model 340
15.9.5 Cook-Torrance Model 342
15.10 Bump Mapping with Textures 343
15.10.1 Approximating Bump Mapping Using Texture 345
15.10.2 Tangent Space 346
15.10.3 Forward Differencing 347
15.10.4 Limitations 351
15.11 Normal Maps 352
15.11.1 Vector Normalization 352
15.12 Bump-mapped Reflections 353
15.13 High Dynamic Range Lighting 354
15.13.1 Bloom and Glare Effects 354
15.14 Global Illumination 355
15.14.1 Virtual Light Technique 355
15.14.2 Combining OpenGL Lighting with Radiosity 356
15.14.3 Ambient Occlusion 357
15.15
Summary 359
PART III
Advanced Techniques 361
CHAPTER 16
CAD and Modeling Techniques 363
16.1 Picking and Highlighting 363
16.1.1 OpenGL Selection 364
16.1.2 Object Tagging in the Color Buffer 365
16.1.3 Proxy Geometry 366
16.1.4 Mapping from Window to Object Coordinates 367
16.1.5 Other Picking Methods 367

16.1.6 Highlighting 367
TEAM LinG
Contents xvii
16.1.7 XOR Highlighting 368
16.1.8 Foreground Object Manipulation 369
16.2 Culling Techniques 369
16.3 Occlusion Culling 370
16.3.1 Choosing Occluders 371
16.3.2 Building the Occlusion Map 371
16.3.3 Building the Depth Estimation Buffer 372
16.3.4 Occlusion Testing 372
16.3.5 Other Occlusion Testing Methods 373
16.4 Geometric Level of Detail 373
16.4.1 Changing Detail 374
16.4.2 Transition Techniques 375
16.5 Visualizing Surface Orientation 377
16.6 Visualizing Surface Curvature 379
16.7 Line Rendering Techniques 380
16.7.1 Wireframe Models 381
16.7.2 Hidden Lines 382
16.7.3 Polygon Offset 384
16.7.4 Depth Range 384
16.7.5 Haloed Lines 385
16.7.6 Silhouette Edges 386
16.7.7 Preventing Antialiasing Artifacts 389
16.7.8 End Caps on Wide Lines 390
16.8 Coplanar Polygons and Decaling 390
16.9 Capping Clipped Solids 392
16.10 Constructive Solid Geometry 393
CHAPTER 17

Scene Realism
403
17.1
Reflections 404
17.1.1 Object vs. Image Techniques 404
17.1.2 Planar Reflectors 407
17.1.3 Curved Reflectors 411
17.1.4 Interreflections 419
17.1.5 Imperfect Reflectors 422
TEAM LinG
xviii Contents
17.2
Refraction 424
17.2.1 Refraction Equation 424
17.2.2 Planar Refraction 426
17.2.3 Texture Mapped Refraction 428
17.2.4 Environment Mapped Refraction 429
17.2.5 Modeling Multiple Refraction Boundaries 430
17.2.6 Clipping Refracted Objects 431
17.3 Creating Environment Maps 432
17.3.1 Creating Environment Maps with Ray Casting 433
17.3.2 Creating Environment Maps with Texture Warping 434
17.3.3 Cube Map Textures 437
17.3.4 Sphere Map Textures 440
17.3.5 Dual-paraboloid Maps 443
17.3.6 Updating Environment Maps Dynamically 448
17.4 Shadows 449
17.4.1 Projective Shadows 450
17.4.2 Shadow Volumes 452
17.4.3 Shadow Maps 459

17.4.4 Creating Soft Shadows 463
17.5 Summary
465
CHAPTER 18
Natural Detail 467
18.1 Particle Systems 467
18.1.1 Representing Particles 469
18.1.2 Number of Particles 473
18.1.3 Modeling Particle Interactions 473
18.1.4 Updating and Rendering Particles 475
18.1.5 Applications 478
18.2
Dynamic Meshes 484
18.3 Procedural Texture Generation 487
18.3.1 Filtered Noise Functions 487
18.3.2 Generating Noise Functions 489
18.3.3 Filtering Using Texture Convolution 490
18.3.4 Optimizing the Convolution Process 492
18.3.5 Spectral Synthesis 495
18.3.6 Turbulence 496
TEAM LinG
Contents xix
18.3.7 Random Image Warping 498
18.3.8 Generating 3D Noise 498
18.4 Summary
500
CHAPTER 19
Illustration and Artistic Techniques
501
19.1 Projections for Illustration 501

19.1.1 Axonometric Projection 502
19.1.2 Oblique Projection 503
19.2 Nonphotorealistic Lighting Models 505
19.2.1 Matte Surfaces 505
19.2.2 Metallic Surfaces 506
19.3
Edge Lines 507
19.4 Cutaway Views 508
19.4.1 Surface Texture 510
19.5 Depth Cuing
511
19.6 Patterns and Hatching 512
19.6.1 Cross Hatching and 3D Halftones 513
19.6.2 Halftoning 515
19.7 2D Drawing Techniques 516
19.7.1 Accuracy in 2D Drawing 516
19.7.2 Line Joins 517
19.7.3 2D Trim Curves 518
19.8 Text Rendering 520
19.8.1 Image-based Text 520
19.8.2 Geometry-based Text 523
19.9 Drawing and Painting 525
19.9.1 Undo and Resolution Independence 527
19.9.2 Painting in 3D 527
19.9.3 Painting on Images 529
19.10 Summary 530
CHAPTER 20
Scientific Visualization 531
20.1 Mapping Numbers to Pictures 531
TEAM LinG

xx Contents
20.2
Visual Cues and Perception 531
20.3 Data Characterization 532
20.4 Point Data Visualization 534
20.4.1 Scatter Plots 534
20.4.2 Iconographic Display 535
20.4.3 Andrews Plots 536
20.4.4 Histograms and Charts 537
20.5 Scalar Field Visualization 538
20.5.1 Line Graphs 538
20.5.2 Contour Lines 539
20.5.3 Annotating Metrics 539
20.5.4 Image Display 540
20.5.5 Surface Display 543
20.5.6 Isosurfaces 545
20.5.7 Volume Slicing 546
20.5.8 Volume Rendering 547
20.5.9 Texture Slicing 549
20.5.10 Splatting 556
20.5.11 Creating Volume Data 559
20.6 Vector Field Visualization 560
20.6.1 Icons 561
20.6.2 Particle Tracing 561
20.6.3 Stream Lines 563
20.6.4 Illuminated Stream Lines 563
20.6.5 Line Integral Convolution 564
20.7 Tensor Field Visualization 568
20.7.1 Hyperstreamlines 569
20.8 Summary 570

CHAPTER 21
Structuring Applications for Performance
571
21.1 Structuring Graphics Processing 571
21.1.1 Scene Graphs 572
21.1.2 Vertex Updates 575
21.1.3 Texture Updates 576
21.2 Managing Frame Time
577
21.2.1 Input Phase 579
TEAM LinG
Contents xxi
21.2.2 Rendering Phase 579
21.2.3 Computation Phase 580
21.2.4 The Safety Margin 581
21.3
Application Performance Tuning 581
21.3.1 Locating Bottlenecks 581
21.3.2 Finding Application Bottlenecks 583
21.3.3 Measuring Performance 587
21.3.4 Measuring Depth Complexity 589
21.3.5 Pipeline Interleaving 591
21.4
Summary 592
APPENDIX A
Using OpenGL Extensions 593
A.1 How OpenGL Extensions are Documented
593
A.2 Finding OpenGL Extension Specifications 594
A.3 How to Read an OpenGL Extension Specification 594

A.3.1 ARB Extensions 598
A.4
Portable Use of OpenGL Extensions 599
A.5 Using Extension Function Pointers 602
APPENDIX B
Equations 605
B.1 3D Vectors 605
B.1.1 Spherical Coordinates 606
B.1.2 Linear Interpolation of 3D Vectors 606
B.1.3 Barycentric Coordinates 607
B.2 Projection Matrices 607
B.2.1 Orthographic Projection 607
B.2.2 Perspective Projection 607
B.2.3 Perspective z-Coordinate Transformations 608
B.2.4 Alternative Perspective Projection 608
B.3
Viewing Transforms 608
B.4
Modeling Transforms 609
B.4.1 Scaling 609
TEAM LinG
xxii Contents
B.4.2 Translation 609
B.4.3 Rotation 609
B.5 Parallel and Perpendicular Vectors 610
B.6 Reflection Vector 610
B.7
Lighting Equations 610
B.8 Function Approximations
612

B.8.1 Taylor Series Expansion 612
B.8.2 Newton-Raphson Method 612
B.8.3 Hypotenuse 613
Bibliography 615
Subject Index 629
TEAM LinG
Preface
Overview
Computer graphics has come a long way from the early days of line drawings and
light pens. Today anyone can run interactive and realistic graphics applications on the
hardware available on an affordable personal computer. While hardware progress has
been impressive, widespread gains in software expertise has been more elusive. There
are many computer graphics professionals and enthusiasts out there, but a compre-
hensive understanding of the accelerated graphics pipeline and how to exploit it is less
widespread.
This book attempts to bring the computer graphics enthusiast, whether professional
or amateur, beyond the basics covered in computer graphics texts, and introduce them to
a mix of more intense practical and theoretical discussion that is hard to obtain outside
of a professional computer graphics environment.
We emphasize the algorithmic side of computer graphics, with a practical appli-
cation focus. We try to strike a balance between useful examples and approachable
theory. We present usable techniques for real world problems, but support them
with enough theory and background so the reader can extend and modify the ideas
presented here.
This book is about graphics techniques, techniques that don’t require esoteric hard-
ware or custom graphics libraries, that are written in a comprehensible style, and do
useful things. This book will teach you some graphics, especially areas that are some-
times underrepresented in graphics texts. But it also goes further, showing you how to
apply those techniques in real world applications, filling real world needs.
Since there are already a number of excellent books that provide an introduction

to computer graphics (Foley, 1994; Watt, 1989; Rogers, 1997; Angel, 1997; Newman,
1973) and to OpenGL programming (Neider, 1997; Angel, 1997) we have been necessar-
ily brief in these areas. We assume that the reader is comfortable with these fundamentals;
however, we have included extra introductory material where we thought it would
improve understanding of later sections.
We also note that the computer graphics field has a lot of competing notation
and vocabulary. We have tried to be consistent with terminology and notation used
in the OpenGL specification and the “standard” OpenGL books while at the same time
providing some mention of alternative terminology when it is relevent.
xxiii
TEAM LinG
xxiv Preface
OpenGL
We chose OpenGL as our base graphics language for a number of reasons. It is designed
to be full featured, to run efficiently on a wide range of graphics architectures, and is clean
and straightforward to use. It also dictates very little policy. It is easy to mix and match
graphics features in OpenGL to get new effects that may not have even been considered
when the language was designed. Its clear specification gives the application programmer
confidence that applications written in OpenGL will act predictably on many different
graphics hardware and driver implementations.
OpenGL is also widely available. It can be obtained for free on all the impor-
tant architectures today: Apple Machintosh, all flavors of Microsoft Windows, nearly
all Unix variants including Linux, and OS/2. Most commercial system and graphics
hardware vendors support OpenGL as well, and support for hardware accelerated imple-
mentations has grown rapidly, especially in the personal computer space. OpenGL runs
on a wide range of graphics hardware; from “big iron” compute clusters, to OpenGL ES,
which is designed to provide 3D graphics on embedded devices as small as a cell phone.
Given the broad applicability, scalability, and wide availability, OpenGL is an easy
choice as the basis for describing graphics algorithms. However, even if you don’t use
OpenGL, the graphics APIs in common use are conceptually similar enough that you

will still find this book useful. OpenGL remains an evolving specification. Through-
out the book we make references to various revisions of the specification (versions
1.0–1.5) and discuss both OpenGL architecture review board (ARB) extensions and
various vendor-specific extensions when we believe they enhance the discussion of a
particular subject. Rather than focus on the feature set of the most advanced versions
of OpenGL, we have included a broad range of algorithms with varying requirements.
For many techniques we describe algorithm variations that cover a range of earlier and
more advanced versions of OpenGL. We have followed this path since a wide range of
OpenGL versions are deployed across various environments including the burgeoning
embedded space.
Book Organization
This book is divided into three parts. We start with a conceptual overview of com-
puter graphics, emphasizing areas important to the techniques in this book, with extra
attention in some overlooked areas. Hand in hand with our introduction to computer
graphics, we’ll describe the OpenGL pipeline, with extra detail on the parts of the pipeline
most techniques rely on heavily: lighting, texture mapping, rasterization, and depth
buffering. We also use this opportunity to describe OpenGL system deployment, includ-
ing the platform embedding layer and an overview of common hardware acceleration
techniques for the pipeline.
TEAM LinG
Preface xxv
With this foundation in place, Part II introduces a set of important basic tech-
niques. Useful by themselves, they help re-enforce the insights gleaned from the overview.
These sequences are also used as building blocks for more complex and sophisticated
techniques. To help tie them more tightly to the graphics concepts described in the pre-
vious part, these techniques are presented and organized with respect to the OpenGL
architecture.
The third and final part covers more sophisticated and complex techniques. These
techniques are categorized into application areas to help organize the material. The start
of each application section has an introduction to the problems and issues important for

that area, making these sections more interesting and useful to readers not well versed in
that particular field.
The book is heavily cross-referenced. Complex techniques reference the simple
ones they build upon, and both levels of technique reference the conceptual overview.
This allows the reader to use the book as a self-guided tutorial, learning more about
techniques and concepts of interest in greater depth.
Example Code
To avoid cluttering the text with large fragments of example code, code fragments are
used sparingly. Algorithms are usually described as a sequence of steps. However, since
details are often the difference between a working program and a black screen, we have
tried to include full blown example programs for most algorithms. This example code is
available for internet download from www.mkp.com/opengl.
Conventions
We use conventions and terminology similar to that found in the OpenGL specification
and in the “red-blue-green-white” series of OpenGL books. In addition, we use the
following conventions:
• Equations involving matrices, vectors, and points use single uppercase letters for
most variables. Vectors are emboldened (V), whereas points and matrices are not
(M, P). In rare occasions vectors or points are in lower case.
• Occasionally symbols are context specific, but generally the following meanings
hold:
– N - normal vector
– L - light vector
TEAM LinG

×