开发者

NSOpenGLLayer and multithreading

开发者 https://www.devze.com 2023-04-04 08:20 出处:网络
I have written a 3D viewer using Cocoa. The OpenGL renderings are performed in a separate thread that creates开发者_运维知识库 its own NSOpenGLContext.

I have written a 3D viewer using Cocoa. The OpenGL renderings are performed in a separate thread that creates开发者_运维知识库 its own NSOpenGLContext.

Without layer handling, the 3D view drawRect method is called on refresh, and the OpenGL thread does its refresh and every things works perfectly...

Now, I have to implement the application using Cocoa layers. When the 3D NSView is created, a subclass of NSOpenGLLayer is created and attached to the view. The method

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

is called by Cocoa, but I am unable to make my OpenGL thread render anything.

I had tried to use the OpenGL context passed to drawInOpenGLContext in the OpenGL thread, I have tried to do a

[layer setOpenGLContext:ctx]

in the OpenGL thread with the OpenGL context created in the thread, and so on, but nothing works.


Did you remember to call setWantsLayer on your NSView? You need to call that from your NSView probably to make it a layer-hosted view. See the documentation for NSView setWantsLayer.

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

In my TestOpenGLLayer class, I only had to define the drawInOpenGLContext function. I added my opeNGL commands to it and the view rendered the layer properly. I did not have to call [layer setContext].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号