Texture Mapping in OpenGL & SFML

Texture mapping

Check out my texture mapping notes for an overview of texture mapping principles

  1. Load the texture data from file
  2. Generate an texture 'object'
  3. Select current texture
  4. Specify filtering
  5. Generate the textures from the image data
  6. Enable texture mapping
  7. Draw the scene, providing texture coordinates for each vertex

Loading the picture for the texture

Use SFML's Image class to load the picture file;

Generating the texture 'object'

We need to ask OpenGL to generate a texture object for every texture we are going to use in the app.

The function glGenTextures(...) generates a texture object. We will use the integer as a texture ID

Select Current 2D texture

Set current texture (the following texture operations apply to current texture). We will nedd to do this every time we change textures.

Specify texel filtering

Need to tell OpenGL how filtering is to be applied. There are two categories of filtering;

  1. Magnification; the pixel is smaller than the corresponding texel
  2. Minification; the pixel is larger than corresponding texel

     

Generate the textures from image data

The texture data is stored internally within the OpenGL system (possibly on a graphics card). This phase takes the bitmap data and stores it as a texture.

If we are using Mipmaps, then we need to call this function once for each mipmap, changing the mipmap level number appropriately.

 

Enable texture mapping

Before we draw anything, must turn on texturing;

Draw the scene

This code creates a quad, associating the vertices of the quad with the corners of the texture;

Putting it all together

Download this project to see it in action