I am trying to render to an off-screen texture using OpenGL = for post processing effects - but the frame buffer doesn't seem to work. If I just render straight to the back buffer like normal the scene is rendered fine. But when I try to do the simplest use of frame buffers - draw to a frame buffer and then just read out of that frame buffer - I just get a black screen. This has been bugging me for hours.
So here's the setup stuff at the start of the program for the FBOs:
Code:
void init(){
// Setup GL etc....
//Buffer textures
glGenTextures(2, ppTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ppTexture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, ppTexture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
//Render buffer for depth buffer
glGenRenderbuffersEXT(1, &renderbufferID);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbufferID);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, textureWidth, textureHeight);
//FBOs
glGenFramebuffersEXT(6, framebufferID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[0]);
const GLenum bufs[2] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
glDrawBuffers(2, bufs);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderbufferID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, ppTexture[0], 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, ppTexture[2], 0);
}
Yes I do generate and attach a second texture, but I don't use it.
And here's the display function:
Code:
//GLUT displays the scene with this function
void display(){
glEnable(GL_DEPTH_TEST);
glBindFramebuffer(GL_FRAMEBUFFER, framebufferID[0]);
glViewport(0,0,textureWidth, textureHeight);
glUseProgram(GM->getShaderProgram());
//Set projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT, Z_NEAR_CLIP, Z_FAR_CLIP);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//.... draw the scene
//Now read back from the frame buffer
glUseProgram(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ppTexture[0]);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);
glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 0.0f);
glVertex2f(1.0f, -1.0f);
glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();
//And flush
glutSwapBuffers();
}
My shader does use glFragData[0] and glFragData[1] instead of glFragColor.
There are no OpenGL errors and the frame buffer has no error status.
Can anyone see a problem here?
I am using Windows XP and Dev-C++, old I know but I've gotten used to it and VC++ doesn't want to work for some reason.