Hello all,
I’m wondering if the texture loader I have is good enough for 2D games in LWJGL. It works great with 3D development, but with 2D textures with alpha values, there’s small artifacts in the image. What can I do to improve this for 2D games?
I’m converting the image data to bytes myself.
// Loads a texture.
public int loadTexture(String filename) {
int ID = 0;
try {
BufferedImage image = ImageIO.read(getClass().getResource(filename));
int imageW = image.getWidth();
int imageH = image.getHeight();
int size = imageW*imageH;
IntBuffer data = BufferUtils.createIntBuffer(size);
for (int y=0; y<imageH; y++) {
for (int x=0; x<imageW; x++) {
Color c1 = new Color(image.getRGB(x, y), true);
Color c2 = new Color(c1.getBlue(), c1.getGreen(), c1.getRed(), c1.getAlpha());
data.put(c2.getRGB());
}
}
data.flip();
IntBuffer buf = BufferUtils.createIntBuffer(1);
GL11.glGenTextures(buf);
ID = buf.get(0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, ID);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, imageW, imageH, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data);
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);
} catch(Exception e) {
e.printStackTrace();
Sys.alert("Error", "Unable to load texture: "+filename);
System.exit(-1);
}
return ID;
}
I’m using the following blend function:
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);