Is there any way to draw images from files without

reverting to sill swing/awt libs?

This is seriously getting on my nerves, the amount ot tutorials that don’t help me with this are enourmous. I’ve spent hours trying to find alternatives to awt/swing image rendering.

I can render silly shapes with several colours and have them all move in strange directions, but I can’t seem to be able to load images in LWJGL. I never had any trouble doing this on an applet, so why is it so difficult under GL?

Second of all, why doesn’t Keyboard.key do anything?

Thanks

You can use swing/awt to load you images. Create a BufferedImage and use getRGB to fetch the data. You’ll need to send the image data to opengl as an array of ints.

Or you could use an independant image loader. There is a tga loader in the Shaven Puppy Game Library:
http://cvs.sourceforge.net/viewcvs.py/spgl/spgl-tools/src/com/shavenpuppy/jglib/tools/

Keyboard.key is used when keyboard buffering is enabled.

[quote]Second of all, why doesn’t Keyboard.key do anything?
[/quote]
Use Keyboard.poll(), then call Keyboard.isKeyDown(Keyboard.KEY_XXXX) to determine if that key was down when you called .poll().

Nehe lesson 6 handles texture mapping. It shows you a way to load images into opengl. The nehe lessons can be downloaded here:
http://sourceforge.net/project/showfiles.php?group_id=58488

private final static int loadTexture(String path)
{
Image image = (new javax.swing.ImageIcon(path)).getImage();

          // Extract The Image 
        BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_3BYTE_BGR); 
        Graphics2D g = (Graphics2D) tex.getGraphics(); 
        g.drawImage(image, null, null); 
        g.dispose(); 

          // Flip Image 
        AffineTransform tx = AffineTransform.getScaleInstance(1, -1); 
        tx.translate(0, -image.getHeight(null)); 
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
        tex = op.filter(tex, null); 

          // Put Image In Memory 
        ByteBuffer scratch = ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight()); 

        byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null); 
        scratch.clear(); 
        scratch.put(data); 
        scratch.rewind(); 

          // Create A IntBuffer For Image Address In Memory    
        IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();

        GL.glGenTextures(buf); // Create Texture In OpenGL    

        GL.glBindTexture(GL.GL_TEXTURE_2D, buf.get(0)); // Typical Texture Generation Using Data From The Image 

          // Linear Filtering 
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); 

          // Generate The Texture 
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, scratch); 

        return buf.get(0); // Return Image Address In Memory 

}

I’m not sure what the layout will look like because I pasted this.

Ok…
This works for me.
The only thing is that the image/texture HAS to be 256x256 pixels

The way to use it:
define a variable of type int
int num = 0;
when you ini* openGL call the method
num = loadTexture(“path”);

Then you can use that ‘num’ to bind the texture

You can use any size texture provided that its dimensions are a power-of-two. (Independent of each other), eg. 64x16, 512x512, etc.

Cas :slight_smile:

Cool, I didn’t know that. Thanks for the correction.

Thanks fellas.

Though I am skeptical when it comes down to using Sun’s libraries for the buffered image.

Are there alternatives?

Code your own proprietry format and preformat your images into this format. That’s what I do; saves me quite a bit of download space due to using a slightly better compression algorithm.

Cas :slight_smile:

Found this after some googeling:

imagero: lots of formats supported. Returns java.awt.image.ImageProducer though. So you have to go threw awt anyway.
http://reader.imagero.com/

Formatting Objects Processor: A project that has custom image loaders. Source is available but I haven’t looked at it:
http://xml.apache.org/fop/index.html

And there also some kits from sun:
http://java.sun.com/products/jimi/
http://java.sun.com/products/java-media/jai/

Writing a Raw TGA, BMP or RGB/SGI image loader is really, really easy. Grab yourself a copy of the specs and give it a try. This will free you from dependence on AWT (if that’s what you want), and it’s educational, too! ;D