Drawing the Texture

After opening a graphics importer and retrieving the necessary information about the texture, you can go ahead and draw the texture. There are three steps to drawing the texture.

  1. Create an offscreen buffer to hold the texture.
  2. Draw the image in the offscreen buffer.
  3. Specify the texture in OpenGL so you can use the texture in your OpenGL application.

Creating the Offscreen Buffer

QuickTime’s graphic importers need offscreen buffers to draw images. Call the function QTNewGWorldFromPtr() to create an offscreen buffer. This function takes eight arguments.

  • The buffer QTNewGWorldFromPtr() creates.
  • The pixel format.
  • The boundary rectangle for the buffer.
  • A color table handle. You can pass NULL. Mac OS X has poor support for color depths that use color tables.
  • A handle to a graphics device, which is a destination for drawing such as the screen or a printer. Passing NULL tells the operating system to use the main graphics device, which works well for Macs with one monitor. On Macs with multiple monitors, passing NULL uses the monitor that is set as the main monitor.
  • Flags. If you’re creating a universal binary, you should pass the flag kNativeEndianPixMap. If you don’t pass this flag, the operating system creates a big-endian pixel map, which causes a performance hit on Intel Macs.
  • A pointer to the image data.
  • The number of bytes in one row of the image.

Pixel formats deserve more explanation. Each pixel has a color value with three color components: red, green, and blue. The color value may have a fourth component, alpha, that measures transparency. The pixel format defines the size of each color component. There are four pixel formats for QTNewGWorldFromPtr() on Mac OS X.

  • k16BE555PixelFormat creates a 16-bit color buffer with PowerPC byte ordering. Each pixel has 5 bits of red, green, and blue. The last bit is not used.
  • k16LE555PixelFormat creates a 16-bit color buffer with Intel byte ordering.
  • k32ARGBPixelFormat creates a 32-bit color buffer with PowerPC byte ordering. Each pixel has 8 bits of alpha followed by eight bits of red, eight bits of green, and eight bits of blue. This pixel format is the Mac’s native pixel format and is the one you’ll end up using most.
  • k32BGRAPixelFormat creates a 32-bit color buffer with Intel byte ordering.

Unless you’re writing code specifically for Intel Macs, you should use one of the pixel formats with PowerPC byte ordering. Macs with an Intel processor can use pixel formats with PowerPC byte ordering, but PowerPC Macs cannot use pixel formats with Intel byte ordering. The following code demonstrates how to create an offscreen buffer using QuickTime:

Ptr imageData;
GworldPtr offscreenBuffer;
long bytesPerRow;
OSErr error;

bytesPerRow = width * depth / 8;
error = QTNewGWorldFromPtr(&offscreenBuffer, k32ARGBPixelFormat,
        &imageRect, NULL, NULL, kNativeEndianPixMap,
        imageData, bytesPerRow);

Drawing the Image in the Offscreen Buffer

After creating the offscreen buffer, you must set the graphics importer to use the offscreen buffer and draw the texture. Call the function GraphicsImportSetGWorld() to set the graphics importer. Call GraphicsImportDraw() to draw the texture.

ComponentResult error;

error = GraphicsImportSetGWorld(fileImporter, offscreenBuffer, NULL);
error = GraphicsImportDraw(fileImporter);

Next (Specifying the Texture in OpenGL)

Previous (Retrieving Information about the Texture)