Problems with FBO

Hi all

I’ve worked several times with FBO. I’m trying to render to texture my scene. When I attach the color works fine but if I attach the depth too it doesn’t work.

I’ve reduced my code to the minimun to find the error but it still doesn’t work and I can’t see anything strange. Here’s my code:


//init textures
glGenTextures(1, &colorTexID);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, colorTexID);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, SCREEN_RES_X, SCREEN_RES_Y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

glGenTextures(1, &depthStencilTexID);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, depthStencilTexID);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT32, SCREEN_RES_X, SCREEN_RES_Y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

glEnable(GL_DEPTH_TEST);
glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glGenFramebuffersEXT(1, &fboID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, colorTexID, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, depthStencilTexID, 0);

glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(0,0,1);
glVertex3f(0.5,0,2);
glVertex3f(0.5,0.5,3);
glEnd();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor4f(1.0,1.0,1.0,1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glBindTexture(GL_TEXTURE_RECTANGLE_ARB, colorTexID);
glBegin(GL_QUADS);	
glTexCoord2f(0,0); glVertex3f(-1,-1,0);
glTexCoord2f(SCREEN_RES_X,0); glVertex3f(1,-1,0);
glTexCoord2f(SCREEN_RES_X,SCREEN_RES_Y); glVertex3f(1,1,0);
glTexCoord2f(0,SCREEN_RES_Y); glVertex3f(-1,1,0);
glEnd();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

If I comment the line

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, depthStencilTexID, 0);

works fine, otherwise there’s no result. What I’m doing wrong?

Thanks.

Texture_Rectangle may require GL_LINEAR as the filter.
Have you tried GL_TEXTURE_2D instead of TEXTURE_RECTANGE as this may be adding an extra complication.

I tried both ways, and it still doesn’t work. It’s really amazing because I’ve one class that have exactly the same code and it works. Maybe I’m forgetting some initialization… but I can’t find which.

Anyway, thanks for your reply.

Finally I found the “error”. Depens on the order of clearing the buffers and then the FBO attachments. I mean, that way:


//fisrt clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//then attach
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, colorTexID, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, depthStencilTexID, 0);

//render my scene

doesn’t works. That way:


//fisrt attach
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, colorTexID, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, depthStencilTexID, 0);

//then clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//render my scene

works. I post for someone who maybe has the same problem. But anybody knows why is happening this?

Thanks

Is your framebuffer complete?

In the first code snippet, first, you clear the previous bound framebuffer, i.e maybe the the same fbo, if it has been already bound, or another fbo, or the application provided framebuffer.

So there are very little chances that you clear the right fbo…

Yes, the framebuffer is completed. That’s was the first I checked.

That’s what I thougth. In my fisrt code I was clearing the frame buffer because I was clearing the depth buffer before attach the FBO. In that case the depth buffer was not clear and any fragment in my scene didn’t pass the depth test. That’s because I coudn’t see anything…

The second code is the rigth way. Now it works perfectly. Now I have some problems with the stencil but I will open a new thread asking for that question.

Thanks for your reply.

Ah! I misread your post, At the beginning I understood:

“doesn’t works that way:” whereas it was “doesn’t works. That way:”. :slight_smile: