Drawing to Textures does not work LWJGL

I'm trying to draw to a 2d texture and to draw that texture in a 3D scene.

Code for Initializing the framebuffer objects and textures:

public static void initFBO()
{
    renderEngine.framebuffers = new int[7];
    renderEngine.monitorTextures = new int[7];

    for(int i = 0;i < 7;i++)
    {
        renderEngine.framebuffers[i] = glGenFramebuffers();
        renderEngine.monitorTextures[i] = glGenTextures();

        glBindFramebuffer(GL_FRAMEBUFFER, renderEngine.framebuffers[i]);
        glBindTexture(GL_TEXTURE_2D, renderEngine.monitorTextures[i]);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer)null);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderEngine.monitorTextures[i], 0);
    }       

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        System.err.println("Error while creating FBO");

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

Code for rendering a quad to the framebuffer: glUseProgram(0);

    for(int i = 0;i < 6;i++)
    {
        glBindFramebuffer(GL_FRAMEBUFFER, framebuffers[i]);
        glPushAttrib(GL_VIEWPORT_BIT);
        glViewport(0, 0, 512, 512);
        glClearColor(0f, 0f, 0f, 1f);
        glClear(GL_COLOR_BUFFER_BIT);

        //glActiveTexture(GL_TEXTURE0);
        //glEnable(GL_TEXTURE_2D);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 512, 512, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

        glUseProgram(0);

        glColor4f(1f, 0f, 0f, 1f);
        glBegin(GL_QUADS);
        glVertex2f(20, 20);
        glVertex2f(420, 20);
        glVertex2f(420, 420);
        glVertex2f(20, 420);
        glEnd();

        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glPopAttrib();
    }

If i change the clear color, the texture gets that color when rendered in my 3D scene. But I'm unable to see the quad. Am i doing something the wrong way?


If found the solution. Cause I was using a 3D environement and had GL_CULL_FACE enabled and set to CCW (counterclockwise), the quad drawn clockwise was not shown.

I just disabled this before drawing to the framebuffer and reenabled it aftwerwards.

glDisable(GL_CULL_FACE);
glEnable(GL_CULL_FACE);
链接地址: http://www.djcxy.com/p/34040.html

上一篇: LWJGL纹理不渲染

下一篇: 绘制纹理不起作用LWJGL