QuickTime, Texturing, and OpenGL

The movies I’ve examined don’t have an alpha channel so I’m using RGB and it works fine.

// Create an off-screen gworld where QT can draw
gworldBuffer = calloc(width * height * 3, 1);
QTNewGWorldFromPtr(&gworld, k24RGBPixelFormat, &movieBox, NULL, NULL, 0,
gworldBuffer, movieBox.right * 3);
gworldPixMap = GetGWorldPixMap(gworld);
LockPixels(gworldPixMap);
gworldPixBase = GetPixBaseAddr(gworldPixMap);

And then later on when I want to render to the texture:

// Yield to the movie so it can draw the newest frame
MoviesTask(sMovie, 0);
// The image we get from QuickTime is going to be upside down from the perspective of GL.

// Either render your movie upside down, or use UV mapping in the scene to fix this by flipping the y axis.
glPushAttrib(GL_TEXTURE_BIT);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, movieWidth, movieHeight,
GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)gworldPixBase);
glPopAttrib();

I’m using GWorlds instead of the more modern APIs because my app has to be cross-platform, and there doesn’t seem to be a way to create an OpenGL graphics context on Windows. I’ve filed a Radar bug for this and have hopes it’ll make Leopard or a new release of QuickTime.

If your movie does have an alpha channel or you want to use 32-bit instead of 24-bit textures for some reason, the situation is also pretty easy to handle, though it appears tricky at first because OpenGL wants RGBA or GBRA while the GWorld wants ARGB. Use the k32ARGBPixelFormat when you get your new GWorld, and then later when you render it to the texture, you can use GL_GBRA and tell OpenGL the bytes are reversed, depending on your machine’s byte order:


#if __BIG_ENDIAN__
TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
#else
TYPE = GL_UNSIGNED_INT_8_8_8_8;
#endif

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, movieWidth, movieHeight,
GL_BGRA, TYPE, (GLvoid *)gworldPixBase);

I tried posting this to the QuickTime developers mailing list in response to a question, but it was bounced by the list administrators because it exceeded the list’s maximum posting size of 12 kilobytes — my post was something like 22 kilobytes with formatting. Horrors! I always wondered why that mailing list was so terse and unhelpful, and now I suspect it’s because people are discouraged from posting examples.

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.