2012-07-22 46 views
6

如何在NSOpenGLView的自定义实现中创建核心配置文件?我应该重写哪种方法,并且应该在那里放置哪些代码?OpenGL 3.2 w/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 

回答

10

苹果有一个名为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]; 
} 

还要记住,如果你使用的是已被删除任何OpenGL的API调用从3.2 API你的应用程序将崩溃。这是3.2规范的PDF document,所以你可以看到更改。