OGeek|极客世界-中国程序员成长平台

标题: ios - 使用深度缓冲区和抗锯齿技术时 glreadpixel() 读取数据时出现问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:46
标题: ios - 使用深度缓冲区和抗锯齿技术时 glreadpixel() 读取数据时出现问题

我想使用 glreadpixel() 捕获我的游戏屏幕。 它在模拟器上也可以在 ios 版本 3.1.1 的 2g iphone 上正常工作。 但在 iOS 版本 4.2.1 的 ipad 上却没有。我开始知道这个问题。适用于特定设备 (ipad) 上的 ios 4.0 以上版本 我们绑定(bind)深度缓冲区并使用抗锯齿技术。当我们使用 opengl 的 glreadpixel() 从帧缓冲区捕获数据时,在目标缓冲区中返回全 0...

如果我们不将深度缓冲区绑定(bind)到帧缓冲区并且不使用抗锯齿技术,它可以正常工作。

我使用的代码是:-

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];*/

知道如何在使用 glreadpixel() 或 opegl es 中的任何其他类似函数时包含深度信息和抗锯齿吗?



Best Answer-推荐答案


想通了!在调用 glReadPixels() 之前,您必须将解析帧缓冲区绑定(bind)回 GL_FRAMEBUFFER

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

确保在渲染下一帧之前将您的示例帧缓冲区绑定(bind)为 GL_FRAMEBUFFER,但默认的 Apple 模板已经这样做了。

关于ios - 使用深度缓冲区和抗锯齿技术时 glreadpixel() 读取数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6731385/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4