simple texture question

Started by
4 comments, last by gbook2 15 years, 11 months ago
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();

Advertisement
Does it make sense to call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); twice? I only call it once in my main loop and I see that you use it twice, could that be the problem? Just a guess though.
Also, you use glClearColor 4 times in that code. Isn't once during initializing good enough to set the background of the window a color?
@Tenac

You dont make a clear call for your framebuffer object and it works?
Tenac: You are correct, there were some vestigal glClearColor calls in the Init() function. But, I do want 2 calls in the main drawing function, one to color the fbo background, and one for the main scene (onto which the texture is mapped).

I was also having a problem previously (though I deleted the code that did it) where the last color used in the fbo code was being used as the texture color. So if the last glColor call was for magenta, the entire gl_quad was magenta, regardless of what the texture was.

I'm wondering if I'm not binding the texture correctly before calling the glTexCoord()s? Or if its not being released from the fbo.
I did not see any problem. Either the quad is getting culled and never gets rendered or there is some bug in the driver having to do with

glEnable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_1D);
glDisable(GL_TEXTURE_3D);

so maybe get rid of the
glDisable(GL_TEXTURE_1D);
glDisable(GL_TEXTURE_3D);

because it is unnecessary.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
It turns out it was simply missing this piece before the glBindTexture:

glActiveTexture(GL_TEXTURE0);


The code in my first post occurred after several other multitexturing operations and many other glActiveTexture calls. So the above code was fine, but an extra glActiveTexture call way back in another part of the program had prevented it from working.

This topic is closed to new replies.

Advertisement