Edit AllPages

Here is an example of how to do Cocoa OpenGL drawing without using Apple’s NSOpenGLView class. It requires that you drag an NSView custom view into the window in InterfaceBuilder and then set it to the customGLView class. This example also uses the customGLView as the application delegate in order to initialize the context’s view to itself when the application is finished launching.

March 2nd, 2006: I tried using this code and found a few problems. First, there’s a missing closing curly brace that prevents the compiler from recognizing the drawRect: method. After fixing that, I found that the window is blank (no triangles show up at all), and the window shows some garbage when you resize it. Can anyone provide some suggestions on how to find the problems?

March 3rd, 2007: I’ve fixed the example below, based on Apple’s Custom Cocoa OpenGL sample code. I’ve tested the code below on 10.4.

// customGLView.h

#import <AppKit/AppKit.h> #import <OpenGL/OpenGL.h> #import <OpenGL/gl.h>

@interface customGLView : NSView {

NSOpenGLContext *oglContext;
GLfloat *verts;
GLubyte *tris; }

@end // ———

// customGLView.m

#import “customGLView.h”

@implementation customGLView

/* default initializer for descendents of NSView */

// this is called whenever the view changes (is unhidden or resized)

// this tells the window manager that nothing behind our view is visible -(BOOL) isOpaque {

return YES; }

-(void) dealloc {

// clean ourselves up nicely by freeing our dynamic memory allocations if( verts ) free( verts ); if( tris ) free( tris );

[super dealloc]; }