Interactive Computer Graphics: a Top-Down Approach with Shader
Total Page:16
File Type:pdf, Size:1020Kb
This page intentionally left blank INTERACTIVE COMPUTER GRAPHICS A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL® 6th Edition This page intentionally left blank INTERACTIVE COMPUTER GRAPHICS A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL® 6th Edition EDWARD ANGEL University of New Mexico • DAVE SHREINER ARM, Inc. Editorial Director: Marcia Horton Editor-in-Chief: Michael Hirsch Acquisitions Editor: Matt Goldstein Editorial Assistant: Chelsea Bell Vice President, Marketing: Patrice Jones Marketing Manager: Yezan Alayan Marketing Coordinator: Kathryn Ferranti Vice President, Production: Vince O’Brien Managing Editor: Jeff Holcomb Senior Production Project Manager: Marilyn Lloyd Senior Operations Supervisor: Alan Fischer Operations Specialist: Lisa McDowell Text Designer: Beth Paquin Cover Designer: Central Covers Cover Art: Hue Walker, Fulldome Project, University of New Mexico Media Editor: Daniel Sandin Media Project Manager: Wanda Rockwell Full-Service Project Management: Coventry Composition Composition: Coventry Composition, using ZzTEX Printer/Binder: Edwards Brothers Cover and Insert Printer: Lehigh-Phoenix Color Text Font: Minion Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on appropriate page within text. Copyright © 2012, 2009, 2006, 2003, 2000 Pearson Education, Inc., publishing as Addison- Wesley. All rights reserved. Manufactured in the United States of America. This publication is protected by Copyright, and permission should 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. To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 501 Boylston Street, Suite 900, Boston, Massachusetts 02116. Many of the designations 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 in initial caps or all caps. Library of Congress Cataloging-in-Publication Data Angel, Edward. Interactivecomputergraphics:atop-downapproachwithshader-based OpenGL / Edward Angel, David Shreiner. — 6th ed. p. cm. Includes bibliographical references and index. ISBN-13: 978-0-13-254523-5 (alk. paper) ISBN-10: 0-13-254523-3 (alk. paper) 1. Computer graphics. 2. OpenGL. I. Shreiner, Dave. II. Title. T385.A5133 2012 006.6—dc22 2011004742 10987654321—EB—15 14 13 12 11 Addison-Wesley is an imprint of ISBN 10: 0-13-254523-3 ISBN 13: 978-0-13-254523-5 To Rose Mary —E.A. To Vicki, Bonnie, Bob, and Phantom —D.S. This page intentionally left blank CONTENTS Preface xxi CHAPTER 1 GRAPHICS SYSTEMS AND MODELS 1 1.1 Applications of Computer Graphics 2 1.1.1 Display of Information 2 1.1.2 Design 3 1.1.3 Simulation and Animation 3 1.1.4 User Interfaces 4 1.2 A Graphics System 5 1.2.1 Pixels and the Frame Buffer 5 1.2.2 The CPU and the GPU 6 1.2.3 Output Devices 7 1.2.4 Input Devices 9 1.2.5 Physical Input Devices 10 1.2.6 Logical Devices 12 1.2.7 Input Modes 13 1.3 Images: Physical and Synthetic 15 1.3.1 Objects and Viewers 15 1.3.2 Light and Images 16 1.3.3 Imaging Models 18 1.4 Imaging Systems 20 1.4.1 The Pinhole Camera 20 1.4.2 The Human Visual System 22 1.5 The Synthetic-Camera Model 23 1.6 The Programmer’s Interface 25 1.6.1 The Pen-Plotter Model 27 1.6.2 Three-Dimensional APIs 28 1.6.3 A Sequence of Images 31 1.6.4 The Modeling–Rendering Paradigm 32 1.7 Graphics Architectures 33 1.7.1 Display Processors 34 1.7.2 Pipeline Architectures 34 vii viii Contents 1.7.3 The Graphics Pipeline 35 1.7.4 Vertex Processing 36 1.7.5 Clipping and Primitive Assembly 36 1.7.6 Rasterization 37 1.7.7 Fragment Processing 37 1.8 Programmable Pipelines 37 1.9 Performance Characteristics 38 Summary and Notes 39 Suggested Readings 40 Exercises 41 CHAPTER 2 GRAPHICS PROGRAMMING 43 2.1 The Sierpinski Gasket 43 2.2 Programming Two-Dimensional Applications 46 2.3 The OpenGL Application Programming Interface 50 2.3.1 Graphics Functions 51 2.3.2 The Graphics Pipeline and State Machines 53 2.3.3 The OpenGL Interface 53 2.3.4 Coordinate Systems 55 2.4 Primitives and Attributes 56 2.4.1 Polygon Basics 58 2.4.2 Polygons in OpenGL 59 2.4.3 Approximating a Sphere 60 2.4.4 Triangulation 62 2.4.5 Text 64 2.4.6 Curved Objects 65 2.4.7 Attributes 65 2.5 Color 67 2.5.1 RGB Color 69 2.5.2 Indexed Color 71 2.5.3 Setting of Color Attributes 72 2.6 Viewing 73 2.6.1 The Orthographic View 74 2.6.2 Two-Dimensional Viewing 77 2.7 Control Functions 78 2.7.1 Interaction with the Window System 78 2.7.2 Aspect Ratio and Viewports 79 2.7.3 The main, display, and init Functions 80 2.7.4 Program Structure 83 Contents ix 2.8 The Gasket Program 83 2.8.1 Rendering the Points 84 2.8.2 The Vertex Shader 85 2.8.3 The Fragment Shader 86 2.8.4 Combining the Parts 86 2.8.5 The initShader Function 86 2.9 Polygons and Recursion 88 2.10 The Three-Dimensional Gasket 91 2.10.1 Use of Three-Dimensional Points 91 2.10.2 Use of Polygons in Three Dimensions 92 2.10.3 Hidden-Surface Removal 96 2.11 Adding Interaction 98 2.11.1 Using the Pointing Device 98 2.11.2 Window Events 101 2.11.3 Keyboard Events 102 2.11.4 The Idle Callback 103 2.11.5 Double Buffering 105 2.11.6 Window Management 106 2.12 Menus 106 Summary and Notes 108 Suggested Readings 109 Exercises 110 CHAPTER 3 GEOMETRIC OBJECTS AND TRANSFORMATIONS 115 3.1 Scalars, Points, and Vectors 116 3.1.1 Geometric Objects 116 3.1.2 Coordinate-Free Geometry 117 3.1.3 The Mathematical View: Vector and Affine Spaces 118 3.1.4 The Computer Science View 119 3.1.5 Geometric ADTs 119 3.1.6 Lines 120 3.1.7 Affine Sums 121 3.1.8 Convexity 122 3.1.9 Dot and Cross Products 122 3.1.10 Planes 123 3.2 Three-Dimensional Primitives 125 3.3 Coordinate Systems and Frames 126 3.3.1 Representations and N-Tuples 128 3.3.2 Change of Coordinate Systems 129 3.3.3 Example Change of Representation 132 x Contents 3.3.4 Homogeneous Coordinates 133 3.3.5 Example Change in Frames 136 3.3.6 Working with Representations 137 3.4 Frames in OpenGL 139 3.5 Matrix and Vector Classes 144 3.6 Modeling a Colored Cube 146 3.6.1 Modeling the Faces 146 3.6.2 Inward- and Outward-Pointing Faces 146 3.6.3 Data Structures for Object Representation 147 3.6.4 The Color Cube 148 3.6.5 Interpolation 150 3.6.6 Displaying the Cube 151 3.7 Affine Transformations 152 3.8 Translation, Rotation, and Scaling 155 3.8.1 Translation 155 3.8.2 Rotation 156 3.8.3 Scaling 158 3.9 Transformations in Homogeneous Coordinates 159 3.9.1 Translation 160 3.9.2 Scaling 161 3.9.3 Rotation 162 3.9.4 Shear 163 3.10 Concatenation of Transformations 164 3.10.1 Rotation About a Fixed Point 165 3.10.2 General Rotation 167 3.10.3 The Instance Transformation 168 3.10.4 Rotation About an Arbitrary Axis 169 3.11 Transformation Matrices in OpenGL 172 3.11.1 Current Transformation Matrices 173 3.11.2 Rotation, Translation, and Scaling 174 3.11.3 Rotation About a Fixed Point 175 3.11.4 Order of Transformations 176 3.12 Spinning of the Cube 176 3.12.1 Updating in the Display Callback 177 3.12.2 Uniform Variables 178 3.13 Interfaces to Three-Dimensional Applications 180 3.13.1 Using Areas of the Screen 180 3.13.2 A Virtual Trackball 181 3.13.3 Smooth Rotations 184 3.13.4 Incremental Rotation 185 Contents xi 3.14 Quaternions 186 3.14.1 Complex Numbers and Quaternions 186 3.14.2 Quaternions and Rotation 187 Summary and Notes 190 Suggested Readings 190 Exercises 191 CHAPTER 4 VIEWING 195 4.1 Classical and Computer Viewing 195 4.1.1 Classical Viewing 197 4.1.2 Orthographic Projections 197 4.1.3 Axonometric Projections 198 4.1.4 Oblique Projections 200 4.1.5 Perspective Viewing 201 4.2 Viewing with a Computer 202 4.3 Positioning of the Camera 204 4.3.1 Positioning of the Camera Frame 204 4.3.2 Two Viewing APIs 209 4.3.3 The Look-At Function 212 4.3.4 Other Viewing APIs 214 4.4 Parallel Projections 215 4.4.1 Orthogonal Projections 215 4.4.2 Parallel Viewing with OpenGL 216 4.4.3 Projection Normalization 217 4.4.4 Orthogonal-Projection Matrices 219 4.4.5 Oblique Projections 220 4.4.6 An Interactive Viewer 224 4.5 Perspective Projections 226 4.5.1 Simple Perspective Projections 226 4.6 Perspective Projections with OpenGL 229 4.6.1 Perspective Functions 230 4.7 Perspective-Projection Matrices 232 4.7.1 Perspective Normalization 232 4.7.2 OpenGL Perspective Transformations 236 4.7.3 Perspective Example 238 4.8 Hidden-Surface Removal 238 4.8.1 Culling 241 4.9 Displaying Meshes 241 4.9.1 Displaying Meshes as a Surface 244 xii Contents 4.9.2 Polygon Offset 246 4.9.3 Walking Through a Scene 247 4.10 Projections and Shadows 249 Summary and Notes 253 Suggested Readings 254 Exercises 254 CHAPTER 5 LIGHTING AND SHADING 257 5.1 Light and Matter 258 5.2 Light Sources 261 5.2.1 Color Sources 262 5.2.2 Ambient Light 262 5.2.3 Point Sources 263 5.2.4 Spotlights 264 5.2.5 Distant Light Sources 264 5.3 The Phong Reflection Model 265 5.3.1 Ambient Reflection 267 5.3.2 Diffuse Reflection 267 5.3.3 Specular Reflection 269 5.3.4 The Modified Phong Model 270 5.4 Computation of Vectors 271 5.4.1 Normal Vectors 272 5.4.2 Angle of Reflection 274 5.5 Polygonal Shading 275 5.5.1 Flat Shading 276 5.5.2 Smooth and Gouraud Shading 277 5.5.3 Phong Shading 279 5.6 Approximation of a Sphere by Recursive Subdivision 280 5.7 Specifying Lighting Parameters 283 5.7.1 Light Sources 283 5.7.2 Materials 284 5.8 Implementing a Lighting Model 286 5.8.1 Applying