Read png data

Started by Mihai_Ionut_Floares, December 04, 2020, 20:33:55

Previous topic - Next topic

Mihai_Ionut_Floares

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:


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

Mihai_Ionut_Floares

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