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.