Pro OpenGL ES for iOS

(singke) #1

110 CHAPTER 4: Turning On the Lights^


The default value is a dim gray formed by a color with red=.2, green=.2, and blue=.2.
This helps ensure that your objects are always visible no matter what. And while we’re at
it, there is one other value for glLightModelfv(), and that is defined by the parameter of
GL_LIGHT_MODEL_TWO_SIDE. The parameter is actually a Boolean float. If it is 0.0, only one
side will be illuminated; otherwise, both will. The default is 0.0. And if for any reason you
wanted to change which faces were front ones, you may use glFrontFace() and specify
the triangles ordered clockwise or counterclockwise represent the front face. CCW is the
default.

Taking a Step Back


So, what is actually happening here? Quite a lot, actually. There are three general
shading models in use for real-time computer graphics. OpenGL ES 1 uses two of
those, both of which we’ve seen. The first, the flat model, simply shades each triangle
with one constant value. You’ve seen that that looks like in Figure 4-5. And in the good
ol’ days, this was a valid option, considering it was much faster than any others.
However, when the iPhone in your pocket is roughly the equivalent of a handheld Cray-1
(minus about 3 tons and liquid cooling), those kinds of speed tricks are really a thing of
the past. The smooth model uses interpolative shading, calculating the colors at each
vertex and then interpolating them across the faces. The actual kind of shading OpenGL
uses is a special form of this called Gouraud shading. This is why the vertex normals are
generated based on normals of all the adjacent faces.
The third kind of shading is called Phong and is not used in OpenGL because of high
CPU overhead. Instead of interpolating color values across the face, it interpolates
normals, generating a normal for each fragment (that is, pixel). This helps remove some
of the artifacting along edges defined by high curvatures, which produce very sharp
angles. Phong can diminish that effect, but so can using more triangles to define your
object.
There are numerous other models. Jim Blinn of the JPL-Voyager animations in the 1970s
created a modified form of Phong shading, now called the Blinn-Phong model. If the
light source and viewer can be treated as if they are at infinity, it can be less
computationally intensive.
The Minnaert model tends to add a little more contrast to diffuse materials. Oren-Nayer
adds a ‘‘roughness’’ component to the diffuse model in order to match reality just a little
bit better.

Emissive Materials


Still another significant parameter we need to cover here that factors into the final color
is GL_EMISSION. Unlike the diffuse, ambient, and specular bits, GL_EMISSION is for
materials only and specifies that a material is emissive in quality. An emissive object has
its own internal light source such as the sun, which will come in handy in the solar-
system model. To see this in action, add the following line to the other material code in
initLighting() and remove the ambient material:
Free download pdf