glDrawPixels equivalent - how to load a saved buffer

Hi everyone,
I’m pretty new to OpenGL, and I’m making an iphone application where I need to save/load the screen contents; I’m saving with glReadPixels, and was hoping to use glDrawPixels to reload, but alas it doesn’t exist. From googling the problem it seems that I need to load the saved buffer into a texture, and then write the texture to the screen, but I’m sort of lost as to how I would do this, and I really need some sample code to wrap my head around it. Can anyone help me out?


You can use glTexSubImage2D to write the pixels back into a texture that has been previously created.

void setup()
    glGenTextures(1, &textureId);
    // Allocate texture memory only (pass NULL for data)
    glTexImage2D(..., NULL);

void copySurfaceToTexture()
    // Copy surface contents to 'data'
    glReadPixels(..., data);

    // Upload data into our texture
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexSubImage2D(..., data);

glReadPixels introduces a pipeline stall and is generally a slow operation, so make sure you need it.

If it’s available, eglBindTexImage is another way to render to texture (OpenGL ES 1.x + EGL 1.1), which may be more efficient.

If you are using OpenGL ES 2.0, you can use framebuffer objects to render to texture, which is a much better alternative.

I will try this if this will work with mine, too.

Anyhow, thanks and God bless,