Home > Articles > Programming > Graphic Programming

Texture Mapping in OpenGL: Beyond the Basics

  • Print
  • + Share This
Texture mapping is perhaps one of the most exciting features of OpenGL and is heavily relied on in the games and simulation industry. In this chapter, you'll learn some of the finer points of texture mapping in OpenGL.
This chapter is from the book
  • Richard S. Wright Jr.


How To

Functions You'll Use

Add specular highlights to textured objects


Use anisotropic texture filtering


Load and use compressed textures


Use points as textured quads


Texture mapping is perhaps one of the most exciting features of OpenGL (well, close behind shaders anyway!) and is heavily relied on in the games and simulation industry. In Chapter 8, "Texture Mapping: The Basics," you learned the basics of loading and applying texture maps to geometry. In this chapter, we'll expand on this knowledge and cover some of the finer points of texture mapping in OpenGL.

Secondary Color

Applying texture to geometry, in regard to how lighting works, causes a hidden and often undesirable side effect. In general, you set the texture environment to GL_MODULATE, causing lit geometry to be combined with the texture map in such a way that the textured geometry also appears lit. Normally, OpenGL performs lighting calculations and calculates the color of individual fragments according to the standard light model. These fragment colors are then multiplied by the filtered texel colors being applied to the geometry. However, this process has the side effect of suppressing the visibility of specular highlights on the geometry. Basically, any texture color multiplied by ones (the white spot) is the same texture color. You cannot, by multiplication of any number less than or equal to one, make a color brighter than it already is!

For example, Figure 9.1 shows the original lit SPHEREWORLD sample from Chapter 5, "Color, Materials, and Lighting: The Basics." In this figure, you can see clearly the specular highlights reflecting off the surface of the torus. In contrast, Figure 9.2 shows the SPHEREWORLD sample from Chapter 8. In this figure, you can see the effects of having the texture applied after the lighting has been added.

Figure 9.1

Figure 9.1 The original SPHEREWORLD torus with specular highlights.

Figure 9.2

Figure 9.2 The textured torus with muted highlights.

The solution to this problem is to apply (by adding instead of multiplication) the specular highlights after texturing. This technique, called the secondary specular color, can be manually applied or automatically calculated by the lighting model. Usually, you do this using the normal OpenGL lighting model and simply turn it on using glLightModeli, as shown here:


You can switch back to the normal lighting model by specifying GL_SINGLE_COLOR for the light model parameter:


Figure 9.3 shows the output from this chapter's version of SPHEREWORLD with the restored specular highlights on the torus. We do not provide a listing for this sample because it simply contains the addition of the preceding single line of code.

Figure 9.3

Figure 9.3 Highlights restored to the textured torus.

You can also directly specify a secondary color after texturing when you are not using lighting (lighting is disabled) using the glSecondaryColor function. This function comes in many variations just as glColor does and is fully documented in the reference section. You should also note that if you specify a secondary color, you must also explicitly enable the use of the secondary color by enabling the GL_COLOR_SUM flag:


Manually setting the secondary color only works when lighting is disabled.

  • + Share This
  • 🔖 Save To Your Account