Many programming languages
are available to developers on Mac OS X. One very popular language on
the Mac (but not quite so popular elsewhere) is Objective-C. To the
uninitiated, Objective-C may appear as a strange blend of C and C++ with
some completely new syntax thrown in. But Objective-C is also the
foundation of a very popular application development technology called
Cocoa.
Cocoa is best described as both a collection of
application framework classes and a visual programming paradigm.
Developers do quite a bit of work in Interface Builder, designing user
interfaces, assigning properties, and even making connections between
events. Objective-C classes are subclassed from controls or are created
from scratch to add application functionality. Fortunately, OpenGL is a
first-class citizen in this development environment.
Creating a Cocoa Program
A Cocoa-based program can be created using the New Project Assistant in Xcode. When we created our first GLUT-based OpenGL program using Xcode. This
time, however, we do not replace the generated project with GLUT-based
code. Figure 1 shows our newly created CocoaGL project after we added the OpenGL framework (do not add the GLUT framework this time either!).
Adding an OpenGL View
Cocoa applications store resources and GUI layouts in
a XIB file (a compiled version of the old NIB, which for historic
reasons stands for NEXTSTEP Interface Builder). Double-click the
MainMenu.xib file under the Resources folder. This starts Interface
Builder and opens the main XIB for editing. You should see a screen
similar to that shown in Figure 2, with the main window already open.
In the library palette, use the tabs at the top to select Classes and then scroll down until you see NSOpenGLView. Click and drag an NSOpenGLView to the main window and resize it to fill most of the main window. You can also resize the main window to taste. You can see in Figure 3 that we now have an NSOpenGLView ready to go in the center of the window.
Creating a Custom OpenGL Class
The next task is to create a custom class derived from NSOpenGLView and associate it with the OpenGL view in the window. Click the Classes tab in the library window, scroll down to the NSOpenGLView entry, and right-click it as shown in Figure 4. Then select New Subclass.
In the pop-up window that is presented, name your
subclass. In this case, we can go with the default MyOpenGLView. It is
very important that you check the Generate Source Files box, as shown in
Figure 5.
The next pop-up asks for the name of the file to put your derived class in. This is shown in Figure 6. Make sure you check the Create ‘.h’ File box, if it isn’t already.
Finally, Interface Builder asks whether you want to add this class to your project as shown in Figure 7.
Check the box next to the project name and then click the Add button.
Now that you have a real Cocoa OpenGL view class, it’s time to start
putting things together.
Wiring It Together
There are still two more things we need to do in
Interface Builder before we can start writing code. The first is we need
to set our NSOpenGLView window to be connected to our custom MyOpenGLView class. Select the NSOpenGLView window in Interface Builder and from the Tools menu, select Inspector. The Inspector window is shown in Figure 8 with the Identity tab selected. In the class combo box, change NSOpenGLView to MyOpenGLView.
The second is to change the parent window so that it
does not use One Shot memory. This flag is on by default, and it tells
the parent window that it is okay to delete the subwindow objects when
it is minimized to the dock or hidden. With an OpenGL window, this would
have the unfortunate side effect of breaking the link between the view
and the OpenGL context, which would prevent further rendering operations
from being displayed. Figure 9 shows the One Shot box unchecked in the Attributes tab. Click the caption of the main window to get to it.
Setting OpenGL View Properties
Interface Builder also gives you access to all the
framebuffers’ properties in an NSOpenGLView control. Click the control
itself and then select the Attributes tab of the inspector window. You
see a myriad of options shown in Figure 10.
Here you can select things
like the bit depth and format of the color, depth, and stencil buffer.
You can also configure an accumulation buffer, but this feature is
deprecated in the core profile, and its use is discouraged . You can also select a
multisampled color buffer, stereo (left and right buffers), and even
force a fallback software renderer instead of using OpenGL hardware.