You are on page 1of 910
OpenGL Programming Guide Sixth Edition The Official Guide to Learning OpenGL*, Version 2.1 OpenGL Architecture Review Board Dave Shreiner Mason Woo Jackie Neider Tom Davis vv Addison-Wesley 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: USS. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside of the U'S., please contact: International Sales international@pearsoned.com Visit us on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data OpenGL programming guide : the official guide to learning OpenGL, version 2.1 / OpenGL Architecture Review Board, Dave Shreiner ... [et al.]. — 6th ed. p.cm. Includes index. ISBN-13: 978-0-321-48100-9 (pbk. : alk. paper) ISBN-10: 0-321-48100-3 1. Computer graphics. 2. OpenGL. I. Shreiner, Dave. Il. OpenGL Architecture Review Board. 385.0636 2007 006.6'6—de22 2007023543 Copyright © 2008 by Silicon Graphics, 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 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 ISBN 13: 978-0-321-48100-9 ISBN 10: 0-321-48100-3 Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts. First printing, July 2007 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 and Celeste Fowler. Contents Figures .... Tables .. Examples .... About This Guide .... What This Guide Contains... What's New in This Edition . What You Should Know Before Reading This Guide How to Obtain the Sample Code Nate Robins’ OpenGL Tutors ... Errata., Style Conventions Acknowledgments .. Introduction to OpenGL... What Is OpenGL? .. A Smidgen of OpenGL Code OpenGL Command Syntax OpenGL as a State Machine . OpenGL Rendering Pipeline. Display Lists. Evaluators Per-Vertex Operations. vil viii Primitive Assembly. v2 Pixel Operations. Texture Assembly Rasterization.... Fragment Operations OpenGL-Related Libraries Include Files .. GLUT, the OpenGL Utility Toolki Animation ... The Refresh That Pauses Motion = Redraw + Swap . 2. State Management and Drawing Geometric Objects ... A Drawing Survival Kit. Clearing the Window. Specifying a Color. Forcing Completion of Drawing Coordinate System Survival Kit Describing Points, Lines, and Polygons... What Are Points, Lines, and Polygons? Specifying Vertices . OpenGL Geometric Drawing Primitives. Basic State Management... Displaying Points, Lines, and Polygons Point Detail: Line Details Polygon Details Normal Vectors.. Vertex Arrays Step 1: Enabling Arrays... Step 2: Specifying Data for the Arrays Step 3: Dereferencing and Rendering Interleaved Arrays Buffer Objects... Creating Buffer Objects... Contents Making a Buffer Object Active Allocating and Initializing Buffer Objects with Data Updating Data Values in Buffer Objects . Cleaning Up Buffer Objects . Using Buffer Objects with Vertex-Array Data Attribute Groups Some Hints for Building Polygonal Models of Surfaces An Example: Building an Icosahedron .... Viewing... Overview: The Camera Analogy. A Simple Example: Drawing a Cube .. General-Purpose Transformation Commands... Viewing and Modeling Transformations Thinking about Transformations. Modeling Transformations .. Viewing Transformations. Projection Transformations... Perspective Projection .. Orthographic Projection Viewing Volume Clipping Viewport Transformation. Defining the Viewport ... The Transformed Depth Coordinate. Troubleshooting Transformations Manipulating the Matrix Stacks. The Modelview Matrix Stack. The Projection Matrix Stack. Additional Clipping Planes ... Examples of Composing Several Transformations. Building a Solar System... Building an Articulated Robot Arm Reversing or Mimicking Transformation: Contents 4. Color... Color Perception . Computer Color RGBA versus Color-Index Mode. RGBA Display Mode... Color-Index Display Mode Choosing between RGBA and Color-Index Mode.. Changing between Display Modes Specifying a Color and a Shading Model. Specifying a Color in RGBA Mode.. Specifying a Color in Color-Index Mode.. Specifying a Shading Model. 5. Lighting. A Hidden-Surface Removal Survival Kit Real-World and OpenGL Lighting... Ambient, Diffuse, Specular, and Emissive Light. Material Colors... RGB Values for Lights and Materials . A Simple Example: Rendering a Lit Sphere. Creating Light Sources Color. Position and Attenuation. Spotlights .... Multiple Light: Controlling a Light’s Position and Direction . Selecting a Lighting Model... Global Ambient Light . Local or Infinite Viewpoint Two-Sided Lightin, Secondary Specular Colo! Enabling Lighting Defining Material Properties Diffuse and Ambient Reflection. Specular Reflection... Contents Emission .. Changing Material Properties Color Material Mode... The Mathematics of Lighting Material Emission... Scaled Global Ambient Light Contributions from Light Sources Putting It All Together Secondary Specular Colo: Lighting in Color-Index Mode . The Mathematics of Color-Index Mode Lighting... Blending, Anti sing, Fog, and Polygon Offset. Blending... The Source and De: Enabling Blending Combining Pixels Using Blending Equation Sample Uses of Blending A Blending Example. Three-Dimensional Blending with the Depth Buffer Antialiasing. Antialiasing Points or Lines Antialiasing Geometric Primitives with Multisampling Antialiasing Polygons... Fog Using Fog.. Fog Equations. Point Parameters. Polygon Offset Display Lists... Why Use Display Lists? An Example of Using a Display List Display List Design Philosophy... Contents xi xii Creating and Executing a Display List .. Naming and Creating a Display List. What's Stored in a Display List? Executing a Display List Hierarchical Display List: Managing Display List Indices Executing Multiple Display Lists. Managing State Variables with Display Lists . Encapsulating Mode Changes.. Drawing Pixels, Bitmaps, Fonts, and Images ... Bitmaps and Fonts... The Current Raster Position.. Drawing the Bitmap... Choosing a Color for the Bitmap Fonts and Display Lists . Defining and Using a Complete Font Images. Reading, Writing, and Copying Pixel Data 313 Imaging Pipeline .. Pixel Packing and Unpacking .. Controlling Pixel-Storage Modes Pixel-Transfer Operations .. Pixel Mapping ... Magnifying, Reducing, or Flipping an Image. Reading and Drawing Pixel Rectangles. The Pixel Rectangle Drawing Process Using Buffer Objects with Pixel Rectangle Data Using Buffer Objects to Transfer Pixel Data Using Buffer Objects to Retrieve Pixel Data. Tips for Improving Pixel Drawing Rates . Imaging Subset .. Color Tables Convolutions Contents

You might also like