OpenGL SuperBible, 5th Edition free download ebook for read

  

  OpenGL ®

  SUPERBIBLE Fifth Edition

  ® OpenGL

  

SUPERBIBLE

Fifth Edition

  

Comprehensive Tutorial and Reference

Richard S. Wright, Jr.

  

Nicholas Haemel

Graham Sellers

Benjamin Lipchak

  Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid

  Cape Town • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distin- guish their products are claimed as trademarks. Where those designa- tions 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.

  Technical Reviewer:

  Copy Editor:

  Geneil Breeze

  Indexer:

  Brad Herriman

  Proofreader:

  Language Logistics, LLC

  Paul Martz

  Project Editor:

  Publishing Coordinator:

  Kim Boedigheimer

  Cover Designer:

  Alan Clements

  Senior Compositor:

  Gloria Schurick

  Anne Goebel

  Kristy Hart

  The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include elec- tronic 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 corpsales@pearsontechgroup.com For sales outside the United States please contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw

  Songlin Qiu

  Development Editor:

  Debra Williams Cauley

  Acquisitions Editor:

  Mark Taub

  Editor-in-Chief:

  Managing Editor:

  Pearson Education, Inc Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671 3447

  All rights reserved. Printed in the United States of America. This publi- cation 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, elec- tronic, mechanical, photocopying, recording, or likewise. For informa- tion regarding permissions, write to:

  T385.W728 2010 006.6’6—dc22 2010014489 Copyright © 2011 Pearson Education, Inc.

  ISBN 978-0-321-71261-5 (pbk. : alk. paper) 1. Computer graphics. 2. OpenGL. I. Wright, Richard S., 1965- II. Wright, Richard S., 1965- OpenGL super bible.

  Wright Jr. ... [et al.]. — 5th ed. p. cm.

  Library of Congress Cataloging-in-Publication Data: OpenGL super bible : comprehensive tutorial and reference / Richard S.

ISBN-13: 978-0-32-171261-5

  ISBN-10: 0-32-171261-7 Text printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan. First printing July 2010

  

For my wife, LeeAnne,

For not killing me in my sleep

(when I deserved it).

To the memory of Richard S. Wright, Sr.

Thanks, Dad, for just letting me be a nerd.

  

—Richard S. Wright, Jr.

  

To my wife, Anna,

Who has put up with all my engineering nonsense and

given me undying love and support.

And to my parents for providing me with encouragement and more

LEGOs than I could get both arms around.

  

—Nicholas Haemel

For my wife, Chris, and my son, Jeremy.

I have everything I need in you.

  

—Graham Sellers

  Contents at a Glance Preface to the Fifth Edition. ........................................................................xxi Preface to the Fourth Edition ....................................................................xxiii Preface to the Third Edition.......................................................................xxvi Introduction . ..................................................................................................1

  

PART I Basic Concepts ...............................................................................................7

  1 Introduction to 3D Graphics and OpenGL.....................................................9

  2 Getting Started . ...........................................................................................33

  3 Basic Rendering . ..........................................................................................79

  4 Basic Transformations: A Vector/Matrix Primer . .....................................125

  5 Basic Texturing. ..........................................................................................179

  6 Thinking Outside the Box: Nonstock Shaders . ........................................229

  7 More Advanced Texture Topics . ................................................................289

  

