Hello Guest

Read png data

  • 1 Replies
  • 3431 Views
Read png data
« on: December 04, 2020, 20:33:55 »
What's wrong here?:
Code: [Select]
public int load(String imagepath) {
BufferedImage bi;
try {
bi = ImageIO.read(new File(imagepath));
width = bi.getWidth();
height = bi.getHeight();

int[] pixels_raw = new int[width * height * 4];
pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);
ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4);

for(int i = 0; i < width;i++) {
for(int j = 0;j<height;j++) {
int pixel = pixels_raw[i*width + j];
pixels.put((byte)((pixel >> 16) & 0xFF));
pixels.put((byte)((pixel >> 8) & 0xFF));
pixels.put((byte)(pixel  & 0xFF));
pixels.put((byte)((pixel >> 24) & 0xFF));
}
}

pixels.flip();
id = glGenTextures();

glBindTexture(GL_TEXTURE_2D, id);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_BYTE, pixels);

}
catch(IOException e) {
e.printStackTrace();
}

return id;


}

With this code I get this weird texture:


The original png doesn't have any blue pixels... The wood texture is recognizable.

Re: Read png data
« Reply #1 on: December 04, 2020, 21:21:00 »
Never Mind. At this row
Code: [Select]
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_BYTE, pixels); It should be GL_UNSIGNED_BYTE. I also changed from java buffered image reader to stb image loading...