如何恢复GL_RENDERBUFFER?

How to restore a GL_RENDERBUFFER?

我正在努力存储和恢复基于 OpenGL ES 的应用程序的状态。

我有一个函数可以保存 GL_RENDERBUFFER 以使用以下代码转储数据:

glBindFramebuffer(GL_FRAMEBUFFER, fboTextureBufferData.framebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, fboTextureBufferData.colorbuffer);

GLint x = 0, y = 0, width2 = backingWidth, height2 = backingHeight;
NSInteger dataLength = width * height * 4;
GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));

// Read pixel data from the framebuffer
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

我没有看到 glWritePixels 函数。用上面填充的 GLubyte 数据重新填充 GL_RENDERBUFFER 的最佳方法是什么?一个例子将不胜感激。

编辑 3:

以下是我尝试配置纹理渲染缓冲区的方式,以及用于绘制它的函数。如代码中所述,如果我为 glFramebufferTexture2D 参数指定 GL_COLOR_ATTACHMENT1,则会恢复存储的像素数据,但我无法获得任何要绘制的更新。但是,如果我改用 GL_COLOR_ATTACHMENT0,我会得到绘图更新但没有恢复像素数据。

我已经尝试了各种组合(例如,也使用 GL_COLOR_ATTACHMENT1 作为 glFramebufferRenderbuffer 参数),但是在尝试渲染时出现无效的帧缓冲区错误。看起来我很接近,但无法弄清楚如何让它们同时恢复和渲染。

- (bool)configureRenderTextureBuffer {

    [EAGLContext setCurrentContext:context];

    glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];

    glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];

    glGenFramebuffers(1, &fboTextureBufferData.framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, fboTextureBufferData.framebuffer);

    glGenRenderbuffers(1, &fboTextureBufferData.colorbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, fboTextureBufferData.colorbuffer);

    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, backingWidth, backingHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fboTextureBufferData.colorbuffer);

    // Generate texture name, stores in .textureID
    glGenTextures(1, &fboTextureBufferData.textureID);

    glBindTexture(GL_TEXTURE_2D, fboTextureBufferData.textureID);

    ////////////////// Read Existing texture data //////////////////
    NSString *dataPath = [TDTDeviceUtilitesLegacy documentDirectory]; //This just returns the app's document directory
    NSData *data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/buffer.data", dataPath]];
    GLubyte *pixelData = (GLubyte*)[data bytes];

    // If I use GL_COLOR_ATTACHMENT1 here, my existing pixel data is restored
    // but no drawing occurs. If I use GL_COLOR_ATTACHMENT0, then data isn't
    // restored but drawing updates work
    glFramebufferTexture2D ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, fboTextureBufferData.textureID, 0 );

    // Populate with existing data
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, backingWidth, backingHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pixelData[0]); //&image[0]

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
    if(status != GL_FRAMEBUFFER_COMPLETE) {
        NSLog(@"failed to make complete render texture framebuffer object %x", status);
        return false;
    }

    return true;
}

这是渲染代码。 viewFramebuffer 附加到 GL_COLOR_ATTACHMENT0 并用于纹理帧缓冲区可以缩放和定位在视图内。

- (void)renderTextureBuffer {

    //Bind the texture frame buffer, if I don't use this, I can't get it to draw
    //glBindFramebuffer(GL_FRAMEBUFFER, fboTextureBufferData.framebuffer);

    //If I use this instead of binding the framebuffer above, I get no drawing and black background
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTextureBufferData.textureID, 0);

    renderParticlesToTextureBuffer();

    //Bind the view frame buffer.
    glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);

    drawFboTexture();

    [context presentRenderbuffer:GL_RENDERBUFFER];
}

如果您需要将数据直接写入渲染目标,使用渲染缓冲区不是一个好的选择。在这种情况下,最好改用纹理。

使用纹理作为 FBO 附件的工作方式与使用渲染缓冲区非常相似。在您当前使用 glRenderbufferStorage() 分配所需尺寸的渲染缓冲区的地方,您创建一个纹理,并分配其存储空间:

GLuint texId = 0;
glGenTextures(1, &texId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
    GL_RGBA, GL_UNSIGNED_SHORT_5_6_5, 0);

然后你将它附加到帧缓冲区:

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);

现在,如果您以后想用数据填充渲染目标,只需再次调用 glTexImage2D(),如果大小不变,甚至更好 glTexSubImage2D(),就可以做到这一点。