Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

.OpenGL®Programming GuideSeventh Edition.OpenGL Series®Visit informit.com /opengl for a ppt
Nội dung xem thử
Mô tả chi tiết
OpenGL®
Programming Guide
Seventh Edition
The OpenGL graphics system is a software interface to graphics
hardware. (“GL” stands for “Graphics Library.”) It allows you to
create interactive programs that produce color images of moving, threedimensional objects. With OpenGL, you can control computer-graphics
technology to produce realistic pictures, or ones that depart from reality
in imaginative ways.
The OpenGL Series from Addison-Wesley Professional comprises
tutorial and reference books that help programmers gain a practical
understanding of OpenGL standards, along with the insight needed to
unlock OpenGL’s full potential.
Visit informit.com/opengl for a complete list of available products
OpenGL®
Series
OpenGL®
Programming Guide
Seventh Edition
The Official Guide to
Learning OpenGL®
, Versions 3.0 and 3.1
Dave Shreiner
The Khronos OpenGL ARB Working Group
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and the publisher was
aware of a trademark claim, the designations have been printed with initial capital letters or
in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or
omissions. No liability is assumed for incidental or consequential damages in connection
with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales, which may include electronic versions and/or custom covers and
content particular to your business, training goals, marketing focus, and branding interests.
For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside of the U.S., please contact:
International Sales
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Shreiner, Dave.
OpenGL programming guide : the official guide to learning OpenGL, versions 3.0 and 3.1
/ Dave Shreiner; the Khronos OpenGL ARB Working Group — 7th ed.
p. cm.
Includes index.
ISBN 978-0-321-55262-4 (pbk. : alk. paper)
1. Computer graphics. 2. OpenGL. I. Title.
T385.O635 2009
006.6'6—dc22
2009018793
Copyright © 2010 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by
copyright, and permission must be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by any means,
electronic, mechanical, photocopying, recording, or likewise. For information regarding
permissions, write to:
Pearson Education, Inc.
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax (617) 671-3447
ISBN 13: 978-0-321-55262-4
ISBN 10: 0-321-55262-8
Text printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan.
First printing, July 2009
For my family—Felicity, Max, Sarah, and Scout.
—JLN
For my family—Ellyn, Ricky, and Lucy.
—TRD
To Tom Doeppner and Andy van Dam, who started me along this path.
—MW
For my family—Vicki, Bonnie, Bob, Phantom, Squiggles, Tuxedo, and Toby.
—DRS
In memory of Phil Karlton, Celeste Fowler, and Ben Cheatham.
This page intentionally left blank
vii
Contents
Figures ................................................................................................xxi
Tables ................................................................................................xxv
Examples ..........................................................................................xxix
About This Guide .............................................................................xxxv
What This Guide Contains...............................................................xxxv
What’s New in This Edition ..........................................................xxxviii
What You Should Know Before Reading This Guide............................xl
How to Obtain the Sample Code .........................................................xli
Errata....................................................................................................xlii
Style Conventions ...............................................................................xlii
Distinguishing Deprecated Features...................................................xliii
Acknowledgments .................................................................................xlv
1. Introduction to OpenGL............................................................................1
What Is OpenGL? ...................................................................................2
A Smidgen of OpenGL Code ..................................................................5
OpenGL Command Syntax....................................................................7
OpenGL as a State Machine ...................................................................9
OpenGL Rendering Pipeline.................................................................10
Display Lists....................................................................................11
Evaluators .......................................................................................11
Per-Vertex Operations ....................................................................12
Primitive Assembly.........................................................................12
viii Contents
Pixel Operations............................................................................. 13
Texture Assembly........................................................................... 13
Rasterization................................................................................... 14
Fragment Operations ..................................................................... 14
OpenGL-Related Libraries .................................................................... 14
Include Files ................................................................................... 15
GLUT, the OpenGL Utility Toolkit................................................ 17
Animation ............................................................................................ 22
The Refresh That Pauses................................................................. 23
Motion = Redraw + Swap ............................................................... 24
OpenGL and Its Deprecation Mechanism ........................................... 27
OpenGL Contexts .......................................................................... 27
Accessing OpenGL Functions ........................................................ 29
2. State Management and Drawing Geometric Objects .......................... 31
A Drawing Survival Kit......................................................................... 34
Clearing the Window .................................................................... 34
Specifying a Color.......................................................................... 37
Forcing Completion of Drawing.................................................... 38
Coordinate System Survival Kit ..................................................... 40
Describing Points, Lines, and Polygons............................................... 42
What Are Points, Lines, and Polygons?......................................... 42
Specifying Vertices......................................................................... 46
OpenGL Geometric Drawing Primitives........................................ 47
Basic State Management ...................................................................... 53
Displaying Points, Lines, and Polygons............................................... 55
Point Details................................................................................... 55
Line Details .................................................................................... 56
Polygon Details .............................................................................. 60
Normal Vectors .................................................................................... 68
Vertex Arrays ........................................................................................ 70
Step 1: Enabling Arrays .................................................................. 72
Step 2: Specifying Data for the Arrays ........................................... 73
Step 3: Dereferencing and Rendering ............................................ 77
Restarting Primitives...................................................................... 83
Instanced Drawing......................................................................... 86
Interleaved Arrays .......................................................................... 88
Contents ix
Buffer Objects .......................................................................................91
Creating Buffer Objects ..................................................................92
Making a Buffer Object Active .......................................................93
Allocating and Initializing Buffer Objects with Data.....................93
Updating Data Values in Buffer Objects ........................................96
Copying Data Between Buffer Objects.........................................101
Cleaning Up Buffer Objects..........................................................102
Using Buffer Objects with Vertex-Array Data ..............................102
Vertex-Array Objects...........................................................................104
Attribute Groups.................................................................................110
Some Hints for Building Polygonal Models of Surfaces.....................113
An Example: Building an Icosahedron ........................................115
3. Viewing.................................................................................................. 123
Overview: The Camera Analogy.........................................................126
A Simple Example: Drawing a Cube ............................................129
General-Purpose Transformation Commands .............................134
Viewing and Modeling Transformations ...........................................137
Thinking about Transformations.................................................137
Modeling Transformations...........................................................140
Viewing Transformations.............................................................146
Projection Transformations................................................................152
Perspective Projection ..................................................................153
Orthographic Projection ..............................................................156
Viewing Volume Clipping............................................................158
Viewport Transformation...................................................................158
Defining the Viewport .................................................................159
The Transformed Depth Coordinate............................................161
Troubleshooting Transformations......................................................162
Manipulating the Matrix Stacks .........................................................164
The Modelview Matrix Stack........................................................167
The Projection Matrix Stack.........................................................168
Additional Clipping Planes.................................................................168
Examples of Composing Several Transformations.............................172
Building a Solar System ................................................................172
Building an Articulated Robot Arm..............................................175
Reversing or Mimicking Transformations..........................................179
x Contents
4. Color ...................................................................................................... 185
Color Perception ................................................................................ 186
Computer Color ................................................................................. 188
RGBA versus Color-Index Mode ........................................................ 190
RGBA Display Mode..................................................................... 191
Color-Index Display Mode........................................................... 193
Choosing between RGBA and Color-Index Mode....................... 195
Changing between Display Modes .............................................. 196
Specifying a Color and a Shading Model........................................... 196
Specifying a Color in RGBA Mode............................................... 197
Specifying a Color in Color-Index Mode..................................... 199
Specifying a Shading Model......................................................... 200
5. Lighting ................................................................................................. 203
A Hidden-Surface Removal Survival Kit............................................. 205
Real-World and OpenGL Lighting..................................................... 207
Ambient, Diffuse, Specular, and Emissive Light.......................... 208
Material Colors............................................................................. 209
RGB Values for Lights and Materials ........................................... 209
A Simple Example: Rendering a Lit Sphere........................................ 210
Creating Light Sources ....................................................................... 214
Color ............................................................................................ 216
Position and Attenuation ............................................................ 217
Spotlights ..................................................................................... 219
Multiple Lights............................................................................. 220
Controlling a Light’s Position and Direction .............................. 221
Selecting a Lighting Model ................................................................ 227
Global Ambient Light .................................................................. 228
Local or Infinite Viewpoint ......................................................... 229
Two-Sided Lighting...................................................................... 229
Secondary Specular Color ............................................................ 230
Enabling Lighting ........................................................................ 231
Defining Material Properties .............................................................. 231
Diffuse and Ambient Reflection .................................................. 233
Specular Reflection....................................................................... 234
Emission....................................................................................... 234
Contents xi
Changing Material Properties ......................................................235
Color Material Mode ....................................................................237
The Mathematics of Lighting .............................................................240
Material Emission.........................................................................241
Scaled Global Ambient Light .......................................................242
Contributions from Light Sources ...............................................242
Putting It All Together..................................................................244
Secondary Specular Color.............................................................245
Lighting in Color-Index Mode ...........................................................246
The Mathematics of Color-Index Mode Lighting........................247
6. Blending, Antialiasing, Fog, and Polygon Offset.............................. 249
Blending..............................................................................................251
The Source and Destination Factors.............................................252
Enabling Blending........................................................................255
Combining Pixels Using Blending Equations..............................255
Sample Uses of Blending ..............................................................258
A Blending Example .....................................................................260
Three-Dimensional Blending with the Depth Buffer...................263
Antialiasing.........................................................................................267
Antialiasing Points or Lines..........................................................269
Antialiasing Geometric Primitives with Multisampling ..............275
Antialiasing Polygons...................................................................279
Fog.......................................................................................................280
Using Fog......................................................................................281
Fog Equations ...............................................................................284
Point Parameters.................................................................................291
Polygon Offset ....................................................................................293
7. Display Lists ......................................................................................... 297
Why Use Display Lists?.......................................................................298
An Example of Using a Display List ...................................................299
Display List Design Philosophy..........................................................302
Creating and Executing a Display List ...............................................305
Naming and Creating a Display List ............................................306
What’s Stored in a Display List?...................................................307
xii Contents
Executing a Display List............................................................... 309
Hierarchical Display Lists............................................................. 310
Managing Display List Indices..................................................... 311
Executing Multiple Display Lists........................................................ 312
Managing State Variables with Display Lists ..................................... 318
Encapsulating Mode Changes...................................................... 319
8. Drawing Pixels, Bitmaps, Fonts, and Images .................................... 321
Bitmaps and Fonts.............................................................................. 323
The Current Raster Position......................................................... 325
Drawing the Bitmap..................................................................... 327
Choosing a Color for the Bitmap ................................................ 328
Fonts and Display Lists ................................................................ 329
Defining and Using a Complete Font.......................................... 331
Images ................................................................................................ 333
Reading, Writing, and Copying Pixel Data ................................. 333
Imaging Pipeline ................................................................................ 343
Pixel Packing and Unpacking ...................................................... 346
Controlling Pixel-Storage Modes................................................. 347
Pixel-Transfer Operations ............................................................ 351
Pixel Mapping .............................................................................. 354
Magnifying, Reducing, or Flipping an Image.............................. 356
Reading and Drawing Pixel Rectangles.............................................. 359
The Pixel Rectangle Drawing Process .......................................... 359
Using Buffer Objects with Pixel Rectangle Data ................................ 362
Using Buffer Objects to Transfer Pixel Data ................................ 363
Using Buffer Objects to Retrieve Pixel Data ................................ 365
Tips for Improving Pixel Drawing Rates ............................................ 366
Imaging Subset ................................................................................... 367
Color Tables ................................................................................. 369
Convolutions ............................................................................... 374
Color Matrix................................................................................. 382
Histogram..................................................................................... 383
Minmax........................................................................................ 387
Contents xiii
9. Texture Mapping................................................................................... 389
An Overview and an Example............................................................395
Steps in Texture Mapping ............................................................395
A Sample Program ........................................................................397
Specifying the Texture........................................................................400
Texture Proxy ...............................................................................406
Replacing All or Part of a Texture Image......................................408
One-Dimensional Textures ..........................................................412
Three-Dimensional Textures ........................................................414
Texture Arrays ..............................................................................419
Compressed Texture Images.........................................................420
Using a Texture’s Borders.............................................................423
Mipmaps: Multiple Levels of Detail .............................................423
Filtering...............................................................................................434
Texture Objects...................................................................................437
Naming a Texture Object .............................................................438
Creating and Using Texture Objects ............................................438
Cleaning Up Texture Objects .......................................................441
A Working Set of Resident Textures.............................................442
Texture Functions...............................................................................444
Assigning Texture Coordinates ..........................................................448
Computing Appropriate Texture Coordinates.............................450
Repeating and Clamping Textures...............................................452
Automatic Texture-Coordinate Generation .......................................457
Creating Contours........................................................................458
Sphere Map...................................................................................463
Cube Map Textures.......................................................................465
Multitexturing ....................................................................................467
Texture Combiner Functions .............................................................472
The Interpolation Combiner Function ........................................477
Applying Secondary Color after Texturing ........................................478
Secondary Color When Lighting Is Disabled...............................478
Secondary Specular Color When Lighting Is Enabled .................479
Point Sprites........................................................................................479
The Texture Matrix Stack....................................................................481
xiv Contents
Depth Textures................................................................................... 483
Creating a Shadow Map............................................................... 483
Generating Texture Coordinates and Rendering ........................ 485
10. The Framebuffer ................................................................................... 489
Buffers and Their Uses........................................................................ 492
Color Buffers ................................................................................ 493
Clearing Buffers............................................................................ 495
Selecting Color Buffers for Writing and Reading ........................ 497
Masking Buffers............................................................................ 499
Testing and Operating on Fragments ................................................ 501
Scissor Test ................................................................................... 502
Alpha Test .................................................................................... 502
Stencil Test ................................................................................... 504
Depth Test.................................................................................... 510
Occlusion Query .......................................................................... 511
Conditional Rendering ................................................................ 514
Blending, Dithering, and Logical Operations.............................. 515
The Accumulation Buffer ................................................................... 518
Motion Blur.................................................................................. 520
Depth of Field .............................................................................. 520
Soft Shadows ................................................................................ 525
Jittering ........................................................................................ 525
Framebuffer Objects ........................................................................... 526
Renderbuffers ............................................................................... 529
Copying Pixel Rectangles............................................................. 539
11. Tessellators and Quadrics................................................................... 541
Polygon Tessellation .......................................................................... 542
Creating a Tessellation Object ..................................................... 544
Tessellation Callback Routines .................................................... 544
Tessellation Properties ................................................................. 549
Polygon Definition ...................................................................... 554
Deleting a Tessellation Object ..................................................... 557
Tessellation Performance Tips ..................................................... 557
Describing GLU Errors ................................................................. 557
Backward Compatibility .............................................................. 558