Rendering to framebuffer shows wrong result

I am trying to render all my renderings to a framebuffer instead of the backbuffer, while using the LWJGL.
I do the following things for the graphics.
Setting up the display and setting up OpenGL:

    try {
        Display.setDisplayMode(new DisplayMode(1024, 768));
        Display.create();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
    Display.setTitle(WINDOW_NAME);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 1024, 0, 768, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

Then I create a framebuffer object:

    m_width = p_width;
    m_height = p_height;


    m_frameBufferObject = glGenFramebuffersEXT();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_frameBufferObject);
    m_depthBuffer = glGenRenderbuffersEXT();
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_depthBuffer);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, p_width, p_height);

    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_depthBuffer);
    m_frameBufferTexture = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, m_frameBufferTexture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, p_width, p_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (java.nio.ByteBuffer)null);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_frameBufferTexture, 0);

The rendering is then done like this (currently I try only to draw one pixel):

    // Bind the framebuffer object.
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_frameBufferObject);
    glPushAttrib(GL_VIEWPORT_BIT);
    glViewport(0, 0, m_width, m_height);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  

    // Clear with black
    GL11.glColor3f(0.0f, 0.0f, 0.0f);

    GL11.glBegin(GL11.GL_POINTS);
    setOpenGLColour(new Colour(Colour.RED));
    GL11.glVertex2f(100, 100);
    GL11.glEnd();

    //Deactivate the framebuffer object
    glPopAttrib();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    //Render the framebuffer object to the screen
    GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0f, 0f);
    GL11.glVertex2f(0, 0);
    GL11.glTexCoord2f(1f, 0f);
    GL11.glVertex2f(1024, 0); 
    GL11.glTexCoord2f(1f, 1f);
    GL11.glVertex2f(1024, 768);
    GL11.glTexCoord2f(0f, 1f);
    GL11.glVertex2f(0, 768);
    GL11.glEnd();

The problem is: The complete screen is red now. Not just a red pixel, but the complete screen is coloured red. If I do not draw the pixel, then the screen ist just black.
Can someone tell me what I may be doing wrong, or how this is done right?


It looks like you're not getting texturing when you try to copy the FBO rendering result to the screen. Since you set the current draw color to red, it will simply draw the screen size quad in red.

The one thing I see missing is that texturing is never enabled. Add this before starting the final draw command:

glEnable(GL_TEXTURE_2D);

The texture also needs to be bound, which would typically happen during the draw function. But since you bound the texture during setup, and never unbound it, that part is most likely not causing the issue.

链接地址: http://www.djcxy.com/p/34030.html

上一篇: lwjgl纹理剔除不正确

下一篇: 渲染帧缓冲显示错误的结果