Lesson 06 Texturing Update

The old lesson 06 used the glaux library for loading textures. But glaux has been deprecated for a long time.

We will now have a look at SOIL (http://www.lonesock.net/soil.html) a really useful library that loads any image format you want with just one line of code!

The only changes we have to make to the code of the old lesson 06 is including the header file of SOIL instead of glaux. In this tutorial we will assume SOIL.h and the library SOIL.lib are in the same folder as Lesson06.cpp (and the new sample code includes them). So at the top of Lesson06.cpp we change #include <gl/glaux.h> to #include "SOIL.h".

The next step is linking against the SOIL library. In Visual Studio 2010 this works as follows: Right-click on the project name in the left hand tree-view and choose Properties. There under "Configuration Properties -> Linker -> Input -> Additional Dependencies" we  add SOIL.lib by editing the field.

So now how do we use this library to load a texture from our harddrive? Nothing easier than that. Remove the method LoadBMP completely from the Lesson06.cpp and replace the LoadGLTextures method with the following:

 

int LoadGLTextures()									// Load Bitmaps And Convert To Textures
{
	/* load an image file directly as a new OpenGL texture */
	texture[0] = SOIL_load_OGL_texture
		(
		"Data/NeHe.bmp",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_INVERT_Y
		);

	if(texture[0] == 0)
		return false;


	// Typical Texture Generation Using Data From The Bitmap
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	return true;										// Return Success
}

The whole task of loading the texture is now hidden in the call SOIL_load_OGL_texture. Its parameters are first the filename of the image file, then a format specifier where we could tell SOIL to create a grayscale image for us etc, but we want the image as it is on the disk and thus say SOIL_LOAD_AUT).

The next parameter tells SOIL to create a new ID for this texture. Recalling the explanation in Lesson  06, each OpenGL texture is accessed by it's own ID, and if we used glaux we would have to generate this ID ourselves. But SOIL does this for us if we say SOIL_CREATE_NEW_ID.

The last parameter allows a lot of options, which are all documented in the header file SOIL.h. But for now we just need one option and that is to flip the y-axis of the image. The reason is, that nearly all image formats are stored starting at the upper left corner, but OpenGL defines the origin of the image in the lower left corner. Thus flipping it vertically solves this discrepancy.


Download:

Forum:

Support this Author