Good evening.
I am trying to get FBOs to work, to render my scene to a texture to apply afterwards some post-processing effects.
I am using SDL for portability, so I am using SDL_GL_GetProcAddress(). All the function pointers return a valid address.
I'm quite clueless as to what I may be missing, and any suggestion would be greatly appreciated. I can get this to work with glCopyTexImage2D, but I'd rather use FBOs.
Here's the code:
void RenderSystem::CreateFrameBuffer(GLuint* fbo, GLuint* depthBuffer, GLuint* targetTexture, GLuint width, GLuint height) {
// Create the texture to which we will be rendering to
glGenTextures(1, targetTexture);
glBindTexture(GL_TEXTURE_2D, *targetTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Create a frame buffer object
glGenRenderbuffersEXT(1, fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, *fbo);
// Create a render buffer in which we will store the depth data
glGenRenderbuffersEXT(1, depthBuffer);
glBindFramebufferEXT(GL_RENDERBUFFER_EXT, *depthBuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
// Attach the texture to the frame buffer
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, *targetTexture, 0);
// Attach our depth buffer as well
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, *depthBuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) returns GL_FRAMEBUFFER_COMPLETE_EXT.
This is the render to texture function - all it should do is clear it to white.
void RenderSystem::RenderToTexture() {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mScreenFbo);
glClearColor(1.0f, 1.0f, 1.0f, 1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
And finally the code that renders a rectangle on the screen, the rectangle with the texture to which we previously rendered.
RenderToTexture();
ViewOrtho();
glBindTexture(GL_TEXTURE_2D, mScreenTexture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 300.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(300.0f, 300.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(300.0f, 0.0f);
glEnd();
ViewPerspective();
SDL_GL_SwapBuffers();
Instead of getting a white rectangle, I get a black one - for all I can see, I simply am not getting anything rendered to my texture at all.
Any ideas would be greatly appreciated.
My thanks,
Pedro