PyOpenGl 还是 pyglet?

发布于 2024-07-08 20:55:53 字数 1449 浏览 6 评论 0 原文

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

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

发布评论

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

评论(9

遇见了你 2024-07-15 20:55:54

pyglet 的 GL API 远不如 PyOpenGL 的好 - pyglet 位于原始 ctypes 层,这意味着您还需要学习 ctypes。 如果您计划进行大量 OpenGL 编程,您将需要使用 PyOpenGL。

好处是你可以将两者很好地混合。 使用 pyglet 提供 GL 上下文、声音、事件、图像加载和纹理管理、文本渲染等。使用 PyOpenGL 进行您需要执行的实际 OpenGL 编程。

pyglet's GL API is nowhere near as nice as PyOpenGL's - pyglet's is at the raw ctypes layer which means you'll need to learn ctypes as well. If you're planning on doing a bunch of OpenGL programming you'll want to use PyOpenGL.

The nice thing is you can mix the two just fine. Use pyglet to provide the GL context, sounds, events, image loading and texture management, text rendering, etc, etc. Use PyOpenGL for the actual OpenGL programming you need to do.

绿光 2024-07-15 20:55:54

我会推荐 Pyglet,因为它很容易上手并运行一些基本的东西,然后您可以按照自己的节奏添加更高级的技术。

I would recommend Pyglet because it is very easy to get started and have something basic running, and then you can add more advanced techniques at your own pace.

烟酒忠诚 2024-07-15 20:55:53

正如托尼所说,这实际上取决于您的目标。 如果你正在“修修补补”地尝试学习一般的 OpenGL 或 3D 渲染,那么我会放弃所有的客套话并开始使用 PyOpenGL,这与你使用 Python 进行“原始”3D 编程一样接近。

另一方面,如果您通过模拟游戏或多媒体应用程序来“修补”,或者尝试了解一般的编程实践,那么 Pyglet 将通过为输入事件提供钩子来为您节省大量的前期开发时间、声音、文本/广告牌等。通常,这种前期投资会阻碍人们完成他们的项目,因此为您完成这些投资是不容忽视的。 (避免重新发明轮子也是非常 Pythonic 的。)

如果您正在寻找任何类型的重型提升(这通常超出了我对“修补”的定义,但如果您正在修补 3D 引擎设计,则可能不是) )那么你可能想看看 Python-Ogre,它封装了非常功能齐全且健壮的OGRE 3D 图形引擎。

As Tony said, this is really going to depend on your goals. If you're "tinkering" to try to learn about OpenGL or 3D rendering in general that I would dispense with all pleasantries and start working with PyOpenGL, which is as close are you're going to get to "raw" 3D programming using Python.

On the other hand, if you're "tinkering" by way of mocking up a game or multimedia application, or trying to learn about programming practices in general than Pyglet will save you lots of up-front development time by providing hooks for input events, sounds, text/billboarding, etc. Often, this up-front investment is what prevents people from completing their projects, so having it done for you is not something to be ignored. (It is also very Pythonic to avoid reinventing the wheel.)

