Hello Guest

Loading texture images to byte buffers

  • 2 Replies
Loading texture images to byte buffers
« on: November 29, 2005, 15:03:44 »
Does someone have sample code which loads images from the standard imaging apis (BufferedImage) to ByteBuffers. I would like to use compressed images (.jpg, .gif) as textures, but the only way to feed OpenGL with textures seems to be using raw image formats in byte buffers.

Thanks in advance


Offline napier

  • ***
  • 104
    • http://potatoland.org
Loading texture images to byte buffers
« Reply #1 on: November 29, 2005, 20:32:21 »
This class loads an image:    

The constructor GLImage(String imgName) calls
loadImage(String imgName) to load a jpeg/gif/png to a Java Image class (could be BufferedImage easily enough), then loads pixels to a ByteBuffer for use in LWJGL.

To use:

Code: [Select]
public void init() {
   GLImage img = new GLImage(String imgName);
   int textureHandle;
   if (img.isLoaded()) {
      textureHandle = makeTexture(img.pixelBuffer, img.w, img.h);

public static int makeTexture(ByteBuffer pixels, int w, int h)
   // get a new empty texture
   int textureHandle = allocateTexture();
   // 'select' the new texture by it's handle
   // set texture parameters
   GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
   GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
   // Create the texture from pixels
   GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, w, h, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels);
   return textureHandle;
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

Loading texture images to byte buffers
« Reply #2 on: November 29, 2005, 23:51:44 »
Thanks. It really helped!