Tag Archives: glBindTexture

LWJGL Textures

By the end of this tutorial you will have learned how to load and display textures.

A quick note

For this tutorial the classes provided by the “slick_util” jar-file will be used.

Loading the LWJGL textures

Firstly, an instance of Texture is created. It is not possible to invoke the constructor of the Texture class, so to initialize the instance, write the following code.

TextureLoader.getTexture(String, InputStream);

This method may throw two exceptions, both of which need to be caught and dealt with.

Texture texture;
try {
	texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/image.png")));
        // Replace PNG with your file extension
} catch (FileNotFoundException e) {
} catch (IOException e) {

Drawing the textures

In the upper-left you can see the texture data. Note: “0,1” should be “0,0”. In the bottom-right you can see the shape that uses the texture data.

OpenGL knows where to find its texture data through texture coordinates. Texture coordinates are coordinates ranging from 0.0 to 1.0 ( (0,0) being upper-left; (1,1) being bottom-left ). They represent a point on the texture. Specify a texture coordinate using the following code.

glTexCoord2f(float, float);

In order for OpenGL to know which texture it wants to extract the data from, the texture needs to be bound. Just like with colours, it uses a global state; only one texture can be bound at a time. The binding of textures works with the following method.

glBindTexture(GL_TEXTURE_2D, texture_id);

You can retrieve your texture id from the texture object by calling.


Similarly, you can unbind a texture from OpenGL when you no longer need to draw with it.

glBindTexture(GL_TEXTURE_2D, 0);

This is how one would draw a textured rectangle on-screen.

glTexCoord2f(1, 0);
glVertex2i(450, 10);
glTexCoord2f(0, 0);
glVertex2i(10, 10);
glTexCoord2f(0, 1);
glVertex2i(10, 450);
glTexCoord2f(0, 1);
glVertex2i(10, 450);
glTexCoord2f(1, 1);
glVertex2i(450, 450);
glTexCoord2f(1, 0);
glVertex2i(450, 10);

Releasing the LWJGL textures

Lastly, the texture needs to be destroyed when it’s no longer needed.



That concludes this tutorial. Should you have any questions or remarks I would love to see them in the comments section below.