OpenGL: Shading Models

Shading Models

In this C++ OpenGL video, we explain the difference between the two shading models available in OpenGL: GL_FLAT and GL_SMOOTH. The difference between these models is most often seen with lighting effects. However, we gave an example of coloring the vertices of triangle red, green, and blue to illustrate that vertices may be colored differently without the use of lighting. This example more clearly demonstrates the effects of smooth shading.

Smooth shading is accomplished via linear interpolation and the method is known as Gouraud shading. The shading model is set via a call to the function glShadeModel() with the appropriate argument. If no shade model is set, smooth shading is used by default. In this lesson, we drew tha same objects with both flat and smooth shading. Below, we have the two spheres that we rendered. Notice that the smoothly shaded sphere looks more spherical than the flatly-shaded one.

From the outside, we can look at the scene as we have rendered it. The spheres are centered on the plane z = –2 and the triangles are in the plane z = –.2. The small red sphere shows that placement of the viewer at the origin. The small white sphere shows the position of the light source at (0.0, 1.0, –.5). The bounding box is in gray and runs from –1 to 1 in the x and y directions and –3 to 3 in the z-direction. The viewer looks toward the negative z-axis.

To render our spheres, we use the glutSolidSphere() function. This function takes three parameters: the radius, the slice count, and the stack count. The slice count determines polygonal boundaries along lines of longitude and the stack count determines boundaries along lines of latitude. As we increase the number of slices and stacks, our spheres look more and more spherical.

In flat shading mode, polygons are colored the color of a single vertex. This vertex is that last vertex that is specified for all geometric primitives, except GL_POLYGON, which uses the first vertex. In our example code, the triangle that rendered was colored blue, the color of the last specified vertex.