Introduction

I have a confession to make. The first time I ever heard of OpenGL was at the 1992 Win32 Developers Conference in San Francisco. Windows NT 3.1 was in early beta (or late alpha), and many vendors were present, pledging their future support for this exciting new graphics technology. Among them was a company called Silicon Graphics, Inc. (SGI). The SGI representatives were showing off their graphics workstations and playing video demos of special effects from some popular movies. Their primary purpose in this booth, however, was to promote a new 3D graphics standard called OpenGL. It was based on SGI's proprietary IRIS GL and was fresh out of the box as a graphics standard. Significantly, Microsoft was pledging future support for OpenGL in Windows NT.

I had to wait until the beta release of NT 3.5 before I got my first personal taste of OpenGL. Those first OpenGL-based screensavers only scratched the surface of what was possible with this graphics API. Like many other people, I struggled through the Microsoft help files and bought a copy of the OpenGL Programming Guide (now called simply “The Red Book” by most). The Red Book was not a primer, however, and it assumed a lot of knowledge that I just didn't have.

Now for that confession I promised. How did I learn OpenGL? I learned it by writing a book about it. That's right, the first edition of the OpenGL SuperBible was me learning how to do 3D graphics myself…with a deadline! Somehow I pulled it off, and in 1996 the first edition of the book you are holding was born. Teaching myself OpenGL from scratch enabled me somehow to better explain the API to others in a manner that a lot of people seemed to like. The whole project was nearly canceled when Waite Group Press was acquired by another publisher halfway through the publishing process. Mitchell Waite stuck to his guns and insisted that OpenGL was going to be “the next big thing” in computer graphics. Vindication arrived when an emergency reprint was required because the first run of the book sold out before ever making it to the warehouse.

That was a long time ago, and in what seems like a galaxy far, far away…

Only three years later 3D accelerated graphics were a staple for even the most stripped-down PCs. The “API Wars,” a political battle between Microsoft and SGI, had come and gone; OpenGL was firmly established in the PC world; and 3D hardware acceleration was as common as CD-ROMs and sound cards. I had even managed to turn my career more toward an OpenGL orientation and had the privilege of contributing in some small ways to the OpenGL specification for version 1.2 while working at Lockheed Martin/Real 3D. The second edition of this book, released at the end of 1999, was significantly expanded and corrected. We even made some modest initial attempts to ensure all the sample programs were more friendly in non-Windows platforms by using the GLUT framework.

Now, nearly five years later (eight since the first edition!), we bring you yet again another edition, the third, of this book. OpenGL is now without question the premier cross-platform real-time 3D graphics API. Excellent OpenGL stability and performance are available on even the most stripped-down bargain PC today. OpenGL is also the standard for UNIX and Linux operating systems, and Apple has made OpenGL a core fundamental technology for the new MacOS X operating system. OpenGL is even making inroads via a new specification, OpenGL ES, into embedded and mobile spaces. Who would have thought five years ago that we would see Quake running on a cell phone?

It is exciting that, today, even laptops have 3D acceleration, and OpenGL is truly everywhere and on every mainstream computing platform. Even more exciting, however, is the continuing evolution of computer graphics hardware. Today, most graphics hardware is programmable, and OpenGL even has its own shading language, which can produce stunningly realistic graphics that were undreamed of on commodity hardware back in the last century. (I just had to squeeze that in someplace!)

With this third edition, I am pleased that we have added Benjamin Lipchak as a coauthor. Benj is primarily responsible for the chapters that deal with OpenGL shader programs, and coming from the ARB groups responsible for this aspect of OpenGL, he is one of the most qualified authors on this topic in the world.

We have also fully left behind the “Microsoft Specific” characteristics of the first edition and have embraced a more multiplatform approach. All the programming examples in this book have been tested on Windows, MacOS X, and at least one version of Linux. There is even one chapter apiece on these operating systems, with information about using OpenGL with native applications.

What's in This Book

The OpenGL SuperBible is divided into three parts. In the first, we cover what we call Classic OpenGL. This is the fixed pipeline functionality that has been a characteristic of OpenGL from the beginning. Many of these chapters have been greatly expanded since the second edition because OpenGL has had a number of revisions since version 1.2. Fixed pipeline programming will still be with us for a long time to come, and its simple programming model will still make it the choice of many programmers for years.

In these chapters, you will learn the fundamentals of real-time 3D graphics programming with OpenGL. You'll learn how to construct a program that uses OpenGL, how to set up your 3D-rendering environment, and how to create basic objects and light and shade them. Then we'll delve deeper into using OpenGL and some of its advanced features and different special effects. These chapters are a good way to introduce yourself to 3D graphics programming with OpenGL and provide the conceptual foundation on which the more advanced capabilities later in the book are based.

In the second part, three chapters provide specific information about using OpenGL on the three mainstream operating system families: Windows, MacOS X, and Linux/UNIX.

Finally, the third part contains the newest features not just of OpenGL, but of 3D graphics hardware in general today. The OpenGL Shading Language, in particular, is the principal feature of OpenGL 2.0, and it represents the biggest advance in computer graphics in many years.

Part I: Classic OpenGL

Chapter 1—Introduction to 3D Graphics and OpenGL

This introductory chapter is for newcomers to 3D graphics. It introduces fundamental concepts and some common vocabulary.

Chapter 2—Using OpenGL

In this chapter, we provide you with a working knowledge of what OpenGL is, where it came from, and where it is going. You will write your first program using OpenGL, find out what headers and libraries you need to use, learn how to set up your environment, and discover how some common conventions can help you remember OpenGL function calls. We also introduce the OpenGL state machine and error-handling mechanism.

Chapter 3—Drawing in Space: Geometric Primitives and Buffers

Here, we present the building blocks of 3D graphics programming. You'll basically find out how to tell a computer to create a three-dimensional object with OpenGL. You'll also learn the basics of hidden surface removal and ways to use the stencil buffer.

Chapter 4—Geometric Transformations: The Pipeline

Now that you're creating three-dimensional shapes in a virtual world, how do you move them around? How do you move yourself around? These are the things you'll learn here.

Chapter 5—Color, Materials, and Lighting: The Basics

In this chapter, you'll take your three-dimensional “outlines” and give them color. You'll learn how to apply material effects and lights to your graphics to make them look real.

Chapter 6—More on Colors and Materials

Now it's time to learn about blending objects with the background to make transparent (see-through) objects. You'll also learn some special effects with fog and the accumulation buffer.

Chapter 7—Imaging with OpenGL

This chapter is all about manipulating image data within OpenGL. This information includes reading a TGA file and displaying it in an OpenGL window. You'll also learn some powerful OpenGL image-processing capabilities.

Chapter 8—Texture Mapping: The Basics

Texture mapping is one of the most useful features of any 3D graphics toolkit. You'll learn how to wrap images onto polygons and how to load and manage multiple textures at once.

Chapter 9—Texture Mapping: Beyond the Basics

In this chapter, you'll learn how to generate texture coordinates automatically, use advanced filtering modes, and use built-in hardware support for texture compression. You'll also learn about OpenGL's powerful texture combiner functionality.

Chapter 10—Curves and Surfaces

The simple triangle is a powerful building block. This chapter gives you some tools for manipulating the mighty triangle. You'll learn about some of OpenGL's built-in quadric surface generation functions and ways to use automatic tessellation to break complex shapes into smaller, more digestible pieces. You'll also explore the utility functions that evaluate Bézier and NURBS curves and surfaces. You can use these functions to create complex shapes with an amazingly small amount of code.

Chapter 11—It's All About the Pipeline: Faster Geometry Throughput

For this chapter, we show you how to build complex 3D objects out of smaller, less complex 3D objects. We introduce OpenGL display lists and vertex arrays for improving performance and organizing your models. You'll also learn how to create a detailed analysis showing how to best represent large, complex models.

Chapter 12—Interactive Graphics

This chapter explains two OpenGL features: selection and feedback. These groups of functions make it possible for the user to interact with objects in the scene. You can also get rendering details about any single object in the scene.

Part II: OpenGL Everywhere

Chapter 13—Wiggle: OpenGL on Windows

Here, you'll learn how to write real Windows (message-based) programs that use OpenGL. You'll learn about Microsoft's “wiggle” functions that glue OpenGL rendering code to Windows device contexts. You'll also learn how to respond to Windows messages.

Chapter 14OpenGL on the MacOS X

In this chapter, you'll learn how to use OpenGL in native MacOS X applications. Sample programs show you how to start working in Carbon or Cocoa using the Xcode development environment.

Chapter 15—GLX: OpenGL on Linux

This chapter discusses GLX, the OpenGL extension used to support OpenGL applications through the X Window System on Unix and Linux. You'll learn how to create and manage OpenGL contexts as well as how to create OpenGL drawing areas with several of the common GUI toolkits.

Part III: OpenGL: The Next Generation

Chapter 16—Buffer Objects: It's Your Video Memory; You Manage It!

In this chapter, you'll learn about OpenGL 1.5's vertex buffer object feature. Buffer objects allow you to store vertex array data in memory that can be more efficiently accessed by the GPU, such as local VRAM or AGP-mapped system memory.

Chapter 17—Occlusion Queries: Why Do More Work Than You Need To?

Here, you'll learn about OpenGL 1.5's occlusion query mechanism. This feature effectively lets you perform an inexpensive test-render of objects in your scene to find out whether they will be hidden behind other objects, in which case you can save time by not drawing the actual full-detail version.

Chapter 18—Depth Textures and Shadows

This chapter covers OpenGL 1.4's depth textures and shadow comparisons. You'll learn how to introduce real-time shadow effects to your scene, regardless of the geometry's complexity.

Chapter 19—Programmable Pipeline: This Isn't Your Father's OpenGL

Out with the old, in with the new. This chapter revisits the conventional fixed functionality pipeline before introducing the new programmable vertex and fragment pipeline stages. Programmability allows you to customize your rendering in ways never before possible using shader programs.

