Open GL Super Bible

Previous Table of Contents Next

Display Mode: Single-Buffered

The next line of code

auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);

tells the AUX library what type of display mode to use when creating the window. The flags here tell it to use a single-buffered window (AUX_SINGLE) and to use RGBA color mode (AUX_RGBA). A single-buffered window means that all drawing commands are performed on the window displayed. An alternative is a double-buffered window, where the drawing commands are actually executed to create a scene off screen, then quickly swapped into view on the window. This is often used to produce animation effects and will be demonstrated later in this chapter. RGBA color mode means that you specify colors by supplying separate intensities of red, green, and blue components (more on color modes in Chapter 8).

Position the Window

After setting the display mode, you need to tell the AUX library where to put the window and how big to make it. The next line of code does this:


The parameters represent the upper-left corner of the window and its width and height. Specifically, this line tells the program to place the upper-left corner at coordinates (100,100), and to make the window 250 pixels wide and 250 pixels high. On a screen of standard VGA resolution (640 x 480), this window will take up a large portion of the display. At SuperVGA resolutions (800 x 600 and above), the window will take less space even though the number of pixels remains the same (250 x 250).

Here is the prototype for this function:

auxInitPosition(GLint x, GLint y, GLsizei width, GLsizei height);

The GLint and GLsizei data types are defined as integers (as described in the earlier section about data types). The x parameter is the number of screen pixels counted from the left side of the screen, and y is the number of pixels counted down from the top of the screen. This is how Windows converts desktop screen coordinates to a physical location by default. OpenGL’s default method for counting the x coordinate is the same; however, it counts the y coordinate from bottom to top—just the opposite of Windows. See Figures 3-3 and 3-4.

Figure 3-3  Default Windows screen coordinate mapping

Figure 3-4  Default OpenGL window coordinate mapping

Porting Note
Although Windows maps desktop coordinates as shown in Figure 3-3, the X Window System maps desktop coordinates the same way that OpenGL does in Figure 3-4. If you are porting an AUX library program from another environment, you may need to change the call to auxInitPosition() to account for this.

Create the OpenGL Window

The last call to the AUX library actually creates the window on the screen. The code

auxInitWindow("My first OpenGL Program");

creates the window and sets the caption to “My first OpenGL Program.” Obviously, the single argument to auxInitWindow is the caption for the window title bar. If you stopped here, the program would create an empty window (black background is the default) with the caption specified, and then terminate, closing the OpenGL window immediately. The addition of our last getch() prevents the window from disappearing, but still nothing of interest happens in the window.

Clear a Window (Erase with a Color)

The three lines of code we’ve looked at so far from the AUX library are sufficient to initialize and create a window that OpenGL will draw in. From this point on, all OpenGL commands and function calls will operate on this window.

The next line of code

glClearColor(0.0f, 0.0f, 1.0f, 0.0f);

is your first real OpenGL function call. This function sets the color used when clearing the window. The prototype for this function is

void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf

GLclampf is defined as a float under most implementations of OpenGL. In OpenGL, a single color is represented as a mixture of red, green, and blue components. The range for each component can vary from 0.0 to 1.0. This is similar to the Windows specification of colors using the RGB macro to create a COLORREF value. (See the Windows95 API Bible from Waite Group Press for details.) The difference is that in Windows each color component in a COLORREF can range from 0 to 255, giving a total of 256 x 256 x 256—or over 16 million colors. With OpenGL, the values for each component can be any valid floating-point value between 0 and 1, thus yielding a theoretically infinite number of potential colors. Practically speaking, OpenGL represents colors internally as 32-bit values, yielding a true maximum of 4,294,967,296 colors (called true color on some hardware). Thus the effective range for each component is from 0.0 to 1.0, in steps of approximately .00006.

Naturally, both Windows and OpenGL take this color value and convert it internally to the nearest possible exact match with the available video hardware and palette. We’ll explore this more closely in Chapter 8.

Previous Table of Contents Next