interface-builderosx-lionopengl-3nsopenglviewinitwithcoder

NSOpenGLView with OpenGL 3.2 Core


I apologies in advance if this question will be trivial or a nonsense...this is one of the first app that I'm developing. (Unfortunatley) I'm not a developer.

I have a NSWindow which contains a custom View which is a subclass of NSOpenGLView.

Since the NSWindow and the NSOpenGLView are created through Interface Builder, I don't need to init neither NSOpenGLContext nor NSOpenGLPixelFormat.

I've recently switched to OS X Lion and I now want to leverage new OpenGL 3.2.

From Apple documentation I found out that to enable OpenGL 3.2 I simply need to write something like:

NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
    {
        NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
        0
    };

    NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];
    NSOpenGLContext* openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];

before initializing the NSOpenGLContext.

This is quite easy (even for me), but how can I implement this in my app since I never init NSOpenGLContext?

Since the NSOpenGLView is created from Interface Builder, the method -initWithFormat:shareContext: doesn't get called.

Therefore I tried to override the -initWithCoder: method with something like this:

-(id)initWithCoder:(NSCoder *)aDecoder
{
 NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
    {
        NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
        0
    };

    NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];
    NSOpenGLContext* openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
    [super initWithCoder:aDecoder];
    [self setOpenGLContext:openGLContext];
    [openGLContext makeCurrentContext];
}

and OpenGL 3.2 is correctly loaded as reported by glGetString(GL_VERSION) but the created Context is no longer interacting with the app...nothing is drawn in the view..

I tried everything I know of...but I'm not able to solve this.

How should I do instead?


Solution

  • Sounds your code is OpenGL 2.1 based. The OpenGL 3.2 in Lion only support the core profile (without compatibility profile), which means all fixed-function pipeline stuff (e.g. glBegin/End, glVertex, glPushMatrix, glMultMatrix) all have gone.

    You have to update your code very much. The work will be less if you already use VBO.