如何在 OS X 上不在主线程中使用 GLUT?

发布于 2024-08-19 19:32:01 字数 971 浏览 4 评论 0原文

我曾经尝试从子线程打开一个 GLUT 窗口,但遇到了很多令人讨厌的问题。我记得lists.apple.com上的这篇文章:

GLUT functions may only be called from the application's main thread

Mac OS X上的GLUT在这方面有什么改变吗?是否有线程安全的 GLUT 可以让您从任何线程打开窗口?

如果 GLUT 不是一个选项,是否有一个小型库可以替代 GLUT 并且可以在任何线程中工作?

[编辑]

这是我的测试结果,由作为答案提出的各种解决方案触发:

  • GLFW 看起来不错,但没有编译(当前分支已有 3 岁)
  • Agar 是另一个伪装者,但它太大了我对 SDL 的微小需求
  • 与 BSD 许可证不兼容,它是一个巨大的代码库,应该适合单个文件
  • ,但不能在任何线程中运行。

我决定重新发明轮子(是的,有时这很好),最终的类只有 200 行代码。它让我可以从任何线程打开和关闭窗口(openGL 在新线程中绘制),并且我可以完全控制垂直同步等(SDL 使用双缓冲 = openGL 速度慢)。我必须绕过 NSApp 才能正确启动和停止应用程序(否则不使用事件循环)。

对于那些告诉我 OpenGL 不是线程安全的人来说,这并不完全正确:您可以运行多个 OpenGL 线程,并且绘制命令将在分配给该线程的 OpenGL 状态下执行。 OpenGL 是线程特定的

如果有人需要一些简单的代码来使用 Cocoa 创建 OpenGL 窗口: gl_window.mm

I once tried to open a GLUT window from a sub-thread and got lots of nasty problems. I remember this post on lists.apple.com:

GLUT functions may only be called from the application's main thread

Has anything changed in this regard with GLUT on Mac OS X ? Is there a thread-safe GLUT that let's you open windows from any thread ?

If GLUT is not an option, is there a tiny library that replaces GLUT and would work from any thread ?

[edit]

Here is the result of my tests triggered by the various solutions proposed as answers:

  • GLFW looked nice but did not compile (current branch is 3 years old)
  • Agar was another pretender but it's too big for the tiny need I had
  • SDL is not BSD-license compatible and it's a huge library for code that should fit on a single file
  • GLUT cannot run in any thread.

I decided to reinvent the wheel (yes, that's good sometimes) and the final class is just 200 lines of code. It let's me open and close a window from any thread (openGL draw in new thread) and I have full control on vertical sync and such (SDL uses double buffering = slow for openGL). I had to trick around the NSApp to properly start and stop the application (which does not use an event loop otherwise).

To those telling me that OpenGL is not thread-safe, that's not exactly true: you can run multiple OpenGL threads and the draw commands will be executed in the OpenGL state assigned to that thread. OpenGL is thread-specific.

If anyone needs some bare-bones code to create OpenGL windows using Cocoa: gl_window.mm

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

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

发布评论

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

评论(3

淡淡の花香 2024-08-26 19:32:01

GLUT 不是线程安全的。无论您选择实现什么解决方案,您都需要锁定原语。我建议在 Cocoa 中设置您自己的 GL 视图并重写 GLUT 提供的管道。

看看 SDL 作为现代 GLUT 替代品。它应该为您提供您想要的所有跨平台功能。就跨平台线程而言,Boost 提供了一个可移植的库< /a>.

GLUT is not thread safe. You'll need locking primitives with whatever solution you choose to implement. I'd recommend setting up your own GL view in Cocoa and rewriting the plumbing that GLUT provides.

Take a look at SDL as a modern GLUT replacement. It should give you all the cross-platform you want. As far a cross-platform threading, Boost provides a portable library.

嘿看小鸭子会跑 2024-08-26 19:32:01

作为 GLUT 的替代品,请查看 GLFW。它的目的和工作原理相似,但更好。而且它没有让您的程序陷入困境的 glfwMainLoop;它可以让您完全控制。自从我发现 GLFW 以来,我从来没有需要切换回 GLUT。

请注意,GLFW 不是线程安全的,从某种意义上说,从不同线程调用 GLFW 函数是不安全的 (常见问题解答条目)。但是,只要您从同一线程调用所有 GLFW 函数,您就可以选择使用哪个线程。

As a replacement for GLUT, have a look at GLFW. It's similar in purpose and workings, but better. And it does not have a glfwMainLoop that your program is stuck with; it allows you full control. Never since I discovered GLFW have I had a need to switch back to GLUT.

Note that GLFW is not thread-safe, in the sense that it is unsafe to call GLFW functions from different threads (FAQ entry). However, as long as you call all GLFW functions from the same thread, it's your choice which thread that will be.

高速公鹿 2024-08-26 19:32:01

GLUT 不仅不是线程安全的,而且 OpenGL 是一个状态机,因此也不是线程安全的。话虽如此,您可以拥有使用 OpenGL 的多线程应用程序。只需确保所有 OpenGL 调用都是从同一线程进行的。

Mac OS X 上的 GLUT 的下一步是 Cocoa OpenGL 示例代码。这是一个真正的 Cocoa 应用程序,演示了设置 OpenGL 窗口的 Cocoa 方式,以及使用 Cocoa 事件模型的交互性。从这个起点开始,在 OpenGL 绘图代码的单独线程(或多个线程)中添加代码来处理程序逻辑是相当容易的。

Not only is GLUT is not thread safe, but OpenGL is a state machine, and therefore isn't thread safe. Having said that, you can have multithreaded applications that use OpenGL. Just make sure all your OpenGL calls are made from the same thread.

The next step up from GLUT on Mac OS X is the Cocoa OpenGL Sample Code. This is a true Cocoa application that demonstrates the Cocoa way of setting up an OpenGL window, with interactivity using the Cocoa event model. From this starting point, it's fairly easy to add code to handle your program logic in a separate thread (or threads) from your OpenGL drawing code.

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