So…
Here’s the TextureLoader:
public class TextureLoader {
/* singleton */
private static TextureLoader instance;
/* Contains the path of the Texture and the Texture itself */
private HashMap<String, Texture> textureMap;
/* the color models for alpha and non-alpha images */
private ColorModel glAlphaColorModel;
private ColorModel glColorModel;
// initialize fields
private TextureLoader() {
textureMap = new HashMap<String, Texture>();
glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[]{8, 8, 8, 8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[]{8, 8, 8, 8},
false,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
}
// return the singleton
public static synchronized TextureLoader getInstance() {
if (instance == null) {
instance = new TextureLoader();
}
return instance;
}
public Texture load(String path) throws IOException {
Texture texture = textureMap.get(path);
if (texture == null) {
texture = getTexture(path,
GL11.GL_TEXTURE_2D,
GL11.GL_RGBA,
GL11.GL_LINEAR,
GL11.GL_LINEAR);
textureMap.put(path, texture);
}
return texture;
}
private Texture getTexture(String path, int target, int dstPixelFormat, int minFilter, int magFilter) throws IOException {
int srcPixelFormat;
int textureID = createTextureID();
Texture texture = new Texture(target, textureID);
GL11.glBindTexture(target, textureID);
BufferedImage image = ImageLoader.load(path);
texture.setWidth(image.getWidth());
texture.setHeight(image.getHeight());
boolean hasAlpha = image.getColorModel().hasAlpha();
if (hasAlpha) {
srcPixelFormat = GL11.GL_RGBA;
} else {
srcPixelFormat = GL11.GL_RGB;
}
ByteBuffer buffer = convertImageData(image, texture, hasAlpha);
if (target == GL11.GL_TEXTURE_2D) {
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
}
GL11.glTexImage2D(target,
0,
dstPixelFormat,
Number.getPowerOfTwo(2, image.getWidth()),
Number.getPowerOfTwo(2, image.getHeight()),
0,
srcPixelFormat,
GL11.GL_UNSIGNED_BYTE,
buffer);
return texture;
}
private int createTextureID() {
return GL11.glGenTextures();
}
private ByteBuffer convertImageData(BufferedImage image, Texture texture, boolean hasAlpha) {
ByteBuffer buffer;
WritableRaster raster;
BufferedImage textureImage;
int textureWidth = Number.getPowerOfTwo(2, image.getWidth());
int textureHeight = Number.getPowerOfTwo(2, image.getHeight());
texture.setTextureWidth(textureWidth);
texture.setTextureHeight(textureHeight);
int bands = 3;
ColorModel textureColorModel = glColorModel;
if (hasAlpha) {
bands = 4;
textureColorModel = glAlphaColorModel;
}
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
textureWidth,
textureHeight,
bands,
null);
textureImage = new BufferedImage(textureColorModel, raster, false, new Hashtable());
Graphics g = textureImage.createGraphics();
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, textureImage.getWidth(), textureImage.getHeight());
g.drawImage(image, 0, 0, textureImage.getWidth(), textureImage.getHeight(), null);
byte[] data = ((DataBufferByte) textureImage.getRaster().getDataBuffer()).getData();
int dataLength = data.length;
buffer = ByteBuffer.allocateDirect(dataLength);
buffer.order(ByteOrder.nativeOrder());
buffer.put(data, 0, dataLength);
buffer.flip();
return buffer;
}
}
That’s how I draw the texture.
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
if(rotation != 0) {
GL11.glPushMatrix();
GL11.glTranslatef(centerX, centerY, 0);
GL11.glRotatef(rotation, 0, 0, 1);
GL11.glTranslatef(-centerX, -centerY, 0);
}
GL11.glVertexPointer(2, 0, vertexBuffer);
GL11.glTexCoordPointer(2, 0, textureBuffer);
GL11.glDrawArrays(GL11.GL_QUADS, 0, 4);
if(rotation != 0) {
GL11.glPopMatrix();
}
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDisable(GL11.GL_TEXTURE_2D);
I want to solve the problem as soon as possible, cause its annoying to test with it.
I’ve really no idea what the problem is or how to solve it.
A image was posted before, hope this is enough illustrative material.