Open GL Super Bible

Previous Table of Contents Next


Part III
Advanced Topics and Special Effects

If you’ve been reading this book from front to back as a tutorial, you are now quite well grounded in the use of OpenGL for a variety of purposes. In the third part of this book, we are going to cover a few remaining topics that will enhance your knowledge and understanding of OpenGL. We will also be covering some special effects and capabilities of the API that may take a little more time to digest than the previous material.

First, we visit the OpenGL State Machine in Chapter 14. Until now we have taken this for granted and covered some of the variables only when they have been relevant to our discussion. Now a look at the entire concept and how to take advantage of it is in order. Then a more complete discussion of the OpenGL buffers (Chapter 15) will be in order.

Many scenes and objects can benefit by some of the visual fine-tuning that is afforded by the techniques presented in Chapter 16. Here you will see how to soften or sharpen images, and how to create some spectacular effects made possible with translucence.

Complex surface generation can be a real headache. Chapter 17 will give you some high-level tools that can make these surfaces easier to create. Useful techniques for breaking down your polygons into smaller ones are shown in Chapter 18, and in Chapter 19 you’ll learn how to interact with your scenes and objects using the OpenGL features of selection and feedback.

Finally, we will end our coverage of the API with a closer look at just one use for OpenGL. You’ll see how Virtual Reality over the Internet has its roots in an OpenGL C++ class library called Open Inventor.

Chapter 14
The OpenGL State Machine

What you’ll learn in this chapter:

How to… Functions You’ll Use

Enable and disable rendering options glEnable/glDisable
Query the state of rendering options glIsEnabled/glGetInteger/glGetFloat/glGetDouble
Save and restore some or all of the current state glPushAttrib/glPopAttrib

The rendering state is one of the things that make OpenGL so fast and efficient at drawing 3D graphics. This state is grouped logically into different categories such as color, lighting, texturing, and so forth. Each rendering context (HRC) that you create has its own rendering state specific to a window or off-screen bitmap.

Unlike most of the other chapters, this chapter does not contain any complete example programs. Rather, you will find these state functions used in examples for every other chapter in the book.

Basic OpenGL State Functions

OpenGL’s two functions that enable and disable rendering features are called, appropriately enough, glEnable and glDisable. You pass these functions a single enumerated constant, such as GL_DEPTH_TEST, as follows:

glEnable(GL_DEPTH_TEST);    /* Enable depth buffer testing */
glDisable(GL_DEPTH_TEST);   /* Disable depth buffer testing */

You can retrieve the current state using glIsEnabled, glIsDisabled, and glGetBooleanv, as in the following:

GLboolean state;

/*
 * GL_TRUE if depth testing is enabled…
 */
state = glIsEnabled(GL_DEPTH_TEST);

/*
 * GL_TRUE if depth testing is disabled…
 */
state = glIsDisabled(GL_DEPTH_TEST);

/*
 * Returns Boolean state value; GL_TRUE if depth testing is enabled…
 */
glGetBooleanv(GL_DEPTH_TEST, &state);

Most OpenGL state variables are Boolean values, on or off. Some, like the current viewport, are an array of integers, or an array of floating point numbers for the current RGBA color. To address these types of state values, OpenGL adds glGetDoublev, glGetFloatv, and glGetInteger:

GLint      istate[4];
GLfloat    fstate[4];
GLdouble   dstate[3];

glGetIntegerv(GL_VIEWPORT, istate);
glGetFloatv(GL_CURRENT_COLOR, fstate);
glGetDoublev(GL_CURRENT_NORMAL, dstate);

You’ll learn more about the various state variables further into the chapter.


Previous Table of Contents Next