问题
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);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
/*
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(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 ?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/6731385/problem-reading-data-by-glreadpixel-while-using-depth-buffer-and-anti-aliasing