将纹理绘制到帧缓冲区会扭曲纹理

时间:2013-05-12 15:08:41

标签: java opengl textures lwjgl framebuffer

我正在使用LWJGL OpenGL绑定java,我想将纹理绘制到帧缓冲区,这样我就可以渲染总是同时使用的纹理。

我遵循LWJGL wiki关于使用framebuffer对象的教程。起初我根本看不到任何东西,然后我创建了一个准系统测试类。

这是我的精灵的样子:link

这是输出的样子:link

这是代码(它使用slickutil进行纹理加载):

public class ComboSpriteTest {
public static void main(String[] args) {
    try {
        Display.setDisplayMode(new DisplayMode(400, 300));
        Display.create();
        Display.setFullscreen(false);
    } catch (LWJGLException e) {
        e.printStackTrace();
    }
    initOGL();

    // use slickUtil to load a textre
    Texture tex;
    try {
        tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("walkcycle.png"));
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    int texId = createFBOAandDraw(tex);
    GL11.glClearColor(1, 0, 0, 1);

    while (!Display.isCloseRequested()) {
        try {
            Thread.sleep(1000 / 30);
        } catch (InterruptedException e) {

        }
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindTexture(GL_TEXTURE_2D, texId);

        glLoadIdentity();
        glBegin(GL_QUADS);

        glTexCoord2d(0, 0);
        glVertex2d(0, 0);

        glTexCoord2d(0, 1);
        glVertex2d(0, tex.getTextureHeight());

        glTexCoord2d(1, 1);
        glVertex2d(tex.getTextureWidth(), tex.getTextureHeight());

        glTexCoord2d(1, 0);
        glVertex2d(tex.getTextureWidth(), 0);

        glEnd();

        Display.update();
    }
    Display.destroy();
}

public static void initOGL() {
    Display.setTitle("FramebufferTest");
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 400, 300, 0, -1, 1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glViewport(0, 0, 400, 300);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}

public static int createFBOAandDraw(Texture texture) {
    int framebufferID = glGenFramebuffersEXT();
    int colorTextureID = glGenTextures();

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);

    glBindTexture(GL_TEXTURE_2D, colorTextureID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture.getTextureWidth(), texture.getTextureHeight(),
            0, GL_RGBA, GL_INT, (java.nio.ByteBuffer) null);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTextureID, 0);

    glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT);
    glViewport(0, 0, texture.getTextureWidth(),texture.getTextureHeight());

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID);
    glBindTexture(GL_TEXTURE_2D, texture.getTextureID());

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

    glLoadIdentity();

    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);

    glTexCoord2f(texture.getWidth(), 0);
    glVertex2f(texture.getImageWidth(), 0);

    glTexCoord2f(texture.getWidth(), texture.getHeight());
    glVertex2f(texture.getImageWidth(), texture.getImageHeight());

    glTexCoord2f(0, texture.getHeight());
    glVertex2f(0, texture.getImageHeight());

    glEnd();

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    glPopAttrib();

    return colorTextureID;
}
}

纹理显示为缩放到原始高度的大约1/8并且上下颠倒。 有关它为何如此呈现的任何见解?

1 个答案:

答案 0 :(得分:1)

我认为你已经搞砸了宽度和高度。看下面

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindTexture(GL_TEXTURE_2D, texId);

    glLoadIdentity();
    glBegin(GL_QUADS);

    glTexCoord2d(0, 0);
    glVertex2d(0, 0);

    glTexCoord2d(1, 0);
    glVertex2d(tex.getTextureWidth(), 0);

    glTexCoord2d(1, 1);
    glVertex2d(tex.getTextureWidth(), tex.getTextureHeight());

    glTexCoord2d(0, 1);
    glVertex2d(0, tex.getTextureHeight());

    glEnd();