Solved LWJGL Opengl binding, binding only one texture!

Hello JGO, i have a problem with binding images. The problem is that the id of the first image loaded its used for all images.
I know where is the problem but i can’t figure it out how to make a system for this.

Here is my load function:


	public static BufferedImage loadImage(String path){
		BufferedImage bufferedimage = null;
		try {
			bufferedimage = ImageIO.read(new File("res/assets/alphagame/textures" + path));
			int w = bufferedimage.getWidth();
			int h = bufferedimage.getHeight();
			int[] pixels = new int[w * h];
			bufferedimage.getRGB(0, 0, w, h, pixels, 0, w);
		    ByteBuffer buffer = BufferUtils.createByteBuffer(w * h * 3);
		    
		    for(int y = 0; y < h; y++){
		        for(int x = 0; x < w; x++){
		            int pixel = pixels[y * w + x];
		            buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
		            buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
		            buffer.put((byte) (pixel & 0xFF));             // Blue component
		        }
		    }
		    buffer.flip();
		    
		    textureID = GL11.glGenTextures();
		    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
		    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);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, w, h, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bufferedimage;
	}

Here is my render function:


	public static void drawImage(int x, int y, BufferedImage bufferedimage){
		if(bufferedimage !=null)
		{
	    	GL11.glPushMatrix();
		    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
	    	GL11.glColor3f(1.0f, 1.0f, 1.0f);
	    	GL11.glBegin(GL11.GL_QUADS);
	    	GL11.glTexCoord2f(0,0);
			GL11.glVertex2f(x,y);
	    	GL11.glTexCoord2f(1,0);
			GL11.glVertex2f(x+bufferedimage.getWidth(),y);
	    	GL11.glTexCoord2f(1,1);
			GL11.glVertex2f(x+bufferedimage.getWidth(),y+bufferedimage.getHeight());
	    	GL11.glTexCoord2f(0,1);
			GL11.glVertex2f(x,y+bufferedimage.getHeight());
			GL11.glLoadIdentity();
			GL11.glEnd();
	        GL11.glPopMatrix();
		}
	}

I think its really obviously what happend, the two images are diffrent but bind with same texture ID.

http://img856.imageshack.us/img856/9030/6s4a.png

You need to store your texture ID of the bufferedimage you just generated. Use a HashMap to map the bufferimage object to the id.


private static Map<BufferedImage, Integer> texMap = new HashMap<BufferedImage, Integer>();

Then you map the buffered images whenever they are loaded with their texture id in the loadImage method.


texMap.put(bufferedimage, textureID);

Then whenever you are drawing it, you get the id from the map and bind that texture.


int textureid = texMap.get(bufferedimage);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureid);

// Continue to draw it

However, this code can be highly expensive, because it involves a lot of state changes for a complete scene and also you are using the old and deprecated immediate mode rendering. I highly advice you to look at modern opengl and texture atlasses.

Thank you for reply. I’ll have in mind your advice, my next step is to implent texture atlases, but i wanted to have load/render function espacilly for single texture. I’m familiar with old render style but in future i’ll move to modern opengl.

Edit: I added

texMap.put(bufferedimage, textureID);

after

textureID = GL11.glGenTextures();

I got this error.


Exception in thread "main" java.lang.ExceptionInInitializerError
	at net.alphagame.entity.Player.render(Player.java:21)
	at net.alphagame.client.Alpha.render(Alpha.java:100)
	at net.alphagame.client.Alpha.gameLoop(Alpha.java:108)
	at net.alphagame.client.Alpha.main(Alpha.java:117)
Caused by: java.lang.NullPointerException
	at net.alphagame.client.renderer.Sprite.loadImage(Sprite.java:43)
	at net.alphagame.client.renderer.Sprite.<clinit>(Sprite.java:18)
	... 4 more

Try to initialize in the loadimage method.


private static Map<BufferedImage, Integer> texMap;

public static BufferedImage loadImage(String path)
{
    if (texMap == null)
        texMap = new HashMap<BufferedImage, Integer>();

   ...[snip]...
}

That should solve your problem. That exception [icode]ExceptionInInitializerError[/icode] occurs if static initialization fails. This also occurs in static blocks.

Thanks for your help, now its working. ;D