Pro OpenGL ES for iOS

(singke) #1

102 CHAPTER 4: Turning On the Lights^


What’s happening here:
„ In line 1, the normal array is allocated with one three-component
normal per vertex. (And while you’re at it, don’t forget to add the
instance variable GLfloat *m_NormalData; to Planet.h.)

„ Lines 2ff and 3ff generate the normal data. It doesn’t look like any
fancy-schmancy normal averaging scheme covered earlier, so what
gives? Since we’re dealing with a very simple symmetrical form of a
sphere, the normals are identical to the vertices without any scaling
v a l u e s ( t o e n s u r e t h e y a r e u n i t v e c t o r s -----that is, of length 1.0). Notice
that the calculations for the vPtr values and the nPtrs are virtually the
same as a result.
„ And as with the other two pointers, nPtr is incremented.

Note You’ll rarely need to actually generate your own normals. If you do any real work in
OpenGL ES, you’ll likely be importing models from third-party applications, such as 3D-Studio
or Strata. They will generate the normal arrays along with the others for you. More will be
covered on this later.

Add the following to the planet’s interface in Planet.h:
GLfloat *m_NormalData;

The final step is to modify the execute() method in Planet.m to look like Listing 4-3.
Listing 4-3. Supporting lighting in the Planet Execute routine


  • (bool)execute
    {
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);


glEnableClientState(GL_NORMAL_ARRAY); //1
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, m_VertexData);
glNormalPointer(GL_FLOAT, 0, m_NormalData); //2

glColorPointer(4, GL_UNSIGNED_BYTE, 0, m_ColorData);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (m_Slices+1)*2*(m_Stacks-1)+2);

return true;
}
It’s not much different than the original, except with the addition of lines 1 and 2 for
sending the normal data to the OpenGL pipeline alongside the color and vertex
information. If you have a very simple model in which many of the vertices all share the
Free download pdf