To extend the answer to @Jerry, I will walk you through the steps since you are a beginner. First we will create a frame buffer object:
GLuint framebuffer; glGenFramebuffersOES(1, &framebuffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
Then we will create an empty texture to hold our snapshot. This is common material for creating OpenGL textures, and you can modify it to suit your needs, of course. The only line that should be noted is the glTexImage2D line - note that instead of the pixel data, you can pass NULL as the last coordinate, which creates an empty texture.
GLuint texture; glGenTextures(1, &texture); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); glDisable(GL_TEXTURE_2D);
Now we associate the texture with the frame buffer:
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0);
and check if everything is ok:
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) return false;
Now we are set to draw!
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer); // do drawing here glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
Finally, clear the frame buffer if you no longer need it:
glDeleteFramebuffersOES(1, &framebuffer);
Some reservations:
- The frame buffer size must be strong.
- You can go up to 1024x1024 on the latest iPhone, but perhaps this level of detail is not necessary.