Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL 3.2 w/ NSOpenGLView

How would I create a core profile in a custom implementation of NSOpenGLView? Which method should I override and what code should I put there?

So far I have this code:

// Header File
#import <Cocoa/Cocoa.h>

@interface TCUOpenGLView : NSOpenGLView

@end

// Source File
#import "TCUOpenGLView.h"
#import <OpenGL/gl.h>

@implementation TCUOpenGLView

- (void)drawRect:(NSRect)dirtyRect {
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

@end
like image 724
Oskar Avatar asked Jul 22 '12 17:07

Oskar


1 Answers

Apple has a sample code project called GLEssentials that shows exactly how to do this (note that it is a Mac OS X and iOS sample code project).

Essentially you will need to subclass NSOpenGLView (the NSGLView class in the sample code) and implement the awakeFromNib method using the following:

- (void) awakeFromNib
{   
    NSOpenGLPixelFormatAttribute attrs[] =
    {
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFADepthSize, 24,
        // Must specify the 3.2 Core Profile to use OpenGL 3.2
        NSOpenGLPFAOpenGLProfile,
        NSOpenGLProfileVersion3_2Core,
        0
    };

    NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];

    if (!pf)
    {
        NSLog(@"No OpenGL pixel format");
    }

    NSOpenGLContext* context = [[[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil] autorelease];

    [self setPixelFormat:pf];

    [self setOpenGLContext:context];
}

Also remember that if you use any OpenGL API calls that have been removed from the 3.2 API your app will crash. Here is a PDF document of the 3.2 spec so you can see the changes.

like image 77
dbainbridge Avatar answered Sep 20 '22 12:09

dbainbridge