Loading texture images to byte buffers

Started by dbergh, November 29, 2005, 15:03:44

Previous topic - Next topic

dbergh

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

napier

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;
}
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

dbergh