PART II Intermediate to Advanced Ideas ................................................................321

  8 Buffer Objects: Storage Is Now in Your Hands . .......................................323

  9 Advanced Buffers: Beyond the Basics . .....................................................359

  10 Fragment Operations: The End of the Pipeline . .......................................391

  11 Advanced Shader Usage . ...........................................................................411

  12 Advanced Geometry Management . ..........................................................471

  PART III Platform-Specific Notes .............................................................................539

  13 OpenGL on Windows . ...............................................................................541

  14 OpenGL on OS X . .....................................................................................569

  15 OpenGL on Linux . .....................................................................................597

  16 OpenGL ES on Mobile Devices . .................................................................619 Appendix A Further Reading ........................................................................................653 Appendix B Glossary . ....................................................................................................655 Appendix C OpenGL Man Pages for (Core) OpenGL 3.3 ..............................................661 Index............................................................................................................939

  Table of Contents

  

  

  

   Going Electric ......................................................................................10 Going 3D .............................................................................................11

   Transformations and Projections ........................................................14 Rasterization ........................................................................................14 Shading ................................................................................................16 Texture Mapping..................................................................................16 Blending...............................................................................................17 Connecting the Dots ...........................................................................18

   Real-Time 3D .......................................................................................18 Non-Real-Time 3D ...............................................................................22 Shaders .................................................................................................22

   Not a Toolbox ......................................................................................23 Coordinate Systems .............................................................................24 Projections: Getting 3D to 2D.............................................................28

  

  

   Evolution of a Standard.......................................................................34 Licensing and Conformance ...............................................................38 The Future of OpenGL ........................................................................38 Deprecated Functionality ....................................................................40

   Our Supporting Cast............................................................................42 OpenGL API Specifics ..........................................................................44 OpenGL Errors . ...................................................................................46 Identifying the Version........................................................................47 Getting a Clue with glHint..................................................................47 The OpenGL State Machine ................................................................48

   Including Paths....................................................................................50 Creating the Project.............................................................................52 Adding Our Files . ................................................................................53

   Custom Build Settings .........................................................................55 Creating a New Project ........................................................................56 Frameworks, Headers, and Libraries....................................................59

   What to “Include” ...............................................................................64 Starting GLUT . ....................................................................................65 Coordinate System Basics ....................................................................67 Setting Things Up ................................................................................70 Getting Down to Business ...................................................................73

   Special Keys..........................................................................................74 Refreshing the Display.........................................................................75 Simple Automated Animation.............................................................76

  

  

   Client-Server ........................................................................................81 Shaders . ...............................................................................................82

   Orthographic Projections ....................................................................85 Perspective Projections ........................................................................85

   Attributes .............................................................................................87 Uniforms ..............................................................................................87

   Points and Lines ..................................................................................90 Drawing Triangles in 3D......................................................................94 Individual Triangles . ...........................................................................95 A Simple Batch Container .................................................................100 Unwanted Geometry .........................................................................101 Polygon Offset ...................................................................................108 Cutting It Out with Scissors ..............................................................111

   Combining Colors .............................................................................114 Changing the Blending Equation .....................................................116 Antialiasing . ......................................................................................117 Multisampling....................................................................................121

  

  

  

  Vectors, or Which Way Is Which? ....................................................127 The Matrix . .......................................................................................130

   Eye Coordinates .................................................................................132 Viewing Transformations ..................................................................133 Modeling Transformations ................................................................134 The Modelview Duality .....................................................................135 Projection Transformations ...............................................................136 Viewport Transformations .................................................................137

  xii OpenGL SuperBible

   Matrix Construction ..........................................................................138 Applying a Modelview Matrix...........................................................144

   Using the Triangle Batch Class. ........................................................146 Making a Sphere . ..............................................................................146 Making a Torus . ................................................................................147 Making a Cylinder or a Cone. ..........................................................148 Making a Disk . ..................................................................................150

   Orthographic Projections ..................................................................151 Perspective Projection. ......................................................................153 The ModelviewProjection Matrix......................................................155

   Using a Matrix Stack..........................................................................159 Managing Your Pipeline ....................................................................161 Spicing It Up! . ...................................................................................165

   An Actor Frame..................................................................................168 Euler Angles: “Use the Frame, Luke!” ...............................................169 Camera Management ........................................................................170 Adding More Actors...........................................................................173 What About Lights?...........................................................................175

  

  

   Pixel Packing......................................................................................181 Pixmaps. ............................................................................................183 Packed Pixel Formats .........................................................................185 Saving Pixels ......................................................................................186 Reading Pixels ....................................................................................188

   Using the Color Buffer ......................................................................193 Updating Textures. ............................................................................194 Texture Objects . ................................................................................195

   Texture Coordinates ..........................................................................196 Texture Parameters . ..........................................................................199 Putting It All Together .......................................................................202

   Mipmap Filtering ...............................................................................209 Generating Mip Levels.......................................................................210 Mipmaps in Action............................................................................210

  

  Compressing Textures........................................................................224 Loading Compressed Textures...........................................................225 A Final Example. ...............................................................................226

  

  

   Variables and Data Types...................................................................231 Storage Qualifiers. .............................................................................234 A Real Shader . ...................................................................................236 Compiling, Binding, and Linking .....................................................240 Using the Shader................................................................................247 Provoking Vertex . .............................................................................248

   Finding Your Uniforms......................................................................249 Setting Scalars and Vector Uniforms.................................................250 Setting Uniform Arrays......................................................................251 Setting Uniform Matrices ..................................................................252 The Flat Shader . ................................................................................252

   Trigonometry Functions ....................................................................256 Exponential Functions ......................................................................256 Geometric Functions . .......................................................................257 Matrix Functions . .............................................................................257 Vector Relational Functions ..............................................................258 Common Functions...........................................................................259

   Simple Diffuse Lighting.....................................................................262 The Point Light Diffuse Shader .........................................................264 The ADS Light Model . ......................................................................269 Phong Shading. .................................................................................273

   Just the Texels Please .........................................................................277 Light the Texels..................................................................................279 Discarding Fragments . ......................................................................281 Cell Shading—Texels as Light ...........................................................284

  

  

   Loading a Rectangle Texture .............................................................290 Using a Rectangle Texture .................................................................291

   Loading Cube Maps...........................................................................295 Making a Skybox . .............................................................................297 Making a Reflection. .........................................................................299

   Multiple Texture Coordinates ...........................................................301 A Multitextured Example . ................................................................302

   Texturing Points.................................................................................305 Point Sizes . ........................................................................................306 Putting This All Together . ................................................................307 Point Parameters . ..............................................................................310 Shaped Points . ..................................................................................311 Rotating Points . ................................................................................312

   Loading a 2D Texture Array ..............................................................314 Indexing the Texture Array ...............................................................316 Accessing Texture Arrays . .................................................................317

  

  

  

   Creating Your Very Own Buffers .......................................................324 Filling Buffers. ...................................................................................325 Pixel Buffer Objects . .........................................................................326 Texture Buffer Objects . .....................................................................334

   How to Use FBOs ...............................................................................336 Renderbuffer Objects . .......................................................................337 Draw Buffers . ....................................................................................339 Framebuffer Completeness ................................................................342 Copying Data in Framebuffers ..........................................................345 Putting It All Together—Using FBOs.................................................346

  

  

   Mapping Buffers ................................................................................360 Copying Buffers .................................................................................361

  

   Floats—True Precision at Last!...........................................................364 Multisampling....................................................................................380 Integers . ............................................................................................385 sRGB. .................................................................................................386 Texture Compression. .......................................................................387

  

  

  

  Sample Coverage................................................................................393 Sample Mask . ....................................................................................394 Putting It All Together . .....................................................................394

  

  Depth Clamp .....................................................................................402

  Blend Equation ..................................................................................402 Blend Function ..................................................................................403 Putting It All Together .......................................................................404

  

  xvi OpenGL SuperBible

   Color ..................................................................................................408 Depth .................................................................................................408 Stencil ................................................................................................408 Usage ..................................................................................................409

  

  

   Physical Simulation in the Vertex Shader.........................................412

   The Pass-Through Geometry Shader .................................................420 Using Geometry Shaders in an Application .....................................422 Discarding Geometry in the Geometry Shader ................................426 Modifying Geometry in the Geometry Shader.................................429 Generating Geometry in the Geometry Shader................................430 Changing the Primitive Type in the Geometry Shader....................434 New Primitive Types Introduced by the Geometry Shader ..............438

   Post-Processing in the Fragment Shader—Color Correction ............442 Post-Processing in the Fragment Shader—Convolution. .................444 Generating Image Data in the Fragment Shader . ............................448 Discarding Work in the Fragment Shader. .......................................451 Controlling Depth Per Fragment . ....................................................453

   Interpolation and Storage Qualifiers.................................................454 Other Advanced Built-In Functions ..................................................458

   Building Uniform Blocks ...................................................................461

  

  

   Preparing a Query..............................................................................472 Issuing a Query ..................................................................................473 Retrieving Query Results ...................................................................474 Using the Results of a Query .............................................................475 Getting OpenGL to Make Decisions for You ....................................478 Measuring Time Taken to Execute Commands ................................480

   Using Buffers to Store Vertex Data....................................................484 Storing Vertex Indices in Buffers. .....................................................489

  

  Combining Drawing Functions.........................................................492 Combining Geometry Using Primitive Restart .................................494 Instanced Rendering. ........................................................................496 Getting Your Data Automatically. ....................................................503

   Transform Feedback...........................................................................509 Turning Off Rasterization ..................................................................514 Counting Vertices Using Primitive Queries ......................................515 Using the Results of a Primitive Query .............................................516 Example Uses for Transform Feedback..............................................517

   Clip Distances—Defining Your Own Custom Clip Space.................528

  

  

  

   Microsoft’s OpenGL...........................................................................542 Modern Graphics Drivers ..................................................................542 Extended OpenGL .............................................................................544 WGL Extensions ................................................................................545

   GDI Device Contexts .........................................................................547 Pixel Formats . ...................................................................................548 The OpenGL Rendering Context ......................................................556

   Creating the Window ........................................................................560

  

  Eliminating Visual Tearing ................................................................567

  

  

  Creating a Cocoa Program ................................................................571 Wiring It All Together. ......................................................................578 Double or Single Buffered? ................................................................580 SphereWorld . ....................................................................................580

   Going Full-Screen with Cocoa...........................................................586

   Sync Frame Rate.................................................................................593 Increasing Fill Performance ...............................................................594 Multithreaded OpenGL .....................................................................595

  

  

   Brief History.......................................................................................598 What Is X? .........................................................................................598

   Checking for OpenGL .......................................................................599 Setting Up Mesa. ...............................................................................599 Setting Up Hardware Drivers. ...........................................................600 Setting Up GLUT and GLEW.............................................................600 Building OpenGL Apps......................................................................601

   Displays and X Windows ..................................................................603 Config Management and Visuals ......................................................603 Windows and Render Surfaces ..........................................................607 Extending OpenGL and GLX ............................................................608 Context Management. ......................................................................609 Synchronization . ..............................................................................612 GLX Queries. .....................................................................................613 Putting It All Together . .....................................................................614

  

  

   What’s the ES For? .............................................................................620 A Brief History . .................................................................................620

   ES 2.0..................................................................................................622

   Application Design Considerations. .................................................627 Dealing with a Limited Environment ...............................................628 Fixed-Point Math . .............................................................................629

   EGL Displays ......................................................................................631 Creating a Window............................................................................632 Context Management........................................................................636 Presenting Buffers and Rendering Synchronization .........................636 More EGL Stuff ..................................................................................637

   Popular Operating Systems. ..............................................................638 Vendor-Specific Extensions . .............................................................639 For the Home Gamer. .......................................................................639

   Setting Up An iPhone Project. ..........................................................640 Moving to the iPhone . .....................................................................644

  

  

  

  

  

  

  In nature, occasionally a forest becomes overgrown, and the forest ecosystem begins to collapse a bit under its own weight. Lightning sometimes strikes and burns down the forest, enabling a fresh new start, and a new blueprint from what was before emerges. The fifth edition of this book has undergone just such a radical transformation. We have burnt the fourth edition to the ground, and save for a few isolated patches, this entire book represents new growth.

  The same can be said for OpenGL. With OpenGL 3.0, the word deprecated was introduced to the specification for the first time. Features and functionality were marked for removal, and developers were urged to move forward. A leaner, meaner OpenGL was envisioned as the baggage of the fixed pipeline was jettisoned. Things don’t always go as planned, however. During the late 1990s OpenGL was besieged in what is sometimes now called the

  API Wars, by Microsoft’s Direct 3D API. Ultimately developers, however, are in charge of

  dominant APIs, and OpenGL refused to die. Instead it flourished and has become the world standard for real-time 3D graphics rendering. Again developers seem to have spoken, and despite the efforts of OpenGL’s own greatest champions on the ARB, the fixed pipeline simply refuses to die off. Today, we have two flavors of OpenGL, the compatibility

  profile and the core profile.

  For this reason, it is entirely likely that the fourth edition of this book, which covers what could now simply be called the classic OpenGL 2.1 fixed function profile, will remain in demand for years to come. The fixed function pipeline has a tremendous amount of legacy behind it, is very easy to program for, and is today fully hardware accelerated on a modern graphics card. Many nonperformance minded and nongraphics specialists may well prefer this programming model for some time to come. Only time will tell.

  Meanwhile, we were faced with what to do for the fifth edition of this book. OpenGL is being updated every 6 months or so, and we had two updates during the writing of this book! Trying to cover the compatibility and the core profile simultaneously leads to a lot of confusion. In addition, many of the newer more modern effects are only possible with shaders. The fixed pipeline mode of graphics programming, while still useful to many, seems more and more rudimentary with each passing year. So, moving forward, we thought it best to focus this edition solely on the core profile. As far as I know, this will be the first book on the market to do so. In teaching OpenGL at Full Sail University, I faced a huge challenge trying to figure out how to teach OpenGL without the fixed pipeline. I, as well as many others that I have observed, taught shader programming as an extension of the fixed pipeline. How do you start to use OpenGL without several initially boring chap- ters on shader programming? A number of tools allow you to write shaders in an IDE type environment, and some have taken this as the initial approach. This has its uses, and I have no criticism of this approach. My preference, however, is to have an actual program that I can run that does something interesting. This is how I learned to program…then I can send the program to my mom, girlfriend, buddy, boss, and so on, and show them how clever I am. This is a powerful feedback mechanism when it comes to mastering new tech- nologies. It’s how I learned, and it was the pattern of the first four (and best-selling) editions of this book. It’s an approach I did not want to abandon for this edition either. I hope you like the results of our efforts.

  —Richard S. Wright, Jr.

  

  My career has been built on a long history of making “stupid” choices and accidentally being right. First, I went to Microsoft’s DOS, instead of the wildly popular CP/M. Later, I recall, friends counseled me that Windows was dead, and too hard to program for, and that OS/2 was the future (you couldn’t lose by sticking with IBM, they’d say).

  Just got lucky, I guess. There were a few other minor wrong turns that just happened to fortunately have me pointed away from some other collapsing industry segment, but my next really big stupid decision was writing the first edition of this book. I had already built a nice comfortable career out of fixing SQL database problems and was making the transition to large-scale enterprise IT solutions in the healthcare industry. A book on OpenGL? I had no idea what I was doing. The first time I read the official OpenGL specification, I had to all but breathe in a paper bag, my first co-author quit in disgust, and the whole project was very nearly canceled before the book was half-finished. As soon as the book came out, I had some meager credibility outside my normal field of expertise. I was offered a job at Lockheed-Martin/Real3D doing “real” OpenGL work. My then-current boss (God bless you, David, wherever you are!) tried really hard to talk me out of throwing my career away. Everybody knows, he insisted, that whatever Microsoft does is going to be the way the industry goes, and Microsoft’s Talisman graphics platform was going to bury OpenGL into obscurity. Besides, there was only one other book on OpenGL in existence; how big a thing could it possibly be? Eleven years have passed, and as I finish yet the fourth edition of this book (and look at a shelf full of OpenGL books), the number of people reading this who remember the short- lived hype of Talisman would probably fit in the back of my minivan. An OpenGL engi- neer I used to know at IBM had in her e-mail signature: “OpenGL. It’s everywhere. Do the math.” This has never been truer than it is today. OpenGL today is the industry-leading standard graphics API on nearly every conceivable platform. This includes not only desktop Windows PCs and Macs, but also UNIX worksta- tions, location-based entertainment systems, major game consoles (all but one), handheld gaming devices, cell phones, and a myriad of other embedded systems such as avionic and vehicle instrumentation. Across platforms, OpenGL is the undisputed champion of 3D content creation applica- tions, 3D games, visualization, simulation, scientific modeling, and even 2D image and video editing. OpenGL’s widespread success can be attributed to its elegance and ease of use, its power and flexibility, and the overwhelming support it has received from the developer and IHV communities. OpenGL can be extended as well, providing all the bene- fits of an open standard, as well as giving vendors the ability to add their own proprietary added value to implementations. You have probably heard that programmable hardware is the future of 3D graphics programming, and of graphics APIs. This is no longer true. Programmable hardware is no longer in the future; it is here now, today, even on the lowest cost motherboard embedded

  3D chipsets. It is not a fluke that this edition follows the last at the closest interval of the series. The pace of evolving graphics technology is simply staggering, and this edition brings you up-to-date on the now-latest OpenGL version 2.1. We have reinforced the chapters on fixed-pipeline programming, which is not going away anytime soon, and have affectionately deemed them “The Old Testament;” still relevant, illustrative, and the foundation on which the “New Testament” of programmable hard- ware is based. I find the analogy quite appropriate, and I would refute anyone who thinks the fixed pipeline is completely dead and irrelevant. The rank and file of application devel- opers (not necessarily cutting-edge game developers) would, I’m sure, agree.

  That said, we have still trimmed some dead weight. Color Index mode is ignored as much as possible, some old paletted rendering material from the Windows chapter has been pruned, and we have eliminated all the old low-level assembly-style shader material to make room for updated and expanded coverage of the high-level shading language (GLSL). You’ll also find a whole new chapter on OpenGL on handheld systems, totally rewritten Mac OS X and Linux chapters, and a really great new chapter on advanced buffer tech- niques such as off-screen rendering and floating-point textures.

  Another big change some readers will notice is that the OpenGL SuperBible has been acquired and adopted into the Addison-Wesley Professional OpenGL series. I can’t begin to express how grateful I am and how humbled I feel by this honor. I myself have worn out the covers on at least one edition of every volume in this series.

  One of the reasons, I think, for the longevity of this book has been the unique approach it takes among OpenGL books. As much as possible, we look at things through the eyes of someone who is excited by 3D graphics but knows very little about the topic. The purpose of a tutorial is to get you started, not teach you everything you will ever need to know. Every professional knows that you never reach this place. I do occasionally get some criti- cism for glossing over things too much, or not explaining things according to the strictest engineering accuracy. These almost never come from those for whom this book was intended. We hope for a great many of you that this will be your first book on OpenGL and 3D graphics. We hope for none of you that it will be your last. Well, I did make one really “smart” decision about my career once. Once upon a time in the early 1980s, I was a student looking at a computer in an electronics store. The sales- man approached and began making his pitch. I told him I was just learning to program and was considering an Amiga over his model. I was briskly informed that I needed to get serious with a computer that the rest of the world was using. An Amiga, he told me, was not good for anything but “making pretty pictures.” No one, he assured me, could make a living making pretty pictures on his computer. Unfortunately, I listened to this “smart” advice and regretted it for more than ten years. Thank God I finally got stupid.

  As for making a living “making pretty pictures?” Do the math. Oh, and my latest stupid decision? I’ve left Windows and switched to the Mac. Time will tell if my luck holds out.

  —Richard S. Wright, Jr.

  

  I have a confession to make. The first time I ever heard of OpenGL was at the 1992 Win32 Developers Conference in San Francisco. Windows NT 3.1 was in early beta (or late alpha), and many vendors were present, pledging their future support for this exciting new graph- ics technology. Among them was a company called Silicon Graphics, Inc. (SGI). The SGI representatives were showing off their graphics workstations and playing video demos of special effects from some popular movies. Their primary purpose in this booth, however, was to promote a new 3D graphics standard called OpenGL. It was based on SGI’s propri- etary IRIS GL and was fresh out of the box as a graphics standard. Significantly, Microsoft was pledging future support for OpenGL in Windows NT.

  I had to wait until the beta release of NT 3.5 before I got my first personal taste of OpenGL. Those first OpenGL-based screensavers only scratched the surface of what was possible with this graphics API. Like many other people, I struggled through the Microsoft help files and bought a copy of the OpenGL Programming Guide (now called simply “The Red Book” by most). The Red Book was not a primer, however, and it assumed a lot of knowledge that I just didn’t have.

  Now for that confession I promised. How did I learn OpenGL? I learned it by writing a book about it. That’s right, the first edition of the OpenGL SuperBible was me learning how to do 3D graphics myself…with a deadline! Somehow I pulled it off, and in 1996 the first edition of the book you are holding was born. Teaching myself OpenGL from scratch enabled me somehow to better explain the API to others in a manner that a lot of people seemed to like. The whole project was nearly canceled when Waite Group Press was acquired by another publisher halfway through the publishing process. Mitchell Waite stuck to his guns and insisted that OpenGL was going to be “the next big thing” in computer graphics. Vindication arrived when an emergency reprint was required because the first run of the book sold out before ever making it to the warehouse.

  That was a long time ago, and in what seems like a galaxy far, far away…. Only three years later 3D accelerated graphics were a staple for even the most stripped- down PCs. The “API Wars,” a political battle between Microsoft and SGI, had come and gone; OpenGL was firmly established in the PC world; and 3D hardware acceleration was as common as CD-ROMs and sound cards. I had even managed to turn my career more toward an OpenGL orientation and had the privilege of contributing in some small ways to the OpenGL specification for version 1.2 while working at Lockheed-Martin/Real3D.

  The second edition of this book, released at the end of 1999, was significantly expanded and corrected. We even made some modest initial attempts to ensure that all the sample programs were more friendly in non-Windows platforms by using the GLUT framework.