Nektarios
Nektarios

Reputation: 10371

How to switch between rendering and presenting framebuffers in iPhone OpenGL ES 2.0?

iPhone OpenGL ES 2.0..

How do I do this?

Upvotes: 2

Views: 3113

Answers (1)

Brad Larson
Brad Larson

Reputation: 170309

You should be able to set up a renderbuffer that has a texture backing it using code like the following:

// Offscreen position framebuffer object
glGenFramebuffers(1, &positionFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffer);

glGenRenderbuffers(1, &positionRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, positionRenderbuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, FBO_WIDTH, FBO_HEIGHT);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, positionRenderbuffer); 

// Offscreen position framebuffer texture target
glGenTextures(1, &positionRenderTexture);
glBindTexture(GL_TEXTURE_2D, positionRenderTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FBO_WIDTH, FBO_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

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

Switching buffers is as simple as using code like this:

glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffer);        
glViewport(0, 0, FBO_WIDTH, FBO_HEIGHT);

You can then render to that buffer and display the resulting texture by passing it into a simple shader that displays it within a rectangular piece of geometry. That texture can also be fed into a shader which renders into another similar renderbuffer that is backed by a texture, and so on.

If you need to do some CPU-based processing or readout, you can use glReadPixels() to pull in the pixels from this offscreen renderbuffer.

For examples of this, you can try out my sample applications here and here. The former does processing of video frames from a camera, with one of the settings allowing for a passthrough of the video while doing processing in an offscreen renderbuffer. The latter example renders into a cube map texture at one point, then uses that texture to do environment mapping on a teapot.

Upvotes: 9

Related Questions