Open GL Super Bible

Previous Table of Contents Next


Interestingly enough, the Windows StretchBlt function can display bitmap images faster than glDrawPixels. Of course, StretchBlt cannot perform the glPixelMap and glPixelTransfer functions, though.

The final code for the RepaintWindow function is in Listing 11-12.

Listing 11-12 RepaintWindow function

void
RepaintWindow(RECT *rect) /* I - Client area rectangle */
{
  GLint   xoffset,        /* X offset of image */
          yoffset;        /* Y offset of image */
  GLint   xsize,          /* X size of scaled image */
          ysize;          /* Y size of scaled image */
  GLfloat xscale,         /* Scaling in X direction */
          yscale;         /* Scaling in Y direction */

 /*
  * Reset the viewport and clear the window to white…
  */

  glViewport(0, 0, rect->right, rect->bottom);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, rect->right - 1.0, 0.0, rect->bottom - 1.0, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);

  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);

 /*
  * If we have loaded a bitmap image, scale it to fit the window…
  */

  if (BitmapBits != NULL)
  {
    xsize = rect->right;
    ysize = BitmapInfo->bmiHeader.biHeight * xsize /
            BitmapInfo->bmiHeader.biWidth;
    if (ysize > rect->bottom)
    {
      ysize = rect->bottom;
      xsize = BitmapInfo->bmiHeader.biWidth * ysize /
              BitmapInfo->bmiHeader.biHeight;
    };

    xscale  = (float)xsize / (float)BitmapInfo->bmiHeader.biWidth;
    yscale  = (float)ysize / (float)BitmapInfo->bmiHeader.biHeight;

    xoffset = (rect->right - xsize) * 0.5;
    yoffset = (rect->bottom - ysize) * 0.5;

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glPixelZoom(xscale, yscale);
    glRasterPos2i(xoffset, yoffset);
    glDrawPixels(BitmapInfo->bmiHeader.biWidth,
                 BitmapInfo->bmiHeader.biHeight,
                 GL_RGB, GL_UNSIGNED_BYTE, BitmapBits);
  };

  glFinish();
}

Summary

In this chapter you have learned about most of the OpenGL bitmap functions. Beyond the simple application of character fonts, bitmaps can be full-color images for window backgrounds or texture images (explored in the chapter coming up). OpenGL functions such as glPixelMap, glPixelTransfer, and glPixelZoom can be used for special effects, as well.

Reference Section

glCopyPixels

Purpose
Copies a rectangular block of pixels in the frame buffer.
Include File
<GL/gl.h>
Syntax
void glCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type);
Description
This function copies pixel data from the indicated area in the framebuffer to the current raster position. Use glRasterPos to set the current raster position. If the current raster position is not valid, then no pixel data is copied.
Calls to glPixelMap, glPixelTransfer, and glPixelZoom affect the operation of glCopyPixels, as indicated in their pages in this Reference Section.

Parameters

x
GLint: The lower-left corner window horizontal coordinate.
y
GLint: The lower-left corner window vertical coordinate.
width
GLsizei: The width of the image in pixels.
height
GLsizei: The height of the image in pixels. If negative, the image is drawn from top to bottom. By default, images are drawn bottom to top.
type
GLenum: The type of pixel values to be copied. Valid types are as follows:
GL_COLOR Color buffer values
GL_STENCIL Stencil buffer values
GL_DEPTH Depth buffer values
Returns
None.
Example
See the example in CH11\OGLVIEW.C.
See Also
glPixelMap, glPixelStore, glPixelTransfer, glPixelZoom


Previous Table of Contents Next