OpenGL

The Open Graphics Library (OpenGL) is a framework for rendering 2D and 3D graphics. OpenGL usually interacts directly with the GPU, achieving hardware-accelerated rendering. OpenGL is a cross-platform, multi-language API and it is widely used in visualization software and video games.

OpenGL ES is the version of the library that runs on embedded devices such as the iPhone and iPad. It is a subset of desktop implementations of OpenGL. There are two versions of OpenGL ES available for use on iOS. Version 1.1 uses a fixed pipeline, meaning that you use a set of built-in functions to build your scene. Version 2.0 uses a programmable pipeline, meaning that there are no built-in functions and everything must be built from scratch. As you’ve probably guessed, 2.0 is a lot more complicated but allows for more control than 1.1. For this lesson, we’ll be using version 1.1 as we’re just getting our hands dirty with OpenGL.

The OpenGL API for iOS is written in pure C, much like the Core Graphics framework, and integrates seamlessly with Objective-C classes. You need to link against three additional frameworks to use OpenGL code in your apps: Quartz Core, OpenGL ES, and GLKit.

GL View

GLKView is a UIView subclass used to render OpenGL graphics.

The GLKView needs to be attached to an EAGLContext. This is a drawing context, which manages all of the data iOS needs to draw with OpenGL.

The GLKView must also be connected to a GLKViewDelegate, which will be polled periodically to draw content in the view. This is done by implementig the glkView:drawInRect: method.

We can try pulsing the red background by using a couple of variables to set the color.

You’ll notice that this does not work as expected, and that is because glkView:drawInRect: is not called regularly. Similarly to drawInRect:, this method is only called when the view is first shown, when it is resized, etc.

When using OpenGL, you will often need to redraw the view at every frame. We need to replace the default redraw behaviour by another that redraws in sync with the screen refresh rate. This can be achieved using a CADisplayLink timer.

GL View Controller

We just saw how to build and render to a GL view from scratch. GLKit offers a much easier solution for doing this using a GLKViewController. This is a UIViewController subclass that also implements an OpenGL ES rendering loop. The GLKViewController will create the GLKView and set itself as its delegate automatically, but you still need to set the EAGLContext manually.

Header Source

At this point, it’s a good idea to cleanly initialize and destroy the context.

GLKViewController also provides us with an update method, which is called every frame like the draw method, but intended for app logic.

GLKViewController allows us to set the desired frame rate using the preferredFramesPerSecond property. Note that the actual frame rate may be lower than this, depending on how long it takes to render a frame. The actual frame rate can be read using the framesPerSecond property.

Drawing Shapes

Rendering geometry in OpenGL is done using vertices (points). Vertices can have multiple properties such as position, colour, texture coordinates, etc. These vertices can be passed to the buffer using many different methods. We will use one of the simplest called vertex arrays.

Note that the origin (0, 0) is set to the center of the view by default.

OpenGL ES can only render triangles, so you will often need to combine multiple triangles to build the desired shape.

As we are only working in 2D, we can get rid of the z-coordinate.

We can set the shape color using glColor4f(). This will change the color of anything that is drawn until the next call to glColor4f().

We can also set the color on a vertex-by-vertex basis by passing a color array along with the vertex array.

We can optimize our data by removing duplicate points. We can then use an index array to indicate the order in which the vertices should be drawn.


  1. Beginning OpenGL ES 2.0 with GLKit Part 1. Ray Wenderlick, Oct 19 2011.
  2. OpenGL ES Programming Guide for iOS. Apple, Feb 24 2011.

Comments are closed.