Pro OpenGL ES for iOS

(singke) #1

66 CHAPTER 3: Building a 3D World^


Note “Windingness” is based on the direction the vertices describe. The first triangle formed
by vertices 1, 0, and 3 is facing us because the vertices are ordered counterclockwise. All
triangles in the first fan are counterclockwise.

„ In line 4, the projection matrix initialization is turned off, leaving only
the modelview matrix to be modified. Projection will be handled below.
„ In line 5, only the modelview is set as the current matrix, in case
someone else changed it elsewhere.
„ In line 6, the final coordinate of the original glTranslatef() call,
which had been fixed at 0 because it wasn’t needed, is now changed
to z (-2.0).
„ The original squareVertices pointer is replaced by cubeVertices in
line 7.
„ Line 8 replaces the squareColors with cubeColors.
„ The original glDrawArrays() is removed to be replaced by two calls to
glDrawElements() on line 9, one for each of the triangle fans. Of note,
the second argument is for the number of elements in the tFan
connectivity arrays: six vertices times three elements on each vertex.
OpenGL will take the connectivity arrays, tfan1 and tfan2; look up
each vertex in the vertex pointer array (line 7); and render the object.
You should be able to get a compile at this point, but you won’t see anything because
the viewing frustum has yet to be defined. The default far clip plane is -1.0, meaning that
anything further than that will be culled, that is, not be visible. If you substitute -1.5 for z,
in place of -2.0, the ‘‘cube’’ is moved closer and should be partially visible. Although it
looks like the whole thing, it is actually just the closest face poking through. Change the
z to -1.500001, and it vanishes. It’s not going to look like much of a cube right now,
because only a portion of it will be poking through. Also, because the frustum is not
defined, the square viewport is stretched out when being adapted to fill the window.
(The coordinate -1.5 is where the origin of the cube needs to be to ensure the closest
face is at -1.0.) Now move it back to -2.0.
Free download pdf