绘制到纹理不适用于LWJGL

时间:2015-07-12 18:37:55

标签: java opengl lwjgl framebuffer

我试图绘制到2d纹理并在3D场景中绘制该纹理。

初始化帧缓冲区对象和纹理的代码:

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);
}

将四边形渲染到帧缓冲区的代码:     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();
    }

如果我更改了清晰的颜色,纹理会在我的3D场景中渲染时获得该颜色。但是我无法看到四边形。我做错了吗?

1 个答案:

答案 0 :(得分:0)

如果找到解决方案。因为我正在使用3D环境并启用了GL_CULL_FACE并设置为CCW(逆时针),顺时针方向绘制的四边形未显示。

我在绘制到帧缓冲区之前禁用了它,然后重新启用它。

glDisable(GL_CULL_FACE);
glEnable(GL_CULL_FACE);