2011-09-13 82 views
3

我寫了一個使用Cocoa的3D查看器。 OpenGL渲染是在一個獨立的線程中執行的,它創建了自己的NSOpenGLContext。NSOpenGLLayer和多線程

沒有層處理,3D視圖的drawRect方法被調用刷新,且OpenGL線程做了刷新,每個東西的作品完美...

現在,我要實現使用可可層的應用。創建3D NSView時,會創建NSOpenGLLayer的子類並將其附加到視圖。方法

(void)drawInOpenGLContext:NSOpenGLContext *)ctx 
       pixelFormat:(NSOpenGLPixelFormat *)pixelFormat 
      forLayerTime:(CFTimeInterval)timeInterval 
       displayTime:(const CVTimeStamp *)timeStamp; 

被Cocoa調用,但我無法讓我的OpenGL線程渲染任何東西。

我曾試圖用傳遞給drawInOpenGLContext在OpenGL的線程OpenGL上下文,我試圖做與在線程創建的OpenGL上下文OpenGL的線程

[layer setOpenGLContext:ctx] 

,等等,但沒有任何工作。

回答

0

你記得在你的NSView上調用setWantsLayer嗎?你需要從你的NSView中調用它來使它成爲一個圖層託管的視圖。請參閱NSView setWantsLayer的文檔。

_testOglView = [[NSView alloc]initWithFrame:[self.view bounds]]; 
[_testOglView setLayer:[[TestOpenGLLayer alloc] init]]; 
[_testOglView setWantsLayer:YES]; 
[self.view addSubview:_testOglView]; 

在我的TestOpenGLLayer類中,我只需要定義drawInOpenGLContext函數。我添加了我的opeNGL命令,並且視圖正確地渲染了圖層。我不必調用[layer setContext]。