无法绘制到FrameBufferObject,然后在LWJGL中绘制到frameBuffer

时间:2014-12-10 19:45:52

标签: java opengl rendering lwjgl framebuffer

我的问题是我已经为帧缓冲对象绘制了一个小四边形,并试图将其绘制到窗口上的帧缓冲区。这是我的java代码:

import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL11.*;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.*;

public class Main
{
    private static int fboID;
    private static int texID;
    private static int depthBuff;

    public static void main(String[] args)
    {
        try
        {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.create();
        } catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }

        glEnable(GL_TEXTURE_2D);
        initFramebuffer();
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

        while (!Display.isCloseRequested())
        {
            glBindTexture(GL_TEXTURE_2D, texID);
            glBegin(GL_QUADS);

            glTexCoord2f(0, 0);
            glVertex2i(10, 10);

            glTexCoord2f(0, 1);
            glVertex2i(10, 470);

            glTexCoord2f(1, 1);
            glVertex2i(630, 470);

            glTexCoord2f(1, 0);
            glVertex2i(630, 10);

            glEnd();
            glBindTexture(GL_TEXTURE_2D, 0);

            Display.update();
            Display.sync(60);
        }
    }

    public static void initFramebuffer()
    {
        fboID = glGenFramebuffers();
        glBindFramebuffer(GL_FRAMEBUFFER, fboID);

        texID = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, texID);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        depthBuff = glGenRenderbuffers();
        glBindRenderbuffer(GL_RENDERBUFFER, depthBuff);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 640, 480);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuff);

        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0);

        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
            System.out.println("GOD DAMT");

        glBindFramebuffer(GL_FRAMEBUFFER, fboID);
        {
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(0, 640, 480, 0, 1, -1);
            glMatrixMode(GL_MODELVIEW);

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glViewport(0, 0, 640, 480);

            //glLoadIdentity();

            glBegin(GL_QUADS);
            glColor3f(1, 0, 0);
            glVertex2i(0, 0);
            glVertex2i(100, 0);
            glVertex2i(100, 100);
            glVertex2i(0, 100);
            glEnd();
        }
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

    }
}

每当我运行此代码时,我只得到黑屏。 我做错了什么?

1 个答案:

答案 0 :(得分:2)

我对OpenGL并不熟悉,但

而不是

    glEnable(GL_TEXTURE_2D);
    initFramebuffer();

初始化fb后应设置纹理:

    initFramebuffer();
    glEnable(GL_TEXTURE_2D);

这是由glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);引起的,它似乎在你的fb纹理框前面渲染了未初始化的(非fb纹理),导致空白屏幕