OpenGL on OS X : OpenGL with Cocoa (part 1) - Creating a Cocoa Program

2/27/2013 8:46:14 PM

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!).

Figure 1. The initial CocoaGL project.

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.

Figure 2. Interface Builder—ready to go!

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.

Figure 3. A very basic interface 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.

Figure 4. Subclassing the NSOpenGLView class.

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.

Figure 5. Generating the derived class.

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.

Figure 6. Actually adding the file to the project.

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.

Figure 7. Actually adding the file to the project.

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.

Figure 8. Connecting the custom MyOpenGLView class.

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.

Figure 9. Turn off the One Shot memory attribute.

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.

Figure 10. The OpenGL View Attributes window.

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.

Video tutorials
- How To Install Windows 8 On VMware Workstation 9

- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th