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