Jump to content
  • Advertisement
Sign in to follow this  
medevilenemy

OpenGL FBO woes

This topic is 2321 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

I've been trying to come up with a way to draw an opaque quad over my scene through which holes would be punched to allow one to see the scene below. Through some research and with some help I've determined that the best way to do this is probably by rendering to a texture through a framebuffer object, alphablending the holes into the quad and then drawing the texture over the scene. I'm having trouble getting it to work, though, and I can't find examples of what I'm trying to do.

So far I've succeeded in creating my framebuffer object and target texture based on the example block at the bottom of http://www.songho.ca...gl/gl_fbo.html. After that, I've attempted various configurations of drawing what I want to the texture, but all I can seem to manage is to draw an opaque quad to it. I can't seem to get depth, alpha blending, or even texture mapping to work properly (no matter how I map the texture coords, the result is the same). glClear(GL_COLOR_BUFFER_BIT) on the texture causes my whole scene to get messed up, as well. The following code demonstrates how I'm trying to set up rendering to the texture:


glPushMatrix();
// set rendering destination to FBO
glBindFramebuffer(GL_FRAMEBUFFER, 92);

glViewport(0,0,screen_width, screen_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Clearing buffers
glClearDepth(1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT); // Clearing the GL_COLOR_BUFFER_BIT causes things to break

// Set up internal rendering projection
glOrtho(-0.5 * width, 0.5*width, -1, 1, 1, 0);

glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);

// Set blend func
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Draw stuff here! At the moment just trying to draw a couple overlapping quads as a proof of concept

// Unbind fbo
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glPopMatrix();

// Set scene-relative blending function.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // function for blending

// Draw the rendered texture on top of the underlying scene
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 90);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0f); glVertex3f(-0.1,-0.1,0.01); // Bottom-Left Vertex
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.1,-0.1,0.01); // Bottom-Right Vertex
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.1,0.1,0.01); // Top-Right Vertex
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.1,0.1,0.01); // Top-Left Vertex
glEnd();
glDisable(GL_TEXTURE_2D);


Any help is greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
I've been doing some more tweaking, and have managed to be able to glClear(GL_COLOR_BUFFER_BIT) on the texture without messing other things up, but that's about as far as I've been able to get. I still can't seem to get something to take up only part of the texture, and can't seem to get multiple quads drawn to it (as a test) to actually blend together... I just see whichever was drawn later.

My best guess is that the texture itself isn't functioning properly (its as if it isn't mapping at all)... but that doesn't make any sense... I've done everything the couple examples I've seen does. Edited by medevilenemy

Share this post


Link to post
Share on other sites
The link seems to be down so I can't check your FBO setup. Can you post that code too? Did you actually bind at least one color buffer and a depth buffer to the FBO?

In the rendering code, after binding your FBO, you can check it with

GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
cry_out_in_pain();

Just to see if s.th. broke it on the way.

Also (for debugging purposes) throw in this define somewhere

#define CHECKGLERROR(f, l) \
{ \
GLuint error = glGetError(); \
if (error) \
std::cout << "GL Error in " << f << " " << l << ": " << gluErrorString(error) << " (" << error << "). " << std::endl; \
}

and do a CHECKGLERROR(__FILE__, __LINE__) after each OpenGL call.


Edit: fixed the preprocessor macro Edited by Ohforf sake

Share this post


Link to post
Share on other sites
somehow the period got added to the link... the page itself is still there: http://www.songho.ca...gl/gl_fbo.html.

I tested for FBO completeness, and it seems to be ok. I'll give the macro you suggest a try, and get back to you. Based on my most recent tests, the best I can guess is that for whatever reason, the texture isn't functional (either not rendering or not complete or something) which results in the textured quad that's supposed to be drawing it just drawing as a straight-up colored quad.

Share this post


Link to post
Share on other sites
Uppon closer inspection this might also be a problem:
glOrtho(-0.5 * width, 0.5*width, -1, 1, 1, 0);

I think you switched the zNear and zFar plane at the end and that it should be:
glOrtho(-0.5 * width, 0.5*width, -1, 1, 0, 1);

Share this post


Link to post
Share on other sites
That is the ortho setup I use in my main scene as well... it works just fine (I like to have bigger z = farther, I find it easier to remember). It seems like the problem has something to do with the rbo/texture setup or something, but I haven't a clue what. The fbo tests as complete.

Share this post


Link to post
Share on other sites
But don't you have to clear the depth to 0.0f then?
glClearDepth(0.0f);
Edit: Also glDepthFunc(GL_GEQUAL); but maybe thats hiding somewhere outside of that snippet. Edited by Ohforf sake

Share this post


Link to post
Share on other sites
No effect. All I see is a box of the size of the textured quad I'm drawing as a test in whatever the active color is. It does not seem to succesfully map a texture (which suggests the texture is somehow invalid).

I've done some testing for gl errors and I get the following when I try to bind the framebuffer object as I prepare to draw to it:
GL ERROR: invalid operation (1282)

This doesn't make any sense as the framebuffer object itself tests as complete.

[edit] by the way, thanks for your help so far. Edited by medevilenemy

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!