Question

I'm drawing a texture quad, but the texture used is one that was bound previously, even if I'm trying to bind a new texture.

Creating the texture in 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);

The rendering code

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

Does anyone know why a previously bound texture is being used instead of the right one? P.S I'm using LWJGL

No correct solution

OTHER TIPS

Your texture creation code does actually not bind the newly created texture object, but updates the one previously bound.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top