LWJGL Forum

Programming => OpenGL => Topic started by: dbergh on November 29, 2005, 15:03:44

Title: Loading texture images to byte buffers
Post by: dbergh 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
Title: Loading texture images to byte buffers
Post by: napier on November 29, 2005, 20:32:21
This class loads an image:    
    http://potatoland.org/code/gl/GLImage.java

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:

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
  GL11.glBindTexture(GL11.GL_TEXTURE_2D,textureHandle);
  // 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);
  GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); //GL11.GL_NEAREST);
  GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); //GL11.GL_NEAREST);
  // 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;
}
Title: Loading texture images to byte buffers
Post by: dbergh on November 29, 2005, 23:51:44
Thanks. It really helped!

/d