使用OpenGL / LWJGL绘制纹理四边形使用旧纹理

时间:2014-03-06 22:53:30

标签: java opengl rendering lwjgl

我正在绘制纹理四边形,但使用的纹理是之前绑定的纹理,即使我正在尝试绑定新纹理。

在GuiHandler.java中创建纹理

    tex = GL11.glGenTextures();
    InputStream in = null;
    try {
        in = new FileInputStream(new File("res/gui.png"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    PNGDecoder decoder = null;
    try {
        decoder = new PNGDecoder(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth()
            * decoder.getHeight());
    try {
        decoder.decode(buffer, decoder.getWidth() * 4,
                PNGDecoder.Format.RGBA);
    } catch (IOException e) {
        e.printStackTrace();
    }
    buffer.flip();
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
            GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
            GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0,
            GL11.GL_RGB8, decoder.getWidth(), decoder.getHeight(), 0,
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

渲染代码

GL11.glPushAttrib(GL11.GL_TEXTURE_BINDING_2D);
    GL11.glLoadIdentity();
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, GuiHandler.tex);
    GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);
    GL11.glVertex2f(gui.getX(), gui.getY());
    GL11.glTexCoord2f(1, 0);
    GL11.glVertex2f(gui.getX()+gui.getWidth(), gui.getY());
    GL11.glTexCoord2f(1, 1);
    GL11.glVertex2f(gui.getX()+gui.getWidth(), gui.getY()+gui.getHeight());
    GL11.glTexCoord2f(0, 1);
    GL11.glVertex2f(gui.getX(), gui.getY()+gui.getHeight());
    //GL11.glRectf(gui.getX(), gui.getY(), gui.getWidth()+gui.getWidth(), gui.getY() + gui.getHeight());
    GL11.glEnd();
    GL11.glPopAttrib();

有谁知道为什么使用先前绑定的纹理而不是正确的纹理? P.S我正在使用LWJGL

1 个答案:

答案 0 :(得分:0)

您的纹理创建代码确实绑定新创建的纹理对象,但更新之前绑定的纹理对象。