loading a TGA image , wrong colors

hello, i started to learn OpenGL watching the 3dbuzz OpenGL VTMs (www.3dbuzz.com) , and i decided to port my C++ code to jogl, because im a java coder ^^ and i love java, so i was doing the TGA texture loader (uncompress) and it worked fine, i had to do some changes because jogl manage a BufferByte insted of a byte array , well my problem is that the loader show the colors wrong, i textured a cube, i know that the TGA file format use BGR insted of RGB but i already flip the order, and it show wrong still, if you can help me with this, here is the 2 code i used, btw i used the JOGL version with comes with the NeHe demos

this the texture = http://www.joyalstudios.com/files/joyal_wood_texture.tga

Texture.java


package com.ogltest;


import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

import com.sun.opengl.util.BufferUtil;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Vector;

public class Texture{
	

public ByteBuffer imageData;
public int   bpp;
public int   width;
public int   height;
public int texID[] = new int[1];

public String   name;


private GLAutoDrawable drawable;

static Vector<Texture>textures = new Vector<Texture>();


public Texture(String in_filename, String in_name,GLAutoDrawable drawable)
{

	this.drawable = drawable;
	
	loadTGA(in_filename);

	name = in_name;

	textures.add(this);
}
boolean loadTGA(String filename)
{
	
  try{
	FileInputStream file = new FileInputStream(filename);
	DataInputStream dis = new DataInputStream(file);
	
		
	// TGA_Header
	byte ID_Length 				=  dis.readByte();
	byte ColorMapType 			=  dis.readByte();
	byte ImageType 				=  dis.readByte();
	byte ColorMapSpecification[]=  new byte[5];
	
	dis.read(ColorMapSpecification);

	short xOrigin 				= flipEndian(dis.readShort());
	short yOrigin 				= flipEndian(dis.readShort());
	short ImageWidth 			= flipEndian(dis.readShort());
	short ImageHeight			= flipEndian(dis.readShort());
	
	byte pixelDepth =  dis.readByte();
	
	
	bpp = pixelDepth;
	width = ImageWidth;
	height = ImageHeight;
	
	int bytesPerPixel = bpp / 8;
	int imageSize = width * height * bytesPerPixel;
	

	if (ImageType != 2)
		return false;
	
	if ( width <=0 || height <= 0 || (bpp != 24 && bpp != 32))
		return false;


	int type;
	byte[] rawData = new byte[imageSize];

	if ( bpp == 24){
		type = GL.GL_RGB;
		for ( int i = 0; i < imageSize; i += bytesPerPixel )
		{
			rawData[i + 2] = dis.readByte();    // R
			rawData[i + 1] = dis.readByte(); 	// G
			rawData[i    ] = dis.readByte(); 	// B
	
		}
	}else{
		type = GL.GL_RGBA;
		for ( int i = 0; i < imageSize; i += bytesPerPixel )
		{
			rawData[i + 2] = dis.readByte();    // R
			rawData[i + 1] = dis.readByte(); 	// G
			rawData[i	 ] = dis.readByte(); 	// B
			rawData[i + 3] = dis.readByte(); 	// Alpha

		}
	}
	
	
	imageData = BufferUtil.newByteBuffer(rawData.length);
	imageData.put(rawData);
	imageData.flip();

	createTexture(imageData, width, height,type);

  }catch(IOException e){
    e.printStackTrace();
  }
	return true;



}
boolean createTexture(ByteBuffer imageData, int width, int height, int type)
{
	GL gl = drawable.getGL();

	
	gl.glGenTextures(1, texID,0);
	
	gl.glBindTexture(GL.GL_TEXTURE_2D, texID[0]);

	gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
	gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
	

	gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, type, width, height, 0, type, GL.GL_UNSIGNED_BYTE , imageData);
	
	return true;

}

//this method is provided by Slick engine https://bob.newdawnsoftware.com/repos/slick/trunk/Slick/src/org/newdawn/slick/opengl/TGAImageData.java
private short flipEndian(short signedShort) {
	int input = signedShort & 0xFFFF;
	return (short) (input << 8 | (input & 0xFF00) >>> 8);
}

}

OpenGLDemo.java


package com.ogltest;

import com.sun.opengl.util.Animator;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;


public class OpenGLDemo implements GLEventListener,KeyListener {

  static Frame frame=null;
  static int windowWidth=500;
  static int windowHeight=500;

  float cubeRotateX = 45.0f;
  float cubeRotateY = 45.0f;

  boolean keys[]=new boolean[255];
  
 com.ogltest.Texture texture = null;

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        establishProjectionMatrix(gl,windowWidth,windowHeight);

