The code listings in this section show how to set up full scene anti-aliasing using the NSOpenGLPixelFormat class, AGL, and CGL. You'll see that the code to set buffer and renderer attributes and to create a context looks similar to what you'd normally use to set up any rendering context. Regardless of the API that you use, you need to specify the appropriate attributes. Although you need to specify the context slightly differently for each of the APIs, the outcome is the same—a pixel format and context that supports full-scene anti-aliased rendering.
Listing 10-1 sets up full scene anti-aliasing using the NSOpenGLPixelFormat class, but does not provide a hint, which is optional. A detailed explanation for each numbered line of code appears following the listing.
Listing 10-1 Using NSOpenGLPixelFormat to set up full scene anti-aliasing
#import <Cocoa/Cocoa.h> |
@implementation BasicOpenGLView |
+ (NSOpenGLPixelFormat*)defaultPixelFormat |
{ |
NSOpenGLPixelFormatAttribute attributes [] = { // 1 |
NSOpenGLPFAWindow, |
NSOpenGLPFADoubleBuffer, |
NSOpenGLPFASampleBuffers, 1, |
NSOpenGLPFASamples, 2, |
NSOpenGLPFANoRecovery, |
(NSOpenGLPixelFormatAttribute)nil |
}; |
return [[[NSOpenGLPixelFormat alloc] |
initWithAttributes:attributes] autorelease]; // 2 |
} |
-(id) initWithFrame: (NSRect) frameRect |
{ |
NSOpenGLPixelFormat *pixelFormat = [BasicOpenGLView |
defaultPixelFormat]; // 3 |
self = [super initWithFrame: frameRect |
pixelFormat: pixelFormat]; // 4 |
return self; |
} |
// Define other class methods here. |
@end |
Here's what the code in Listing 10-1 does:
Sets up attributes for OpenGL to use for choosing the pixel format. The attributes include the two required for multisampling: NSOpenGLPFASampleBuffers and NSOpenGLPFASamples, along with those to support a Cocoa window, double buffering, and no recovery.
Allocates and initializes an NSOpenGLPixelFormat object with the requested multisampling (and other) attributes.
Creates an NSOpenGLPixelFormat object for a custom NSOpenGLView class (BasicOpenGLView) that was previously created by the application using Interface Builder but is not shown in this example.
Initializes the view with the newly created pixel format.
Listing 10-2 sets up full scene anti-aliasing in Carbon and provides a hint for supersampling. A detailed explanation for each numbered line of code appears following the listing.
Listing 10-2 Using AGL to set up full scene anti-aliasing with a hint for supersampling
#include <AGL/agl.h> |
GLint attribs[] = { AGL_RGBA, // 1 |
AGL_DOUBLEBUFFER, |
AGL_SAMPLE_BUFFERS_ARB, 1, |
AGL_SAMPLES_ARB, 2, |
AGL_SUPERSAMPLE, |
AGL_NO_RECOVERY, |
AGL_NONE }; |
AGLPixelFormat pixelFormat = NULL; |
AGLContext context = NULL; |
pixelFormat = aglChoosePixelFormat (NULL, 0, attribs); // 2 |
if (pixelFormat) { |
context = aglCreateContext (pixelFormat, NULL); // 3 |
aglDestroyPixelFormat (pixelFormat); |
} |
Here's what the code in Listing 10-2 does:
Sets up attributes for OpenGL to use for creating an AGL pixel format object. The attributes include the two required for multisampling (AGL_SAMPLE_BUFFERS_ARB and AGL_SAMPLES_ARB) along with those to support RGBA pixels, double buffering, and no recovery.
Creates a pixel format object. Prior to this call you can optionally provide a list of GDHandle values that specify the supported displays.
Creates a rendering context based on the newly created pixel format object that is set up to support full scene antialiasing.
Listing 10-3 sets up full scene anti-aliasing using the CGL API and provides a hint for multisampling. A detailed explanation for each numbered line of code appears following the listing.
Listing 10-3 Using CGL to set up full scene anti-aliasing with a hint for multisampling
#include <OpenGL/OpenGL.h> |
CGLPixelFormatAttribute attribs[] = { kCGLPFADisplayMask, 0, // 1 |
kCGLPFAFullScreen, |
kCGLPFADoubleBuffer, |
kCGLPFASampleBuffers, 1, |
kCGLPFASamples, 2, |
kCGLPFAMultisample |
kCGLPFANoRecovery, |
0 }; |
CGLPixelFormatObj pixelFormat = NULL; |
CGLContextObj context = NULL; |
long numPixelFormats = 0; |
attribs[1] = CGDisplayIDToOpenGLDisplayMask (CGMainDisplayID ()); // 2 |
CGLChoosePixelFormat (attribs, &pixelFormat, &numPixelFormats)); // 3 |
if (pixelFormat) { |
CGLCreateContext (pixelFormat, NULL, &context); // 4 |
CGLDestroyPixelFormat (pixelFormat); |
} |
Here's what the code in Listing 10-3 does:
Sets up attributes for OpenGL to use for creating a CGL pixel format object. The attributes include the two multisampling attributes: kCGLPFASampleBuffers and kCGLPFASamples, along with those to support full-screen drawing, double buffering, and no recovery. The associated value for kCGLPFASamples is the number of samples per multisample buffer, which in this case is 2. The associated value for kCGLPFASampleBuffers is a nonnegative integer that indicates the number of existing independent sample buffers, which in this case is 1.
Sets the value of the display mask attribute to the main display. (Note that this code example does not capture the main display. See Listing 3-3.)
Creates a pixel format object with the requested attributes.
Creates a context for the pixel format that isn't shared.
Last updated: 2008-06-09