2012-07-22 56 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,所以你可以看到更改。