Loading Textures with QuickTime

Author: Mark Szymczyk
Last Update: June 20, 2006

Most OpenGL games use texture mapping. There are many online articles and tutorials on OpenGL texture mapping but they gloss over loading the textures from disk. The reason many articles gloss over loading textures is that the code to load a texture depends on the texture’s file type and the operating system on which the code is running. This article fills a gap for Mac OS X developers by showing how to use QuickTime to load textures.

Updates

Eliminated the mention that textures should be at least 64 pixels for widest compatibility. There is no problem with having textures smaller than 64 pixels. I misread a statement in the OpenGL Red Book. (June 20, 2006)

Introduction

QuickTime is Apple’s multimedia technology, and it is huge. It plays video and audio, which I covered in a previous article. But QuickTime can also load image files and draw the images by using graphics importers. QuickTime graphics importers can import JPEG, GIF, PICT, TIFF, PNG, TGA, and BMP (Windows bitmap) files. You can load textures in any of these formats with one set of code.

To use QuickTime’s graphics importers to load textures, you must perform the following steps:

  1. Open a graphics importer.
  2. Retrieve information about the texture image, including its width, height, and color depth.
  3. Create an offscreen buffer to draw the texture.
  4. Draw the texture.

Before you can open a graphics importer you must locate the texture file and let QuickTime know where it is.

Setting Up Your Xcode Project

To use QuickTime to load textures, you must add the QuickTime framework and the texture files to your Xcode project. Choose Project > Add To Project to add files and frameworks to your project. The QuickTime framework should be in the directory System/Library/Frameworks. In your source code files you must add the header files Movies.h and QuickTimeComponents.h.

#include < QuickTime/Movies.h >
#include < QuickTime/QuickTimeComponents.h >

The code for this article uses the Carbon framework, but there’s no reason why you can’t use the code in a Cocoa application. The area of the code you’ll change is the code that finds the file in the application bundle. The code uses the Core Foundation framework, which is the C equivalent of Cocoa’s foundation classes. A Core Foundation string variable, CFStringRef, is the same as a pointer to a NSString object. A Cocoa application would replace the Core Foundation code (the variables and functions have the prefix CF) with the Cocoa equivalent.

Next (Finding the File in the Application Bundle)