I'm creating a very simple texture using framebuffer objects and am attempting to display it. I'm able to create the framebuffer object correctly, and using gDEbugger, I can see that it is rendered correctly. But when I go to attach the texture to a polygon, it only displays the glClearColor from the FBO drawing section. So for example, if my clearcolor is (0,1,0,1), my fbo object scene is rendered correctly, but then when I display it using the glTexCoord(), glVertex() calls, all I see is the green background.
Here is the initialization code:
glClearColor(0.0, 0.0, 0.0, 0);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.2f, 0.5f);
glEnable(GL_DEPTH_TEST);
glViewport(0,0,w,h);
// Setup our FBO
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
// Create the render buffer for depth
glGenRenderbuffersEXT(1, &depthBuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, 512, 512);
// Now setup a texture to render to
glGenTextures(1, &img);
glBindTexture(GL_TEXTURE_2D, img);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, 512, 512, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// And attach it to the FBO so we can render to it
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, img, 0);
// Attach the depth render buffer to the FBO as it's depth attachment
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
exit(1);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind the FBO for now
and the drawing code (executed each time the scene is drawn)
// First we bind the FBO so we can render to it
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glDisable(GL_BLEND);
// Save the view port and set it to the size of the texture
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, 512, 512);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0,2.0,-2.0,2.0,-100.0,100.0);
glMatrixMode(GL_MODELVIEW);
// Then render as normal
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();
glTranslatef(-0.5f,-0.5f,0.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
drawQuads(1.0,1.0,1.0);
// Restore old view port and set rendering back to default frame buffer
glPopAttrib();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0,2.0,-2.0,2.0,-100.0,100.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
// glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_1D);
glDisable(GL_TEXTURE_3D);
// Now bind the texture to use it
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(-xrot,1.0f,0.0f,0.0f);
glRotatef(-yrot,0.0f,1.0f,0.0f);
glColor4f(1.0f,1.0f,1.0f,1.0f);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, img);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5, 0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5, -0.5, 0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5, 0.5, 0.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5, 0.5, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
xrot+=xspeed;
yrot+=yspeed;
glLoadIdentity();