OpenGL ES for iPhone: Drawing a Circle - Part I

Posted by Jan Vantomme on 16 May 2009.
Tags: , , ,

Moving from Processing to iPhone development is hard. The OpenGL implementation is a little different and there is little documentation available. I’ve learned a lot by reading Beginning iPhone Development by Dave Mark and Jeff Lamarche and the iPhone Development blog. In these articles I want to share some of the things I’ve learned. Read this article as an introduction to Generative Design on the iPhone. So let’s do some OpenGL programming.

Step 1: Seting up the Project

Create a new project using the OpenGL ES Application template and save your project with the name openglcircle. Make sure you use the “UIStatusBarHidden trick”: to hide the status bar and get a true fullscreen application.

Step 2: Some Handy Functions

Add a file with the name Constants.h to your project. This file is going to hold some handy functions that are used a lot in generative design. One to convert degrees to radians and one to generate a random float between two numbers. Add the following lines of code to this file.[1]

#define DEGREES_TO_RADIANS(x) (3.14159265358979323846 * x / 180.0)
#define RANDOM_FLOAT_BETWEEN(x, y) (((float) rand() / RAND_MAX) * (y - x) + x) 

Step 3: Slowing Down the Application

Open openglcircleAppDelegate.m and change the animationInterval to 1.0 / 2.0 so the view will only update twice a second. You can change this back later when the project is finished. But you’ll need a slow speed now to see what happens on the screen.

Step 4: Editing the View

Open EAGLView.m and add an import statement for the Constants.h file. Next up is deleting the code in the drawView method to do your own custom drawing. Leave these lines of code in there so you won’t have to type them all over again.

- (void)drawView {
    [EAGLContext setCurrentContext
glOrthof(-1.0f1.0f, -1.5f1.5f, -1.0f1.0f);
    // custom drawing will be done right here ...
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

Step 5: Clear the background

In processing you would use background(0) to clear the background with black. In OpenGL ES you will need to use glClearColor() in combination with glClear(). glClearColor() sets the color that will be used to clear the background. This function takes 4 color components (red, green, blue and alpha) as arguments. Values should be between 0.0 and 1.0. glClear() will clear the display and takes GL_COLOR_BUFFER_BIT as the argument. Add these two lines right after the call to the glOrthof() function. Add these lines to the project:


Step 6: Drawing the circle

To draw a nice looking circle you’ll need to define 360 points on the perimeter of the circle. For each of those points you’ll need an x and y value so you’ll will need to calculate 720 values. To do this you’ll need an empty GLfloat array with a length of 720. See the line of code below.

GLfloat vertices[720]

Next up is filling the array with the right x and y values for the points on the perimeter of the “unit circle”:

for (int i 0720+= 2{
// x value
vertices[i]   = (cos(DEGREES_TO_RADIANS(i)) * 1);
// y value
vertices[i+1] = (sin(DEGREES_TO_RADIANS(i)) * 1);

To draw the circle you need to tell OpenGL what array of vertices to use. This can be done with the glVertexPointer() method. You’ll also have to use the glEnableClientState() method to enable the use of vertex arrays.


Before you can draw the circle, OpenGL needs to know what color to use. In Processing you would use the fill() function. In OpenGL you can set this color with the glColor4f() method. This method takes 4 color parameters just like glClearColor(). The line below will set the fill color to red.


And finally you can draw the circle with the glDrawArrays() method.


If you run the application in the iPhone Simulator and everything goes ok, your application will look like this.

Screenshot of the finished OpenGL ES application. A red circle is showing in the iPhone Simulator.


You can download the project files for this tutorial right here: (XCode project with sample code to draw a circle in OpenGL ES.). In part two of this article I’ll show you how to draw a better circle by changing some little things.

Tweet this article

Oldskool Comments (2)

Gravatar for haqu

From: haqu
Date: 19.05.2009

hey Jan, good tutorial. when you fill up vertices you should use (i/2) to calculate cos and sin. this will keep angle in range 0-360. pre. for (int i = 0; i < 720; i += 2) { vertices = (cos(DEGREES_TO_RADIANS(i/2)) * 1); vertices[i+1] = (sin(DEGREES_TO_RADIANS(i/2)) * 1); }

Top · Permanent link to this comment

Gravatar for Jan Vantomme

From: Jan Vantomme
Date: 19.05.2009

Thanks for noticing the little bug. I’ll keep it in mind when I’m posting the second part of the tutorial later this evening. I took the freedom to reformat your code example to fit with the rest of the blog.

Top · Permanent link to this comment