FBO issues - nothing is rendered

Started by
9 comments, last by pnunes 15 years, 3 months ago
Good evening. I am trying to get FBOs to work, to render my scene to a texture to apply afterwards some post-processing effects. I am using SDL for portability, so I am using SDL_GL_GetProcAddress(). All the function pointers return a valid address. I'm quite clueless as to what I may be missing, and any suggestion would be greatly appreciated. I can get this to work with glCopyTexImage2D, but I'd rather use FBOs. Here's the code:

void RenderSystem::CreateFrameBuffer(GLuint* fbo, GLuint* depthBuffer, GLuint* targetTexture, GLuint width, GLuint height) {
	// Create the texture to which we will be rendering to
	glGenTextures(1, targetTexture);
	glBindTexture(GL_TEXTURE_2D, *targetTexture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	// Create a frame buffer object
	glGenRenderbuffersEXT(1, fbo);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, *fbo);

	// Create a render buffer in which we will store the depth data
	glGenRenderbuffersEXT(1, depthBuffer);
	glBindFramebufferEXT(GL_RENDERBUFFER_EXT, *depthBuffer);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

	// Attach the texture to the frame buffer
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, *targetTexture, 0);

	// Attach our depth buffer as well
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, *depthBuffer);

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) returns GL_FRAMEBUFFER_COMPLETE_EXT. This is the render to texture function - all it should do is clear it to white.

void RenderSystem::RenderToTexture() {
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mScreenFbo);

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

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
And finally the code that renders a rectangle on the screen, the rectangle with the texture to which we previously rendered.

	RenderToTexture();

	ViewOrtho();
	glBindTexture(GL_TEXTURE_2D, mScreenTexture);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(0.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex2f(0.0f, 300.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(300.0f, 300.0f);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(300.0f, 0.0f);
	glEnd();
	ViewPerspective();

	SDL_GL_SwapBuffers();
Instead of getting a white rectangle, I get a black one - for all I can see, I simply am not getting anything rendered to my texture at all. Any ideas would be greatly appreciated. My thanks, Pedro
Advertisement
Your render to texture call binds the FBO, and then unbinds it....

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); <--- means bind the original openGL buffers

so you need to bind your fbo, draw, then bind the original, and then draw that fbo fullscreen.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

The code is fine. Try adding
glTexEnv(........, GL_DECAL) just for rendering the quad.
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);
dpadam450 - although it unbinds it, it does so after clearing it to white - so the texture would be white, and good to be used when I use it later? I removed the unbinding, but I still get the same result (everything black).

V-man - just tried it, still black...

My thanks to both, much appreciated.
Try a call to glViewport with the texture size (0, 0, width, height) in RenderToTexture() after glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
NumberXaero: just checked it, still black. Thanks for the idea :)

I'm beginning to wonder if there may be something more to this than meets the eye. I took a look at the code from the sample at http://www.songho.ca/opengl/gl_fbo.html and actually did a good share of copy pasting around to see if I had skipped something, but everything seems to be there and in the correct order. The greatest difference is that I am using SDL, he's using GLUT. Another poster in here the forum also commented on some apparently strange behaviour in his program (SDL) when compared to that sample as well, so I'll run a few tests to see if, for some reason, SDL may be causing this.

glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

in the first create call, try taking that out
Nothing new, still black. :/ Cheers.
Stab in the dark, is the main window possibly being cleared to black and the screen quad being culled by accident via cull state?

glTexCoord2f(0.0f, 1.0f); // use (0.0, 0.0)?
glVertex2f(0.0f, 0.0f); // use (0.0, 0.0)?
glTexCoord2f(0.0f, 0.0f); // use (1.0, 0.0)?
glVertex2f(0.0f, 300.0f); // use (300.0, 0.0)?
glTexCoord2f(1.0f, 0.0f); // use (1.0, 1.0)?
glVertex2f(300.0f, 300.0f); // use (300.0, 300.0)?
glTexCoord2f(1.0f, 1.0f); // use (0.0, 1.0)?
glVertex2f(300.0f, 0.0f); // use (0.0, 300.0)?

these look a little strange, the vertices seem to build the quad from
lower left, upper left, upper right, lower right
while the texture coordinates go from
upper left, lower left, lower right, upper right


Just found out what the problem was... I needed to clear the depth buffer before calling RenderToTexture().

Thanks to everyone for their time :)

This topic is closed to new replies.

Advertisement