Search code examples
c++openglrenderingframebufferoff-screen

OpenGL Framebuffer Offscreen Rendering


I'm doing an SDL/OpenGL project on MinGW Windows Code::Blocks 12.11, using GLEW. I'm trying to implement 2D off-screen rendering, so that I can eventually try some fragment shader effects.

Here is the scene rendered to the default framebuffer:

Scene Normal

However, if I render the scene to a Framebuffer Object's texture (called fbo_texture) and then try and render that to a full-screen quad, I get this:

Scene with Framebuffer Object

The image appears flipped, mirrored and green. I'm pretty sure that the rendering to the Framebuffer is working correctly, but for the life of me I can't figure out why the texture is appearing so skewed.

Here is how I tried to render fbo_texture to a textured quad, after calling glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0). I am using glMatrixMode(GL_MODELVIEW) and glOrtho(0, screen_width, screen_height, 0, 0, 1) when the program initializes. Screen_width is 400 and screen_height is 400.

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fbo_texture);
glBegin(GL_QUADS);
    x1= 0.5;
    x2= 400.5;
    y1= 0.5;
    y2 = 400.5;
    glTexCoord2f(0.0f, 0.0f); glVertex2f(x1, y1);
    glTexCoord2f(1.0f, 0.0f); glVertex2f(x2, y1);
    glTexCoord2f(0.0f, 1.0f); glVertex2f(x2, y2);
    glTexCoord2f(1.0f, 1.0f); glVertex2f(x1, y2);
glEnd();


glDisable(GL_TEXTURE_2D);

I really appreciate any help, please let me know if I should provide any more information.


Solution

  • This might not be your only problem, but from the part you show, two vertices are swapped in your texture coordinates. The 3rd of the 4 vertices is the top-right corner, with coordinates (x2, y2), so it should have texture coordinates (1.0f, 1.0f):

    glTexCoord2f(0.0f, 0.0f); glVertex2f(x1, y1);
    glTexCoord2f(1.0f, 0.0f); glVertex2f(x2, y1);
    glTexCoord2f(1.0f, 1.0f); glVertex2f(x2, y2);
    glTexCoord2f(0.0f, 1.0f); glVertex2f(x1, y2);
    

    You're also saying that you call glOrtho() after glMatrixMode(GL_MODELVIEW). glOrtho() sets a projection matrix, so it should normally be called after glMatrixMode(GL_PROJECTION).