具有 OpenGL 3.2 核心的 NSOpenGLView

And*_*000 4 interface-builder nsopenglview opengl-3 osx-lion initwithcoder

如果这个问题是微不足道的或无意义的,我提前道歉......这是我正在开发的第一个应用程序之一。(不幸的是)我不是开发人员。

我有一个 NSWindow,其中包含一个自定义 View,它是NSOpenGLView.

由于 和NSWindowNSOpenGLView通过 Interface Builder 创建的,所以我既不需要init也不NSOpenGLContext需要NSOpenGLPixelFormat

我最近切换到 OS X Lion,现在想利用新的 OpenGL 3.2。

从 Apple 文档中我发现要启用 OpenGL 3.2,我只需要编写如下内容:

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

    NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];
    NSOpenGLContext* openGLContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
Run Code Online (Sandbox Code Playgroud)

在初始化NSOpenGLContext.

这很容易(即使对我来说也是如此),但是我如何在我的应用程序中实现这一点,因为我从来没有这样做过init NSOpenGLContext

由于是从 Interface Builder 创建的,因此不会调用NSOpenGLView该方法。-initWithFormat:shareContext:

因此我尝试-initWithCoder:用这样的方法重写该方法:

-(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];
}
Run Code Online (Sandbox Code Playgroud)

并且 OpenGL 3.2 已按照报告正确加载,glGetString(GL_VERSION)但创建的上下文不再与应用程序交互...视图中未绘制任何内容。

我尝试了我所知道的一切...但我无法解决这个问题。

我该怎么办呢?

Nei*_*l M 5

NSOpenGLView 有一个 setOpenGLContext 方法。以这种方式将 3.2 上下文传递给视图,然后在 NSOpenGLContext 上调用 setView 。

我还没有测试过这个,但它应该可以工作,尽管我刚刚制作了自己的 OpenGL 视图。