Using Interface Builder's NSOpenGLView or Custom View objects for an OpenGL application

Q: Should I use Interface Builder's NSOpenGLView object or the Custom View object when I'm creating a window to draw into with OpenGL?

A: While either one can be used to draw with OpenGL, the Custom View offers greater flexibility for the application than the NSOpenGLView in the palette with a minor amount of additional setup. The current Interface Builder inspector for NSOpenGLView offers a limited set of pixel format attributes, so applications that require additional pixel format attributes should instead use the Custom View approach. For instance, double buffering is not currently available as a pixel format attribute in the NSOpenGLView inspector.

Note that if the view is created by dragging an NSOpenGLView out of the Graphics Views palette, the initialization routines -initWithFrame: and -initWithFrame: pixelFormat: are not called for the NSOpenGLView. In this case, setup and initialization code should go into the view's -awakeFromNib method, however any calls that require an active OpenGL context (such as most of the gl...() type calls) must be made after an OpenGL context has been created and made current. The first time through the -drawRect: method is a good place for this. It is also worth mentioning that with a dragged-in NSOpenGLView, the pixel format and other initialization items usually placed in -initWithFrame: and -initWithFrame: pixelFormat: have already been done and are loaded when the nib file itself is loaded.

Listing 1: Sample setup for an NSOpenGLView


@interface OpenGLView
{
    // Instance variable to control our initialization code
    BOOL readyToDraw;
}
@end


-(void)awakeFromNib
{
     // Do any other initialization code needed
     // in here.
     readyToDraw = NO;
}

-(void)drawRect:(NSRect)rect
{
     // Check to see if we're initialized
    if(!readyToDraw)
    {
       // ... do some setup code here
       // we have a valid OpenGL context at this point

       // Make sure we set this so we don't reinitialize next time
       readyToDraw = YES;
    }
}

Note: As an aside, an NSOpenGLContext can be attached to any NSView, so using an NSOpenGLView is not strictly required. NSOpenGLView is really just a lightweight subclass of NSView that provides some convenience methods for setting up OpenGL drawing. However, keep in mind that attaching an NSOpenGLContext to any view will override any Quartz drawing or other content that is present within its boundaries.

Document Revision History

DateNotes
2004-10-28Edited out the capital 'I' in NSOpenGLPixelFormat, Edited code block for -awakeFromNib to be more accurate, Edited code block to add the @interface section to show an instance variable, Added additional descriptive information regarding a dragged-in NSOpenGLView, Added 'NOTE:' box for text following code listing
2004-10-04Describes usage cases for both NSOpenGLViews and Custom Views for Cocoa OpenGL applications

Posted: 2004-10-28


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.