Mac OS X provides three interfaces for drawing OpenGL content onscreen: the NSOpenGL classes, AGL, and CGL. (See “Programming Interfaces” for more information). You use the NSOpenGL classes from within the Cocoa application framework, while AGL is the interface that supports drawing OpenGL content to a Carbon application. CGL can be used from either a Cocoa or Carbon application. For drawing to a view or a window, you'll either use the NSOpenGL classes (for a Cocoa view) or AGL (for a Carbon window), because CGL supports drawing only to the full screen.
Regardless of the application framework, to draw OpenGL content to a window or view, you need to perform these tasks:
Set up the renderer and buffer attributes that support the OpenGL drawing you want to perform.
Each of the OpenGL APIs in Mac OS X has its own set of constants that represent renderer and buffer attributes. For example, the all-renderers attribute is represented by the NSOpenGLPFAAllRenderers constant in Cocoa and the AGL_ALL_RENDERERS constant in the AGL API.
Request, from the operating system, a pixel format object that encapsulates pixel storage information and the renderer and buffer attributes required by your application. The returned pixel format object contains all possible combinations of renderers and displays available on the system that your program runs on and that meets the requirements specified by the attributes. The combinations are referred to as virtual screens. (See “Virtual Screens.”)
There may be situations for which you want to ensure that your program uses a specific renderer. “Techniques for Choosing Attributes” discusses how to set up an attributes array that will guarantee the system passes back a pixel format object that uses only that renderer.
You'll need to provide code that handles the case of getting back a NULL pixel format object.
Create a rendering context and bind the pixel format object to it. The rendering context keeps track of state information that controls such things as drawing color, view and projection matrices, characteristics of light, and conventions used to pack pixels.
Your application needs a pixel format object to create a rendering context.
Release the pixel format object. Once the pixel format object is bound to a rendering context, its resources are no longer needed.
Bind a drawable object to the rendering context. You'll either bind a Cocoa view or a Carbon window to the context.
Make the rendering context the current context. The system sends OpenGL drawing to whichever rendering context is designated as the current one. It's possible for you to set up more than one rendering context, so you'll need to make sure that the one you want to draw to is the current one.
Perform your drawing.
The specific functions or methods that you use to perform each of the steps are discussed in the sections that follow.
Last updated: 2008-06-09