EXT_framebuffer_object help!

Hey all,
I’m new to using framebuffer objects and am having some trouble getting a simple coding example to work with it:

        ///
        /// Texturing is already enabled at this point
        ///
 
	glEnable(GL_LIGHTING);
	GLfloat ambientColor[] = {0.5, 0.5, 0.5,1.0};
	GLfloat lightPosition[] = {1.0,1.0,1.0,1.0};
	glLightfv(GL_LIGHT1, GL_AMBIENT, ambientColor);
	glLightfv(GL_LIGHT1, GL_POSITION, lightPosition);
	glEnable(GL_LIGHT1);
 
	GLuint color_tex;
	GLuint fb;
 
	glGenFramebuffersEXT(1, &fb);
	glGenTextures(1, &color_tex);
 
	glBindTexture(GL_TEXTURE_2D, color_tex);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 512, 512, 0,
                     GL_RGB, GL_INT, 0);
 
        // Enable render-to-texture
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
 
        // Set up color_tex and depth_rb for render-to-texture
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
                                  GL_COLOR_ATTACHMENT0_EXT,
                                  GL_TEXTURE_2D, color_tex, 0);
 
        ///
        /// Attempting to draw a red square to the texture.  Note that my
        /// clear color isn't black, its a dark bluish (to help see black objects)
        ///
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,0.0,0.0);
	glBegin(GL_POLYGON);
		glVertex3f(-1.0, -1.0, 0.0);
		glVertex3f(1.0, -1.0, 0.0);
		glVertex3f(1.0, 1.0, 0.0);
		glVertex3f(-1.0, 1.0, 0.0);
	glEnd();
 
        // Re-enable rendering to the window
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 
        glBindTexture(GL_TEXTURE_2D, color_tex);
 
        ///
        /// Attempt to draw a textured quad using the texture we rendered to.
        /// The quad should be a dark blue background with a red square on it.
        /// Instead it is just black.
        ///
	glColor3f(1.0,1.0,1.0);
	glBegin(GL_POLYGON);
		glTexCoord2f(0.0, 0.0);	glVertex3f(0.0, 0.0, -3.0);
		glTexCoord2f(1.0, 0.0);	glVertex3f(1.0, 0.0, -3.0);
		glTexCoord2f(1.0, 1.0);	glVertex3f(1.0, 1.0, -3.0);
		glTexCoord2f(0.0, 1.0);	glVertex3f(0.0, 1.0, -3.0);
	glEnd();  

This is derived pretty closely from the examples in the specs. As you can see, it tries to render a red square to texture, and then tries to texture a quad based on that texture. I end up with a black quad on the screen. Can anyone see what I might be doing wrong here?

Did you check if the framebuffer is complete? It is possible that you will have to disable the depth test (as you don’t have a depth buffer). Also, you should disable texturing when rendering to the same texture (as this will produce undefined results).

I disabled my depth test, but that hasn’t made a difference so far.

I’m not sure what you mean by disabling texturing while rendering to the same texture. Are you saying that I need to glDisable(GL_TEXTURE_2D) when I’m rendering to the texture? Doing so simply turns my solid black quad into a lighter gray quad.

The sample code I’ve seen always shows setting up and binding the texture that is to be rendered to. I don’t know how to UNbind a texture besides just binding a new/different one. I’m not quite sure what to disable.

The problem is that the texture you are rendering to is the same that is currently bound. Reading from a texture while rendering to it is not possible, so there are two ways to solve your problem:
Use glDisable(GL_TEXTURE_2D) before rendering anything to your FBO texture (as Zengar said), or simply bind another texture.

IIRC disabling the depth test is not necessary. If an FBO has no depth attachments, the depth test is simply not performed.

I have tried both methods. Disabling textures while rendering the red square causes my textured quad in the end to become dark gray instead of black… I’m not quite sure why. It should be getting rendered with the texture that I rendered the red square on.

If I simply bind a different texture, my quad remains black.

My clear color is set as a light blue color, so even if, for some reason, the red square wasn’t actually in the frustum when I draw, the quad should still be that light blue color, not black.

The only reason I can think of is that your FBO is not complete. Use glCheckFramebufferStatusEXT after your FBO setup to find out what is wrong with your FBO.
Also make sure to check for any other gl errors.

Interesting. The FBO is complete, but I am getting a GL_INVALID_ENUM error. The error seems to be occuring somewhere in the code I posted here. I’ll see if I can track down exactly what is causing the error, but nothing jumps out at me from looking at the code.

OK. I checked for errors at different places throughout my code. After the line where I “release” the framebuffer (i.e. start rendering to the window again, I am re-enabling the depth test and the texturing.

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glEnable(GL_DEPTH_TEST | GL_TEXTURE_2D);

The glEnable line is what is causing the GL_INVALID_ENUM error. I’m pretty confused as to why that would throw an error, anyone know what is going on?

You can enable only one capability at a time, bitwise or’ing won’t work for glEnable.

Ah, right. Error is gone. The FBO is complete. Still have a black quad :_(.

Thanks so much for all the help so far. I hope we can get this thing fixed :slight_smile: .

You’re welcome :slight_smile:
I think I might have found something: you have enabled GL_LIGHTING, and if you don’t have GL_COLOR_MATERIAL enabled, using glColor* will not affect the things you draw (at least I think so, I don’t use the fixed function pipeline anymore, so I might be wrong).
Maybe you should try without lighting first.

And I would suggest that you try rendering without the FBO first, it is always possible that your matrices are skrewed.

There it is! I enabled GL_COLOR_MATERIAL and there is my red square image, properly textured onto my quad!

Thanks a ton!