Getting texture data to OpenGL, if

Hi, I was just wondering if there is some smarter way of getting texture data to OpenGL using imageio.

Basically when I load a texture with imageio I get a BufferedImage it return. Then i need to copy the data to an int[] and (might also need to copy int[] to a ByteBuffer, if I don’t the JNI layer will probably still do an extra copying :wink: ) then I can bind the data to GL.

Like this:



Image image = ImageIO.read(file);
int[] rgb = image.getRGB(....);
byteBuffer.put(rgb);
glTexImage2D(....)

The problem is that this approach waste a lot of memory under during a short timeframe and if you are handling a large set of textures this is real problem.

I could write my own imageio->bytebuffer but that dosen’t sound like a fun approach.

Any solutions ?
// Tomas

Take a look at the source code for the VertexProgRefract demo. It uses ImageIO to load PNG images and fetches the Raster and then the DataBuffer out of the BufferedImage. Instead of putting the data into a ByteBuffer you could just pass the DataBuffer’s int[] down into glTexImage2D. No copies are necessary unless you’re doing something like mipmap generation. JOGL’s native code makes no copies of data. If the underlying OpenGL API can accept a pointer into the Java heap, array arguments are accepted and the data is fetched using GetPrimitiveArrayCrirical; otherwise direct buffers are required at the Java level.

thx :slight_smile:

I looked VertexProgRefract code and saw that you used a nio buffer passing the bitmap to GL.


int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
dest = ByteBuffer.allocateDirect(data.length * BufferUtils.SIZEOF_INT);
dest.order(ByteOrder.nativeOrder());
dest.asIntBuffer().put(data, 0, data.length);

Is there a reason ? Since for what I can understand gluBuildMipMaps accepts a int[] as input.

Cheers
// Tomas :slight_smile:

The only reason (as far as I remember) was to avoid duplicating the calls to gluBuild2DMipmaps and glTexImage2D. You have a point in that the current code is pretty inefficient and a bad example; I’ve fixed this in the VertexProgRefract and JRefract demos.