glClear on FBO problems

I am trying to use a framebuffer object to render to textures. I have already made sure that my system supports fbos, and the value returned by glCheckFramebufferStatusEXT is GL_FRAMEBUFFER_COMPLETE_EXT. The problem I am having is that if I call glClear after binding the fbo every poly I draw after that, no matter if its using the FBO or the regular framebuffer is drawn as clear color. If I don’t call glClear when working with the fbo nothing appears to be written to the texture.

here is the relevant code:


glGenFramebuffersEXT(1, &fbo);
glBindTexture(GL_TEXTURE_2D, texture);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
GLint viewport[4];
glGetIntegerv( GL_VIEWPORT, viewport );
glViewport(0, 0, text_dat->w, text_dat->h);
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -100.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(0.0,0.0,20.0,
      0.0,1.0,0.0,
      0.0,0.0,0.0);
glClearColor(1.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);

glColor4f(1.0, 1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(1.0, 1.0);
glVertex2f(1.0, -1.0);
glVertex2f(-1.0, -1.0);
glVertex2f(-1.0, 1.0);
glEnd();

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glViewport( viewport[0],viewport[1],viewport[2],viewport[3]);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDeleteFramebuffersEXT(1, &fbo);

any help is greatly appreciated

Be sure to set your DrawBuffer to the intended color attachment after binding the framebuffer texture, and be careful to reset it to the backbuffer when done rendering to the render target texture. See the framebuffer_object spec for more details.

No. DrawBuffer is per-framebuffer state, and will default to COLOR_ATTACHMENT0 for non-zero FBOs. You don’t need to set it explicitly unless you’re doing MRT or depth-only rendering.

If every poly is drawn in the clear color, how do you know anything is drawn at all?

thanks for the replies

If every poly is drawn in the clear color, how do you know anything is drawn at all? [/QUOTE]

I should have been more specific, the polygons are drawn in the clear color I set for the fbo. so if the clear color for the fbo is red and the clear color for the backbuffer is black, I end up with a bunch of red polys on a black background

movax, I think that we might help you if you give us the whole code. I mean in the above pasted code snippet there is a lot of initialization mixed with drawing calls… If it is what you are doing in the drawing function this is not the good way.
Moreover, you are drawing to a texture, but the above code does not show how you vizualize its content.

If it is necessary clean up your code, minimize using comments and uncomment when you are completly sure of what you have written. I am pretty sure that the error is more simple than you think and is not where you are looking.

I have written a little program with glut inspired from yours. Maybe it can help you to find the solution. It just draw a yellow quad centered on the screen in a texture using a fbo. Then, it pastes the previous drawn texture on the screen to display its content. The clear color for the texture is blue and green for the framebuffer.


#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>

using namespace std;

GLuint fbo, texture, depthBuffer;


//------------------------------------------------------------	Reshape()
void OnReshape(int w, int h)
{
	// prevent a division by zero when minimising the window
	if (h == 0)
		h = 1;

	// set the drawable region of the window
	glViewport(0, 0, w, h);

	// set up the projection matrix 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// just use a perspective projection
	glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -100.0f, 1000.0f);

	// go back to modelview matrix so we can move the objects about
	glMatrixMode(GL_MODELVIEW);
}

//------------------------------------------------------------	Draw()
//
void OnDraw()
{
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
	glClearColor(0.0, 0.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glDisable(GL_TEXTURE_2D);
	
	glPushAttrib(GL_VIEWPORT_BIT);
	glViewport(0, 0, 512, 512);

	glColor4f(1.0, 1.0, 0.0, 0.0);
	glBegin(GL_QUADS);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
	glEnd();
	
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glClearColor(0.0, 1.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_TEXTURE_2D);
	
	glPopAttrib();
	
	glColor4f(1.0, 1.0, 1.0, 0.0);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0, 0.0); glVertex2f(1.0, 1.0);
		glTexCoord2f(0.0, 1.0); glVertex2f(1.0, -1.0);
		glTexCoord2f(1.0, 1.0); glVertex2f(-1.0, -1.0);
		glTexCoord2f(1.0, 0.0); glVertex2f(-1.0, 1.0);
	glEnd();
	
	
	glutSwapBuffers();
	
	cout<<"coucou"<<endl;

}

void idle()
{
	
}

//------------------------------------------------------------	OnInit()
//
void OnInit()
{
	glewInit();
	
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 512, 512, 0, GL_RGB, GL_UNSIGNED_INT, NULL);
	//glGenerateMipmapEXT(GL_TEXTURE_2D);
	
	glGenFramebuffersEXT(1, &fbo);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
	
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0);
	
	if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)!=GL_FRAMEBUFFER_COMPLETE_EXT)
	{
			cout<<"The fbo is not complete"<<endl;
			cout<<"Error: "<<glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)<<endl;
	}
	
}

//------------------------------------------------------------	OnExit()
//
void OnExit()
{
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glDeleteFramebuffersEXT(1, &fbo);

	glutSwapBuffers();
	glutPostRedisplay();
}

//------------------------------------------------------------	main()
//
int main(int argc, char** argv)
{

	// initialise glut
	glutInit(&argc, argv);

	// request a depth buffer, RGBA display mode, and we want double buffering
	glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);

	// set the initial window size
	glutInitWindowSize(512, 512);

	// create the window
	glutCreateWindow("A basic glut example");

	// set the function to use to draw our scene
	glutDisplayFunc(OnDraw);

	// set the function to handle changes in screen size
	glutReshapeFunc(OnReshape);

	// run our custom initialisation
	OnInit();

	// this function runs a while loop to keep the program running.
	glutMainLoop();
	
	OnExit();
	
	return 0;
}


dletozeun, you were right the problem wasn’t where i was looking, i forgot to disable textures when i wasn’t drawing textured polygons

thank you for your help