I want to display something in an NSOpenGLView, but since there is a total of zero bytes of documentation on it, and the sample code is as big and complex as the documentation should be, I can't get any wise out of it. This is my code so far, and my ANOpenGLView is an NSOpenGLView in a NIB subclassed as ANOpenGLView:
@implementation ANOpenGLView
@synthesize animationTimer;
// MEM
- (void)dealloc {
[animationTimer release];
[super dealloc];
}
// INIT
- (id)initWithFrame:(NSRect)frameRect {
if (self = [super initWithFrame:frameRect]) {
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 32,
0
};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
[self setOpenGLContext:[[[NSOpenGLContext alloc] initWithFormat:format shareContext:nil] autorelease]];
}
return self;
}
- (void)awakeFromNib {
/* 60 FPS */
animationTimer = [[NSTimer timerWithTimeInterval:(1.0f/60.0f) target:self selector:@selector(redraw:) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:animationTimer forMode:NSDefaultRunLoopMode];
}
// DRAW
- (void)redraw:(NSTimer*)theTimer {
[self drawRect:[self bounds]];
}
- (void)drawRect:(NSRect)dirtyRect {
NSLog(@"Redraw");
[[self openGLContext] clearDrawable];
[[self openGLContext] setView:self];
[[self openGLContext] makeCurrentContext];
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glViewport(0, 0, [self frame].size.width, [self frame].size.height);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glBegin( GL_TRIANGLES );
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(1.0f, -1.0f);
glEnd();
[[self openGLContext] flushBuffer];
[NSOpenGLContext clearCurrentContext];
}
@end
How can I get the triangle to show up? The only thing I get is a blank, white screen.
P.S. I want to draw in 2D.
EDIT I have updated my code, and this is what I have now: