Welcome to OpenGL SuperBible! 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 platform. Among them was a company called Silicon Graphics, Inc. (SGI). They were showing off their graphics workstations and playing video demos of special effects from some popular movies. NT was running on MIPS processors—now owned by SGI—but their primary purpose in this booth 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 avoids platform issues and uses for all its examples the Auxiliary (AUX) library, a platform-independent program framework forOpenGL graphics.
At that time, the Red Book was the only book available for learning OpenGL. Though quite thorough in its coverage of OpenGL functions, it is lacking in two important respects. First, it is not a primer. Whatever the intent of the authors, the book assumes a substantial working knowledge of 3D graphics concepts in general. The Red Book’s second drawback is its platform independence. As a Windows developer, I needed answers to some important questions, such as how to use a .BMP file as a texture, how to create an OpenGL-usable palette for an 8-bit display device, and how to use all those “wiggle” functions Microsoft threw in.
OpenGL SuperBible fills in those gaps. I wanted to provide a 3D graphics introduction and an OpenGL tutorial rolled into one. In addition, I approached the whole subject within the context of the single most popular desktop operating system of all time, Microsoft Windows. And I added a Reference Section of thorough function definitions at the end of each chapter, making this book a good complement to the Waite Group line of bible reference books.
Who This Book Is For
This book will suit a wide audience of OpenGL and Windows programmers. Windows programmers wanting to learn about 3D graphics and how to implement them using OpenGL will find what they need. So will experienced Windows and 3D graphics programmers wanting to learn more about the industry standard OpenGL. This book will also be of value to seasoned OpenGL programmers who have a workstation background but need some assistance porting their applications and experience to the Microsoft Windows platforms.
System Requirements for OpenGL
OpenGL is not available on the 16-bit versions of Microsoft Windows (3.1, 3.11, and so forth) from Microsoft. Microsoft added OpenGL to Windows NT 3.5, and to Windows 95 via a separate distribution of some DLLs. (These DLLs are available via Microsoft’s FTP and Web sites and are included on this book’s CD, in the \Windows95 subdirectory.)
OpenGL SuperBible does not attempt to cover any third-party OpenGL or OpenGL-like libraries for the 32- or 16-bit environments. Programmatically, OpenGL used under Windows 95 is the same as OpenGL used under Windows NT. The first set of DLLs shipped by Microsoft for Windows NT supports all of the OpenGL 1.0 functions that are also available under Windows NT 3.5 and 3.51. OpenGL 1.1 functions are being added to Windows NT 4.0, and a new set of DLLs should be ready for Windows 95 by the time this book ships. See the readme.txt file on the CD for any late-breaking information.
All of the samples in the book should run fine on a fast 486 (that’s a “real” 486, mind you, which means a built-in math coprocessor!) with at least 8MB of RAM. Most programming environments will require at least this much horsepower, anyway. If you’re interested, all the code in the book and on the CD was developed and found to run acceptably on a 90MHz Pentium with 32MB of RAM and a 16/24-bit color display card. You will need a display card capable of at least 256 colors (an 8-bit color card). There is significant improvement in OpenGL’s speed and appearance when you give it a good color depth to work with. If you can run in a mode that gives you 65,000 or more colors, your results will be even better.
With the exception of two chapters that specifically deal with C++ frameworks, all the source code in this book is written in C. The choice between C and C++ can become an almost religious crusade between two warring camps. It is reasonable to expect that any competent C++ programmer can also follow well-structured C code, but the converse is not always true. There is a popular C++ library for OpenGL called Open Inventor; any attempt here to build a C++ class library around OpenGL would be a duplication of an already fine effort and is beyond the scope and purpose of this book anyway. This brings us to our choice of tools.
All of the sample code was originally developed using Microsoft’s Visual C++ 4.0. (Yes, you can compile C with it!) With each sample you will find Visual C++ project files. Since all samples are in C and make no use of vendor-specific libraries, you shouldn’t have any trouble building the projects with any other 32-bit compiler. I will assume that you are familiar with your environment of choice and know how to add libraries and header files to your projects.
For programmers who prefer C++ application frameworks such as MFC or OWL, chapters are included that deal with these two in particular. In addition, many of the C samples are also provided in an MFC (Visual C++) version and an OWL (Borland C++) version.These samples can be found in the \MFC and \OWL subdirectories on the CD. Project files for the Borland Compiler are also provided for these samples, prepared using Borland C++ 5.0.
Another special consideration has been made for users of Borland tools: the CD contains a Borland-specific version of the OpenGL Auxiliary library. This library isn’t part of the official OpenGL specification, but it is usually implemented on the same various platforms as OpenGL. For reasons unknown, Borland includes a header file for this library but not the library itself, and the version of the AUX library that ships with Microsoft tools is incompatible with Borland C++. For additional notes on using Borland C++ with this book, see the \Borland subdirectory on the CD.
What’s in This Book
OpenGL SuperBible is divided into four sections. Part I is an introduction to OpenGL and the fundamentals of using it from within Microsoft Windows. In Part II we cover the basics of programming with OpenGL. This includes primitives, viewing and modeling transformations, lighting, and texture mapping. In Part III we dig into some of the more advanced topics and functionality within OpenGL—the OpenGL State Machine, special visual effects, more detail on the OpenGL buffers, advanced surface generation, and some interactive graphics. For Part IV, we’ve added supplementary information on using OpenGL from different programming environments (MFC, OWL, and Visual Basic). Finally, there’s a discussion of the future of OpenGL under Windows.
Part I: Introduction to OpenGL
Chapter 1 - What Is OpenGL?
In this chapter, we provide you with a working knowledge of what OpenGL is, where it came from, and where it is going. We also discuss at a high level the differences between and compatibilities of OpenGL and the Microsoft Windows graphics system.
Chapter 2 - 3D Graphics Fundamentals
This chapter is for newcomers to 3D graphics. It introduces fundamental concepts and some common vocabulary.
Chapter 3 - Learning OpenGL with the AUX Library
In this chapter, you will begin writing programs that use OpenGL. For starters, we’ll make things simple by using the AUX library. This common toolkit library is platform- and windowing system-independent. We also cover OpenGL function and variable naming conventions, as well as the DLLs and libraries that contain the OpenGL functionality.
Chapter 4 - OpenGL for Windows: OpenGL + Win32 = Wiggle
Here you’ll begin writing 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. We’ll also talk about which Windows messages should be responded to and how.
Chapter 5 - Errors and Other Message from OpenGL
We’ll explore OpenGL’s method of reporting errors, and how it provides information about its version and vendor.
Part II: Using OpenGL
Chapter 6 - Drawing in 3D: Lines, Points, and Polygons
Here you’ll learn how all 3D objects are created by assembling 2D primitives. All the OpenGL primitives are covered, as well as how to hide surfaces within your scenes.
Chapter 7 - Manipulating 3D Space: Coordinate Transformations
In this chapter you’ll learn about moving your objects or view within your scenes. You’ll learn how to rotate, translate, and scale. We take a simplified approach to our study of matrix transformations, so you will understand how to use them even if youdon’t know the first thing about matrices.
Chapter 8 - Color and Shading
Here you’ll learn how to liven up your objects by adding color. Shading objects smoothly from one color to another will be child’s play after you’ve completed this chapter. We also show you how and why you need to construct a 3-3-2 palette for OpenGL when your code runs on a 256-color video card.
Chapter 9 - Lighting and Lamps
OpenGL supports up to eight independent light sources per scene. You’ll learn how to use these lamps, how to set lighting parameters and properties, and how they interact with reflective material properties that you can assign to your objects.
Chapter 10 - 3D Modeling and Object Composition
For this chapter, we show you how to build complex 3D objects out of smaller, less complex 3D objects. We also introduce OpenGL display lists as a method of breaking down your objects and improving performance, as well.
Chapter 11 - Raster Graphics in OpenGL
In this chapter you’ll learn how to manipulate bitmap graphics from within OpenGL. This includes reading in a Windows .BMP file and displaying it in an OpenGL scene.
Chapter 12 - Texture Mapping
Texture mapping is one of the most useful features of any 3D graphics toolkit. You’ll learn how to wrap bitmaps onto polygons, and how to use automatic texture coordinate generation.
Chapter 13 - Quadrics: Spheres, Cylinders, and Disks
This chapter covers the OpenGL Utility library (glu) functions for quickly constructing some common shapes.
Part III: Advanced Topics and Special Effects
Chapter 14 - The OpenGL State Machine
Many global OpenGL parameters and settings are maintained via the OpenGL State Machine. In this chapter you’ll learn about this mechanism, as well as some generalized functions for setting and accessing the various parameters.
Chapter 15 - Buffers: Not Just for Animation
This chapter goes into more depth about the various OpenGL buffers. As you’ll see, they’re not just for doing screen flipping.
Chapter 16 - Visual Effects, Blending, and Fog
Some other visual special effects are covered in this chapter. These include alpha blending and fog effects for transparency and depth cues.
Chapter 17 - Curves and Surfaces: What the #%@!&* Are NURBS?
This chapter explores the utility functions that evaluate Bazier and NURBS curves and surfaces. You can use these functions to create complex shapes with a small amount of code.
Chapter 18 - Polygon Tessellation
Here you’ll learn how to break down complex or concave polygons into smaller, more manageable pieces.
Chapter 19 - 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.
Chapter 20 - OpenGL on the ‘Net: VRML
This chapter introduces VRML (Virtual Reality Modeling Language) and its history with OpenGL. Open Inventor is discussed, as well, and its relationship to OpenGL and VRML.
Part IV: OpenGL with...
Chapter 21 - MFC-Based OpenGL Programming
This chapter is for C++ programmers using Microsoft’s MFC class library. We’ll show you how to use OpenGL from an MFC-based application, and how to add rendering capabilities to any CWnd window.
Chapter 22 - OWL-Based OpenGL Programming
This chapter is for C++ programmers using Borland C++ and the OWL application framework. You’ll learn how to add OpenGL rendering capabilities to any OWL TWindow-derived window.
Chapter 23 - OpenGL Programming from Visual Basic and 4GL
In this chapter we give you an OCX that wraps most of the OpenGL functions and commands. This allows easy OpenGL programming from Visual Basic (4.0 or later) or any 32-bit environment that supports OCXs. Examples are given for both Visual Basic 4.0 and Delphi 2.0.
Chapter 24 - The Future of OpenGL and Windows
This chapter looks at the future of 3D graphics and OpenGL in Windows. We discuss the implications of the Microsoft DirectX API, which includes Direct Draw, Direct Sound, Direct Play, Direct Input, and Direct 3D, and will ultimately incorporate the Reality Labs 3D API.
Appendix A - Performance-Tuning OpenGL for Windows
Here we will provide some general-purpose performance-tuning tips for using OpenGL under Windows NT and Windows 95.
Appendix B - Further Reading
A list of additional reading materials is provided for more in-depth research on any of the topics covered by this book.
Appendix C - OpenGL Version 1.1
OpenGL 1.1 was finalized during development of this book. The new functions and capabilities are not covered here, but Appendix C gives you a high-level overview of the new version’s additions. The CD also contains more up-to-date and complete documentation on the new functions and capabilities being added for Windows NT 4.0, as well as some example programs.
Appendix D - Glossary
A glossary of common 3D graphics and OpenGL terms.
About the Companion CD
OpenGL SuperBible comes with a CD-ROM that’s jam-packed with samples and other OpenGL goodies. A directory called Book, off the root directory of the CD, contains all the source code from the book. In addition, there are many examples demonstrating the concepts presented from each chapter that may not have been described in the text of the book.
Each chapter of the book has its own subdirectory in the Book directory. Within each chapter subdirectory is another subdirectory for each example on the disk. For instance, the bouncing square program from Chapter 3 is located in the X:\Book\Chapt3\bounce subdirectory (where X is your CD-ROM drive).
Some of the chapter directories have a subdirectory called \Tank. This is a roving tank/robot simulation program that we observe as we progress through the book. Though it’s not analyzed chapter by chapter, the simulation becomes more complex as we gradually add more of the functions and features of OpenGL. See the readme.txt file for details on the construction of this example program.
Some of the sample programs from each chapter will also be written in C++ using MFC or OWL. These sample programs are under X:\MFC\ or X:\OWL\. Again, within the MFC and OWL subdirectories there is an additional directory for each chapter.
The two final major subdirectories in the CD root are \Borland and \OpenGL11. The \Borland subdirectory contains a Borland-specific version of the AUX library. See the readme.txt file in that directory for details on the library’s functionality and use.The \OpenGL11directory contains a document describing the OpenGL 1.1 additions that Microsoft is incorporating for Windows NT 4.0. In addition, you’ll also find several example programs that demonstrate these new capabilities.
Be sure to consult the file readme.txt in the root directory for any late-breaking news or additions to the content of the CD. This file also contains a complete listing of all the files and programs on the CD ROM.
If you are learning OpenGL or 3D graphics for the first time, then I sincerely envy you. Nothing is more satisfying and just plain fun than learning a new technology or tool for the first time. Although OpenGL has its roots in scientific modeling and simulation, you don’t need to be a rocket scientist to master it. The step-by-step approach taken throughout this book will guide you to new levels of programming skill. Learning OpenGL is comparable to learning SQL for database programming. Before I knew SQL, I could not quite imagine the new power I would wield as a database developer. If you have been tinkering with 3D graphics or are just wanting to get started, you are only just beginning to glimpse the new power and capabilities that OpenGL will afford you!
—Richard S. Wright, Jr.