I have a texture attached to an FBO that i'm rendering to, and then sending that texture to a shader. It all works fine and well....for 1 frame, the first frame. Then the texture is always black. It seems like the FBO texture can only be drawn to once(!?) in my code, and afterwards, it is erased and only black is drawn to it. It's really driving my nuts. I feel like I'm missing something completely obvious. Here is my main draw function:
void draw() {
printf("Beginning of draw\n");
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
// clear the FBO texture and set the draw color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
// draw a square where the mouse is
float x, y, w, h;
w = 100.0f;
h = 100.0f;
x = mouse.x;
y = mouse.y;
glRectf(x, y, x+w, y+h);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
// generate mipmaps for the FBO texture
glBindTexture(GL_TEXTURE_2D, fbo_tex);
glGenerateMipmapEXT(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// this should all apply to the actual screen now
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbo_tex);
// draw a quad which we'll screen align in a vertex shader, and then paint
// with the FBO texture in the fragment shader
glUseProgram(blur_program);
glRecti(0, 0, 1, 1);
glFlush();
glUseProgram(0);
printf("End of draw\n");
SDL_Delay(1000);
}This is the vertex shader that aligns that second glRecti call to the screen:
void main(void) {
gl_Position = gl_Vertex * 2.0 - 1.0;
gl_TexCoord[0] = gl_Vertex;
}And the fragment shader:
uniform sampler2D fbo;
void main(void) {
gl_FragColor = vec4(texture2D(fbo, gl_TexCoord[0].st));
}
Like I said, it seems to work for only *one frame* (I can see the 100x100 white square for 1 second, thanks to the SDL_Delay(1000))....so it is getting drawn to the FBO texture and the shaders are processing everything correctly. It's just that every time draw() is called *after the first time*, the FBO texture is painted with black.
I've changed the clear color on the FBO to red, and indeed the screen is rendered red instead of black, showing that the FBO texture is still being painted, even when the square for some reason is not. I've also tried attaching an arbitrary image as the texture getting fed into my shader, and that works on every frame, showing that the error isn't in my shaders. I'm really at a loss here...why isn't that white 100x100 square being drawn on every draw() call?
Any ideas? Thanks in advance!






