Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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 LWJGL Opengl binding, binding only one texture!  (Read 501 times)
0 Members and 1 Guest are viewing this topic.
Offline Dwinin
« Posted 2014-07-14 13:49:32 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
   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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   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.
Offline SHC
« Reply #1 - Posted 2014-07-14 14:00:18 »

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

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

1  
texMap.put(bufferedimage, textureID);

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

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

Offline Dwinin
« Reply #2 - Posted 2014-07-14 14:18:00 »

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
1  
texMap.put(bufferedimage, textureID);
after
1  
textureID = GL11.glGenTextures();

I got this error.
1  
2  
3  
4  
5  
6  
7  
8  
9  
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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2014-07-14 14:59:41 »

Try to initialize in the loadimage method.

1  
2  
3  
4  
5  
6  
7  
8  
9  
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
ExceptionInInitializerError
occurs if static initialization fails. This also occurs in static blocks.

Offline Dwinin
« Reply #4 - Posted 2014-07-14 15:07:24 »

Thanks for your help, now its working. Grin
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

theagentd (14 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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