Advanced Graphics Programming Using OpenGL TOM McREYNOLDS DAVID BLYTHE AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO MORGAN KAUFMANN PUBLISHERS IS AN IMPRINT OF ELSEVIER TEAM LinG Advanced Graphics Programming Using OpenGL TEAM LinG The Morgan Kaufmann Series in Computer Graphics and Geometric Modeling Series Editor: Brian A. Barsky University of California, Berkeley This series publishes the finest works for the accomplished and aspiring graphics professional. The series includes intermediate and advanced textbooks, graphics programming books, surveys of important new areas and methods, and reference works. Advanced Graphics Programming Using OpenGL Tom McReynolds and David Blythe Digital Geometry Geometric Methods for Digital Picture Analysis Rienhard Klette and Azriel Rosenfeld Digital Video and HDTV Algorithms and Interfaces Charles Poynton Real-Time Shader Programming Ron Fosner Complete Maya Programming: An Extensive Guide to MEL and the C++ API David Gould MEL Scripting for Maya Animators Mark R. Wilkins and Chris Kazmier Digital Video and HDTV Algorithms and Interfaces Charles Poynton Texturing & Modeling: A Procedural Approach, Third Edition David S. Ebert, F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley Geometric Tools for Computer Graphics Philip Schneider and David Eberly Understanding Virtual Reality: Interface, Application, and Design William Sherman and Alan Craig Jim Blinn’s Corner: Notation, Notation, Notation Jim Blinn Level of Detail for 3D Graphics: David Luebke, Martin Reddy, Jonathan D. Cohen, Amitabh Varshney, Benjamin Watson, and Robert Huebner Pyramid Algorithms: A Dynamic Programming Approach to Curves and Surfaces for Geometric Modeling Ron Goldman Non-Photorealistic Computer Graphics: Modeling, Rendering, and Animation Thomas Strothotte and Stefan Schlechtweg Curves and Surfaces for CAGD: A Practical Guide, Fifth Edition Gerald Farin Subdivision Methods for Geometric Design: A Constructive Approach Joe Warren and Henrik Weimer Computer Animation: Algorithms and Techniques Rick Parent The Computer Animator’s Technical Handbook Lynn Pocock and Judson Rosebush Advanced RenderMan: Creating CGI for Motion Pictures Anthony A. Apodaca and Larry Gritz Curves and Surfaces in Geometric Modeling: Theory and Algorithms Jean Gallier Andrew Glassner’s Notebook: Recreational Computer Graphics Andrew S. Glassner Warping and Morphing of Graphical Objects Jonas Gomes, Lucia Darsa, Bruno Costa, and Luiz Velho Jim Blinn’s Corner: Dirty Pixels Jim Blinn Rendering with Radiance: The Art and Science of Lighting Visualization Greg Ward Larson and Rob Shakespeare Introduction to Implicit Surfaces Edited by Jules Bloomenthal Jim Blinn’s Corner: A Trip Down the Graphics Pipeline Jim Blinn Interactive Curves and Surfaces: A Multimedia Tutorial on CAGD Alyn Rockwood and Peter Chambers Wavelets for Computer Graphics: Theory and Applications Eric J. Stollnitz, Tony D. DeRose, and David H. Salesin Principles of Digital Image Synthesis Andrew S. Glassner Radiosity & Global Illumination François X. Sillion and Claude Puech Knotty: A B-Spline Visualization Program Jonathan Yen User Interface Management Systems: Models and Algorithms Dan R. Olsen, Jr. Making Them Move: Mechanics, Control, and Animation of Articulated Figures Edited by Norman I. Badler, Brian A. Barsky, and David Zeltzer Geometric and Solid Modeling: An Introduction Christoph M. Hoffmann An Introduction to Splines for Use in Computer Graphics and Geometric Modeling Richard H. Bartels, John C. Beatty, and Brian A. Barsky TEAM LinG Publishing Director: Diane Cerra Publishing Services Manager: Simon Crump Project Manager: Brandy Lilly Editorial Coordinator: Mona Buehler Cover Design: Dutton & Sherman Design Text Design: Julio Esperas Composition: Cepha Imaging Pvt. Ltd. Illustrations: Dartmouth Publishing, Inc. Copyeditor: Daril Bentley; Graphic World Proofreader: Graphic World Indexer: Graphic World Interior printer: China Translation & Printing Services, Ltd. Cover printer: China Tranalation & Printing Services, Ltd. Morgan Kaufmann Publishers is an imprint of Elsevier. 500 Sansome Street, Suite 400, San Francisco, CA 94111 This book is printed on acid-free paper. © 2005 by Elsevier Inc. All rights reserved. Designations used by companies to distinguish their products are often claimed as trademarks or registered trademarks. In all instances in which Morgan Kaufmann Publishers is aware of a claim, the product names appear in initial capital or all capital letters. Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration. 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, scanning, or otherwise—without prior written permission of the publisher. Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, e-mail: permissions@elsevier.com.uk. You may also complete your request on-line via the Elsevier homepage (http://elsevier.com) by selecting “Customer Support” and then “Obtaining Permissions.” Library of Congress Cataloging-in-Publication Data Application Submitted ISBN: 1-55860-659-9 For information on all Morgan Kaufmann publications, visit our Web site at www.mkp.com or www.books.elsevier.com Printed in China 10987654321 TEAM LinG To my friends and colleaguesfrom Silicon Graphics; it was a fabulous time and place to learn about 3D graphics. –DB To Ede Forney and HarryMcGinnis; you were there when it counted. – TM TEAM LinG Contents Preface xxiii Acknowledgments xxvii Biographies xxviii PART I Concepts 1 CHAPTER 1 Geometry Representation and Modeling 3 1.1 Polygonal Representation 3 1.2 Decomposition and Tessellation 4 1.3 Shading Normals 8 1.3.1 Smooth Shading 9 1.3.2 Vertex Winding Order 11 1.4 Triangle Stripping 12 1.4.1 Greedy Tri-stripping 13 1.5 Vertices and Vertex Arrays 14 1.5.1 Vertex Buffer Objects 15 1.5.2 Triangle Lists 16 1.6 Modeling vs. Rendering Revisited 17 vi TEAM LinG Contents vii CHAPTER 2 3D Transformations 19 2.1 Data Representation 19 2.2 Overview of the Transformation Pipeline 20 2.2.1 Object Space and the Modelview Transform 20 2.2.2 Eye Space and Projection Transform 21 2.2.3 Clip Space and Perspective Divide 22 2.2.4 NDC Space and the Viewport Transform 22 2.2.5 Window Space 23 2.3 Normal Transformation 23 2.4 Texture Coordinate Generation and Transformation 25 2.4.1 Texture Matrix 25 2.4.2 Texture Coordinate Generation 25 2.5 Modeling Transforms 27 2.6 Visualizing Transform Sequences 28 2.7 Projection Transform 30 2.8 The Z Coordinate and Perspective Projection 30 2.8.1 Z Coordinates and Fog 32 2.9 Vertex Programs 32 2.10 Summary 34 CHAPTER 3 Color, Shading, and Lighting 35 3.1 Representing Color 35 3.1.1 Resolution and Dynamic Range 36 3.1.2 Gamma 37 3.1.3 Alpha 39 3.1.4 Color Index 39 3.2 Shading 40 3.3 Lighting 43 3.3.1 Intensities, Colors, and Materials 46 3.3.2 Light Source Properties 47 TEAM LinG viii Contents 3.3.3 Material Properties 49 3.3.4 Vertex and Fragment Lighting 50 3.4 Fixed-Point and Floating-Point Arithmetic 53 3.4.1 Biased Arithmetic 54 3.5 Summary 56 CHAPTER 4 Digital Images and Image Manipulation 57 4.1 Image Representation 57 4.2 Digital Filtering 60 4.3 Convolution 62 4.4 Images in OpenGL 63 4.5 Positioning Images 65 4.6 Pixel Store Operations 65 4.7 Pixel Transfer Operations 67 4.7.1 Scale and Bias 67 4.7.2 Pixel Mapping Operations 67 4.8 ARB Imaging Subset 68 4.8.1 Convolution 68 4.8.2 Color Matrix Transform 68 4.8.3 Histogram 69 4.8.4 MinMax 70 4.8.5 Color Tables 70 4.8.6 Blend Equation and Constant Color Blending 71 4.9 Off-Screen Processing 72 4.10 Summary 72 CHAPTER 5 Texture Mapping 73 5.1 Loading Texture Images 73 5.1.1 Texture Borders 74 5.1.2 Internal Texture Formats 75 TEAM LinG Contents ix 5.1.3 Compressed Textures 76 5.1.4 Proxy Textures 77 5.2 Texture Coordinates 77 5.2.1 Texture Coordinate Generation and Transformation 79 5.3 Loading Texture Images from the Frame Buffer 79 5.4 Environment Mapping 80 5.4.1 Generating Environment Map Texture Coordinates 81 5.4.2 Texture Maps Used in Environment Mapping 82 5.4.3 Cube Mapping 83 5.4.4 Sphere Mapping 85 5.5 3D Texture 88 5.5.1 Using 3D Textures to Render Solid Materials 89 5.6 Filtering 90 5.7 Additional Control of Texture Level of Detail 91 5.8 Texture Objects 93 5.9 Multitexture 95 5.9.1 Multitexture Model 96 5.9.2 Multitexture Texture Environments 97 5.10 Texture Environment 98 5.10.1 Advanced Texture Environment Functionality 99 5.10.2 Fragment Programs 100 5.11 Summary 102 CHAPTER 6 Rasterization and Fragment Processing 103 6.1 Rasterization 104 6.1.1 Rasterization Consistency 105 6.1.2 Z-Fighting 105 6.1.3 Bitmaps and Pixel Rectangles 107 6.1.4 Texture, Color, and Depth Interpolation 108 6.1.5 w Buffering 109 6.2 Fragment Operations 110 6.2.1 Multisample Operations 111 6.2.2 Alpha Test 111 6.2.3 Stencil Test 111 TEAM LinG x Contents 6.2.4 Blending 112 6.2.5 Logic Op 114 6.3 Framebuffer Operations 115 6.3.1 Accumulation Buffer 116 6.4 Summary 117 CHAPTER 7 Window System and Platform Integration 119 7.1 Renderer and Window State 120 7.2 Address Space and Threads 121 7.3 Anatomy of a Window 122 7.3.1 Overlay and Underlay Windows 122 7.3.2 Multiple Displays 123 7.4 Off-Screen Rendering 124 7.4.1 GLX Pbuffers 125 7.4.2 WGL Pbuffers 126 7.5 Rendering to Texture Maps 126 7.6 Direct and Indirect Rendering 127 CHAPTER 8 OpenGL Implementations 129 8.1 OpenGL Versions 129 8.2 OpenGL Extensions 131 8.3 OpenGL ES for Embedded Systems 131 8.3.1 Embedded Profiles 132 8.3.2 Common and Common-Lite Profiles 133 8.3.3 Safety Critical Profile 136 8.3.4 OpenGL ES Revisions 136 8.4 OpenGL Pipeline Evolution 137 8.5 Hardware Implementations of the Pipeline 138 8.5.1 Rasterization Acceleration 138 8.5.2 Primitive Setup Acceleration 141 8.5.3 Transform and Lighting Acceleration 141 8.5.4 Pipeline Balance 142 TEAM LinG [...]... generally the following meanings hold: – N - normal vector – L - light vector TEAM LinG Preface xxvi – R - reflection vector – T - tangent vector – B - binormal vector – s, t, r, q - texture coordinates – x, y, z, w - vertex coordinates – θ, ϕ - spherical coordinate angles – RGBA - red, green, blue, and alpha components – I - intensity – C - color (usually RGB or RGBA) – V - length of vector V – [n, m] a number... implementations of the OpenGL graphics library, OpenGL extension specifications, and high-level toolkits built on top of OpenGL David’s other industry experience includes embedded and system-on-a-chip design, mobile devices, and wireless networking David is currently a graphics architect in the Windows Graphics and Gaming Technologies division at Microsoft working on DirectX and OpenGL graphics technologies... Interleaving 591 21.3.1 21.3.2 21.3.3 21.3.4 21.3.5 21.4 xxi Summary 592 APPENDIX A Using OpenGL Extensions 593 A.1 How OpenGL Extensions are Documented A.2 Finding OpenGL Extension Specifications A.3 How to Read an OpenGL Extension Specification A.3.1 ARB Extensions 593 594 594 598 A.4 Portable Use of OpenGL Extensions 599 A.5 Using Extension Function Pointers 602 APPENDIX B Equations B.1 3D Vectors B.1.1... space OpenGL runs on a wide range of graphics hardware; from “big iron” compute clusters, to OpenGL ES, which is designed to provide 3D graphics on embedded devices as small as a cell phone Given the broad applicability, scalability, and wide availability, OpenGL is an easy choice as the basis for describing graphics algorithms However, even if you don’t use OpenGL, the graphics APIs in common use are conceptually... to include full blown example programs for most algorithms This example code is available for internet download from www.mkp.com /opengl Conventions We use conventions and terminology similar to that found in the OpenGL specification and in the “red-blue-green-white” series of OpenGL books In addition, we use the following conventions: • Equations involving matrices, vectors, and points use single uppercase... We also note that the computer graphics field has a lot of competing notation and vocabulary We have tried to be consistent with terminology and notation used in the OpenGL specification and the “standard” OpenGL books while at the same time providing some mention of alternative terminology when it is relevent TEAM LinG xxiii xxiv Preface OpenGL We chose OpenGL as our base graphics language for a number... including the end points – A · B - inner product of vectors A and B – A – A × B - cross product of vectors A and B B - max{0, A · B} – the clamped inner product TEAM LinG Acknowledgments This book reflects a significant part of our collective experience in working with OpenGL applications for the past 13 years While the book couldn’t possibly cover everything known about using OpenGL, we are pleased to provide... Blythe has worked in the 3D graphics field professionally for the last 14 years, including serving as Chief Engineer at Silicon Graphics, a representative on the OpenGL Architecture Review Board, editor for the OpenGL ES 1.0 specification, and a frequent SIGGRAPH course presenter While at Silicon Graphics, David contributed to the development of the RealityEngine and InfiniteReality graphics systems He has... Hardware Support for Texture Paging 286 Prefiltered Textures 14.7.1 287 Computing Texel Aspect Ratios 288 TEAM LinG Contents 14.8 xv Dual-Paraboloid Environment Mapping 14.8.1 14.8.2 14.8.3 291 The Mathematics of Dual-Paraboloid Maps Using Dual-Paraboloid Maps 294 OpenGL Dual-Paraboloid Support 296 14.9 Texture Projection 14.10 Texture Color Coding and Contouring 14.11 2D Image Warping 300 14.12 Texture... you some graphics, especially areas that are sometimes underrepresented in graphics texts But it also goes further, showing you how to apply those techniques in real world applications, filling real world needs Since there are already a number of excellent books that provide an introduction to computer graphics (Foley, 1994; Watt, 1989; Rogers, 1997; Angel, 1997; Newman, 1973) and to OpenGL programming . SINGAPORE • SYDNEY • TOKYO MORGAN KAUFMANN PUBLISHERS IS AN IMPRINT OF ELSEVIER TEAM LinG Advanced Graphics Programming Using OpenGL TEAM LinG The Morgan Kaufmann Series in Computer Graphics and Geometric. “Obtaining Permissions.” Library of Congress Cataloging-in-Publication Data Application Submitted ISBN: 1-5 586 0-6 5 9-9 For information on all Morgan Kaufmann publications, visit our Web site at www.mkp.com. aspiring graphics professional. The series includes intermediate and advanced textbooks, graphics programming books, surveys of important new areas and methods, and reference works. Advanced Graphics