Sign in to follow this  
pnunes

FBO issues - nothing is rendered

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

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
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
Good evening everyone.

Today I found a rather strange bug - it was as if depth testing wasn't being done. After some time I discovered that the FBO couldn't possibly have a depth buffer attached... the bug is actually in this code I posted before, and is rather subtle:


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


On the second line, it says "glBindFramebufferEXT" instead of "glBindRenderbufferEXT".

Just thought to share this, so in the future no one tries to run that code and needs to go through the same debugging I had to go.

Regards.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this