Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Trouble with displaying/loading multiple textures  (Read 2609 times)
0 Members and 1 Guest are viewing this topic.
Offline Alectriciti

Junior Newbie

« Posted 2012-06-16 02:16:53 »

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

      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!
Offline Mads

JGO Ninja

Medals: 26
Projects: 3
Exp: 6 years

One for all!

« Reply #1 - Posted 2012-06-16 02:21:13 »

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.

Offline ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

« Reply #2 - Posted 2012-06-16 02:37:51 »

Where do you call glBegin()-glEnd()? Wink

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Alectriciti

Junior Newbie

« Reply #3 - Posted 2012-06-16 02:52:27 »

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?
Offline Alectriciti

Junior Newbie

« Reply #4 - Posted 2012-06-16 03:36:59 »

I'd love to thank you both very much one last time. I solved it and I found a solution. Smiley
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);
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #5 - Posted 2012-06-16 10:34:41 »

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?
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.

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Pages: [1]
  ignore  |  Print  

EgonOlsen (45 views)
2018-06-10 19:43:48

EgonOlsen (25 views)
2018-06-10 19:43:44

EgonOlsen (47 views)
2018-06-10 19:43:20

DesertCoockie (202 views)
2018-05-13 18:23:11

nelsongames (127 views)
2018-04-24 18:15:36

nelsongames (126 views)
2018-04-24 18:14:32

ivj94 (867 views)
2018-03-24 14:47:39

ivj94 (128 views)
2018-03-24 14:46:31

ivj94 (771 views)
2018-03-24 14:43:53

Solater (143 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!