Reading an Image into an IntBuffer for textures

At the moment i’m using this method to create an IntBuffer from an image file :

BufferedImage img = ImageIO.read(input);
int[] rgbs = new int[img.getWidth()*img.getHeight()];
IntBuffer buffer = IntBuffer.wrap(img.getRGB(0,0,img.getWidth(),img.getHeight(),rgbs,0,img.getWidth()));

Is there a better way to do this ?

Koen

For textures you need a direct-buffer, so you’d need a few more lines of code.

It does work this way , the complet code is :

BufferedImage img = ImageIO.read(input);
int[] rgbs = new int[img.getWidth()*img.getHeight()];
IntBuffer buffer = IntBuffer.wrap(img.getRGB(0,0,img.getWidth(),img.getHeight(),rgbs,0,img.getWidth()));
buffer.rewind();

if (mipmapped) {
glu.gluBuild2DMipmaps(target,GL.GL_RGB,img.getWidth(),img.getHeight(),
GL.GL_BGRA,GL.GL_UNSIGNED_BYTE,buffer);
} else {
gl.glTexImage2D(target,0,GL.GL_RGB,img.getWidth(),img.getHeight(),
0, GL.GL_BGRA,GL.GL_UNSIGNED_BYTE,buffer);
}

It just looks like there is a lot of copying going on.

Koen

If you’re using jogl, consider the com.sun.opengl.util.texture package. It lets you do this:


Texture myTexture;
URL url = new URL("data/texture.png");
myTexture = TextureIO.newTexture(url, mipmapped, null);

(you need some exception handling in there too, but you get the gist)

Then, when you want to use the texture in your rendering:


myTexture.bind();

Pretty simple.