8.2. Apple Mac OS XBesides GLUT, Apple fully supports three OpenGL interfaces specific to Apple platforms: CGL, NSOpenGL, and AGL. A description of supported Apple interfaces is available at the following Web site: http://developer.apple.com/qa/qa2001/qa1269.html CGL, or Core OpenGL, is the lowest-level of the three interfaces. It can't be used for windowed rendering but can be used for full-screen and offscreen rendering. NSOpenGL, available through Cocoa, is the most recent Apple interface. It presents an object-oriented interface using a set of Objective-C classes. AGL is the primary OpenGL interface for Carbon applications. OpenGL® Distilled focuses on this interface because of its widespread use, support for operating systems older than Mac OS X, and similarities to the WGL and GLX interfaces. AGL Reference documents the AGL interface. See the OpenGL section of the Apple Developer Connection Web site for this and other Apple-specific OpenGL information: http://developer.apple.com/graphicsimaging/opengl/ 8.2.1. Creating ContextsIn AGL, you must specify a pixel format when creating a context. The pixel format describes the characteristics and capabilities of the renderer and drawing surface(s). To obtain a list of pixel formats, call aglChoosePixelFormat().
Use aglChoosePixelFormat() to specify OpenGL rendering requirements such as color, double buffering, depth buffer size, and color buffer size. The following code, for example, requests a double-buffered RGBA pixel format with 24-bit color buffers and a 24-bit depth buffer: const GLint attrib[] = { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_BUFFER_SIZE, 24, AGL_DEPTH_SIZE, 24, AGL_NONE }; AGLPixelFormat fmt = aglChoosePixelFormat( NULL, 0, attrib ); The attribute array elements shown above specify the drawing-surface characteristics. Other attribute element values control renderer selection. Include the constants AGL_ACCELERATED and AGL_NO_RECOVERY in attrib to select only hardware-accelerated pixel formats, for example. AGLPixelFormat is an opaque type. The pixel formats returned by aglChoosePixelFormat() and aglNextPixelFormat() match as closely as possible to the attributes specified in attrib. To examine the returned pixel formats in detail to ensure that they meet your application's specifications, use aglDescribePixelFormat(). See Apple OpenGL Reference for information on aglDescribePixelFormat(). After you've obtained an appropriate pixel format, pass it to aglCreateContext() to create a context.
aglCreateContext() returns NULL if it's unable to create a context for any reason. To share OpenGL objects between contexts, your application must create the contexts according to certain criteria. If two contexts were created with the same pixel format, AGL allows sharing. There are other, less restrictive rules that also allow sharing; see Apple OpenGL Reference for more information. After creating the context, you can free pixel-format resources by calling aglDestroyPixelFormat( pix ), where pix is the pixel format returned by aglChoosePixelFormat(). 8.2.2. Using ContextsBefore issuing OpenGL commands, your application needs to associate the rendering context with a drawable and make it current. To associate the rendering context with a drawable, call aglSetDrawable().
To disable rendering for a context, call aglSetDrawable( ctx, NULL ). You can associate multiple contexts with a single drawable, but only one context can be current at a time. To set the current context, call aglSetCurrentContext().
The following code demonstrates how to associate a context with a window drawable and make the context current: AGLContext ctx; WindowRef win; ... aglSetDrawable( ctx, GetWindowPort(win) ); aglSetCurrentContext( ctx ); When you have associated a context with a drawable and made it current, subsequent OpenGL commands use state from the current context, and rendering affects its drawable. To make the current context noncurrent without making a new context current, call aglSetCurrentContext( NULL ). 8.2.3. Swapping BuffersIf your context is double buffered (that is, if you specified AGL_DOUBLEBUFFER in the attrib parameter to aglChoosePixelFormat() and created a context with the returned pixel format), you must swap buffers after rendering a frame to display the contents of the back buffer. To swap buffers, call aglSwapBuffers().
AGL supports application control over whether the swap buffer command is synchronized to the monitor vertical retrace. See the description of aglSwapBuffers() in Apple OpenGL Reference for more information. 8.2.4. Deleting ContextsTo delete a context and free its resources, call aglDestroyContext().
Carbon applications typically delete contexts upon receiving the kEventWindowClose event. |