Slick-Util (Part 1) - Loading Images for LWJGL

Intro
Part 1 - Loading Images for LWJGL
Part 2 - Loading Sounds for LWJGL
Part 3 - TrueType Fonts for LWJGL

Slick-Util supports the PNG, JPG, GIF and TGA image formats. It will load them so that you can use them as OpenGL textures.

Loading an image with Slick-Util is pretty simple. The images details will be stored in Slick-Util's Texture class. The Texture class gives you access to various attributes of the image like width, height, etc. To load an image into the Texture class you use Slick's TextureLoader class and its getTexture() method. You first specify the type of image ("PNG, "TGA", etc) and then the path to the image. The following is an example:

Texture texture;
 
public void init() throws IOException {
 
  texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));
}

And that is it, the image is now loaded and you can just use the Texture.bind() method to bind the texture.

A full working example on how to bind a texture onto an OpenGL quad is shown below.

import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
 
public class TextureExample {
 
	/** The texture that will hold the image details */
	private Texture texture;
 
 
	/**
	 * Start the example
	 */
	public void start() {
		initGL(800,600);
		init();
 
		while (true) {
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
			render();
 
			Display.update();
			Display.sync(100);
 
			if (Display.isCloseRequested()) {
				Display.destroy();
				System.exit(0);
			}
		}
	}
 
	/**
	 * Initialise the GL display
	 * 
	 * @param width The width of the display
	 * @param height The height of the display
	 */
	private void initGL(int width, int height) {
		try {
			Display.setDisplayMode(new DisplayMode(width,height));
			Display.create();
			Display.setVSyncEnabled(true);
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}
 
		GL11.glEnable(GL11.GL_TEXTURE_2D);               
 
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);          
 
        	// enable alpha blending
        	GL11.glEnable(GL11.GL_BLEND);
        	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
 
        	GL11.glViewport(0,0,width,height);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
 
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, width, height, 0, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
	}
 
	/**
	 * Initialise resources
	 */
	public void init() {
 
		try {
			// load texture from PNG file
			texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));
 
			System.out.println("Texture loaded: "+texture);
			System.out.println(">> Image width: "+texture.getImageWidth());
			System.out.println(">> Image height: "+texture.getImageHeight());
			System.out.println(">> Texture width: "+texture.getTextureWidth());
			System.out.println(">> Texture height: "+texture.getTextureHeight());
			System.out.println(">> Texture ID: "+texture.getTextureID());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	/**
	 * draw a quad with the image on it
	 */
	public void render() {
		Color.white.bind();
		texture.bind(); // or GL11.glBind(texture.getTextureID());
 
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0,0);
			GL11.glVertex2f(100,100);
			GL11.glTexCoord2f(1,0);
			GL11.glVertex2f(100+texture.getTextureWidth(),100);
			GL11.glTexCoord2f(1,1);
			GL11.glVertex2f(100+texture.getTextureWidth(),100+texture.getTextureHeight());
			GL11.glTexCoord2f(0,1);
			GL11.glVertex2f(100,100+texture.getTextureHeight());
		GL11.glEnd();
	}
 
	/**
	 * Main Class
	 */
	public static void main(String[] argv) {
		TextureExample textureExample = new TextureExample();
		textureExample.start();
	}
}

Additional Credit
Kevin Glass for writing the Slick Library and initial example code.

Intro
Part 1 - Loading Images for LWJGL
Part 2 - Loading Sounds for LWJGL
Part 3 - TrueType Fonts for LWJGL

Comments

Please help
When I bind the texture it binds to everything

Anonymous Ninja
Thu, 12/05/2013 - 19:11

You have to unbind the texture before you draw something else

glBindTexture(GL_TEXTURE_2D, 0)

Anonymous Ninja
Wed, 04/23/2014 - 18:09

the path must be from your texture's folder (FIX: go to your project's file and create a res folder. move the pictures in there change your code so that the image path is now res/[image_name].[extention] )
Hope this works!

Anonymous Ninja
Thu, 11/07/2013 - 18:46

the path must be from your texture's folder (FIX: go to your project's file and create a res folder. move the pictures in there change your code so that the image path is now res/[image_name].[extention] )
Hope this works!

Anonymous Ninja
Thu, 11/07/2013 - 18:46

Did i have to save my texture image in a specific folder?

here my code :
// load texture from PNG file
texture = TextureLoader.getTexture("PNG", new FileInputStream("Users/tremblay_eric/Desktop/texture-photoshop.png"));

when i try to run it i got these error message:
java.io.FileNotFoundException: Users/tremblay_eric/Desktop/texture-photoshop.png (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:106)
at java.io.FileInputStream.(FileInputStream.java:66)
at TextureExample.init(TextureExample.java:78)
at TextureExample.start(TextureExample.java:22)
at TextureExample.main(TextureExample.java:115)
Exception in thread "main" java.lang.NullPointerException
at TextureExample.render(TextureExample.java:96)
at TextureExample.start(TextureExample.java:26)
at TextureExample.main(TextureExample.java:115)

where the problem???

Anonymous Ninja
Sun, 06/05/2011 - 05:04

try
C:\\Users\\tremblay_eric\\Desktop\\texture-photoshop.png
windows uses the '\' symbol, but since that is also the escape character in Java you need to escape it. Hence the double slash.

Anonymous Ninja
Tue, 05/01/2012 - 00:56

You must include the full path to your image, for example: "C:/res/img.png" Note that you didnt put the drive letter. That may be the problem

Anonymous Ninja
Sun, 02/19/2012 - 17:29

Does

ResourceLoader.getResourceAsStream(String)

not pull resources from the project's home directory or the resulting jar file?

Anonymous Ninja
Tue, 09/11/2012 - 12:40

Your program don't find your picture because it's writes "No such file or directory".

Anonymous Ninja
Tue, 07/12/2011 - 14:44