CHAPTER 4: Turning On the Lights (^119)
When calculated, these values act individually on each of the R, G, and B components
of the colors in question.
What’s This All For?
One reason why it is handy to understand what’s going on beneath the hood is that it
helps make OpenGL and related tools less mysterious. Just like when you learn a
foreign language, say Klingon (and if you, dear reader, are Klingon, majQa’ nuqDaq ‘oH
puchpa’ ‘e’!), it ceases to be the mystery that it once was; where growls were growls
and snarls were snarls, now you might recognize it as a lovely poem about fine tea.
And another reason is, as mentioned early on, all of these nice ‘‘high-level’’ tools are
absent in OpenGL ES 2.0. Most any of the earlier shading algorithms will have to be
implemented by you in little bits o’ code that are called shaders. Fortunately, information
on the most common shaders is available on the Internet and, replicating the previous
information, relatively straightforward.
More Fun Stuff
Now, armed with all of this photonic goodness, it’s time to get back to coding and
introduce more than one light. Secondary lights can make a surprisingly large difference
in the authenticity of the scene for little effort.
Go back to initLighting() and make it look like Listing 4-5. Here we add two more
lights, named SS_FILLLIGHT1 and SS_FILLLIGHT2, respectively. Add their definitions to
the header file:
#define SS_FILLLIGHT1 GL_LIGHT1
#define SS_FILLLIGHT2 GL_LIGHT2
Now compile and run. Do you see Figure 4-16 (left)? Here is where the Gouraud shading
model breaks down, as mentioned earlier, exposing the edges of the triangles. And what
is the solution? At this point, simply increase the number of slices and stacks from 20 to
50 each, and you’ll get the much more pleasing image, shown in Figure 4-16 (right).
Listing 4-5. Adding Two Fill Lights
-(void)initLighting
{
GLfloat posMain[]={5.0,4.0,6.0,1.0};
GLfloat posFill1[]={-15.0,15.0,0.0,1.0};
GLfloat posFill2[]={-10.0,-4.0,1.0,1.0};
GLfloat white[]={1.0,1.0,1.0,1.0};
GLfloat red[]={1.0,0.0,0.0,1.0};
GLfloat dimred[]={.5,0.0,0.0,1.0};