Open GL Super Bible

Previous Table of Contents Next


Table 3-3 lists some common colors and their component values. These values can be used with any of the OpenGL color-related functions.

Table 3-3 Some common composite colors

Composite Color Red Component Green Component Blue Component

Black 0.0 0.0 0.0
Red 1.0 0.0 0.0
Green 0.0 1.0 0.0
Yellow 1.0 1.0 0.0
Blue 0.0 0.0 1.0
Magenta 1.0 0.0 1.0
Cyan 0.0 1.0 1.0
Dark gray 0.25 0.25 0.25
Light gray 0.75 0.75 0.75
Brown 0.60 0.40 0.12
Pumpkin orange 0.98 0.625 0.12
Pastel pink 0.98 .04 0.7
Barney purple 0.60 0.40 0.70
White 1.0 1.0 1.0

The last argument to glClearColor() is the alpha component. The alpha component is used for blending and special effects such as translucence. Translucence refers to an object’s ability to allow light to pass through it. Suppose you are representing a piece of red stained glass, but a blue light is shining behind it. The blue light will affect the appearance of the red in the glass (blue + red = purple). You can use the alpha component value to make a blue color that is semitransparent; so it works like a sheet of water—an object behind it shows through. There is more to this type of effect than the alpha value, and in Chapter 16 we will write an example program that demonstrates it; until then you should leave this value as 1.

Actually Clear

Now that we have told OpenGL what color to use for clearing, we need an instruction to do the actual clearing. This accomplished by the line

glClear(GL_COLOR_BUFFER_BIT);

The glClear() function clears a particular buffer or combination of buffers. A buffer is a storage area for image information. The red, green, and blue components of a drawing actually have separate buffers, but they are usually collectively referred to as the color buffer.

Buffers are a powerful feature of OpenGL and will be covered in detail in Chapter 15. For the next several chapters, all you really need to understand is that the color buffer is where the displayed image is stored internally, and that clearing the buffer with glClear removes the drawing from the window.

Flush That Queue

Our final OpenGL function call comes next:

glFlush();

This line causes any unexecuted OpenGL commands to be executed—we have two at this point: glClearColor() and glClear().

Internally, OpenGL uses a rendering pipeline that processes commands sequentially. OpenGL commands and statements often are queued up until the OpenGL server processes several “requests” at once. This improves performance, especially when constructing complex objects. Drawing is accelerated because the slower graphics hardware is accessed less often for a given set of drawing instructions. (When Win32 was first introduced, this same concept was added to the Windows GDI to improve graphics performance under Windows NT.) In our short program, the glFlush() function simply tells OpenGL that it should proceed with the drawing instructions supplied thus far before waiting for any more drawing commands.

The last bit of code for this example

// Stop and wait for a keypress
cprintf("Press any key to close the Window\n");
getch();
}

displays a message in the console window and stops the program until you press a key, at which point the program is terminated and the window is destroyed.

It may not be the most interesting OpenGL program in existence, but shortest.c demonstrates the very basics of getting a window up using the AUX library and it shows you how to specify a color and clear the window. Next we want to spruce up our program by adding some more AUX library and OpenGL functions.

Drawing Shapes with OpenGL

The shortest.c program made an empty window with a blue background. Let’s do some drawing in the window. In addition, we want to be able to move and resize the window so that it behaves more like a Windows window. We will also dispense with using getch() to determine when to terminate the program. In Listing 3-2 you can see the modifications.

The first change you’ll notice is in the headers. The conio.h file is no longer included because we aren’t using getch() or cprintf() anymore.

Listing 3-2 A friendlier OpenGL program

// friendly.c
// A friendlier OpenGL program

#include <windows.h>     // Standard header for Windows
#include <gl\gl.h>       // OpenGL library
#include <gl\glaux.h>    // AUX library

// Called by AUX library to draw scene
void CALLBACK RenderScene(void)
        {
        // Set clear color to blue
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

        // Clear the window
        glClear(GL_COLOR_BUFFER_BIT);

        // Set current drawing color to red
        //     R        G       B
        glColor3f(1.0f, 0.0f, 0.0f);

        // Draw a filled rectangle with current color
        glRectf(100.0f, 150.0f, 150.0f, 100.0f);

        glFlush();
        }

void main(void)
        {
        // AUX library window and mode setup
        auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
        auxInitPosition(100,100,250,250);
        auxInitWindow("My second OpenGL Program");

        // Set function to call when window needs updating
        auxMainLoop(RenderScene);
        }


Previous Table of Contents Next