Pro OpenGL ES for iOS

(singke) #1

CHAPTER 3: Building a 3D World (^65)
glEnable(GL_CULL_FACE); //3
glCullFace(GL_BACK);
// glMatrixMode(GL_PROJECTION); //4
// glLoadIdentity();
glMatrixMode(GL_MODELVIEW); //5
glLoadIdentity();
//glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), 0.0);
glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), z); //6
transY += 0.075f;
//glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices); //7
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeColors); //8
glEnableClientState(GL_COLOR_ARRAY);
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawElements( GL_TRIANGLE_FAN, 6 3, GL_UNSIGNED_BYTE, tfan1); //9
glDrawElements( GL_TRIANGLE_FAN, 6
3, GL_UNSIGNED_BYTE, tfan2);
if(!(counter%100))
NSLog(@"FPS: %d\n",self.framesPerSecond);
counter++;
}
The following changes have been made:
„ A z value has been added in at line 1. For the time being, this will be
static because the animation remains only up and down. A negative
value means that the object has moved away from us, as if it has
moved deeper into the screen.
„ Clear the background to the medium gray once again in line 2.
„ Because we never want to process or draw anything that is not
absolutely necessary, it is possible to just eliminate a bunch of the
triangles that are ‘‘facing away’’ from the viewer, as on line 3. Face
culling is used to remove the otherwise invisible faces. For our cube,
the only triangles we need to see are the ones actually facing us, and
that is determined by the winding, or the order, of the vertices for the
face. If the winding is counterclockwise, it is facing us and should be
visible; otherwise, it is culled. This may sound like a substitute for face
normals, and, yes, it is when it comes to backface elimination, or
culling. But normals are still needed to determine illumination striking
a face.

Free download pdf