Simple texture loading with DevIL revisited

I wrote an article a while back about loading images to use as textures in OpenGL, and in it I’d written my own single line image to texture-handle function because I couldn’t get the built-in ilutGLLoadImage function to work. Since I’ve been getting blank looks and failure to compile reports about it, I thought I’d go and take a look at it again, and this time ilutGLLoadImage seems to work just fine, so let’s document it up properly shall we?


You will need:

  • A C++ compiler of your choice (I prefer Code::Blocks)
  • Working OpenGL drivers
  • A copy of DevIL
  • A copy of GLEW (GL Extension Wrangler – to make the functionality in your OpenGL drivers availble for use)
  • A copy of GLFW (GL FrameWork – to quickly and easily set up a OpenGL context)
  • An image to load

Preparation and libraries

Create a new project in your IDE of choice, and link in the following libraries:
DevIL required libraries

You’ll have to figure out where the libraries are on your own system but these are where they are on mine, where a /usr/local/ address indicates packages I’ve build myself, /usr/lib/ indicates standard system installed packages, and no prefix uses any paths defined by the compiler (in this case /usr/ where the lib and include directories are assumed). I’ve also added /usr/local/ to the include path so that any headers in /usr/local/include/ are picked up before any in /usr/include. Yes, library paths are a pain ;)

Also, be sure that your project includes its own directory as the working directory (i.e. the directory to be in when executing) so that you can place your image (in this example, a file called abstract-image.jpg) in the main project directory and it’ll be picked up when we try to load it. The way that you specify the working directory will vary depending on the IDE you’re using, but in Code::Blocks you set it from Project | Properties | Build targets like this:

Code::Blocks Working Directory

Source Code

Create a new project with source code something like this:

End Result

If you’ve linked in the libraries and used source code like the above, you should end up with a working texture that looks something like this:
ilutGLLoadImage Example

And information regarding the image being used that looks something like this:
DevIL Image Data

I’ve attached a copy of my Code::Blocks project here for anyone who wants it.


Credits: The wallpaper I used as the texture in this guide is Life by N.Design Studio, which you can find here.

3 thoughts on “Simple texture loading with DevIL revisited”

  1. When I try with recommended libraries, above, i get segfault on FreeBSD 10.0-CURRENT.

    However, adding thr library to link works. It seems one of the OpenGL ports is built against the alternate threading library.

    My system is running:

    GLEW version 1.9.0
    Reporting capabilities of display :0.0, visual 0x2b
    Running on a GeForce GT 220/PCIe/SSE2 from NVIDIA Corporation
    OpenGL version 3.3.0 NVIDIA 310.32 is supported

    I did not try Code::Blocks but here is a Makefile for the example:

    CXX= /usr/local/bin/clang++
    CC= /usr/local/bin/clang
    CXXFLAGS+= -std=c++11 -stdlib=libc++ -I/usr/local/include -Wall -Werror -pedantic
    LIBS+= -lGLEW -lGL -lthr -lIL -lILU -lILUT -lglfw -lGLU
    LDFLAGS+= -L/usr/local/lib

    all: testone

    ${CXX} ${CXXFLAGS} ${LDFLAGS} ${LIBS} -o testone testone.cpp

    rm -f testone

    (I also changed a couple of lines, to avoid compiler warning)

    ILstring imageFilename = “abstract-image.jpg”; // Specify filename
    textureHandle = ilutGLLoadImage(imageFilename); // Load image directly to texture


    textureHandle = ilutGLLoadImage((ILstring)”abstract-image.jpg”); // Load image directly to texture

    to get example to compile.

    Thank you.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.