Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking at NeHe's texture mapping tutorial. It looks overly complicated for just loading a texture.

Is there a way to load a texture in SFML and then use it in Open GL? I use SFML for my windowing.

share|improve this question
up vote 11 down vote accepted

Welcome to OpenGL, where the fast things come for free and the easy things are made hard.

If you thought that sample was long and complicated, "just for loading a texture", then you've seen nothing yet. Wait until you have to load DDS textures, process HDR textures, support cubemaps, render to a texture, etc.

Loading a texture in OpenGL consists of two components:

  • Getting the data from your image format. You could load a texture from a file or generate it procedurally. It doesn't matter, as long as you end up with a nice array of color information.

  • Putting the data on the videocard. The data needs to go from system RAM to video RAM.

The first part should be easy, because SFML comes with a standard image loader.

sf::Image img_data;
if (!img_data.LoadFromFile("images/avatar.png"))
    LOG_ERROR("Could not load '%s'.", "images/avatar.png");
    return false;

Next, we'll have to generate an OpenGL texture object.

GLuint texture_handle;
glGenTextures(1, &texture_handle);

There are a number of choices for binding a texture. There are the three main types: 1D, 2D and 3D and you have support for cubemaps (+X, -X, +Y, -Y, +Z, -Z). However, in 99% of the cases, you will want a 2D texture.

glBindTexture(GL_TEXTURE_2D, texture_handle);

Now we get to the interesting part. Let's upload our data to the video card.

    img_data.GetWidth(), img_data.GetHeight(),
    GL_RGBA, GL_UNSIGNED_BYTE, img_data.GetPixelsPtr()

Finally, we can set a number of texture parameters. These answer questions like: what if we use a texture coordinate that is outside of the range (0.0, 1.0)? Do we wrap around (GL_REPEAT) or do we do nothing (GL_CLAMP)? They can also be used to explain to the videocard that we want to use linear interpolation on our textures.


And now you should be able to bind your texture using glBindTexture and use it to draw textured meshes.

share|improve this answer
mmm, I tried doing so and all I get is a white cube: – Darestium Jul 8 '12 at 4:40
Try putting glEnable(GL_TEXTURE2D); in the init function. – knight666 Jul 8 '12 at 5:17
Awesome, works! Thanks! BTW, what does that function do? Enable texturing? – Darestium Jul 8 '12 at 5:42
Yes, it turns on the 2D texture object, so it can receive texture data. You only have to enable it once on initialization, but it is often forgotten when copying code to a new project. Myself included. ;) – knight666 Jul 8 '12 at 8:38
Very nice explanation. You could write tutorials! – danijar Nov 9 '12 at 21:44

SFML 2.0+ makes it even easier to load a texture;

sf::Texture texLid;
std::string image2="images/top.jpg";
if (!texLid.loadFromFile(image2))
    std::cout << "Could not load" << image2;
    char c;
    return false;

glEnable(GL_TEXTURE_2D);//tell OpenGL to use textures when drawing

sf::Texture::bind(&texLid);//select this texture for OpenGL rendering
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.