确定 NSOpenGLView 是否具有有效的上下文 (Cocoa OSX)

发布于 2024-09-03 09:42:33 字数 172 浏览 0 评论 0原文

我正在尝试检查我的 NSOpenGLView 是否具有有效的上下文,但似乎对 openGLContext 的调用无法帮助我解决这个问题。 openGLContext 似乎总是返回一个 NsOpenGlContext (如果视图有一个上下文,它会返回当前上下文,如果没有,它会创建一个上下文并返回该上下文)。我可以使用另一种方法吗?

I am trying to check that my NSOpenGLView has a valid context but it seems that a call to openGLContext will not help me figure this out. openGLContext seems to always returns a an NsOpenGlContext (it returns the current context if the view has one and if not it makes a context and returns that). Is there another method I could use instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2024-09-10 09:42:33

OpenGL 上下文是按线程选择的。 NSOpenGLView 会告诉您应该使用哪个上下文在视图中进行绘制(即哪个上下文已调用 [context setView: view]),但不会告诉您当前在执行代码的线程中选择了哪个上下文。这有点像在旧 Windows 中选择用于绘图的笔/画笔。

我理解您想知道是否可以在视图中绘制。为此,请检查当前上下文是否是 NSOpenGLView 返回的上下文,如果不是,请将其设置为。

获取线程的当前 OpenGL 上下文:

NSOpenGLContext* context = [NSOpenGLContext currentContext];

将其设置为当前:

[context makeCurrentContext];

清除当前上下文(即不设置当前上下文):

[NSOpenGLContext clearCurrentContext];

一般来说,请参阅 NSOpenGLContext 的文档。

OpenGL contexts are selected per-thread. NSOpenGLView will tell you which context should be used to draw in the view (ie. which one has had [context setView: view] called on), but not which context is currently selected in the thread that's executing the code. It's a bit like selecting pen/brush to draw with in old Windows.

I understand that you want to know if you can draw in view. To do that, check if current context is the one that NSOpenGLView returns, and if not, set it to be.

To get current OpenGL context for the thread:

NSOpenGLContext* context = [NSOpenGLContext currentContext];

To set one as current:

[context makeCurrentContext];

To clear current context (ie. set no current context):

[NSOpenGLContext clearCurrentContext];

In general, see docs for NSOpenGLContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文