Hello Guest

Wrong colors in my texture

  • 3 Replies
  • 5989 Views
Wrong colors in my texture
« on: October 17, 2010, 11:36:31 »
Hello,

i got a problem with my textures, their color is completly different from the original.

This is my code to load a texture:

Code: [Select]
    public static int loadTexture2(String s) throws IOException{
    BufferedImage image;

    image = ImageIO.read(new File("./data/grass.jpg"));
 
     
    // 
       
        int width = image.getWidth();
        int height = image.getHeight();
        DataBufferByte rawData =  (DataBufferByte) image.getData().getDataBuffer();

        ByteBuffer data = BufferUtils.createByteBuffer(rawData.getSize());
        data.put(rawData.getData());
        data.rewind();
        IntBuffer scratch = BufferUtils.createIntBuffer(1);

        GL11.glGenTextures(scratch);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, scratch.get(0));
       GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, data);

        return scratch.get(0);
    }

for example i have yellow yellow square as texture it goes blue..


Can anyone tell me what i'm doing wrong?

Thanks in advance

*

Online spasi

  • *****
  • 2261
    • WebHotelier
Re: Wrong colors in my texture
« Reply #1 on: October 17, 2010, 13:32:25 »
Sounds like the BufferedImage is in BGR order. Try using GL12.GL_BGR in the format parameter of glTexImage2D.

Re: Wrong colors in my texture
« Reply #2 on: October 17, 2010, 18:03:41 »
yup indeed, thank you

Re: Wrong colors in my texture
« Reply #3 on: October 17, 2010, 18:32:33 »
Instead of using ImageIO to load images (which is slow and requires additional data copies) you should consider using TWL's PNGDecoder.

JAR: PNGDecoder.jar
Source: PNGDecoder.java

Example usage:
Code: [Select]
InputStream in = new FileInputStream("white_pixel.png");
PNGDecoder decoder = new PNGDecoder(in);

System.out.println("width="+decoder.getWidth());
System.out.println("height="+decoder.getHeight());

ByteBuffer buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
decoder.decode(buf, decoder.getWidth()*4, Format.RGBA);
buf.flip();

// now upload the data
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);