Wrong colors in my texture

Started by xixi, October 17, 2010, 11:36:31

Previous topic - Next topic

xixi

Hello,

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

This is my code to load a texture:

    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

spasi

Sounds like the BufferedImage is in BGR order. Try using GL12.GL_BGR in the format parameter of glTexImage2D.

xixi


Matthias

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:
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);