A finished Xcode project with the code from this post can be found on github.
GLKit is a framework built on top of OpenGL ES. Using it can save time because it reduces the amount of boilerplate code you have to write.
For this example, create a new Empty Application iOS project in Xcode. I'm using automatic reference counting -- you may have to change the code if you want to use manual memory management. I'm also using Xcode 4.4.
Add QuartzCore, OpenGLES and GLKit frameworks to your project (project settings, Build Phases, Link Binary With Libraries):
Now add a new Storyboard file to your project and adjust your project settings to make it your main storyboard. Storyboards allow you to create multiple scenes and specify how they are related. For this example we'll just use a single scene:
With the Storyboard open, check out the Object Library (in the Utilities drawer). You should see a GLKit View Controller. Drag this on to your Storyboard:
If you have the Navigator drawer open you should see the GLKit View Controller. Make sure it's selected:
Back in the Utilities drawer, under the Identity inspector, you should see a Custom Class section where GLKViewController is specified. This means that GLKViewController is the backing controller class for the view:
We'll need to change this to our own custom subclass. Add a new Objectve-C class to your project named MyViewController. Make it a subclass of GLKViewController. Make sure to import GLKit.h in your header file:
#import <GLKit/GLKit.h>
@interface MyViewController : GLKViewController
@end
While you're here you can click on the Connections inspector. Notice how your view controller's outlets have been configured to reference a GLKit View:
Now click on the GLKit View in your Scene:
Check out its Identity inspector. Notice how the custom class is GLKView:
When we added the GLKit View Controller to the scene it was automatically set up to manage a GLKView instance. This GLKView instance, in turn, will manage your framebuffer for you.
OK, let's get back to the code. Open up MyViewController.m. Let's add a property for an EAGLContext:
#import "MyViewController.h"
@interface MyViewController ()
@property (strong) EAGLContext *glContext;
@end
@implementation MyViewController
@synthesize glContext = _glContext;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
}
self.glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
Here we've specified the use of OpenGL ES version 2. Let's check to make sure the context got created properly:
if (!self.glContext) {
NSLog(@"Unable to create OpenGL context");
exit(1);
}
[EAGLContext setCurrentContext:self.glContext];
Before leaving the viewDidLoad method we'll tell the GLKView instance (being managed by our view controller) about the context:
GLKView *view = (GLKView *)self.view;
view.context = self.glContext;
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(150.0/255.0, 200.0/255.0, 255.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
The last thing we need to do before running this application is to go into AppDelegate.m and modify our application:didFinishLaunchingWithOptions method to simply return YES so our Storyboard gets used:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Thanks for reading. Feel free to post comments, questions or suggestions.