OpenGL Programming with Mac OS X Cocoa


Building a Simple OpenGL View Class with MFC Framework

1. Start Xcode

2. Create a New Project and Add the Necessary Frameworks

The following show you how to create a Cocoa program that uses the built-in NSOpenGLView Class for the OpenGL rendering. You will need to perform the following steps:

3. Subclass NSOpenGLView

NSOpenGLView is a component that knows how to get an OpenGL rendering context. It has a nice GUI that you can use to specify a pixel format to use when setting up the rendering context. To get it to do what we want we will subclass it and implement the desired behaviours:


Notice that we are mixing C++ code with Objective-C. This is permissable, but rarely a good idea. We will not learn Objective-C here, but it will reappear later in the semester, so you may want to find a tutorial and see if you can port the C++ code to Objective-C.

4. Add the MyGLView to Your Window and Configure It

Now that you have a class that can draw an OpenGL scene, you can attach it to an OpenGL View in your application window. To do this you will use a separate application, Interface Builder, Apple's GUI design tool. You will see this tool again later in the labs.

5. Build and Run

That's it. You can build and run your Cocoa based OpenGL program from XCode now. Enjoy!

If all you get is a plain white window, make sure that you call the functions suggested by the comments in the initWithCoder, reshape, and drawRect functions.