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 AddisonWesley. 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.
Interactive computer graphics : a top-down approach with shader-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
10 9 8 7 6 5 4 3 2 1—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
CONTE NTS
Preface
xxi
CHAPTER 1
GRAPHICS SYSTEMS AND MODELS
1
1.1
Applications of Computer Graphics
1.1.1
1.1.2
1.1.3
1.1.4
Display of Information
2
Design
3
Simulation and Animation
User Interfaces
4
2
1.2
A Graphics System
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
Pixels and the Frame Buffer
The CPU and the GPU
6
Output Devices
7
Input Devices
9
Physical Input Devices
10
Logical Devices
12
Input Modes
13
1.3
Images: Physical and Synthetic
1.3.1
1.3.2
1.3.3
Objects and Viewers
15
Light and Images
16
Imaging Models
18
1.4
Imaging Systems
1.4.1
1.4.2
The Pinhole Camera
20
The Human Visual System
1.5
The Synthetic-Camera Model
23
1.6
The Programmer’s Interface
25
1.6.1
1.6.2
1.6.3
1.6.4
The Pen-Plotter Model
27
Three-Dimensional APIs
28
A Sequence of Images
31
The Modeling–Rendering Paradigm
1.7
Graphics Architectures
1.7.1
1.7.2
Display Processors
34
Pipeline Architectures
34
3
5
5
15
20
22
32
33
vii
viii
Contents
1.7.3
1.7.4
1.7.5
1.7.6
1.7.7
The Graphics Pipeline
35
Vertex Processing
36
Clipping and Primitive Assembly
Rasterization 37
Fragment Processing 37
1.8
Programmable Pipelines
37
1.9
Performance Characteristics
38
Summary and Notes
39
Suggested Readings
40
Exercises
CHAPTER 2
36
41
GRAPHICS PROGRAMMING
43
2.1
The Sierpinski Gasket
43
2.2
Programming Two-Dimensional Applications
46
50
2.3
The OpenGL Application Programming Interface
2.3.1
2.3.2
2.3.3
2.3.4
Graphics Functions
51
The Graphics Pipeline and State Machines
The OpenGL Interface
53
Coordinate Systems 55
2.4
Primitives and Attributes
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.4.7
Polygon Basics 58
Polygons in OpenGL 59
Approximating a Sphere 60
Triangulation
62
Text
64
Curved Objects
65
Attributes 65
2.5
Color
2.5.1
2.5.2
2.5.3
RGB Color
69
Indexed Color 71
Setting of Color Attributes
2.6
Viewing
2.6.1
2.6.2
The Orthographic View 74
Two-Dimensional Viewing
77
2.7
Control Functions
2.7.1
2.7.2
2.7.3
2.7.4
Interaction with the Window System
78
Aspect Ratio and Viewports
79
The main, display, and init Functions 80
Program Structure 83
53
56
67
72
73
78
Contents
2.8
The Gasket Program
83
2.8.1
2.8.2
2.8.3
2.8.4
2.8.5
Rendering the Points
84
The Vertex Shader
85
The Fragment Shader
86
Combining the Parts
86
86
The initShader Function
2.9
2.10
Polygons and Recursion
The Three-Dimensional Gasket
2.10.1
2.10.2
2.10.3
Use of Three-Dimensional Points
91
Use of Polygons in Three Dimensions
92
Hidden-Surface Removal
96
2.11
Adding Interaction
2.11.1
2.11.2
2.11.3
2.11.4
2.11.5
2.11.6
Using the Pointing Device
98
Window Events
101
Keyboard Events
102
The Idle Callback
103
Double Buffering
105
Window Management
106
2.12
Menus
98
106
Summary and Notes
108
Suggested Readings
109
Exercises
CHAPTER 3
88
91
110
GEOMETRIC OBJECTS AND TRANSFORMATIONS
3.1
Scalars, Points, and Vectors
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.1.6
3.1.7
3.1.8
3.1.9
3.1.10
Geometric Objects
116
Coordinate-Free Geometry
117
The Mathematical View: Vector and Affine Spaces
The Computer Science View
119
Geometric ADTs
119
Lines
120
Affine Sums
121
Convexity
122
Dot and Cross Products
122
Planes
123
3.2
3.3
Three-Dimensional Primitives
Coordinate Systems and Frames
3.3.1
3.3.2
3.3.3
Representations and N-Tuples 128
Change of Coordinate Systems
129
Example Change of Representation
132
115
116
118
125
126
ix
x
Contents
3.3.4
3.3.5
3.3.6
Homogeneous Coordinates
133
Example Change in Frames
136
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
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
Modeling the Faces 146
Inward- and Outward-Pointing Faces 146
Data Structures for Object Representation
147
The Color Cube 148
Interpolation
150
Displaying the Cube
151
3.7
Affine Transformations
152
3.8
Translation, Rotation, and Scaling
155
3.8.1
3.8.2
3.8.3
Translation 155
Rotation
156
Scaling 158
3.9
Transformations in Homogeneous Coordinates
3.9.1
3.9.2
3.9.3
3.9.4
Translation 160
Scaling 161
Rotation
162
Shear 163
3.10
Concatenation of Transformations
3.10.1
3.10.2
3.10.3
3.10.4
Rotation About a Fixed Point 165
General Rotation
167
The Instance Transformation 168
Rotation About an Arbitrary Axis 169
3.11
Transformation Matrices in OpenGL
3.11.1
3.11.2
3.11.3
3.11.4
Current Transformation Matrices
173
Rotation, Translation, and Scaling 174
Rotation About a Fixed Point 175
Order of Transformations
176
3.12
Spinning of the Cube
3.12.1
3.12.2
Updating in the Display Callback
Uniform Variables 178
3.13
Interfaces to Three-Dimensional Applications
3.13.1
3.13.2
3.13.3
3.13.4
Using Areas of the Screen 180
A Virtual Trackball
181
Smooth Rotations
184
Incremental Rotation
185
159
164
172
176
177
180
Contents
3.14
Quaternions
186
3.14.1
3.14.2
Complex Numbers and Quaternions
Quaternions and Rotation
187
Summary and Notes
190
Suggested Readings
190
Exercises
CHAPTER 4
186
191
VIEWING
195
4.1
Classical and Computer Viewing
4.1.1
4.1.2
4.1.3
4.1.4
4.1.5
Classical Viewing
197
Orthographic Projections
197
Axonometric Projections
198
Oblique Projections
200
Perspective Viewing
201
195
4.2
Viewing with a Computer
4.3
Positioning of the Camera
4.3.1
4.3.2
4.3.3
4.3.4
Positioning of the Camera Frame
Two Viewing APIs
209
The Look-At Function
212
Other Viewing APIs
214
4.4
Parallel Projections
4.4.1
4.4.2
4.4.3
4.4.4
4.4.5
4.4.6
Orthogonal Projections
215
Parallel Viewing with OpenGL
216
Projection Normalization
217
Orthogonal-Projection Matrices
219
Oblique Projections
220
An Interactive Viewer
224
4.5
Perspective Projections
4.5.1
Simple Perspective Projections
4.6
Perspective Projections with OpenGL
4.6.1
Perspective Functions
4.7
Perspective-Projection Matrices
4.7.1
4.7.2
4.7.3
Perspective Normalization
232
OpenGL Perspective Transformations
Perspective Example
238
4.8
Hidden-Surface Removal
4.8.1
Culling
4.9
Displaying Meshes
4.9.1
Displaying Meshes as a Surface
202
204
204
215
226
226
229
230
232
236
238
241
241
244
xi
xii
Contents
4.9.2
4.9.3
Polygon Offset 246
Walking Through a Scene
4.10
Projections and Shadows
Summary and Notes
253
Suggested Readings
254
Exercises
CHAPTER 5
5.1
247
249
254
LIGHTING AND SHADING
257
Light and Matter
258
261
5.2
Light Sources
5.2.1
5.2.2
5.2.3
5.2.4
5.2.5
Color Sources
262
Ambient Light
262
Point Sources 263
Spotlights
264
Distant Light Sources
5.3
The Phong Reflection Model
5.3.1
5.3.2
5.3.3
5.3.4
Ambient Reflection 267
Diffuse Reflection
267
Specular Reflection 269
The Modified Phong Model
5.4
Computation of Vectors
5.4.1
5.4.2
Normal Vectors 272
Angle of Reflection
274
5.5
Polygonal Shading
5.5.1
5.5.2
5.5.3
Flat Shading 276
Smooth and Gouraud Shading
Phong Shading
279
5.6
Approximation of a Sphere by Recursive Subdivision
280
5.7
Specifying Lighting Parameters
283
5.7.1
5.7.2
Light Sources 283
Materials 284
5.8
Implementing a Lighting Model
5.8.1
5.8.2
5.8.3
Applying the Lighting Model in the Application
Efficiency
289
Lighting in the Vertex Shader
290
5.9
Shading of the Sphere Model
294
5.10
Per-Fragment Lighting
295
5.10.1
Nonphotorealistic Shading
264
265
270
271
275
297
277
286
286
Contents
5.11
Global Illumination
297
Summary and Notes
299
Suggested Readings
300
Exercises
CHAPTER 6
300
FROM VERTICES TO FRAGMENTS
303
6.1
Basic Implementation Strategies
304
6.2
Four Major Tasks
306
6.2.1
6.2.2
6.2.3
6.2.4
Modeling
306
Geometry Processing
Rasterization
308
Fragment Processing
6.3
Clipping
310
6.4
Line-Segment Clipping
310
6.4.1
6.4.2
Cohen-Sutherland Clipping
310
Liang-Barsky Clipping
313
6.5
Polygon Clipping
314
6.6
Clipping of Other Primitives
317
6.6.1
6.6.2
6.6.3
Bounding Boxes and Volumes
318
Curves, Surfaces, and Text
319
Clipping in the Frame Buffer
319
6.7
Clipping in Three Dimensions
319
6.8
Rasterization
323
6.9
Bresenham’s Algorithm
325
6.10
Polygon Rasterization
327
6.10.1
6.10.2
6.10.3
6.10.4
6.10.5
Inside–Outside Testing
327
OpenGL and Concave Polygons
Fill and Sort
329
Flood Fill
330
Singularities
330
6.11
Hidden-Surface Removal
6.11.1
6.11.2
6.11.3
6.11.4
6.11.5
6.11.6
6.11.7
Object-Space and Image-Space Approaches
331
Sorting and Hidden-Surface Removal 332
Scanline Algorithms
333
Back-Face Removal
334
The z-Buffer Algorithm
335
Scan Conversion with the z-Buffer 338
Depth Sort and the Painter’s Algorithm
340
6.12
Antialiasing
307
309
329
331
342
xiii
xiv
Contents
6.13
Display Considerations
6.13.1
6.13.2
6.13.3
6.13.4
Color Systems 345
The Color Matrix
348
Gamma Correction
349
Dithering and Halftoning 349
Summary and Notes
350
Suggested Readings
352
Exercises
CHAPTER 7
344
352
DISCRETE TECHNIQUES
357
7.1
Buffers
357
7.2
Digital Images
359
7.3
Writing into Buffers
362
7.3.1
7.3.2
Writing Modes
363
Writing with XOR 365
7.4
Mapping Methods
366
7.5
Texture Mapping
368
7.5.1
Two-Dimensional Texture Mapping
7.6
Texture Mapping in OpenGL
368
7.6.1
7.6.2
7.6.3
7.6.4
7.6.5
7.6.6
7.6.7
Two-Dimensional Texture Mapping
Texture Objects
375
The Texture Array 376
Texture Coordinates and Samplers
Texture Sampling
382
Working with Texture Coordinates
Multitexturing
386
7.7
Texture Generation
387
7.8
Environment Maps
388
7.9
Reflection Map Example
393
7.10
Bump Mapping
396
7.10.1
7.10.2
Finding Bump Maps
Bump Map Example
7.11
Compositing Techniques
7.11.1
7.11.2
7.11.3
7.11.4
7.11.5
7.11.6
Opacity and Blending 404
Image Compositing 406
Blending and Compositing in OpenGL
406
Antialiasing Revisited 407
Back-to-Front and Front-to-Back Rendering 409
Scene Antialiasing and Multisampling
410
374
375
376
384
397
400
404
Contents
7.11.7
7.11.8
Image Processing
411
Other Multipass Methods
7.12
Sampling and Aliasing
7.12.1
7.12.2
7.12.3
Sampling Theory
413
Reconstruction
418
Quantization
420
412
413
Summary and Notes
421
Suggested Readings
422
Exercises
CHAPTER 8
422
MODELING AND HIERARCHY
425
8.1
Symbols and Instances
426
8.2
Hierarchical Models
427
8.3
A Robot Arm
429
8.4
Trees and Traversal
432
8.4.1
A Stack-Based Traversal
8.5
Use of Tree Data Structures
437
8.6
Animation
441
8.7
Graphical Objects
443
8.7.1
8.7.2
8.7.3
8.7.4
8.7.5
Methods, Attributes, and Messages 443
A Cube Object
445
Implementing the Cube Object
447
Objects and Hierarchy
447
Geometric Objects
448
8.8
Scene Graphs
449
8.9
Open Scene Graph
451
8.10
Graphics and the Internet
453
8.10.1
8.10.2
8.10.3
8.10.4
Hypermedia and HTML
453
Java and Applets
454
Interactive Graphics and the Web
WebGL
455
8.11
Other Tree Structures
8.11.1
8.11.2
8.11.3
CSG Trees
455
BSP Trees
457
Quadtrees and Octrees
434
455
459
Summary and Notes
461
Suggested Readings
461
Exercises
462
454
xv
xvi
Contents
CHAPTER 9
PROCEDURAL METHODS
465
9.1
Algorithmic Models
465
9.2
Physically Based Models and Particle Systems
467
468
9.3
Newtonian Particles
9.3.1
9.3.2
9.3.3
Independent Particles
470
Spring Forces
471
Attractive and Repulsive Forces
9.4
Solving Particle Systems
473
9.5
Constraints
476
9.5.1
9.5.2
Collisions 476
Soft Constraints
9.6
A Simple Particle System
9.6.1
9.6.2
9.6.3
9.6.4
9.6.5
Displaying the Particles 480
Updating Particle Positions
481
Collisions 482
Forces 483
Flocking
483
9.7
Language-Based Models
484
9.8
Recursive Methods and Fractals
487
9.8.1
9.8.2
9.8.3
9.8.4
9.8.5
Rulers and Length 488
Fractal Dimension
489
Midpoint Division and Brownian Motion
Fractal Mountains
492
The Mandelbrot Set
493
9.9
Procedural Noise
479
500
Suggested Readings
501
CHAPTER 10
480
490
496
Summary and Notes
Exercises
472
501
CURVES AND SURFACES
503
10.1
Representation of Curves and Surfaces
10.1.1
10.1.2
10.1.3
10.1.4
10.1.5
Explicit Representation 503
Implicit Representations 505
Parametric Form 506
Parametric Polynomial Curves 507
Parametric Polynomial Surfaces 508
503
10.2
Design Criteria
509
10.3
Parametric Cubic Polynomial Curves
510
Contents
10.4
Interpolation
511
10.4.1
10.4.2
Blending Functions
513
The Cubic Interpolating Patch
10.5
Hermite Curves and Surfaces
10.5.1
10.5.2
The Hermite Form
517
Geometric and Parametric Continuity
10.6
´
Bezier
Curves and Surfaces
10.6.1
10.6.2
´
Bezier
Curves
521
´
Bezier
Surface Patches
10.7
Cubic B-Splines
10.7.1
10.7.2
10.7.3
The Cubic B-Spline Curve
B-Splines and Basis
528
Spline Surfaces
528
10.8
General B-Splines
10.8.1
10.8.2
10.8.3
10.8.4
10.8.5
Recursively Defined B-Splines
Uniform Splines
532
Nonuniform B-Splines
532
NURBS
532
Catmull-Rom Splines
534
10.9
Rendering Curves and Surfaces
10.9.1
10.9.2
10.9.3
10.9.4
Polynomial Evaluation Methods
536
´
Recursive Subdivision of Bezier
Polynomials 537
Rendering Other Polynomial Curves by Subdivision
´
Subdivision of Bezier
Surfaces
541
515
517
519
520
523
524
525
529
530
535
540
10.10 The Utah Teapot
542
10.11 Algebraic Surfaces
545
10.11.1 Quadrics
545
10.11.2 Rendering of Surfaces by Ray Casting
10.12 Subdivision Curves and Surfaces
10.12.1 Mesh Subdivision
10.13.1 Height Fields Revisited
10.13.2 Delaunay Triangulation
10.13.3 Point Clouds
555
551
551
Summary and Notes
556
Suggested Readings
556
557
546
547
10.13 Mesh Generation from Data
Exercises
546
550
xvii
xviii
Contents
CHAPTER 11
ADVANCED RENDERING
559
11.1
Going Beyond Pipeline Rendering
559
11.2
Ray Tracing
560
564
11.3
Building a Simple Ray Tracer
11.3.1
11.3.2
11.3.3
Recursive Ray Tracing 564
Calculating Intersections 566
Ray-Tracing Variations
568
11.4
The Rendering Equation
569
11.5
Radiosity
571
11.5.1
11.5.2
11.5.3
11.5.4
The Radiosity Equation 572
Solving the Radiosity Equation 574
Computing Form Factors
575
Carrying Out Radiosity 577
11.6
RenderMan
578
11.7
Parallel Rendering
579
11.7.1
11.7.2
11.7.3
Sort-Middle Rendering
581
Sort-Last Rendering 583
Sort-First Rendering
586
11.8
Volume Rendering
11.8.1
11.8.2
Volumetric Data Sets 588
Visualization of Implicit Functions
11.9
Isosurfaces and Marching Cubes
588
589
591
11.10 Mesh Simplification
594
11.11 Direct Volume Rendering
595
11.11.1
11.11.2
11.11.3
11.11.4
Assignment of Color and Opacity
596
Splatting
596
Volume Ray Tracing
598
Texture Mapping of Volumes
599
11.12 Image-Based Rendering
11.12.1 A Simple Example
Summary and Notes
602
Suggested Readings
603
Exercises
APPENDIX A
600
600
604
SAMPLE PROGRAMS
A.1
Shader Initialization Function
A.1.1
Application Code
608
607
608
Contents
A.2
Sierpinski Gasket Program
A.2.1
A.2.2
A.2.3
Application Code
610
Vertex Shader
612
Fragment Shader
612
A.3
Recursive Generation of Sierpinski Gasket
A.3.1
A.3.2
A.3.3
Application Code
613
Vertex Shader
615
Fragment Shader
615
A.4
Rotating Cube with Rotation in Shader
A.4.1
A.4.2
A.4.3
Application Code
615
Vertex Shader
620
Fragment Shader
620
A.5
Perspective Projection
A.5.1
A.5.2
A.5.3
Application Code
621
Vertex Shader
625
Fragment Shader
626
A.6
Rotating Shaded Cube
A.6.1
A.6.2
A.6.3
Application Code
626
Vertex Shader
631
Fragment Shader
632
A.7
Per-Fragment Lighting of Sphere Model
A.7.1
A.7.2
A.7.3
Application Code
632
Vertex Shader
637
Fragment Shader
638
A.8
Rotating Cube with Texture
A.8.1
A.8.2
A.8.3
Application Code
638
Vertex Shader
644
Fragment Shader
645
A.9
Figure with Tree Traversal
A.9.1
A.9.2
A.9.3
Application Code
646
Vertex Shader
659
Fragment Shader
659
A.10
Teapot Renderer
A.10.1
A.10.2
A.10.3
Application Code
659
Vertex Shader
664
Fragment Shader
664
APPENDIX B
SPACES
610
613
615
621
626
632
638
646
659
665
B.1
Scalars
665
B.2
Vector Spaces
666
xix
xx
Contents
B.3
B.4
B.5
B.6
Affine Spaces
Euclidean Spaces
Projections
Gram-Schmidt Orthogonalization
Suggested Readings
Exercises
APPENDIX C
668
669
670
671
672
672
MATRICES
675
C.1
C.2
Definitions
Matrix Operations
675
676
C.3
Row and Column Matrices
677
C.4
C.5
C.6
C.7
Rank
Change of Representation
The Cross Product
Eigenvalues and Eigenvectors
678
679
681
682
C.8
Vector and Matrix Classes
683
Suggested Readings
Exercises
APPENDIX D
684
684
SYNOPSIS OF OPENGL FUNCTIONS
687
D.1
D.2
Initialization and Window Functions
Vertex Buffer Objects
687
689
D.3
D.4
D.5
D.6
Interaction
Setting Attributes and Enabling Features
Texture and Image Functions
State and Buffer Manipulation
690
692
693
694
D.7
D.8
Query Functions
GLSL Functions
694
695
References
699
OpenGL Function Index
Subject Index
711
709
P RE FACE
NEW TO THE SIXTH EDITION
Use of modern Shader-Based OpenGL throughout
No use of OpenGL functions deprecated with OpenGL 3.1
Increased detail on implementing transformations and viewing in both application code and shaders
Consistency with OpenGL ES 2.0 and WebGL
Use of C++ instead of C
Addition of vector and matrix classes to create application code compatible
with the OpenGL Shading Language (GLSL)
Discussion of per-vertex and per-fragment lighting
Addition of polygon and Delaunay triangularization
Introduction to volume rendering
All code examples redone to be compatible with OpenGL 3.1
New co-author, Dave Shreiner, author of the OpenGL Programming Guide
T
his book is an introduction to computer graphics, with an emphasis on applications programming. The first edition, which was published in 1997, was somewhat revolutionary in using a standard graphics library and a top-down approach.
Over the succeeding 13 years and five editions, this approach has been adopted by
most introductory classes in computer graphics and by virtually all the competing
textbooks.
The major changes in graphics hardware over the past few years have led to major
changes in graphics software. For its first fifteen years, new OpenGL versions were
released with new versions always guaranteeing backward compatibility. The ability
to reuse code as the underlying software was upgraded was an important virtue, both
for developers of applications and for instructors of graphics classes. OpenGL 3.0
announced major changes, one of the key ones being that, starting with OpenGL 3.1,
many of the most common functions would be deprecated. This radical departure
from previous versions reflects changes needed to make use of the capabilities of the
xxi
xxii
Preface
latest programmable graphics units (GPUs). These changes are also part of OpenGL
ES 2.0, which is being used to develop applications on mobile devices such as cell
phones and tablets, and WebGL, which is supported on most of the latest browsers.
As the authors of the previous five editions of this textbook (EA) and of the
OpenGL Programming Guide and OpenGL ES 2.0 Programming Guide (DS), we were
confronted with a dilemma as to how to react to these changes. We had been writing books and teaching introductory OpenGL courses at SIGGRAPH for many years.
We found that almost no one in the academic community, or application programmers outside the high-end game world, knew about these changes, and those of our
colleagues who did know about them did not think we could teach these concepts
at the beginning level. That was a challenge we couldn’t resist. We started by teaching a half-day short course at SIGGRAPH, which convinced us that we could teach
someone without previous graphics programming experience how to write a nontrivial shader-based OpenGL application program with just a little more work than
with earlier versions of OpenGL.
As we developed this edition, we discovered some other reasons that convinced
us that this approach is not only possible but even better for students learning computer graphics. Only a short while ago, we touted the advantages OpenGL gave us
by being available for Windows, Linux, and Mac OS X so we could teach a course in
which students could work in the environment they preferred. With OpenGL ES and
WebGL they can now develop applications for their cell phones or Web browsers. We
believe that this will excite both students and instructors about computer graphics
and open up many new educational and commercial opportunities.
We feel that of even greater importance to the learning experience is the removal
of most defaults and the fixed function pipeline in these new versions of OpenGL. At
first glance, this removal may seem like it would make teaching a first course much
harder. Maybe a little harder; but we contend much better. The tendency of most
students was to rely on these functions and not pay too much attention to what the
textbook and instructor were trying to teach them. Why bother when they could use
built-in functions that did perspective viewing or lighting? Now that those functions
are gone and students have to write their own shaders to do these jobs, they have to
start by understanding the underlying principles and mathematics.
A Top-Down Approach
These recent advances and the success of the first five editions continue to reinforce
our belief in a top-down, programming-oriented approach to introductory computer
graphics. Although many computer science and engineering departments now support more than one course in computer graphics, most students will take only a single
course. Such a course is placed in the curriculum after students have already studied
programming, data structures, algorithms, software engineering, and basic mathematics. A class in computer graphics allows the instructor to build on these topics
in a way that can be both informative and fun. We want these students to be programming three-dimensional applications as soon as possible. Low-level algorithms,
Preface
such as those that draw lines or fill polygons, can be dealt with later, after students are
creating graphics.
John Kemeny, a pioneer in computer education, used a familiar automobile
analogy: You don’t have to know what’s under the hood to be literate, but unless
you know how to program, you’ll be sitting in the back seat instead of driving. That
same analogy applies to the way we teach computer graphics. One approach—the
algorithmic approach—is to teach everything about what makes a car function: the
engine, the transmission, the combustion process. A second approach—the survey
approach—is to hire a chauffeur, sit back, and see the world as a spectator. The third
approach—the programming approach that we have adopted here—is to teach you
how to drive and how to take yourself wherever you want to go. As the old auto-rental
commercial used to say, “Let us put you in the driver’s seat.”
Programming with OpenGL and C++
When Ed began teaching computer graphics over 25 years ago, the greatest impediment to implementing a programming-oriented course, and to writing a textbook
for that course, was the lack of a widely accepted graphics library or application programmer’s interface (API). Difficulties included high cost, limited availability, lack of
generality, and high complexity. The development of OpenGL resolved most of the
difficulties many of us had experienced with other APIs (such as GKS and PHIGS)
and with the alternative of using home-brewed software. OpenGL today is supported
on all platforms. It is bundled with Microsoft Windows and Mac OS X. Drivers are
available for virtually all graphics cards. There is also an OpenGL API called Mesa
that is included with most Linux distributions.
A graphics class teaches far more than the use of a particular API, but a good API
makes it easier to teach key graphics topics, including three-dimensional graphics,
lighting and shading, client–server graphics, modeling, and implementation algorithms. We believe that OpenGL’s extensive capabilities and well-defined architecture
lead to a stronger foundation for teaching both theoretical and practical aspects of
the field and for teaching advanced concepts, including texture mapping, compositing, and programmable shaders.
Ed switched his classes to OpenGL about 15 years ago, and the results astounded
him. By the middle of the semester, every student was able to write a moderately complex three-dimensional program that required understanding of three-dimensional
viewing and event-driven input. In previous years of teaching computer graphics, he
had never come even close to this result. That class led to the first edition of this book.
This book is a textbook on computer graphics; it is not an OpenGL manual.
Consequently, it does not cover all aspects of the OpenGL API but rather explains
only what is necessary for mastering this book’s contents. It presents OpenGL at a
level that should permit users of other APIs to have little difficulty with the material.
Unlike previous editions, this one uses C++ rather than C as the dominant
programming language. The reason has to do with the OpenGL Shading Language
(GLSL) used to write shaders, the programs that control the GPU. GLSL is a C-like
language with atomic data types that include vectors and matrices, and overloaded
xxiii