简体   繁体   English

glReadPixels清除openGL ES 2中的缓冲区?

[英]glReadPixels clears buffer in openGL ES 2?

I have an OpenGL ES 2 drawing app (iOS 4), so I'm retaining backing in my CAEAGLLayer rather than clearing on every frame: 我有一个OpenGL ES 2绘图应用程序(iOS 4),所以我在我的CAEAGLLayer中保留支持,而不是在每个帧上清除:

eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:TRUE], kEAGLDrawablePropertyRetainedBacking,
                                        kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                                        nil];

I have a button for saving the work to photo album with the code below. 我有一个按钮,用于将工作保存到相册,下面的代码。 Here's the issue. 这是问题所在。 When it's done saving and I start drawing again, the whole buffer clears and my drawing begins from scratch on a blank screen. 当它完成保存并再次开始绘图时,整个缓冲区清除,我的绘图从空白屏幕上开始。 Is there a way to prevent this? 有办法防止这种情况吗? I'd like to be able to continue drawing from the same state that I just saved. 我希望能够继续从我刚刚保存的状态中抽出来。

I'm running the same code to save in OpenGL ES 1 and this issue doesn't occur there. 我正在运行相同的代码以保存在OpenGL ES 1中,并且此问题不会发生。 Also, this issue is only visible on the iPhone device (3GS), not the simulator. 此外,此问题仅在iPhone设备(3GS)上可见,而不是在模拟器上。 Let me know if you have ideas. 如果您有想法,请告诉我。 Thanks. 谢谢。

-(void)saveCurrentScreenToPhotoAlbum 
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    int width = rect.size.width;
    int height = rect.size.height;

    NSInteger myDataLength = width * height * 4;
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    for(int y = 0; y <height; y++) 
    {
        for(int x = 0; x <width * 4; x++) 
        {
            buffer2[(int)((height - 1 - y) * width * 4 + x)] = buffer[(int)(y * 4 * width + x)];
        }
    }
    free(buffer);

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, releaseData);
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * width;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);     

    UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];    // change this to manual alloc/init instead of autorelease
    CGImageRelease(imageRef);   

    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);   // add callback for finish saving
}

// callback for CGDataProviderCreateWithData
void releaseData(void *info, const void *data, size_t dataSize) 
{

    free((void*)data);   
}

// callback for UIImageWriteToSavedPhotosAlbum
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 
{

    [image release];     
}

Answer by Anna. 安娜回答。

I was making another call while performing this function, which resulted in confused behavior by the buffer 我在执行此功能时正在进行另一个调用,这导致缓冲区的行为混乱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM