我嘗試使用Cocoa窗口和NSOpenGLView的OpenGL 3.2。但是我不能讓NSOpenGLView繪製紅色。我只是得到一個白色的窗戶。這裏是我的代碼(在NSOpenGLView的子類中):NSOpenGLView不會清除
-(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];
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
代碼應該簡單地將視圖清除爲紅色。沒有錯誤或警告 - 只是一個白色的窗口。
試着插入一些NSLog到'drawRect:'。可能它根本沒有被調用?可能你應該告訴OpenGL視圖重繪自己? – cody
我已經添加了一個NSLog()drawRect:它確實最終被調用,所以我仍然不知道什麼是壞的。 – foobar5512