If you are looking to do any sort of heavy-duty lifting (which normally falls outside my definition of "tinkering", but maybe not if you're tinkering with 3D engine design) then you might want to take a look at Python-Ogre, which wraps the very full-featured and robust OGRE 3D graphics engine.

因为看清所以看轻 2024-07-15 20:55:53

从 pyglet 开始。 它包含最好的高级 API,其中包含您入门所需的所有内容,从打开窗口到使用其友好且强大的 Sprite 和 Batch 类绘制精灵和 OpenGL 基元。

稍后,您可能还想编写自己的较低级代码,直接调用 OpenGL 函数,例如 glDrawArrays 等。您可以使用 pyglet 的 OpenGL 绑定或使用 PyOpenGL 来完成此操作。 好消息是,无论您使用哪种方法,您都可以将此类调用直接插入到现有 pyglet 应用程序的中间,并且它们将“正常工作”。 将代码从 Pyglet 转换到 PyOpenGL 相当容易,因此您不需要预先担心太多的决定。 两者之间的权衡是:

PyOpenGL 的绑定使 OpenGL 界面更加友好和Pythonic。 例如,您可以传递多种不同形式的顶点数组、ctypes 数组、numpy 数组、普通列表等,PyOpenGL 会将它们转换为 OpenGL 可以使用的东西。 这样的事情使得 PyOpenGL 变得非常简单和方便。

pyglet 的 OpenGL 绑定是自动生成的,使用起来不如 PyOpenGL 友好。 例如,有时您必须手动创建 ctypes 对象,以便将“C 指针”类型的参数传递给 OpenGL。 这可能很繁琐。 不过,好的一面是 pyglet 的绑定速度明显更快。

这意味着存在一个最佳的中间立场:使用 pyglet 进行窗口、鼠标事件、声音等。然后,当您想要直接调用 OpenGL 函数时,使用 PyOpenGL 的友好 API。 然后,在优化时,仅用 pyglet 等效项替换位于内部渲染循环内的一小部分性能关键的 PyOpenGL 调用。 对我来说,这使我的帧速率提高了 2 到 4 倍,并且 PyOpenGL 可以方便地处理我 90% 的代码。

Start with pyglet. It contains the best high-level API, which contains all you need to get started, from opening a window to drawing sprites and OpenGL primitives using their friendly and powerful Sprite and Batch classes.

Later, you might also want to write your own lower-level code, that makes calls directly to OpenGL functions such as glDrawArrays, etc. You can do this using pyglet's OpenGL bindings, or using PyOpenGL's. The good news is that whichever you use, you can insert such calls right into the middle of your existing pyglet application, and they will 'just work'. Transitioning your code from Pyglet to PyOpenGL is fairly easy, so this is not a decision you need to worry about too much upfront. The trades-off between the two are:

PyOpenGL's bindings make the OpenGL interface more friendly and pythonic. For example, you can pass vertex arrays in many different forms, ctypes arrays, numpy arrays, plain lists, etc, and PyOpenGL will convert them into something OpenGL can use. Things like this make PyOpenGL really easy and convenient.

pyglet's OpenGL bindings are automatically generated, and are not as friendly to use as PyOpenGL. For example, sometimes you will have to manually create ctypes objects, in order to pass 'C pointer' kinds of args to OpenGL. This can be fiddly. The plus side though, is pyglet's bindings tends to be significantly faster.

This implies that there is an optimal middle ground: Use pyglet for windowing, mouse events, sound, etc. Then use PyOpenGL's friendly API when you want to make direct OpenGL function calls. Then when optimising, replace just the small percentage of performance-critical PyOpenGL calls that lie within your inner render loop with the pyglet equivalents. For me, this gives me between 2 and 4 times faster framerates, with PyOpenGL's convenience for 90% of my code.

若水微香 2024-07-15 20:55:53

尝试 ModernGL

pip install ModernGL
  • PyOpenGL 是原始 OpenGL API(使用 SWIG 生成)的自动生成版本。 原始的 OpenGL API 对 python 不友好。 生成的 python 绑定很难使用。

  • pyglet 主要用于窗口创建和事件处理,但是您可以使用类似 PyOpenGL 的 API(例如 pyglet.gl.glClearColor

  • pygame 提供了一个窗口,您可以在其中使用 PyOpenGL 进行渲染。


  • ModernGL 是 PyOpenGL 的绝佳替代品,您可以使用现代 OpenGL API,编写更少的代码。 ModernGL 本身不会创建窗口,但您可以将其集成在 中pygletpygamePyQt5 甚至在 过剩编辑 您现在可以使用moderngl-window独立创建窗口。
    pip安装moderngl-window

在 ModernGL 中,您只需一次调用即可创建一个简单的着色器程序:

prog = ctx.program(
    vertex_shader='''
        #version 330
        in vec2 in_vert;
        void main() {
            gl_Position = vec4(in_vert, 0.0, 1.0);
        }
    ''',
    fragment_shader='''
        #version 330
        out vec4 f_color;
        void main() {
            f_color = vec4(0.3, 0.5, 1.0, 1.0);
        }
    ''',
)

使用 ModernGL,您可以完全控制 OpenGL API。

Try ModernGL.

pip install ModernGL
  • PyOpenGL is an auto-generated version of the original OpenGL API (generated with SWIG). The original OpenGL API is not python friendly. Generated python bindings are hard to use.

  • pyglet is mostly for the window creation and event handling however you can you a PyOpenGL like API (for example pyglet.gl.glClearColor)

  • pygame provides a window where you can use PyOpenGL to do the rendering.

  • ModernGL is a great alternative to PyOpenGL, You can use the modern OpenGL API with less code written. ModernGL will not create a window by itself, but you can integrate it in pyglet, pygame, PyQt5 and even in GLUT. Edit You can use moderngl-window to create windows independently now.
    pip install moderngl-window

In ModernGL you can create a simple shader program with a single call:

prog = ctx.program(
    vertex_shader='''
        #version 330
        in vec2 in_vert;
        void main() {
            gl_Position = vec4(in_vert, 0.0, 1.0);
        }
    ''',
    fragment_shader='''
        #version 330
        out vec4 f_color;
        void main() {
            f_color = vec4(0.3, 0.5, 1.0, 1.0);
        }
    ''',
)

With ModernGL you have full control over the OpenGL API.

似最初 2024-07-15 20:55:53

我想说 Pyglet 实际上比 PyOpenGL 更先进。 它有自己的一个很好的 API,并且有一个完整的 OpenGL 包装器,可以通过 pyglet.gl 模块访问! PyOpenGL 甚至没有包装 OpenGL 具有的所有功能。
Pyglet 还有一个很棒的库,用于通过 OpenGL 进行硬件加速渲染 2D,而且它制作得非常精良。

如果你想要一个强大的现成 3D 引擎,你可以使用 Ogre

I'd say that Pyglet is actually more evolved than PyOpenGL. It has a nice API of it's own, and it has a full wrapper around OpenGL accessed through the pyglet.gl module! PyOpenGL doesn't even wrap all the functions OpenGL has.
Pyglet also has a great library for rendering 2D with hardware acceleration through OpenGL, and it's really well made.

If you want a powerful ready made 3D engine you've got Ogre and such

独闯女儿国 2024-07-15 20:55:53

嗯,我建议使用 pyglet,它确实提供了游戏或应用程序所需的一切。

请注意,您可以使用 PyOpenGL 完成 pyglet 所做的很多事情,例如创建一个窗口,您所需要做的就是:
glutInitWindow(title)

虽然我认为 glutInitDisplayMode 必须在此之前调用。

简单总结:如果你不想编码到哭,就选择 pyglet,但如果你想成为高手,就选择 PyOpenGL。 转到 http://pyopengl.sourceforge.net 阅读有关 PyOpenGL 的文档并访问 http://pyglet.org 阅读有关 pyglet 的文档。 希望这有帮助!

Hmm, i would suggest pyglet, it really provides everything needed by a game or application.

Mind you, you can do a lot of things pyglet does with PyOpenGL, for example to create a window all you need to do is:
glutInitWindow(title)

Although i think glutInitDisplayMode has to be called before that.

Simple summary: if you don't wanna code until you cry, choose pyglet, but if you want to be a master, choose PyOpenGL. Goto http://pyopengl.sourceforge.net to read the docs on PyOpenGL and to http://pyglet.org to read the docs on pyglet. Hope this was helpful!

你与昨日 2024-07-15 20:55:53

pyglet 有很多不错的附加功能(比如图像加载和声音)。 如果您刚开始,我会先尝试 pyglet,然后如果您想更接近金属,则切换到 PyOpenGL。

但真正重要的问题是:你想要实现什么目标?

pyglet has a lot of nice extras included with it (like image loading and sound). If you're starting out, I'd try pyglet first, and then switch to PyOpenGL if you feel like you want to get closer to the metal.

The real important question though is: what are you trying to accomplish?

人疚 2024-07-15 20:55:53

我推广 pyglet 是因为它拥有我所见过的最好的 API。

Pyglet 也有 opengl API。 但使用最近添加的顶点列表支持通常会更好。

pyglet.gl

I promote pyglet because it has the nicest API I've yet seen on stuff like this.

Pyglet has opengl API as well. But it's often nicer to use the recently added vertex list support.

pyglet.gl

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