Take the 2-minute tour ×

I want to capture the screen of my game using glreadpixel(). it works fine over simulator also on 2g iphone with ios version 3.1.1 . but on ipad with ios version 4.2.1 it doesnt . i came to know the issue regarding this. for ios version 4.0 above on a particular device (ipad) we bind depth buffer and use anti-aliasing technique. And when we use glreadpixel() of opengl that capture data from frame buffer returns all 0 in the destination buffer...

if we dont bind the depth buffer to frame buffer and dont use the anti-aliasing technique it works fine.

the code i used is :-

CGRect screenBounds = [[UIScreen mainScreen] bounds];

int backingWidth = screenBounds.size.width;
int backingHeight =screenBounds.size.height;

NSLog(@"width : %f Height : %f",screenBounds.size.width,screenBounds.size.height);
CGSize esize = CGSizeMake(screenBounds.size.width, screenBounds.size.height);
NSInteger myDataLength = esize.width * esize.height * 4;
GLuint *buffer = (GLuint *) malloc(myDataLength);
glReadPixels(0, 0, esize.width, esize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
for(int y = 0; y < backingHeight / 2; y++) {
    for(int xt = 0; xt < backingWidth; xt++) {
        GLuint top = buffer[y * backingWidth + xt];
        GLuint bottom = buffer[(backingHeight - 1 - y) * backingWidth + xt];
        buffer[(backingHeight - 1 - y) * backingWidth + xt] = top;
        buffer[y * backingWidth + xt] = bottom;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, releaseScreenshotData);
const int bitsPerComponent = 8;
const int bitsPerPixel = 4 * bitsPerComponent;
const int bytesPerRow = 4 * backingWidth;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(backingWidth,backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
[snap setImage:myImage];
[self addSubview:snap];*/

Any idea how to include depth information with anti-aliasing while using glreadpixel() or any other similar function in opegl es ?

share|improve this question
add comment

2 Answers

Figured it out! You have to bind the resolve-framebuffer back to GL_FRAMEBUFFER before calling glReadPixels()

glBindFramebuffer(GL_FRAMEBUFFER, resolveFramebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
glReadPixels(xpos, ypos, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelByteArray);
glBindFramebuffer(GL_FRAMEBUFFER, sampleFrameBuffer);

Make sure to bind your sample-framebuffer as GL_FRAMEBUFFER before rendering the next frame, but the default Apple template already does this.

share|improve this answer
hey i tried this but it doesnt work....the data captured is all black –  Tornado Jul 22 '11 at 6:58
also any idea how to check whether the pixelByteArray in glReadPixels contain the valid data ? –  Tornado Jul 22 '11 at 7:14
Are you sure you are rendering anything on that pixel? I copy/pasted this from a working program. –  Gabe Aug 31 '11 at 21:53
i got it worked but there's another issue...glReadPixels is quite slow ...when i use it FPS decreases from 30-40 to 2-3 only any alternative to that ? –  Tornado Sep 1 '11 at 4:47
Yep, glReadPixels is terrible for performance for a number of reasons: it has to wait for the render to completely finish, it has to resynchronize with the CPU after rendering, and accessing video memory from the CPU is very slow on many architects(don't think that applies to iPhone though). It's fine if you need to check a pixel on a single tap though. –  Gabe Oct 19 '11 at 20:32
add comment

This won't solve your problem, but will help tailor your searching. Since you are using OpenGL-ES, glReadPixels() won't be able to read the depth buffer. I'm currently having the same functionality issues with my application.

share|improve this answer
yeah right ! if you find any other way to do so please let me know. meanwhile i should continue with my searching. –  Tornado Jul 19 '11 at 4:38
add comment

Your Answer


