将VBO渲染为全屏Quad

时间:2015-07-18 11:58:51

标签: java opengl render lwjgl vbo

我一直试图将屏幕分辨率为屏幕分辨率1/3的VBO渲染为四倍于屏幕大小的四边形。我做错了什么?

public void initGL() {
    frameBufferID = glGenFramebuffersEXT();
    colorBufferID = glGenTextures();
    depthBufferID = glGenRenderbuffersEXT();

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferID);

    glBindTexture(GL_TEXTURE_2D, colorBufferID);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH / SCALE, HEIGHT / SCALE, 0, GL_RGBA, GL_INT, (java.nio.ByteBuffer)null);

    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorBufferID, 0);

    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBufferID);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL14.GL_DEPTH_COMPONENT24, WIDTH / SCALE, HEIGHT / SCALE);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBufferID);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

public void render() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLU.gluPerspective(90.0f, WIDTH/(float)HEIGHT, 0.001f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glViewport(0, 0, WIDTH / SCALE, HEIGHT / SCALE);

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferID);

    glClearColor(1.0f, 0.0f, 0.0f, 0.5f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1, 1, 1);
    // Render game code here
    glBegin(GL_LINES);
    {
        glVertex2f(0, 0);
        glVertex2f(1, 1);
    }
    glEnd();

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0, WIDTH, 0, HEIGHT, 0.001f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    glBindTexture(GL_TEXTURE_2D, colorBufferID);

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glViewport(0, 0, WIDTH, HEIGHT);

    glTranslatef(0, 0, -1);
    //Draw Quad
    glBegin(GL_QUADS);
    {
        glTexCoord2f(0, 0);
        glVertex2f(0, 0);

        glTexCoord2f(1, 0);
        glVertex2f(WIDTH, 0);

        glTexCoord2f(1, 1);
        glVertex2f(WIDTH, HEIGHT);

        glTexCoord2f(0, 1);
        glVertex2f(0, HEIGHT);
    }
    glEnd();

    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}

我正在尝试在透视中渲染VBO中的内容,然后在正交中绘制全屏四边形。但我尝试的一切都行不通......就像一切。有什么我搞砸了吗?

1 个答案:

答案 0 :(得分:0)

你正在红色背景前渲染一个带有完全红色纹理的四边形,所以没有太多可看的东西。 你试图渲染到纹理中的线实际上是在平截头体之外,当你在z = 0平面上绘制时,你的平截头体在z = [ - 0.001,-1000]范围内。

出于调试目的,我建议您更改两个绘图过程之间的清晰颜色,以便更好地查看两个过程中的哪一个失败。