What I use, it’s not perfect but it work for what I do (you can have problem in the conversion of the BufferedImage to bytes data) :
package net.bonbonchan.basegame.raster;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
/**
*
* @author Bonbon-Chan
*/
public class Texture
{
static protected int lastBind = -1;
private String name;
private int width;
private int height;
private float ratioU;
private float ratioV;
private boolean hasAlpha;
private int handle;
/** Creates a new instance of Texture */
public Texture()
{
ratioU = 1;
ratioV = 1;
}
/**
*
*/
public void destroy()
{
if (handle != -1)
{
IntBuffer scratch = BufferUtils.createIntBuffer(1);
scratch.put(handle);
scratch.flip();
GL11.glDeleteTextures( scratch );
handle = -1;
}
}
/**
*
* @param u
*/
public void setRatioU(float u)
{
ratioU = u;
}
/**
*
* @return
*/
public float getRatioU()
{
return ratioU;
}
/**
*
* @param v
*/
public void setRatioV(float v)
{
ratioV = v;
}
/**
*
* @return
*/
public float getRatioV()
{
return ratioV;
}
/**
*
*/
public void create()
{
IntBuffer scratch = BufferUtils.createIntBuffer(1);
GL11.glGenTextures(scratch);
int error = GL11.glGetError();
if( error != GL11.GL_NO_ERROR)
{
System.out.println("Texture.create : error OpenGL ("+error+")");
}
handle = scratch.get(0);
}
/**
*
* @param minFilter
* @param magFilter
*/
public void setFilter(int minFilter,int magFilter)
{
bind();
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
unbind();
}
/**
*
* @param width
* @param height
* @param type
* @param imageData
*/
public void setImage(int width,int height,int type,ByteBuffer imageData)
{
bind();
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height,
0, type, GL11.GL_UNSIGNED_BYTE, imageData);
int error = GL11.glGetError();
if( error != GL11.GL_NO_ERROR)
{
System.out.println("Texture.setImage : error OpenGL ("+error+")");
}
unbind();
}
/**
*
* @param width
* @param height
* @param type
* @param imageData
*/
public void setImage(int width,int height,int type,IntBuffer imageData)
{
bind();
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height,
0, type, GL11.GL_UNSIGNED_BYTE, imageData);
int error = GL11.glGetError();
if( error != GL11.GL_NO_ERROR)
{
System.out.println("Texture.setImage : error OpenGL ("+error+")");
}
unbind();
}
/**
*
* @param img
*/
public void setImage(BufferedImage img)
{
// System.out.println("IMAGE : opaque("+img.getTransparency()+") type("+img.getType()+")");
if(img.getTransparency() == Transparency.OPAQUE)
{
if (img.getType() == BufferedImage.TYPE_BYTE_INDEXED)
{
BufferedImage img2 = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
Graphics g = img2.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
setImage(img2);
}
else
{
ByteBuffer imageData = ByteBuffer.allocateDirect(3 * img.getWidth() * img.getHeight());
byte data[] = (byte[]) img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), null);
imageData.put(data);
imageData.flip();
setImage(img.getWidth(),img.getHeight(),GL11.GL_RGB,imageData);
}
}
else
{
if (img.getType() == BufferedImage.TYPE_INT_ARGB )
{
IntBuffer imageData = BufferUtils.createIntBuffer(img.getWidth() * img.getHeight());
int data[] = (int[]) img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), null);
imageData.put(data);
imageData.flip();
setImage(img.getWidth(),img.getHeight(),GL11.GL_RGBA,imageData);
}
else
{
ByteBuffer imageData = ByteBuffer.allocateDirect(4 * img.getWidth() * img.getHeight());
byte data[] = (byte[]) img.getRaster().getDataElements(0, 0, img.getWidth(), img.getHeight(), null);
imageData.put(data);
imageData.flip();
setImage(img.getWidth(),img.getHeight(),GL11.GL_RGBA,imageData);
}
}
}
/**
*
* @param warpS
* @param warpT
*/
public void setWarp(int warpS,int warpT)
{
bind();
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, warpS);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, warpT);
unbind();
}
/**
*
* @return
*/
public String getName() {
return name;
}
/**
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
*/
public int getWidth() {
return width;
}
/**
*
* @param width
*/
public void setWidth(int width) {
this.width = width;
}
/**
*
* @return
*/
public int getHeight() {
return height;
}
/**
*
* @param height
*/
public void setHeight(int height) {
this.height = height;
}
/**
*
* @return
*/
public boolean isHasAlpha() {
return hasAlpha;
}
/**
*
* @param hasAlpha
*/
public void setHasAlpha(boolean hasAlpha) {
this.hasAlpha = hasAlpha;
}
/**
*
* @return
*/
public int getHandle() {
return handle;
}
/**
* Bind texture (LWJGL)
*/
public void bind()
{
if (lastBind != handle)
{
GL11.glBindTexture(GL11.GL_TEXTURE_2D, handle);
lastBind = handle;
}
}
/**
*
*/
public void unbind()
{
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
lastBind = -1;
}
}
To use it :
// Create texture
Texture texture = new Texture();
texture.create();
// Load image
BufferedImage image = ImageIO.read("...");
// Set image to the texture
texture.setImage(image);
texture.setFilter(GL11.GL_LINEAR,GL11.GL_LINEAR);
// now you can use the texture
texture.bind();
GL11.glBegin(...