Its just a little function you need:
public static Texture getTexture(BufferedImage bufferedImage, int target, int dstPixelFormat, int minFilter, int magFilter, boolean mipmap) throws IOException
{
int srcPixelFormat = 0;
// create the texture ID for this texture
int textureID = createTextureID();
Texture texture = new Texture(target,textureID);
// bind this texture
GL11.glBindTexture(target, textureID);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
srcPixelFormat = GL11.GL_RGBA;
} else {
srcPixelFormat = GL11.GL_RGB;
}
// convert that image into a byte buffer of texture data
ByteBuffer textureBuffer = convertImageData(bufferedImage,texture);
if (target == GL11.GL_TEXTURE_2D)
{
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
}
glTexImage2D(target, 0, dstPixelFormat, get2Fold(bufferedImage.getWidth()), get2Fold(bufferedImage.getHeight()), 0, srcPixelFormat, GL11.GL_UNSIGNED_BYTE, textureBuffer );
if(mipmap){ glGenerateMipmap(GL_TEXTURE_2D); }
textureBuffer.clear();
textureBuffer = null;
bufferedImage.flush();
bufferedImage = null;
return texture;
}
By default you could use:
int target = GL11.GL_TEXTURE_2D;
int dstPixelFormat = GL11.GL_RGBA;
int minFilter = GL11.GL_LINEAR;
int magFilter = GL11.GL_LINEAR;
boolean mipmap = false;
Image to buffer:
public static MappedByteBuffer convertImageData(BufferedImage bufferedImage) {
WritableRaster raster;
BufferedImage texImage;
int w = get2Fold(bufferedImage.getWidth());
int h = get2Fold(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,w,h,4,null);
texImage = new BufferedImage(glAlphaColorModel,raster,false,null);
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,w,h,3,null);
texImage = new BufferedImage(glColorModel,raster,false,null);
}
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f,0f,0f,0f));
g.fillRect(0,0,w,h);
g.drawImage(bufferedImage,0,0,null);
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
MappedByteBuffer imageBuffer = (MappedByteBuffer)MappedByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
Power of 2 function:
private static int get2Fold(int fold) {
int ret = 2;
while (ret < fold) {
ret *= 2;
}
return ret;
}
*its faster (very much) to skip the bufferedimage and write directly to a bytebuffer.