OpenGL® Programming Guide Seventh Edition OpenGL® Series Visit informit.com/opengl for a complete list of available products 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, three- dimensional 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. 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 [email protected] For sales outside of the U.S., please contact: International Sales [email protected] 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 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 vii 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 viii Contents 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 ofSurfaces.....................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 Contents ix