Open GL Super Bible

Previous Table of Contents Next


Pixel Formats

Drawing in a window with OpenGL also requires that you select a pixel format. Like the rendering context, the pixel format is not really a part of OpenGL per se. It is an extension to the Win32 API (specifically, to the GDI) to support OpenGL functionality. The pixel format sets a device context’s OpenGL properties, such as color and buffer depth, and whether the window is double-buffered. You must set the pixel format for a device context before it can be used to create a rendering context. Here are the two functions you will need to use:

int ChoosePixelFormat(HDC hDC, PIXELFORMATDESCRIPTOR *ppfd)
BOOL SetPixelFormat(HDC hDC, int
iPixelFormat, IXELFORMATDESCRIPTOR  *ppfd)

Setting the pixel format is a three-step process. First, you fill out the PIXELFORMATDESCRIPTOR structure according to the characteristics and behavior you want the window to possess (we’ll examine these fields shortly). You then pass this structure to the ChoosePixelFormat function. The ChoosePixelFormat function returns an integer index to an available pixel format for the specified device context. This index is then passed to the SetPixelFormat function. The sequence looks something like this:

PIXELFORMATDESCRIPTOR pixelFormat;
int nFormatIndex;
HDC hDC;

// initialize pixelFormat structure
….
….

nFormatIndex = ChoosePixelFormat(hDC, &pixelFormat);
SetPixelFormat(hDC, nPixelFormat, &pixelFormat);

ChoosePixelFormat attempts to match a supported pixel format to the information requested in the PIXELFORMATDESCRIPTOR structure. The returned index is the identifier for this pixel format. For instance, you may request a pixel format that has 16 million colors on screen, but the hardware may only support 256 simultaneous colors. In this case, the returned pixel format will be as close an approximation as possible—for this example, a 256-color pixel format. This index is passed to SetPixelFormat.

You’ll find a detailed explanation of the PIXELFORMATDESCRIPTOR structure in the Reference Section under the function DescribePixelFormat. Listing 4-1 shows a function from the GLRECT sample program that establishes the PIXELFORMATDESCRIPTOR structure and sets the pixel format for a device context.

Listing 4-1 A high-level function that sets up the pixel format for a device context

/ Select the pixel format for a given device context
void SetDCPixelFormat(HDC hDC)
        {
        int nPixelFormat;

        static PIXELFORMATDESCRIPTOR pfd = {
                sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
                1,                              // Version of this
                                                    structure

                PFD_DRAW_TO_WINDOW |            // Draw to window
                                                    (not bitmap)

                PFD_SUPPORT_OPENGL |            // Support OpenGL calls
                PFD_DOUBLEBUFFER,               // Double-buffered mode
                PFD_TYPE_RGBA,                  // RGBA Color mode
                24,                             // Want 24bit color
                0,0,0,0,0,0,                    // Not used to select mode
                0,0,                            // Not used to select mode
                0,0,0,0,0,                      // Not used to select mode
                32,                             // Size of depth buffer
                0,                              // Not used to select mode
                0,                              // Not used to select mode
                PFD_MAIN_PLANE,                 // Draw in main plane
                0,                              // Not used to select mode
                0,0,0 };                   // Not used to select mode

         // Choose a pixel format that best matches that described in pfd
         nPixelFormat = ChoosePixelFormat(hDC, &pfd);

         // Set the pixel format for the device context
         SetPixelFormat(hDC, nPixelFormat, &pfd);
         }

As you can see in this example, not all the members of the PIXELFORMATDESCRIPTOR structure are used when requesting a pixel format. Table 4-1 lists the members that are set in Listing 4-1. The rest of the data elements can be set to zero for now.

Table 4-1 Members of PIXELFORMATDESCRIPTOR used when requesting a pixel format

Member Description

nSize The size of the structure, set to sizeof(PIXELFORMATDESCRIPTOR).
nVersion The version of this data structure, set to 1.
dwFlags Flags that specify the properties of the pixel buffer, set to (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER). These indicate the device context is not a bitmap context, that OpenGL will be used for drawing, and that the window should be double buffered.
iPixelType The type of pixel data. Actually, tells OpenGL to use RGBA mode or color index mode. Set to PFD_TYPE_RGBA for RGBA mode.
cColorBits The number of color bitplanes, in this case 24-bit color. If hardware does not support 24-bit color, the maximum number of color bitplanes supported by the hardware will be selected.
cDepthBits The depth of the depth (z-axis) buffer. Set to 32 for maximum accuracy, but 16 is often sufficient (see Reference Section).
iLayerType The type of layer. Only PFD_MAIN_PLANE is valid for the Windows implementation of OpenGL.


Previous Table of Contents Next