glReadPixels problem…Resolved!

September 29th, 2005

I have been pulling my hair out lately trying to resolve two problems with openGL and QuickTime. I am working on a soon to be killer-app (wishful thinking), for the mac platform and it has been a great experience thus far. (I’ll have a beta out soon to get some community feedback.)

The first major problem I ran into (major being classified as taking me more than a day to figure out the problem) involved the glReadPixels function. As you all know, this function reads pixels from the frame buffer into some memory puffer that you point to.

Well it would work fine some times, but other times the image it would read was warped diagonally. There didn’t seem to be a pattern. Finally I realized that I had set GL_PACK_ALIGNMENT incorrectly. I had it set to 8 (double word boundaries) when it should have been set to 4 (word boundaries). I changed this and all of my problems went away. (at least that one)

From:

glPixelStorei(GL_PACK_ALIGNMENT,8);

to:

glPixelStorei(GL_PACK_ALIGNMENT,4);

The next problem had to do with storing image frames in a QuickTime movie object. For some reason, whenever the application wrote the final frame, the frame appeared to be copied back to the video buffer. This resulted in the frame appearing in the upper left corner of the screen and remaining there until Quartz repainted that area (i.e. a window dragging over it). This turned out to be an easy fix, I had not released my movie object enough times.

If you don’t know what release/retain counts are, they are Objective-C’s way of handling object allocation/deallocation. It seemed like a pain to deal with at first, but it actually makes for good design when you follow the rules.

For more information on Retain/Release counting, see this article from stepwise and this article from Apple.

Leave a Reply