OpenGL Application In iPhone

In this application we will see how to create texture from an image. We are using here OpenGL ES to create a texture from the image data. So let see how it will be worked.

Step 1: Create a Window base application using template. Give the application name “OpenGL”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add here UIView class to the project.Choose New file -> Select cocoa touch classes group and then select UIView subclass. Give the name “OpenGLView”.

Step 4: We need to add the resource file called as “Sprite-2.png” into the resource folder. Select resources and add files existing sources and select the Sprite-2.png.

Step 5: In the OpenGLAppDelegate.h file, we have added OpenGLView class. So make the following changes.




Step 6: Open the OpenGLAppDelegate.m file and make the following changes in the file:




Step 7: We need to add  two framework in the application, one OpenGLES.framework and another QuartzCore.framework. Framework path is:
System->developer->platform->iPhone Simulator->developer->SDKs-> iPhoneSimulator 2.2 sdk->System->Library->Frameworks.

Step 8: Open the OpenGLView.h file. and define backing height, width,  renderbuffer and framebuffers used to render to the view. So make the following changes in the file:




Step 9: Double click your MainWindow.xib file and open it to the Interface Builder. Open the window,  drag view from the library and place it to the window. Select the View from the main window and bring up Identity Inspector, change the class name into the OpenGLView. Select OpenGLAppDelegate icon from the main window, bring up connection inspector and drag from the glView to the OpenGLView. Now save it, close it and go back to the Xcode.

Step 10: Make the following changes in the OpenGlView.m file.




Step 11: Now compile and run the application in the Simulator.

You can downloaded SourceCode from here OpenGL 2

Leave a Comment:

davide.k says March 31, 2010

hi, thanks to publish a tutorial like this one, but it doesn’t work… I just started dubbing it but I found too many errors, beginning from a lot of missing imports, spriteVertices never initialized, no Sprite-2.png added to the article, … I hope you will correct (or update it) in the near future because a simple code with a a great output like yours is a plus. With best regards, davide

Aibin says April 23, 2010

First of all thanks for the tutorial, but unfortunately the decoding of the sprite texture is failing in the sample code given above.

Aibin says April 23, 2010

Problem solved :

Add this line after the getting width.

// Get the width and height of the image
width = CGImageGetWidth(spriteImage);
height= CGImageGetHeight(spriteImage); –> This was missing….

Sushant says April 23, 2010

I have added the code into code.. Please check it..

MAKSumon says December 11, 2010

The program crashes when it is built…

Sean says December 15, 2010

I have downloaded the code for this tutorial and built it under Xcode 3.2.4 with iOS 4.1 and I can’t get it to work. I did have to add the following lines to OpenGLView.m in setupView:


and later inside the if block:

spriteData = (GLubyte *)calloc(width*height*4, sizeof(GLubyte));

Have you tried running this project under the latest iOS?

Yali Zhu says March 10, 2011

Sprite.png does not show, only a white square rotating. Why, anyone also gets the problem?

joker2206 says April 6, 2011

Thx, still not checked on this one. Just wanted to say this is one of the best iphone programming tutorial sites. Thx for that.

    nate says January 11, 2013

    That is because you need to place
    spriteData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));

    above the CGBitmapContextCreate

Add Your Reply