Amit Assaraf
Amit Assaraf

Reputation: 504

Java - LWJGL Loading texture, only the lastest texture that I load works

I am creating a game in LWJGL and im currently implementing textures. (3D) When I load only one texture using my method it works fine, but if I load 2 textures or more only the latest texture that I loaded is active and I can only use that one...

See the code and you will understand:

my method to load a texture:

private int[] loadRTexture(String filename) {
    int[] twh = new int[3];
    ByteBuffer buf = null;
    int tWidth = 0;
    int tHeight = 0;

    try {
        // Open the PNG file as an InputStream
        InputStream in = new FileInputStream(filename);
        // Link the PNG decoder to this stream
        PNGDecoder decoder = new PNGDecoder(in);

        // Get the width and height of the texture
        tWidth = decoder.getWidth();
        tHeight = decoder.getHeight();
        twh[1] = tWidth;
        twh[2] = tHeight;   

        // Decode the PNG file in a ByteBuffer
        buf = ByteBuffer.allocateDirect(
                4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
        buf.flip();

        in.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    // Create a new texture object in memory and bind it
    int texId = GL11.glGenTextures();
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);

    // All RGB bytes are aligned to each other and each component is 1 byte
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // Create a new texture object in memory and bind it    
    // Upload the texture data and generate mip maps (for scaling)
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, 
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

    // Setup the ST coordinate system
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);

    // Setup what to do when the texture has to be scaled
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, 
            GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, 
            GL11.GL_LINEAR_MIPMAP_LINEAR);
    twh[0] = texId;
    return twh;
}

I store the generated int texId = GL11.glGenTextures(); in an arraylist after this method.

then when I want to render this is how I bind the texture:

GL11.glBegin(GL11.GL_QUADS); // Start Drawing The Cube
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, game.getResourceManager().getTextures().get(game.getResourceManager().getSpriteSheets().get(0).getTextureID()));
    GL11.glTexCoord2f(x, y);
    GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
    GL11.glTexCoord2f(x, y+celly);
    GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
    GL11.glTexCoord2f(x+cellx, y+celly);
    GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
    GL11.glTexCoord2f(x+cellx, y);
    GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

this line:

game.getResourceManager().getTextures().get(game.getResourceManager().getSpriteSheets().get(0).getTextureID())

gets the needed texture int that was generated.. (it gets the correct one, I have checked.)

So why is this happening?

Upvotes: 0

Views: 3272

Answers (2)

amedley
amedley

Reputation: 121

You can't bind a texture after glBegin has been called. That'll mess up everything,

Upvotes: 0

Flafla2
Flafla2

Reputation: 545

If you are having trouble with your Texture loader, try out mine here: LWJGL Textures and Strings

If you really don't want to, compare your code to mine and see if there is anything wrong.

Upvotes: 1

Related Questions