1

该类NSOpenGLView有一个读/写属性openGLContext,当我阅读文档时,getter 永远不应该返回 nil:

如果接收者没有关联的上下文对象,NSOpenGLContext则创建一个新对象。使用接收器的像素格式信息初始化新对象。

在 macOS 10.13 上,这似乎是真的,但在 macOS 10.9 上,在我将视图添加到窗口后它返回 nil。也就是说,如果我这样做:

NSOpenGLView* glView = [[NSOpenGLView alloc]
    initWithFrame: NSMakeRect( 0.0, 0.0, 100.0, 100.0 )
    pixelFormat: [NSOpenGLView defaultPixelFormat]];
NSLog(@"Pixel format %@, context %@", glView.pixelFormat, glView.openGLContext );

[self.host addSubview: glView];
NSLog(@"Pixel format %@, context %@", glView.pixelFormat, glView.openGLContext );

然后在第二个日志上,上下文为零。有什么解决办法吗?自己制作NSOpenGLContext并分配它没有帮助。我已经验证主机视图是窗口的一部分并且windowNumber是肯定的。

4

1 回答 1

0

显然,在 Mavericks 中,必须设置上下文的视图以及设置视图的上下文。像这样的工作:

NSOpenGLPixelFormat* pf = [NSOpenGLView defaultPixelFormat];

NSOpenGLView* glView = [[[NSOpenGLView alloc]
    initWithFrame: NSMakeRect( 0.0, 0.0, 100.0, 100.0 )
    pixelFormat: pf] autorelease];

NSOpenGLContext* glc = [[[NSOpenGLContext alloc]
    initWithFormat: glView.pixelFormat shareContext: nil] autorelease];

[self.host addSubview: glView];

glc.view = glView; // the key step I was missing
glView.openGLContext = glc;
NSLog(@"Pixel format %@, context %@", glView.pixelFormat, glView.openGLContext );
于 2019-02-27T01:24:06.360 回答