main-tutorialtexturemanager

Texture Manager

Created by Yu-Chi Lai at 2006

1.@ Introduction

Tutorial 10 tells us how to load in textures and to use textures in OpenGL environment. However, the graphic card has very limited resource for the allowable number of textures inside its memory. We do not want to load in the same texture several times. Thus, we must have a good way to keep track of the textures used inside a program. Here, we have provided a Texture Manager that can help you with much of the aspects of loading and building textures, finding texture objects, … It allows you to refer to textures by name, and will load them from several image file formats as needed. There is a primitive mechanism provided for searching a list of directories - you should use this to make sure your program will run both in the CSL lab, and on any other machine you might want to hack on. The following are the source file and header file for the texture manager

2.@ Support format

Here is a list of format supported by this texture managers

  • JPG, JPEG
  • BMP
  • TGA
  • GIF
  • PNG

3.@ Extra linking libraries

Since we use the jpeg, bmp, gif, png image format, we’ll need to add fltkjpegd.lib fltkpngd.lib fltkimagesd.lib fltkzd.lib as a dependency to our project. Open the Solution Explorer window, right click our project (CS559-Texturing), and choose Properties. Select Linker -> Input from the left hand pane and add fltkjpegd.lib fltkpngd.lib fltkimagesd.lib fltkzd.lib to the Additional Dependencies. Make sure to separate the libraries with some whitespace.

4.@ How to use it

Here is a list of things you must pay attention to when you use this texture manager

4.1@ Search directory

The texture manager use the elements inside the texturePaths variable as searching path when it tries to locate the texture file. It will look over all the listed directoried one by one until it finds the texture file. And you can specify the path by absolute path or relative path to the project solution directory.


char* texturePaths[] = {
  ".",
  "..",
  "Textures/signs",
  "../Textures/signs",
  "Textures/Textures",
  "../Textures/Textures",
  "Textures/Objects",
  "../Textures/Objects",
  "c:/src/GraphicsTown/Textures/signs",
  "c:/src/GraphicsTown/Textures/Textures",
  0 
};

4.2@ To grap a texture


GLuint fetchTexture(char* name, bool wrapS, bool wrapT)

fetchTexture function is the main fuctions to interact with GL program when the program needs to grap a texture. There are three parameters for this function.

  1. name: the texture filename such as cs559.tga
  2. wrapS: determine whether the program needs to wrap in the t direction in texture coordinate when using the texture
  3. wrapT: determine whether the program needs to wrap in the t direction in texture coordinate when using the texture

When using it, it is like a black box. Every time when you need a texture, you just call this function, it will give you the reference number generated by OpenGL.

You do not need to understand it when you just try to use it. However, we suggest that you should understand how it works because in the future, you may need to develop your own texture manager. The following is how it works. When it is called, it will check whether it has loaded in this texture already. If the texture exists in the list, the manager will give back the OpenGL reference nubmer generated when the texture has been created. If it cannot find the texture in the list, it will go through all the searching directories one by one to find the image file and load the image file in to create the texture. Then, it will set up the proper parameters and store the texture in the list. It will return the OpenGL reference back to the calling function. If the file does not exist, it will return 0 and you need to handle this situation.

5.@ Common mistakes:

Here are a list of things that often go wrong and cause your textures not to appear (whether or not you use my code):

  • Remember that textures must be of certain sizes (multiples of 16)
  • If you are using textures to modulate material properties (which you need to if you are going to see lighting on the textured surface) don’t forget to make the surface a light color since the texture only darkens the existing color.
  • Don’t forget to turn texturing on in the beginning.
  • Don’t forget to use texture coordinates on your vertices.
  • Remember that OpenGL commands do not work in an FlTk program UNLESS you are either in a draw routine, or have done a “make_current.” Look this up in the FlTk manual if you have questions.
  • Don’t forget to turn off textures (bind to texture 0) when you’re done using a texture.

Many of the things that the OpenGL book programs do are optional. Often, you can use the defaults for things like filtering.

Project Source Code

Page last modified on November 03, 2006, at 11:51 AM