        gl.glShadeModel(GL.GL_SMOOTH);

        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);

        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
        gl.glEnable(GL.GL_PERSPECTIVE_CORRECTION_HINT);
        
        gl.glEnable(GL.GL_TEXTURE_2D);

       texture = new com.ogltest.Texture("data/joyal_wood_texture.tga", "Joyal Box",drawable);


     }
    public void establishProjectionMatrix(GL gl,int width,int height){
        GLU glu = new GLU();

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();

        glu.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 200.0f);
    }
    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();


        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glTranslatef(0,0,-5.0f);
        gl.glRotatef(cubeRotateX,1,0,0);
        gl.glRotatef(cubeRotateY,0,1,0);
        
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glBindTexture(GL.GL_TEXTURE_2D, texture.texID[0]);

        //Draw Cube
        gl.glBegin(GL.GL_QUADS);

        //Top face

    	gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f,-1.0f);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f,-1.0f);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);

    	//Bottom face

     	gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f,-1.0f,-1.0f);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f,-1.0f,-1.0f);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f,-1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f,-1.0f, 1.0f);

    	//Front face

    	gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f,-1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f,-1.0f, 1.0f);

    	//Back face

        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f,-1.0f);
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f,-1.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f,-1.0f,-1.0f);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f,-1.0f,-1.0f);

    	//Left face

    	gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f,-1.0f);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f,-1.0f,-1.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f,-1.0f, 1.0f);

    	//Right face

    	gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f,-1.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f,-1.0f,-1.0f);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f,-1.0f, 1.0f);


        gl.glEnd();
        gl.glFlush();

        displayFPS();
    }

  long lastTime = System.currentTimeMillis();
  long loops = 0;
  float fps= 0.0f;

    public void displayFPS(){
      
        long newTime = System.currentTimeMillis();

       
        if (newTime - lastTime > 100)
        {
            float newFPS = (float)loops / (float) (newTime -  lastTime) * 1000.0f;
            fps = (fps + newFPS) / 2.0f;

            frame.setTitle("OpenGL Demo - "+fps);

            lastTime = newTime;
            loops=0;
        }

        loops++;

    }
    
  long lastTimeKeys = System.currentTimeMillis();
  
    public boolean checkKeys(){
       
    	final float speed = 1.0f;
        final long updateTime = 2;
        
        long newTime = System.currentTimeMillis();
        
        if (newTime - lastTimeKeys > updateTime){
        
	        if (keys[KeyEvent.VK_LEFT])
	            cubeRotateY -= speed;
	
	        if (keys[KeyEvent.VK_RIGHT])
	             cubeRotateY += speed;
	
	        if (keys[KeyEvent.VK_UP])
	             cubeRotateX -= speed;
	
	        if (keys[KeyEvent.VK_DOWN])
	             cubeRotateX += speed;
	        
	        lastTimeKeys = newTime;
        }

        return false;
    }

    public void keyPressed(KeyEvent e) {

        keys[e.getKeyCode()]=true;

    
    }

    public void keyReleased(KeyEvent e) {

        keys[e.getKeyCode()]=false;

    
    }
    public static void main(String[] args) {
    	   
      OpenGLDemo ogl=new OpenGLDemo();
      initWindow("OpenGL Demo",windowWidth, windowHeight,ogl);
    	   
      boolean done=false;
          while  (!done){
  	             ogl.checkKeys();
       
    	   }

   }

    public static void initWindow(String title,int width, int height,OpenGLDemo ogl)
    {
        frame = new Frame(title);
        GLCanvas canvas = new GLCanvas();   

        canvas.addGLEventListener(ogl);
        frame.addKeyListener(ogl);
        frame.add(canvas);
        frame.setSize(width, height);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                // Run this on another thread than the AWT event queue to
                // make sure the call to Animator.stop() completes before
                // exiting
                new Thread(new Runnable() {

                    public void run() {
                        animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });
        // Center frame
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        animator.start();


    }
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

    }
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }


    public void keyTyped(KeyEvent e) {
       
    }


}

You might want to look at TextureIO utility class, I seem to recall that it will handle TGA for you, and all the other complexity around texture loading. I think the demo TestTexture would be a good example…

oh thx a lot cork , it worked so good, i didnt know about that class, i will use this in the future insted of my own class

but if somebody can tell me the error why it shows me the wrong color i will be so thankful too, i just curious and i want to know what happened in there too

good question. I got your code running locally but haven’t figured it out. Maybe you could try a simple TGA texture with bands of solid colors RED GREEN BLUE WHITE to see if that helps work it out. I don’t have a tool here for creating uncompressed TGA otherwise I would have tried it.

hey Cork i did that you told me and i was able to figer out the problem , the color was in the wrong order

i changed this

                        
                        rawData[i + 2] = dis.readByte();     // R
			rawData[i + 1] = dis.readByte(); 	// G
			rawData[i      ] = dis.readByte(); 	// B

for this



	                rawData[i      ] = dis.readByte();      // R
			rawData[i + 2] = dis.readByte(); 	// G
			rawData[i + 1] = dis.readByte(); 	// B

is a little bit strange because the TGA colors are BGR , so you have to swap the B with the R and that’s it , but in this case is RBG ? well, thx for everything :smiley: