`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`SAMSUNG-1030’
`
`Page 1 of 112
`
`SAMSUNG-1030
`Page 1 of 112
`
`
`
`
`
`0
`$3
`
`E t
`
`“
`
`w B
`
`0&3
`SJ
`
`5 5
`
`.
`
`:3
`ac:
`
`Q c g
`
`;
`
`iN
`N.
`
`W 3
`
`3.1
`
`[‘11
`CL.N.
`
`E‘.
`QN
`N
`
`()pcnUL ARM
`
`W00
`
`Noidcr
`
`Davis
`
`Shrcincr
`
`A
`'7
`
`Addison
`
`Wcslcy
`
`SAMSUNG-1030
`
`Page 2 of 112
`
`SAMSUNG-1030
`Page 2 of 112
`
`
`
`OpenGL
`Programming Guide
`
`Third Edition
`
`The Ofiicial Guide to
`Learning OpenGL, Version 1.2
`
`OpenGL Architecture Review Board
`Mason W00 Jackie Neider
`Tom Davis Dave Shreiner
`
`A
`VV
`
`ADDISON-WESLEY
`
`An Imprint of Addison Wesley Longman, Inc.
`
`Reading, Massachusetts 0 Harlow, England - Menlo Park, California
`Berkeley, California 0 Don Mills, Ontario - Sydney
`Bonn 0 Amsterdam ' Tokyo 0 Mexico City
`
`SAMSUNG-1030
`
`Page 3 of 112
`
`SAMSUNG-1030
`Page 3 of 112
`
`
`
`Silicon Graphics, the Silicon Graphics logo, OpenGL and IRIS are registered trademarks, and
`IRIS Graphics Library is a trademark of Silicon Graphics, Inc. X Window System is a
`trademark of Massachusetts Institute of Technology. Display PostScript is a registered
`trademark of Adobe Systems Incorporated.
`
`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 Addison Wesley
`Longman Inc. 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 discounts on this book when ordered in quantity for special sales. For
`more information, please contact:
`AWL Direct Sales
`
`Addison Wesley Longman, Inc.
`One Jacob Way
`Reading, Massachusetts 01867
`(781) 944-3 700
`
`Visit AW on the Web: www.awl.com/cseng/
`
`Library of Congress Cataloging-in-Publicati0n Data
`
`OpenGL programming guide: the official guide to learning OpenGL,
`version 1.2 / OpenGL Architecture Review Board, Mason Woo, Jackie Neider,
`Tom Davis, Dave Shreiner —- 3rd ed.
`p.
`cm.
`Rev. ed. of: OpenGL programming guide / Mason Woo. 1997.
`Includes bibliographical references and index.
`ISBN 0-201-60458-2
`
`I. Woo, Mason. II. Neider, Jackie.
`2. OpenGL.
`1. Computer graphics.
`III. Davis, Tom.
`IV. Shreiner, Dave. V. OpenGL Architecture Review Board.
`Vl. OpenGL programming guide.
`T385.N435 1999
`006.6'93- —dc21
`
`99—31356
`CIP
`
`Copyright © 1999 by Silicon Graphics, Inc.
`
`All rights reserved. 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,
`recording, or otherwise, without the prior consent of the publisher. Printed in the United
`States of America. Published simultaneously in Canada.
`
`Set in 10-point Stone Serif
`
`Text printed on recycled and acid—free paper.
`ISBN 0201604582
`
`3 4567 8 MA 02010099
`
`3111 Printing October 1999
`
`SAMSUNG-1030
`
`Page 4 of 112
`
`SAMSUNG-1030
`Page 4 of 112
`
`
`
`For my family—Felicity, Max, Sarah, and Scout.
`
`—’LN
`
`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, Squiggles, Phantom, and Tuxedo.
`
`——DRS
`
`In memory of Phil Karlton.
`
`SAMSUNG-1030
`
`Page 5 of 112
`
`SAMSUNG-1030
`Page 5 of 112
`
`
`
`
`
`Contents
`
`About This Guide .............................................................................. xvii
`
`What This Guide Contains ................................................................ xvii
`
`What’s New in This Edition .................................................................. xx
`
`What You Should Know Before Reading This Guide ...........................xx
`How to Obtain the Sample Code ....................................................... xxii
`
`Nate Robins’ OpenGL Tutors ............................................................. xxii
`Errata ................................................................................................. xxiii
`
`Style Conventions ............................................................................. xxiii
`
`Acknowledgments .............................................................................. xxv
`
`Figures .............................................................................................. xxix
`
`Tables .............................................................................................. xxxiii
`
`Examples ....................................................................................... xxxvii
`
`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 .................................................................................... 1 1
`
`Evaluators ....................................................................................... 11
`
`Per-Vertex Operations .................................................................... 12
`
`SAMSUNG-1030
`
`Page 6 of 112
`
`SAMSUNG-1030
`Page 6 of 112
`
`
`
`Primitive Assembly ........................................................................ 12
`
`Pixel Operations ............................................................................. 13
`
`Texture Assembly ........................................................................... 13
`Rasterization .................................................................................. 13
`
`Fragment Operations ..................................................................... 14
`
`OpenGL—Related Libraries .................................................................... 14
`Include Files ................................................................................... 15
`
`GLUT, the OpenGL Utility Toolkit ................................................ 16
`Animation ............................................................................... C............. 20
`The Refresh That Pauses ................................................................ 22
`
`Motion = Redraw + Swap ............................................................... 23
`
`State Management and Drawing Geometric Objects ......................... 27
`
`A Drawing Survival Kit ......................................................................... 29
`
`Clearing the Window .................................................................... 29
`
`Specifying a Color .......................................................................... 32
`
`Forcing Completion of Drawing ................................................... 33
`
`Coordinate System Survival Kit ..................................................... 35
`
`Describing Points, Lines, and Polygons ............................................... 37
`
`What Are Points, Lines, and Polygons? ......................................... 37
`
`Specifying Vertices ......................................................................... 42
`
`OpenGL Geometric Drawing Primitives ........................................ 43
`
`Basic State Management ...................................................................... 49
`
`Displaying Points, Lines, and Polygons ............................................... 51
`Point Details ................................................................................... 51
`
`Line Details .................................................................................... 52
`
`Polygon Details .............................................................................. 56
`Normal Vectors .................................................................................... 64
`
`Vertex Arrays ........................................................................................ 67
`Step 1: Enabling Arrays":............................................................... 68
`Step 2: Specifying Data for the Arrays ........................................... 69
`
`Step 3: Dereferencing and Rendering ............................................ 72
`
`Interleaved Arrays .......................................................................... 78
`
`SAMSUNG-1030
`
`Page 7 of 112
`
`SAMSUNG-1030
`Page 7 of 112
`
`
`
`Attribute Groups ................................................................................... 81
`
`Some Hints for Building Polygonal Models
`of Surfaces ............................................................................................. 84
`
`An Example: Building an Icosahedron ........................................... 86
`
`Viewing ................................................................................................. 93
`
`Overview: The Camera Analogy ........................................................... 96
`
`A Simple Example: Drawing a Cube ............................................... 99
`
`General-Purpose Transformation Commands ............................. 105
`
`Viewing and Modeling Transformations ............................................ 106
`
`Thinking about Transformations ................................................. 107
`
`Modeling Transformations ........................................................... 1 10
`
`Viewing Transformations ............................................................. 1 1 6
`
`Projection Transformations ................................................................ 123
`
`Perspective Projection .................................................................. 123
`
`Orthographic Projection ............................................................... 127
`
`Viewing Volume Clipping ............................................................ 128
`
`Viewport Transformation ................................................................... 129
`
`Defining the Viewport .................................................................. 129
`
`The Transformed Depth Coordinate ............................................ 131
`
`Troubleshooting Transformations ...................................................... 132
`
`Manipulating the Matrix Stacks ......................................................... 135
`
`The Modelview Matrix Stack ........................................................ 138
`
`The Projection Matrix Stack ......................................................... 139
`
`Additional Clipping Planes ................................................................. 139
`
`Examples of Composing Several Transformations ............................. 143
`
`Building a Solar System ................................................................ 143
`
`Building an Articulated Robot Arm .............................................. 146
`
`Reversing or Mimicking Transformations .......................................... 150
`
`vii
`
`SAMSUNG-1030
`
`Page 8 of 112
`
`SAMSUNG-1030
`Page 8 of 112
`
`
`
`
`
`4. Color ................................................................................................... 155
`
`Color Perception ................................................................................ 156
`
`Computer Color ................................................................................. 158
`RGBA versus Color-Index Mode ........................................................ 160
`
`RGBA Display Mode .................................................................... 161
`
`Color-Index Display Mode .......................................................... 164
`Choosing between RGBA and Color-Index Mode ....................... 165
`
`Changing between Display Modes .............................................. 166
`
`Specifying a Color and a Shading Model ........................................... 167
`
`Specifying a Color in RGBA Mode ............................................... 167
`
`Specifying a Color in Color-Index Mode .................................... 169
`
`Specifying a Shading Model ........................................................ 170
`
`5. Lighting .............................................................................................. 173
`
`A Hidden—Surface Removal Survival Kit ............................................. 175
`
`Real-World and OpenGL Lighting ..................................................... 177
`
`Ambient, Diffuse, Specular, and Emissive Light ......................... 178
`Material Colors ............................................................................ 179
`
`RGB Values for Lights and Materials ........................................... 179
`
`A Simple Example: Rendering a Lit Sphere................r ....................... 180
`
`Creating Light Sources ....................................................................... 184
`Color ............................................................................................ 186
`
`Position and Attenuation ............................................................ 187
`
`Spotlights ..................................................................................... 189
`
`Multiple Lights ............................................................................. 190
`
`Controlling a Light’s Position and Direction .............................. 191
`
`Selecting a Lighting Model ................................................................ 197
`
`Global Ambient Light ...............'................................................... 1 98
`
`Local or Infinite Viewpoint ......................................................... 199
`
`Two—Sided Lighting ...................................................................... 199
`
`Separate Specular Color ............................................................... 200
`
`Enabling Lighting ........................................................................ 201
`
`viii
`
`SAMSUNG-1030
`
`Page 9 of 112
`
`SAMSUNG-1030
`Page 9 of 112
`
`
`
`Defining Material Properties ............................................................... 201
`
`Diffuse and Ambient Reflection ................................................... 203
`
`Specular Reflection ....................................................................... 203
`
`Emission ....................................................................................... 204
`
`Changing Material Properties ....................................................... 204
`Color Material Mode .................................................................... 207
`The Mathematics of Lighting ............................................................. 211
`Material Emission ......................................................................... 212
`
`Scaled Global Ambient Light ........................................................ 212
`Contributions from Light Sources ................................................ 212
`
`Putting It All Together .................................................................. 214
`
`Separate Specular Color ................................................................ 215
`
`Lighting in Color-Index Mode ........................................................... 216
`
`The Mathematics of Color-Index Mode Lighting ........................ 217
`
`Blending, Antialiasing, Fog, and Polygon Offset .............................. 219
`
`Blending .............................................................................................. 220
`
`The Source and Destination Factors ............................................. 221
`
`Sample Uses of Blending .............................................................. 224
`
`A Blending Example ..................................................................... 226
`
`Three-Dimensional Blending with the Depth Buffer ................... 229
`Antialiasing ......................................................................................... 233
`
`Antialiasing Points or Lines .......................................................... 235
`
`Antialiasing Polygons ................................................................... 241
`
`Fog ....................................................................................................... 242
`
`Using Fog ...................................................................................... 243
`
`Fog Equations ............................................................................... 246
`
`Polygon Offset .................................................................................... 250
`
`ix
`
`SAMSUNG-1030
`
`Page 10 of 112
`
`SAMSUNG-1030
`Page 10 of 112
`
`
`
`7. Display Lists ...................................................................................... 255
`
`An Example of Using a Display List ................................................... 257
`
`Display List Design Philosophy ......................................................... 261
`Creating and Executing a Display List ............................................... 263
`
`Naming and Creating a Display List ........................................... 266
`
`What’s Stored in a Display List? .................................................. 267
`
`Executing a Display List ............................................................... Z69
`
`Hierarchical Display Lists ............................................................ 269
`
`Managing Display List Indices ..................................................... 271
`
`Executing Multiple Display Lists ....................................................... 271
`
`Managing State Variables with Display Lists ..................................... 277
`
`Encapsulating Mode Changes ..................................................... 279
`
`8. Drawing Pixels, Bitmaps, Fonts,
`and Images ........................................................................................ 281
`
`Bitmaps and Fonts ............................................................................. 283
`The Current Raster Position ........................................................ Z86
`
`Drawing the Bitmap .................................................................... 287
`
`Choosing a Color for the Bitmap ................................................ 288
`
`Fonts and Display Lists ................................................................ 289
`
`Defining and Using a Complete Font ......................................... 290
`
`Images ................................................................................................ 294
`
`Reading, Writing, and Copying Pixel Data ................................. 294
`
`Imaging Pipeline ................................................................................ 303
`
`Pixel Packing and Unpacking ...................................................... 306
`
`Controlling Pixel-Storage Modes ................................................. 306
`
`Pixel—Transfer Operations ............................................................ 311
`
`Pixel Mapping .............................................................................. 314
`Magnifying. Reducing, or Flipping an Image .............................. 315
`Reading and Drawing Pixel Rectangles .............................................. 319
`
`The Pixel Rectangle Drawing Process .......................................... 319
`
`Tips for Improving Pixel Drawing Rates ............................................ 323
`
`Imaging Subset ................................................................................... 324
`Color Tables ................................................................................. 326
`
`Convolutions ............................................................................... 331
`
`Color Matrix ................................................................................ 339
`
`SAMSUNG-1030
`
`Page 11 of 112
`
`SAMSUNG-1030
`Page 11 of 112
`
`
`
`M
`
`Histogram ..................................................................................... 341
`Minmax ........................................................................................ 345
`Combining Pixels Using Blending Equations .............................. 347
`Constant Blending Factors ........................................................... 349
`
`9. Texture Mapping ................................................................................. 351
`
`An Overview and an Example ............................................................ 356
`Steps in Texture Mapping............................................................. 356
`A Sample Program ........................................................................ 357
`Specifying the Texture ........................................................................ 361
`Texture Proxy ............................................................................... 365
`Replacing All or Part of a Texture Image ...................................... 367
`One-Dimensional Textures370
`Three-Dimensional Textures ........................................................ 372
`Using a Texture's Borders ............................................................. 378
`Multiple Levels of Detail .............................................................. 379
`Filtering ............................................................................................... 391
`Texture Objects ................................................................................... 394
`Naming A Texture Object ............................................................. 395
`Creating and Using Texture Objects ............................................ 396
`Cleaning Up Texture Objects ....................................................... 399
`A Working Set of Resident Textures ............................................. 400
`Texture Functions ............................................................................... 402
`Post-Texture Application of Specular Color ................................. 405
`Assigning Texture Coordinates ........................................................... 405
`Computing Appropriate Texture Coordinates ............................. 407
`Repeating and Clamping Textures ............................................... 409
`Automatic Texture—Coordinate Generation ....................................... 413
`Creating Contours ........................................................................ 414
`Environment Mapping ................................................................. 418
`Advanced Features .............................................................................. 420
`The Texture Matrix Stack ............................................................. 420
`Multitexturing .............................................................................. 422
`
`xi
`
`SAMSUNG-1030
`
`Page 12 of 112
`
`SAMSUNG-1030
`Page 12 of 112
`
`
`
`
`
`10. The Framebuffer ................................................................................ 429
`
`Buffers and Their Uses ....................................................................... 432
`
`Color Buffers ................................................................................ 433
`
`Clearing Buffers ........................................................................... 434
`Selecting Color Buffers for Writing and Reading ........................ 435
`Masking Buffers ........................................................................... 437
`Testing and Operating on Fragments ................................................ 438
`Scissor Test ................................................................................... 439
`
`Alpha Test .................................................................................... 440
`Stencil Test ................................................................................... 441
`
`Depth Test .................................................................................... 447
`Blending, Dithering, and Logical Operations ............................. 448
`The Accumulation Buffer ................................................................... 450
`
`Scene Antialiasing ........................................................................ 451
`Motion Blur .................................................................................. 458
`
`Depth of Field .............................................................................. 458
`Soft Shadows ................................................................................ 463
`
`Jittering ........................................................................................ 464
`
`11. Tessellators and Quadrics ................................................................ 467
`
`Polygon Tessellation .......................................................................... 468
`
`Creating a Tessellation Object ..................................................... 470
`Tessellation Callback Routines .................................................... 470
`
`Tessellation Properties ................................................................. 475
`
`Polygon Definition .................................'..................................... 481
`
`Deleting a Tessellation Object ..................................................... 483
`
`Tessellation Performance Tips ..................................................... 484
`
`Describing GLU Errors ................................................................. 484
`
`Backward Compatibility .............................................................. 484
`
`Quadrics: Rendering Spheres, Cylinders, and Disks .......................... 486
`
`Managing Quadrics Objects ......................................................... 487
`
`Controlling Quadrics Attributes .................................................. 487
`
`Quadrics Primitives ...................................................................... 489
`
`xii
`
`SAMSUNG-1030
`
`Page 13 of 112
`
`SAMSUNG-1030
`Page 13 of 112
`
`
`
`
`
`12.
`
`Evaluators and NURBS ...................................................................... 495
`
`Prerequisites ........................................................................................ 497
`Evaluators ............................................................................................ 498
`
`One-Dimensional Evaluators ....................................................... 498
`
`Two-Dimensional Evaluators ....................................................... 504
`
`Using Evaluators for Textures ....................................................... 510
`The GLU NURBS Interface .................................................................. 512
`
`A Simple NURBS Example ............................................................ 513
`Managing a NURBS Object ........................................................... 517
`Creating a NURBS Curve or Surface ............................................. 521
`Trimming a NURBS Surface .......................................................... 526
`
`13.
`
`Selection and Feedback ..................................................................... 531
`
`Selection .............................................................................................. 532
`
`The Basic Steps .............................................................................. 533
`Creating the Name Stack .............................................................. 534
`The Hit Record .............................................................................. 536
`
`A Selection Example ..................................................................... 537
`Picking .......................................................................................... 540
`Hints for Writing a Program That Uses Selection ........................ 551
`Feedback .............................................................................................. 553
`
`The Feedback Array ...................................................................... 555
`Using Markers in Feedback Mode ................................................ 556
`A Feedback Example ..................................................................... 556
`
`14.
`
`Now That You Know ........................................................................... 561
`
`Error Handling .................................................................................... 563
`Which Version Am I Using? ............................................................... 565
`Utility Library Version .................................................................. 566
`Window System Extension Versions ............................................ 567
`Extensions to the Standard ................................................................. 567
`
`Extensions to the Standard for Microsoft Windows (WGL) ........ 569
`
`Cheesy Translucency .......................................................................... 570
`An Easy Fade Effect ............................................................................. 570
`Object Selection Using the Back Buffer .............................................. 572
`Cheap Image Transformation ............................................................. 573
`
`xiii
`
`SAMSUNG-1030
`
`Page 14 of 112
`
`SAMSUNG-1030
`Page 14 of 112
`
`
`
`Displaying Layers ............................................................................... 5 74
`Antialiased Characters ....................................................................... 575
`
`Drawing Round Points ....................................................................... 578
`
`Interpolating Images .......................................................................... 578
`
`Making Decals .................................................................................... 579
`
`Drawing Filled, Concave Polygons Using the Stencil Buffer ............. 580
`
`Finding Interference Regions ............................................................. 581
`Shadows ............................................................................................. 583
`
`Hidden-Line Removal ........................................................................ 584
`
`Hidden-Line Removal with Polygon Offset ................................. 584
`Hidden-Line Removal with the Stencil Buffer ............................. 585
`
`Texture Mapping Applications .......................................................... 586
`
`Drawing Depth-Buffered Images ........................................................ 587
`Dirichlet Domains .............................................................................. 587
`
`Life in the Stencil Buffer .................................................................... 589
`
`Alternative Uses for ngrawPixels() and glCopyPiersO .................... 590
`
`A. Order of Operations ........................................................................... 593
`
`Overview ............................................................................................ 594
`
`Geometric Operations ........................................................................ 595
`
`Per-Vertex Operations .................................................................. 595
`
`Primitive Assembly ...................................................................... 596
`
`Pixel Operations ................................................................................. 596
`
`Texture Memory .......................................................................... 597
`
`Fragment Operations ......................................................................... 597
`Odds and Ends ................................................................................... 598
`
`B. State Variables ................................................................................... 599
`
`The Query Commands ....................................................................... 600
`
`OpenGL State Variables ..................................................................... 601
`Current Values and Associated Data ........................................... 603
`
`Vertex Array ................................................................................. 604
`Transformation ............................................................................ 606
`
`Coloring ....................................................................................... 607
`
`Lighting ........................................................................................ 608
`Rasteriz