2013-07-22 46 views
1

我嘗試使用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); 

} 

代碼應該簡單地將視圖清除爲紅色。沒有錯誤或警告 - 只是一個白色的窗口。

+0

試着插入一些NSLog到'drawRect:'。可能它根本沒有被調用?可能你應該告訴OpenGL視圖重繪自己? – cody

+0

我已經添加了一個NSLog()drawRect:它確實最終被調用,所以我仍然不知道什麼是壞的。 – foobar5512

回答

1

我需要我呼籲glClear後替換使用glSwapAPPLE()緩衝區()

我讀的地方,NSOpenGLViews不需要進行交換,因爲它是自動完成的。根據我的經驗,我可以說這完全是不真實的。

+0

我已經設法做到這一點在過去只是沖洗的背景下。在你的繪製代碼的結尾,opengl繪製任何東西都是必要的。我沒有看到你在你的正確代碼的末尾使用任何刷新或交換方法,因此添加glswapapple的工作原理。嘗試添加glFlush();它也應該可以工作。 – Bisder

+0

您的視圖是否設置爲使用雙緩衝?然後從我的理解你需要交換。否則你不會。 – cacau