Adobe® Digital Editions software.This eBook requires the free
Before downloading this DRM-encrypted PDF, be sure to:
OpenGL opens the door to the world of high-quality, high-performance 3D computer graphics. The preferred application programming interface for developing 3D applications, OpenGL is widely used in video game development, visualization and simulation, CAD, virtual reality, modeling, and computer-generated animation.
OpenGL® Distilled provides the fundamental information you need to start programming 3D graphics, from setting up an OpenGL development environment to creating realistic textures and shadows. Written in an engaging, easy-to-follow style, this book makes it easy to find the information you're looking for. You'll quickly learn the essential and most-often-used features of OpenGL 2.0, along with the best coding practices and troubleshooting tips.
A companion Web site includes complete source code examples, color versions of special effects described in the book, and additional resources.
Download the Sample
Chapter related to this title.
About the Author xxv
Chapter 1. An Introduction to OpenGL 1
1.1 What Is OpenGL? 2
1.1.1 Fundamentals and Architecture 4
1.1.2 Syntax 8
1.1.3 State and Queries 10
1.2 GLUT 17
1.3 GLU 18
1.4 Development Environment 18
1.4.1 Apple Mac OS X 19
1.4.2 Linux 20
1.4.3 Microsoft Windows 21
1.5 A Simple Example 22
1.6 History of OpenGL 26
1.6.1 Before 1992 26
1.6.2 OpenGL Compared with Iris GL 28
1.6.3 OpenGL Compared with PEX 28
1.6.4 Continued Evolution of OpenGL 29
1.7 More Information 30
1.8 References 30Chapter 2. Drawing Primitives 33
2.1 OpenGL Primitives 34
2.1.1 Primitive Types 35
2.1.2 Vertex Sharing 37
2.2 Specifying Vertex Data 37
2.2.1 Drawing Primitives Using glBegin()/glEnd() 38
2.2.2 Drawing Primitives Using Vertex Arrays 40
2.3 Drawing Details 54
2.3.1 Clearing the Framebuffer 54
2.3.2 Modeling Transformations 56
2.3.3 Smooth and Flat Shading 56
2.3.4 Polygon Mode 57
2.3.5 The Depth Test 58
2.3.6 Co-Planar Primitives 58
2.3.7 Alpha and Transparency 61
2.4 Performance Issues 63
2.4.1 Display Lists 63
2.4.2 Face Culling 66
2.4.3 Vertex Array Size 67
2.5 More Information 68
2.6 References 68Chapter 3. Transformation and Viewing 69
3.1 Coordinate Systems and Matrices 71
3.1.1 Handedness 71
3.1.2 Matrices 72
3.1.3 Matrix Stacks and Matrix State 73
3.1.4 Matrix Manipulation Commands 74
3.2 The Transformation Pipeline 76
3.2.1 Object Coordinates 77
3.2.2 The Model Transformation 78
3.2.3 World Coordinates 79
3.2.4 The View Transformation 80
3.2.5 Eye Coordinates 80
3.2.6 The Projection Transformation 81
3.2.7 Clip Coordinates 81
3.2.8 Perspective Division 81
3.2.9 Normalized Device Coordinates 81
3.2.10 The Viewport Transformation 82
3.2.11 Window Coordinates 82
3.2.12 Controlling Transformations 82
3.3 Setting the Model-View Matrix 84
3.3.1 Creating the View Transformation with gluLookAt() 85
3.3.2 Creating the View Transformation Matrix 86
3.3.3 View Transformations in the Example Code 87
3.4 Perspective and Parallel Projections 88
3.5 The Viewport 90
3.6 Selection 91
3.6.1 Performing a Selection Pass 92
3.6.2 Selection in the Example Code 96
3.7 More Information 97
3.8 References 98Chapter 4. Lighting 99
4.1 Overview 101
4.1.1 Ambient, Diffuse, and Specular Light 101
4.1.2 Controlling OpenGL Lighting 102
4.1.3 Minimal Lighting Code 103
4.1.4 Internal Lighting Computation 103
4.2 Normals 104
4.3 Light Parameters 105
4.3.1 Typical Usage 106
4.3.2 More Information 106
4.4 Material Parameters 107
4.4.1 Changing Material Parameters with glColor*() 108
4.4.2 Typical Usage 110
4.4.3 More Information 110
4.5 Positional and Directional Lights 111
4.6 Debugging Lights 112
4.6.1 Debugging a Blank Window 112
4.6.2 Normals 113
4.6.3 Incorrect Face Culling 113
4.6.4 Debugging Position and Direction 113
4.6.5 Debugging Light Colors 114
4.6.6 Per-Vertex Lighting Artifacts 114
4.6.7 Missing Specular Highlights 115
4.6.8 Line and Point Colors 115
4.7 More Information 116
4.8 References 117Chapter 5. Pixel Rectangles 119
5.1 Drawing Pixels 120
5.1.1 The Current Raster Position 121
5.1.2 Drawing Pixel Rectangles with glDrawPixels() 124
5.2 Reading Pixels 126
5.3 Copying Pixels 127
5.4 Performance Issues 128
5.4.1 Using Alternatives to glDrawPixels() 128
5.4.2 Flushing the OpenGL Pipeline 129
5.5 Debugging 129
5.5.1 Incorrect Raster Position 129
5.5.2 Clipped Raster Position 130
5.5.3 Texture or Fog Enabled 130
5.5.4 Depth Test 131
5.5.5 Pixel Data Alignment 131
5.5.6 Obscured Windows 132
5.5.7 Memory Allocation Issues 132
5.6 More Information 133
5.7 References 133Chapter 6. Texture Mapping 135
6.1 Using Texture Maps 136
6.1.1 Texture Objects 138
6.1.2 Texture Coordinates 145
6.1.3 Multitexturing 150
6.1.4 Texture Mapping Example 153
6.2 Lighting and Shadows with Texture 157
6.2.1 Static Lighting 158
6.2.2 Light Maps 160
6.2.3 Depth Maps 162
6.2.4 Specular Highlights 167
6.2.5 Environment Maps 170
6.3 Debugging 173
6.3.1 OpenGL Error Code 173
6.3.2 Texture State 174
6.3.3 Texture Completeness 175
6.3.4 Missing Textures 176
6.3.5 Inverted Textures 177
6.4 More Information 178
6.5 References 179Chapter 7. Extensions and Versions 181
7.1 Extensions 182
7.1.1 Querying for an Extension 184
7.1.2 Obtaining and Calling Extension Commands 185
7.2 Versions 188
7.3 Extension- and Version-Safe Code 190
7.4 More Information 194Chapter 8. Platform-Specific Interfaces 197
8.1 Concepts 198
8.2 Apple Mac OS X 199
8.2.1 Creating Contexts 200
8.2.2 Using Contexts 202
8.2.3 Swapping Buffers 203
8.2.4 Deleting Contexts 203
8.3 Linux 203
8.3.1 Creating Contexts 204
8.3.2 Using Contexts 207
8.3.3 Swapping Buffers 208
8.3.4 Deleting Contexts 208
8.4 Microsoft Windows 209
8.4.1 Creating the Window 209
8.4.2 Creating Contexts 209
8.4.3 Using Contexts 211
8.4.4 Swapping Buffers 213
8.4.5 Deleting Contexts 213
8.5 More Information 214
8.6 References 214Appendix A. Other Features 215
A.1 Multisample 215
A.2 Occlusion Queries 216
A.3 Fog 216
A.4 Clip Planes 217
A.5 Stencil 217
A.6 The Accumulation Buffer 218
A.7 Shading Language 218
A.7.1 A Shader Example 219
A.7.2 More Information 221
A.8 References 221Appendix B. Best Practices 223
B.1 State 223
B.2 Errors 224
B.3 Precision 225
B.3.1 Magnitude 226
B.3.2 Round-Off 226
B.3.3 Depth Buffer 227
B.4 Objects 227Appendix C. Performance 229
C.1 Measure Performance 229
C.2 Avoid Software Rendering 231
C.3 Reduce Pipeline Bottlenecks 232
C.3.1 CPU Limited 233
C.3.2 Geometry Limited 234
C.3.3 Fill Limited 235
C.3.4 Closing Thoughts on Pipeline Bottlenecks 236
C.4 Cull Unseen Geometry 237
C.5 State Changes and Queries 238Appendix D. Troubleshooting and Debugging 241
D.1 Debugging Tools 241
D.2 OpenGL Errors 242
D.2.1 Check the OpenGL Error Code 242
D.2.2 Specific Errors 242
D.3 Debugging a Blank Window 245
D.3.1 Use Good Code Development Practices 245
D.3.2 Use glGetError() 245
D.3.3 Enable Vertex Arrays 246
D.3.4 Set Appropriate Clip Planes 246
D.3.5 Use Correct Transformations 247
D.3.6 Swap Buffers 247
D.3.7 Call glClear() 248
"Children crawl before they walk, walk before they run--each generally a precondition for the other." --Lillian Breslow Rubin
Computer graphics technology, like all computer technology, advances at a staggering rate. In the late 1990s, the first hardware-accelerated 3D graphics cards for consumers signaled the dawn of a new age in 3D graphics software development. Already, these cards are gathering in landfills, superseded by more powerful and advanced hardware.
If you're new to OpenGL, you probably wish you could dive in and start writing advanced OpenGL applications. Although many advanced graphics programming books exist, most assume familiarity with OpenGL or some other 3D graphics Application Programmer Interface (API). Before you tackle more advanced programming tasks, you need a way to get up to speed on OpenGL quickly.
OpenGL version 1.0 was introduced by SGI in 1992 and quickly dominated the 3D software development industry. It is available for several versions of Microsoft Windows, Linux, and Unix, and for Apple Mac OS. Graphics hardware manufacturers supporting OpenGL include 3Dlabs, ATI, Intel, Matrox, NVIDIA, and SGI.
The fact that OpenGL is an open standard is certainly one reason for its popularity. The OpenGL feature set is determined by the OpenGL Architecture Review Board (ARB), a governing body composed of representatives from several major hardware and software companies. This group meets regularly to discuss and approve additions and modifications to the OpenGL specification. The ARB's multivendor nature ensures that OpenGL runs equally well on a wide variety of graphics architectures.
This book documents OpenGL version 2.0, released in September 2004. Although OpenGL has evolved significantly since its initial release, each version has remained compatible with previous versions. The benefits of backward compatibility are obvious, and several companies have taken advantage of OpenGL's stability to amortize the expense of software development.
Over time, some older OpenGL features fell into disfavor as the ARB added new rendering methods and paradigms to the OpenGL standard. Still other features never quite caught on with the developer community for a variety of reasons, such as corner-case or platform-specific applicability, overlapping or limited functionality, or nearly universal inefficient implementation.
The addition of new functionality, while continuing to keep older (and increasingly obsolete) features for backward compatibility, has resulted in a bulky and more complex OpenGL specification. Consider:
Although such comprehensive documentation is indispensable for experienced programmers, the documentation's growth in size and complexity over the years has increased the learning curve for new OpenGL developers. Furthermore, finding advice on accepted modern usage is difficult for new OpenGL programmers due to the sheer magnitude of information or time wasted learning outdated methodologies and paradigms.
OpenGL® Distilled addresses these issues by presenting only the essential elements of modern OpenGL used in current 3D software development. To meet this goal, several features of OpenGL have been left out of this book. OpenGL Distilled presents the commonly used features and accepted practices in modern OpenGL programming. Features that have fallen into disfavor are omitted except where they help illustrate accepted practices or important concepts. Each chapter begins with a summary of what is and what is not covered.
To reduce the learning curve further, OpenGL® Distilled presents information in a direct, how-to style so that readers can find information quickly without wading through extraneous information. For this reason, there is little explanation or discussion of graphics algorithms. Shadow algorithms (discussed in Chapter 6) are the exception because of their almost-universal applicability and lack of direct OpenGL support. For other algorithms, OpenGL® Distilled assumes that the reader is familiar with computer graphics or is reading OpenGL® Distilled as part of a university-level computer graphics course.
This book presents the subset of OpenGL that most programmers can use to do the majority of their OpenGL programming. Because this material is a subset, the reader will likely desire access to more information. For this reason, OpenGL® Distilled serves as a gateway or road map to additional OpenGL and 3D graphics programming information. This book will frequently refer to the following resources:
The reader should be familiar with the C++ programming language. The example source code on the book's Web site is in C++, and will build and run in Microsoft Windows, Apple Mac OS, and most flavors of Linux and Unix. Many of the code snippets in the book should be comprehensible to any programmer who is familiar with C and who has access to a C++ reference manual.
The reader should be familiar with vector and matrix mathematics as covered in a typical linear-algebra course. The reader should understand matrix concatenation and vector-matrix multiplication. Readers already familiar with transformation and coordinate systems from another 3D graphics API will find OpenGL® Distilled an easy read. For readers unfamiliar with this subject, many general 3D graphics texts cover the subject adequately, such as Chapter 5, "Geometrical Transformations," in Computer Graphics: Principles and Practice.The reader should, of course, be familiar with basic mathematics such as algebra, geometry, and trigonometry.
Chapter 1, "An Introduction to OpenGL," presents OpenGL architecture and concepts in broad terms. It also introduces the GLUT and GLU support libraries, discusses setting up an OpenGL development environment, and presents a simple OpenGL example. This chapter concludes with an extensive history of the OpenGL API.
Chapter 2, "Drawing Primitives," covers how to use OpenGL to render geometric data. It describes the OpenGL drawing primitive types and how to render them by using the vertex array and buffer object features. This chapter also presents other drawing details, such as clearing the framebuffer, order dependence, and the depth test.
Chapter 3, "Transformation and Viewing," describes the OpenGL transformation pipeline. This chapter covers how to position and orient views, set perspective and orthographic projections, and control the Viewport. It also covers interactive view manipulation using the mouse.
Chapter 4, "Lighting," explains how to set lighting parameters, which control light position, direction, and color, as well as material reflectance.
Chapter 5, "Pixel Rectangles," introduces the OpenGL paradigm for drawing, reading, and copying blocks of pixel data.
Chapter 6, "Texture Mapping," tells you how to create texture objects and apply them to rendered geometry. Texture mapping is a powerful tool for increasing the realism of rendered images. This chapter describes techniques for improving specular highlights, environment mapping, and creating shadows.
Chapter 7, "Extensions and Versions," describes how to write version-safe, platform-independent code that uses implementation-specific graphics hardware functionality.
Chapter 8, "Platform-Specific Interfaces," describes OpenGL interfaces specific to Apple Mac OS, Linux, and Microsoft Windows. This chapter describes how to create and use multiple OpenGL rendering contexts and share objects between them.
Appendix A, "Other Features," briefly covers several OpenGL features outside the scope of this book, such as the OpenGL Shading Language, clip planes, fog, multisampling, and stencil.
Appendix B, "Best Practices," enumerates generally accepted good OpenGL programming practices that help ensure a robust and portable OpenGL application.
Appendix C, "Performance," describes how to take accurate timing measurements and how to identify and eliminate performance bottlenecks.
Appendix D, "Troubleshooting and Debugging," assists in resolving commonly encountered issues.
Each chapter opens with a quick summary of what the reader will learn, as well as topics that aren't covered. Each chapter concludes with pointers to further reading related to the chapter subject.
This book uses the following style conventions:
Blocks of code are set aside from the text and use a monospace font.
This book presents OpenGL commands by using a command summary, which includes function prototypes, descriptions of the command parameters, and OpenGL version information. Here's the template for a command summary:
Function prototype for the command
A description of the command and its parameters.
-> OpenGL version: Versions of OpenGL that support the command
In the function prototype, braces indicate a set of options. As an example, consider the glLight*v() set of functions.
void glLightfdv( GLenum light, GLenum pname, const TYPE* param );
This prototype defines two functions: glLightfv() and glLightdv(). f and d identify the parameter data type--GLfloat or GLdouble, respectively. In the function prototype parameter list, replace TYPE with the actual data type.
The command summary can be used as a concise reference for the command. OpenGL® Reference Manual, however, typically contains a more exhaustive reference.
The sample code requires GLUT, the TIFF library, and GNU Make. The Web site provides links for downloading these libraries and utilities.
OpenGL® Distilled contains references to color plates. These color plates
are available as a PDF file and can be accessed from the downloads link on this
page. If errors are found in this book, corrections will be posted to the Web
Download the Foreword
file related to this title.
Download the Index
file related to this title.