Skip to content

Instantly share code, notes, and snippets.

@beugnen
Created May 20, 2012 00:22
Show Gist options
  • Save beugnen/2732973 to your computer and use it in GitHub Desktop.
Save beugnen/2732973 to your computer and use it in GitHub Desktop.
Creating a Render to Texture Secondary Framebuffer for iOS OpenGL ES 2
GLsizei cxFBO;
GLsizei cyFBO;
GLuint _frameBufferID;
GLuint _colorRenderBuffer;
GLuint _depthRenderBuffer;
GLuint _fboTextureID;
void MyDualFramebufferView::Resize(GLfloat width, GLfloat height, GLfloat scale)
{
_scale = scale;
_aspect = (GLfloat) width / height;
_screen = Vec2 (width, height); // Vec2 is actually a glm::vec2
cxFBO=_screen.x * scale;
cyFBO=_screen.y * scale;
CreateRenderToTextureFBO();
}
void MyDualFramebufferView::CreateRenderToTextureFBO()
{
if (_frameBufferID != 0)
{
glDeleteRenderbuffers(1, &_depthRenderBuffer);
glDeleteRenderbuffers(1, &_colorRenderBuffer);
glDeleteFramebuffers(1, &_frameBufferID);
cout << "Recreating FBO" << endl;
}
GLint maxTextureSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
if ((cxFBO > maxTextureSize) ||
(cyFBO > maxTextureSize))
{
throw "Requested size of frame buffer exceeds maximum";
}
glGenFramebuffers(1, &_frameBufferID);
glGenRenderbuffers(1, &_depthRenderBuffer);
glGenTextures(1, &_fboTextureID);
// texture
glBindTexture(GL_TEXTURE_2D, _fboTextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cxFBO, cyFBO, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
// depth buffer
glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, cxFBO, cyFBO);
// frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
// attachments
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _fboTextureID, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);
GLenum status=glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
throw "Framebuffer is not complete";
}
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment