[Solved] Trouble with displaying/loading multiple textures

I’ve been stuck on this all day. Googling did nothing, neither did the LWJGL wiki.

I have different tiles that will be displayed with multiple textures. Those textures are loaded into a HashMap, and they load just fine. However, when I attempt to display these tiles, they all use the same texture. It’s odd to me, because they all use the last loaded texture.

Let’s say I initialize the textures here. Whichever line of code is the last, that texture will be used for everything. The only way I can tell them apart is because I also have the tiles colored as well. So right now, all my textures in the game are stone.


		texturefile.LoadTexture("textures/water.png", "water");
		texturefile.LoadTexture("textures/metal.png", "metal");
		texturefile.LoadTexture("textures/stone.png", "stone");

Here’s the draw method for each tile. Is there something I am missing here? Or is it in the initialization?

public void paint(int camx, int camy){

		GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturefile.getTexture(type.name().toLowerCase()).getTextureID());

		GL11.glColor3f(R*0.6f, G*0.6f, B*0.6f);
		GL11.glVertex2d(l.getX()+camx-offset, l.getY()+camy-offset);
		GL11.glVertex2d(l.getX()+camx+offset, l.getY()+camy-offset);
		GL11.glVertex2d(l.getX()+camx+offset, l.getY()+camy+offset);
		GL11.glVertex2d(l.getX()+camx-offset, l.getY()+camy+offset);
		
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex2d(l.getX()+camx-offset+1, l.getY()+camy-offset+1);
		GL11.glTexCoord2f(1, 0);
		GL11.glVertex2d(l.getX()+camx+offset-1, l.getY()+camy-offset+1);
		GL11.glTexCoord2f(1, 1);
		GL11.glVertex2d(l.getX()+camx+offset-1, l.getY()+camy+offset-1);
		GL11.glTexCoord2f(0, 1);
		GL11.glVertex2d(l.getX()+camx-offset+1, l.getY()+camy+offset-1);
	}

My game is fairly simple as far as how it’s displayed. I think once I understand loading textures, things will be a lot easier. Any help would be great, thanks for reading!

You may not want to bind “too much” in each loop. Binding a texture is a task that requires a lot of resources, and you should try to make as few calls as possible. Try to bind a texture with all your tile images on, and then just put parts of that bound texture on the actual tiles.

Where do you call glBegin()-glEnd()? :wink:

Bind texture -> glBegin() -> specify vertices + coords + colors -> glEnd() -> repeat

ra4king, I called glBegin and glEnd only once per game tick. But now I put the glBegin and glEnd now for each tile. Is that how it’s done properly?

I got what I wanted to achieve to work, but like Mads said, binding a texture per tile was a bad idea. It bogged my game down ridiculously.

I think I will try binding all my images to one texture. And at that point I would just have to change the vertices to match up with the desired image, correct?

I’d love to thank you both very much one last time. I solved it and I found a solution. :slight_smile:
For the tile type enum, I added a texture x and y constant. Now my texture coordinates can look like so:


		GL11.glTexCoord2f(type.scalex, type.scaley+0.25f);
		GL11.glTexCoord2f(type.scalex+0.25f, type.scaley+0.25f);
		GL11.glTexCoord2f(type.scalex+0.25f, type.scaley);
		GL11.glTexCoord2f(type.scalex, type.scaley);
		

Ideally you call begin/end as little as possible. But you can’t change any opengl state (such as binding textures, or changing matrices) between being/end. This often means that you end up having to rearrange your data (like putting all tiles into one big texture) to minimise begin/end calls. Exactly how/when you call begin/end depends a lot on what you’re drawing and how.