Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 275
Andrea3000 Avatar asked Nov 07 '25 00:11

Andrea3000


1 Answers

NSOpenGLView has a setOpenGLContext method. Pass your 3.2 context to the view that way, and call setView on the NSOpenGLContext afterwards.

I haven't tested this, but it should work, although I just made my own OpenGL View.

like image 139
Neil M Avatar answered Nov 09 '25 05:11

Neil M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!