Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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 1023 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.
1  
2  
3  
      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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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!
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: 345
Projects: 2
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:

1  
2  
3  
4  
5  
      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: 56
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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (14 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!