Material Basics
This
section describes how to define the material properties of the objects in
the scene: the ambient, diffuse, and specular colors, the shininess, and the
color of any emitted light. Most of the material properties are conceptually
similar to ones you've already used to create light sources.
The mechanism for setting them is similar, except that the command used is
called glMaterial*(). void glMaterial{if}[v](GLenum face,
GLenum pname, TYPEparam);
Specifies
a current material property for use in lighting calculations. The face
parameter can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK to indicate which
face of the object the material should be applied to. The particular
material property being set is identified by pname and the desired values
for that property are given by param, which is either a pointer to a group
of values (if the vector version is used) or the actual value (if the
nonvector version is used). The nonvector version works only for setting
GL_SHININESS. The possible values for pname are shown below. Note that
GL_AMBIENT_AND_DIFFUSE allows you to set both the ambient and diffuse
material colors simultaneously to the same RGBA value.
Default Values for pname Parameter of glMaterial*() |
||
Parameter Name |
Default Value |
Meaning |
GL_AMBIENT |
(0.2, 0.2, 0.2, 1.0) |
ambient color of material |
GL_DIFFUSE |
(0.8, 0.8, 0.8, 1.0) |
diffuse color of material |
GL_AMBIENT_AND_DIFFUSE |
ambient and diffuse color of material |
|
GL_SPECULAR |
(0.0, 0.0, 0.0, 1.0) |
specular color of material |
GL_SHININESS |
0.0 |
specular exponent |
GL_EMISSION |
(0.0, 0.0, 0.0, 1.0) |
emissive color of material |
GL_COLOR_INDEXES |
(0,1,1) |
ambient, diffuse, and specular color indices |
As discussed in "Selecting a Lighting Model," you can choose to have
lighting calculations performed differently for the front- and back-facing
polygons of objects. If the back faces might indeed be seen, you can supply
different material properties for the front and the back surfaces by using
the face parameter of glMaterial*().
Note that most of the material properties set with glMaterial*() are
(R, G, B, A) colors. Regardless of what alpha values are supplied for other
parameters, the alpha value at any particular vertex is the diffuse-material
alpha value (that is, the alpha value given to GL_DIFFUSE with the
glMaterial*() command, as described in the next section. (See "Blending"
for a complete discussion of alpha values.) Also, none of the RGBA material
properties apply in color-index mode; see "Lighting in Color-Index Mode" for
more information about what parameters are relevant in color-index mode.
Diffuse
and Ambient Reflection
The GL_DIFFUSE and GL_AMBIENT parameters set with glMaterial*()
affect the color of the diffuse and ambient light reflected by an object.
Diffuse reflectance plays the most important role in determining what you
perceive the color of an object to be. It's affected by the color of the
incident diffuse light and the angle of the incident light relative to the
normal direction. (It's most intense where the incident light falls
perpendicular to the surface.) The position of the viewpoint doesn't affect
diffuse reflectance at all.
Ambient
reflectance affects the overall color of the object. Because diffuse
reflectance is brightest where an object is directly illuminated, ambient
reflectance is most noticeable where an object receives no direct
illumination. An object's total ambient reflectance is affected by the
global ambient light and ambient light from individual light sources. Like
diffuse reflectance, ambient reflectance isn't affected by the position of
the viewpoint.
For real-world objects, diffuse and ambient reflectance are normally the
same color. For this reason, OpenGL provides you with a convenient way of
assigning the same value to both simultaneously with glMaterial*():
GLfloat
mat_amb_diff[] = { 0.1, 0.5, 0.8, 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
mat_amb_diff);
In this
example, the RGBA color (0.1, 0.5, 0.8, 1.0) - a deep blue color -
represents the current ambient and diffuse reflectance for both the front-
and back-facing polygons.
Specular
Reflection
Specular
reflection from an object produces highlights. Unlike ambient and diffuse
reflection, the amount of specular reflection seen by a viewer does depend
on the location of the viewpoint - it's brightest along the direct angle of
reflection. To see this, imagine looking at a metallic ball outdoors in the
sunlight. As you move your head, the highlight created by the sunlight moves
with you to some extent. However, if you move your head too much, you lose
the highlight entirely.
OpenGL
allows you to set the RGBA color of a specular highlight (with GL_SPECULAR)
and to control the size and brightness of the highlight (with GL_SHININESS).
You can assign a number in the range of [0.0, 128.0] to GL_SHININESS - the
higher the value, the smaller and brighter (more focused) the highlight.
Emission
By
specifying an RGBA color for GL_EMISSION, you can make an object appear to
be giving off light of that color. Since most real-world objects (except
lights) don't emit light, you'll probably use this feature mostly to
simulate lamps and other light sources in a scene. In Figure J-21 , the
spheres in the fourth column have a greenish value for GL_EMISSION:
GLfloat
mat_emission[] = {0.3, 0.2, 0.2, 0.0};
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
Notice
that the spheres appear to be slightly glowing; however, they're not
actually acting as light sources. You would need to create a light source
and position it at the same location as the sphere to create that effect.