Choose the “OpenGL ES Application” template
for your project.
Do not remove the
MainWindow.xib
file from the project.
In the method
applicationDidFinishLaunching:
in the file
TriangleAppDelegate.m
,
comment out the call to
startAnimation
and insert the following statement.
[UIApplication sharedApplication].statusBarHidden = YES;In the file
EAGLView.m
,
change the macro
USE_DEPTH_BUFFER
from 0 to 1.
Replace the
drawView
method with the following.
#define COORDINATES_PER_VERTEX 3 typedef GLfloat vertex_t[COORDINATES_PER_VERTEX]; #define VERTICES_PER_TRIANGLE 3 typedef vertex_t triangle_t[VERTICES_PER_TRIANGLE]; typedef GLfloat color_t[4]; //red, green, blue, alpha //Convert degrees to radians. #define RADIANS(degrees) ((degrees) * M_PI / 180.0) - (void) drawView { [EAGLContext setCurrentContext: context]; glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLfloat near = .01; //distance from viewer to near surface of frustum GLfloat length = near * tan(RADIANS(45) / 2.0);//45 degree field of view GLfloat ratio = self.bounds.size.height / self.bounds.size.width; glFrustumf( -length, //left length, //right -length * ratio, //bottom length * ratio, //top near, //near 10.0 //far ); glViewport(0, 0, backingWidth, backingHeight); glMatrixMode(GL_MODELVIEW); glClearColor(0.5, 0.5, 0.5, 1.0); //gray background glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); //The three coordinates of a vertex at distance 1 from the origin. #define V(theta) {cos(RADIANS(theta)), sin(RADIANS(theta)), 0.0} const triangle_t triangle = { V(90.0 + 0.0 * 120.0), V(90.0 + 1.0 * 120.0), V(90.0 + 2.0 * 120.0) }; static const color_t color[] = { {1.0, 0.0, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0}, {0.0, 0.0, 1.0, 1.0} }; glTranslatef(0.0, 0.0, -3.0); glVertexPointer(COORDINATES_PER_VERTEX, GL_FLOAT, 0, &triangle); glColorPointer(4, GL_FLOAT, 0, color); glDrawArrays(GL_TRIANGLES, 0, VERTICES_PER_TRIANGLE); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); [context presentRenderbuffer: GL_RENDERBUFFER_OES]; }