I'm using LWJGL (OpenGL for Java) library for texture mapping. Here is the code to read Image from file:
BufferedImage image = ImageIO.read(new File(url));
The code for getting Data Raster (Pixels of image) as byte array:
DataBufferByte imageByteBuffer = ((DataBufferByte)image.getRaster().getDataBuffer());
byte[] bytePixels = imageByteBuffer.getData();
Now the code of creating and putting the "bytePixels" array in byte buffer:
pixels = BufferUtils.createByteBuffer(bytePixels.length);
pixels.put(bytePixels);
pixels.flip();
Here for binding all of that to buffer:
id = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, image.getWidth(), image.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pixels);
The problem is, the colors of image textured aren't as the original image colors!!
Original Picture:
Textured image:
This answer to OpenGL Renders texture with different color than original image?, can't solve this issue, because GL_BGR
is not valid in lwjgl Class GL11!