LWJGL Forum

Programming => OpenGL => Topic started by: Mihai_Ionut_Floares on December 04, 2020, 20:33:55

Title: Read png data
Post by: Mihai_Ionut_Floares on December 04, 2020, 20:33:55
What's wrong here?:
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:
(https://www49.online-convert.com/dl/web7/download-file/87296e09-c552-4d74-a987-286e421e3f05/New%20Project.png)

The original png doesn't have any blue pixels... The wood texture is recognizable.
Title: Re: Read png data
Post by: Mihai_Ionut_Floares on December 04, 2020, 21:21:00
Never Mind. At this row 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...