Chapter 20—Low-Level Shading: Coding to the Metal

In this chapter, you'll learn about the low-level shader extensions: ARB_vertex_program and ARB_fragment_program. You can use them to customize your rendering via shader programs in a language reminiscent of assembly code, offering full control over the underlying hardware.

Chapter 21—High-Level Shading: The Real Slim Shader

Here, we discuss the OpenGL Shading Language, the high-level counterpart to the low-level extensions. GLSL is a C-like language that gives you increased functionality and productivity.

Chapter 22—Vertex Shading: Do-It-Yourself Transform, Lighting, and Texgen

This chapter illustrates the usage of vertex shaders by surveying a handful of examples, including lighting, fog, squash and stretch, and skinning.

Chapter 23—Fragment Shading: Empower Your Pixel Processing

Again, you learn by example—this time with a variety of fragment shaders. Examples include per-pixel lighting, color conversion, image processing, and procedural texturing. Some of these examples also use vertex shaders; these examples are representative of real-world usage, where you often find vertex and fragment shaders paired together.

Conventions Used in This Book

The following typographic conventions are used in this book:

  • Code lines, commands, statements, variables, and any text you type or see onscreen appear in a computer typeface.

  • Placeholders in syntax descriptions appear in an italic computer typeface. Replace the placeholder with the actual filename, parameter, or whatever element it represents.

  • Italics highlight technical terms when they first appear in the text and are being defined.

About the Companion CD

The CD that comes with the OpenGL SuperBible is packed with sample programs, toolkits, source code, and documentation—everything but the kitchen sink! We dig up stuff to put on this CD all the way until press time, so check out the readme.txt file in the root of the CD for a complete list of all the goodies we include.

The CD contains some basic organization. Off the root directory, you'll find

  • Examples—. Beneath this directory, you'll find a directory for each chapter in the book that has programming examples. Each sample has a real name (as opposed to sample 5.2c), so you can browse the CD with ease and run anything that looks interesting when you first get the book.

  • Tools—. A collection of third-party tools and libraries appears here. Each has its own subdirectory and documentation from the original vendor. Sample programs throughout the book use some of these tools (GLUT in particular).

  • Demos—. This directory contains a collection of OpenGL demo programs. These programs all showcase the rendering capabilities of OpenGL. Some are free; some are commercial demos.

For support issues with the CD, please contact Sams Publishing (www.samspublishing.com). Other OpenGL questions, more samples and tutorials, and, of course, the inevitable list of errata are posted on the book's Web site at

http://www.starstonesoftware.com/OpenGL

Building the Sample Programs

All the sample programs in this book are written in C. They should be easy to move to C++ for those so inclined, but C usually provides a larger audience for a book such as this, and is readily understood by anyone who might prefer C++. Most programs also use the glTools library, which is simply a collection of OpenGL-friendly utility functions written by the authors. The glTools header file (gltools.h) and source .c files are listed in the common directory beneath the Examples directories on the CD.

Beneath Examples, you will find a directory for each OS platform supported. Windows project files are written in Visual C++ 6.0. The reason is that many have chosen not to upgrade to the new .NET-biased version of the tools, and those who have (myself included on many projects) can easily import the projects.

MacOS X sample project files were made with Xcode version 1.1. Anyone still using Project Builder should upgrade…it's free, and it will add years to your life. All Mac samples were tested on OS X version 10.3.3. At the time of printing, Apple had not released drivers that support the OpenGL shading language. Latent Mac bugs could show up when it does. Check the Web site referenced in the preceding section for any necessary updates.

Linux make files are also provided. There are 10,392,444,224,229,349,244,281,999.4 different ways to configure a Linux environment. Well, maybe not quite that many. Without meaning to sound too harsh, you are pretty much on your own. Because Xcode uses a similar gnu compiler that many Linux environments use, you shouldn't have too many problems. I expect some additional notes and tutorials will show up on the book's Web site over time.

Dare to Dream in 3D!

Once upon a time in the early 1980s, I was looking at a computer at an electronics store. The salesman approached and began making his pitch. I told him I was just learning to program and considering an Amiga over his model. I was briskly informed that I needed to get serious with a computer that the rest of the world was using. An Amiga, he told me, was not good for anything but “making pretty pictures.” No one, he assured me, could make a living making pretty pictures on his computer.

This was possibly the worst advice I have ever received in the history of bad advice. A few years ago, I forgot about being a “respectable” database/enterprise/yada-yada-yada developer. Now I write cool graphics programs, teach graphics programming, own my own software company, and generally have more fun with my career than should probably be allowed by law!

I hope I can give you some better advice today. Whether you want to write games, create military simulations, develop scientific visualizations, or visualize large corporate databases, OpenGL is the perfect API. It will meet you where you are as a beginner, and it will empower you when you become your own 3D guru. And yes, my friend, you can make a living making pretty pictures with your computer!

—Richard S. Wright, Jr.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset