简体   繁体   中英

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:

场景正常

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:

带帧缓冲对象的场景

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM