Jump to content
  • Advertisement
Sign in to follow this  

FBO issues - nothing is rendered

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

	// 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

	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);

	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.

	glBindTexture(GL_TEXTURE_2D, mScreenTexture);

		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);

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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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);

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
Just found out what the problem was... I needed to clear the depth buffer before calling RenderToTexture().

Thanks to everyone for their time :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!