The following OpenGL function call generates names for the texture objects that we are creating for our application.
glGenTextures(number, textureNames);where number is of type GLsizei and specifies the number of names to be generated and textureNames is an array of GLuint where the names are stored. We are using one texture in the tutorial and, in turn, we use the following declarations for the parameters.
GLsizei int number = 1; GLuint textureNames[1];Note that many times we have been using the actual data type in our declarations versus the OpenGL alias. For example, we have been using float versus GLfloat. However, when declaring the array of texture names, we must use the OpenGL type.
We next define the properties that are associated with each named texture object. But before doing so, we set the name of the active object with the following call.
glBindTexture(GL_TEXTURE_2D,
textureNames[0]);
One of the properties that we set is the filter method. The filter establishes how we want OpenGL to handle the texel to pixel mapping when there is more than one texel that can be mapped to the pixel. There are several choices but the simpliest is to use the nearest texel as seen in the following function call.
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST;
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);
A second property pertains to wrapping in each texture dimension. The calls for this are as follows:
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, wrapS);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, wrapT);
where wrapS and wrapT is one of the OpenGL constants
GL_REPEAT GL_CLAMP_TO_BORDER GL_CLAMP_TO_EDGE
The last property we set is the environment mode, which pertains to how the colors of the texture are applied. The function call that sets the property is
glTexEnvf(GL_TEXTURE_ENV,
GL_TEXTURE_ENV_MODE,
envMode);
where for the tutorial envMode is either GL_REPLACE or GL_DECAL. Another mode is GL_MODULATE, which is illustrated in the example Two Sided Materals appearing in chapter 9. The effect of GL_REPLACE is the texels and when applicable the border color are used in place of the surface color. In the case of GL_DECAL, the ALPHA value is applied. The alpha value sets the transparency of the texel (or color in the case of border color). When alpha is 1.0, the texel or color is fully opaque while for the value 0.0, it is fully transparent. For example, suppose the mode is GL_DECAL and we clamp to border when at least one coordinate is outside the range [0.0, 1.0]. In this case the border color is seen if its alpha value is 1.0 but if its alpha value is 0.0, the underlying geometry color shows through.
One more thing that must be done is describe how the image map is stored. The following calls handle this where texels is the array storing the image map and width and height are the dimensions of the image map.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, GL_RGBA,
width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, texels);
If you use the class ImageMap in the library Utils to read and store the image map, then both of the calls are included in the class function setTexture.
Last, when using a border color as illustrated in the tutorial, we set it with the call
glTexParameterfv(GL_TEXTURE_2D,
GL_TEXTURE_BORDER_COLOR,
borderColor);
where borderColor is an array of float holding the RGB color values.
As discussed in other pages, we establish the association of texture coordinates to vertices when defining the geometry. See the code snippet given in the Technical Content window for the code used in the tutorial.