Pro OpenGL ES for iOS

(singke) #1

58 CHAPTER 3: Building a 3D World^


glVertexPointer(2, GL_FLOAT, 0, squareVertices); //13
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); //14

if(!(counter%100)); //15
NSLog(@"FPS: %d\n",self.framesPerSecond);

counter++;
}

@end
You’ll recognize a number of similar elements here from Chapter 1, but in a more
compact form. All OpenGL ES 2 code has been removed for clarity.
„ Line 1 defines our viewcontroller as a subclass of GLKViewController
„ In line 2, the API is initialized, with OpenGL ES 1 as the chosen
approach, by passing it the kEAGLRenderingAPIOpenGLES1 flag. The
context is returned and fed to the GLKView.
„ Lines 3ff bind the context that was fetched in line 2. Then the current
context is set. Without that being set, OpenGL will fail on many calls
coming up.
„ The drawInRect() method in line 4 is a delegate call from
GLKViewController. All of the geometry and attributes are specified
here for clarity.
„ Line 5 creates an array of vertices. Because this a 2D demo without
lighting, only two values are needed for each vertex. Line 6 is our color
array, using the standard RGBA format. One color for each of the four
vertices.
„ Lines 7f are identical to the first exercise, filling in the background with
a medium gray.
„ Line 8 sets the current matrix to be a projection matrix, and line 9
initializes it with an ‘‘identity matrix.’’
„ Lines 10 and 11 do the same for the modelview.
„ Instead of generating a matrix of our own and modifying it directly, as
in Chapter 1, ES 1 handles those kind of housekeeping chores for us.
So here in line 12, glTranslatef() moves the square along only the Z-
axis (hence the middle value) using a sin function. Using a sin gives it a
nice smooth motion that slows up at either end.
Free download pdf