OpenGL 3.2 w/NSOpenGLView

Osk*_*kar 6 objective-c nsopenglview opengl-3 nsopengl

如何在NSOpenGLView的自定义实现中创建核心配置文件?我应该覆盖哪种方法以及我应该在哪些代码中添加?

到目前为止,我有这个代码:

// 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
Run Code Online (Sandbox Code Playgroud)

dba*_*dge 11

Apple有一个名为GLEssentials的示例代码项目,它显示了如何执行此操作(请注意,它是一个Mac OS X和iOS示例代码项目).

基本上,您将需要子类化NSOpenGLView(示例代码中的NSGLView类)并使用以下方法实现awakeFromNib方法:

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

另请记住,如果您使用已从3.2 API中删除的任何OpenGL API调用,您的应用程序将崩溃.这是3.2规范的PDF文档,因此您可以看到更改.