python PyOpenGl 还是 pyglet?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/279912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyOpenGl or pyglet?
提问by Hortitude
I am looking to do some tinkering with openGL and Python and haven't been able to find good reasons for using PyOpenGl versus pyglet
我希望对 openGL 和 Python 进行一些修补,但一直找不到使用 PyOpenGl 与 pyglet 的充分理由
Which would you recommend and why?
你会推荐哪个,为什么?
回答by bouvard
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.
正如托尼所说,这真的取决于你的目标。如果您正在“修补”以尝试了解 OpenGL 或一般的 3D 渲染,那么我将放弃所有的玩笑并开始使用 PyOpenGL,这与您将使用 Python 进行“原始”3D 编程一样接近.
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.)
另一方面,如果您通过模拟游戏或多媒体应用程序的方式“修补”,或者尝试了解一般的编程实践,那么 Pyglet 将通过为输入事件提供挂钩来为您节省大量的前期开发时间、声音、文本/广告牌等。通常,这种前期投资会阻止人们完成他们的项目,因此为您完成项目是不容忽视的。(避免重新发明轮子也是非常 Pythonic 的。)
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 veryfull-featured and robust OGRE 3Dgraphics engine.
如果你想做任何繁重的工作(这通常不属于我对“修补”的定义,但如果你正在修补 3D 引擎设计可能不会),那么你可能想看看 Python-Ogre ,它包含了功能非常齐全且强大的OGRE 3D图形引擎。
回答by Jonathan Hartley
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.
从 pyglet 开始。它包含最好的高级 API,其中包含您入门所需的一切,从打开窗口到使用其友好且强大的 Sprite 和 Batch 类绘制精灵和 OpenGL 基元。
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:
稍后,您可能还想编写自己的低级代码,直接调用 OpenGL 函数,例如 glDrawArrays 等。您可以使用 pyglet 的 OpenGL 绑定或使用 PyOpenGL 的绑定来完成此操作。好消息是,无论您使用哪种方法,都可以将此类调用直接插入到现有 pyglet 应用程序的中间,它们将“正常工作”。将您的代码从 Pyglet 转换到 PyOpenGL 是相当容易的,因此这不是您需要预先考虑太多的决定。两者之间的权衡是:
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.
PyOpenGL 的绑定使 OpenGL 界面更加友好和 Pythonic。例如,您可以以多种不同的形式传递顶点数组,ctypes 数组、numpy 数组、普通列表等,PyOpenGL 会将它们转换为 OpenGL 可以使用的东西。像这样的事情使 PyOpenGL 非常简单方便。
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.
pyglet 的 OpenGL 绑定是自动生成的,使用起来不如 PyOpenGL 友好。例如,有时您必须手动创建 ctypes 对象,以便将“C 指针”类型的 args 传递给 OpenGL。这可能很繁琐。不过,好的一面是 pyglet 的绑定往往要快得多。
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.
这意味着有一个最佳的中间立场:将 pyglet 用于窗口、鼠标事件、声音等。然后在您想要进行直接 OpenGL 函数调用时使用 PyOpenGL 的友好 API。然后在优化时,用 pyglet 等效项替换位于内部渲染循环中的一小部分性能关键 PyOpenGL 调用。对我来说,这使我的帧速率提高了 2 到 4 倍,而 PyOpenGL 为我 90% 的代码提供了便利。
回答by Szabolcs Dombi
Try ModernGL.
试试ModernGL。
pip install ModernGL
PyOpenGLis 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.
pygletis mostly for the window creation and event handling however you can you a PyOpenGL like API (for example
pyglet.gl.glClearColor
)pygameprovides a window where you can use PyOpenGL to do the rendering.
ModernGLis 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, PyQt5and even in GLUT.
PyOpenGL是原始 OpenGL API(使用 SWIG 生成)的自动生成版本。原始的 OpenGL API 不是 Python 友好的。生成的 python 绑定很难使用。
pyglet主要用于窗口创建和事件处理,但是您可以使用 PyOpenGL 之类的 API(例如
pyglet.gl.glClearColor
)pygame提供了一个窗口,您可以在其中使用 PyOpenGL 进行渲染。
ModernGL是PyOpenGL的绝佳替代品,您可以使用现代 OpenGL API 编写更少的代码。ModernGL 不会自行创建窗口,但您可以将其集成到pyglet、pygame、PyQt5甚至GLUT 中。
In ModernGL you can create a simple shader program with a single call:
在 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);
}
''',
)
With ModernGL you have full control over the OpenGL API.
使用 ModernGL,您可以完全控制 OpenGL API。
回答by Zoomulator
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.
我想说 Pyglet 实际上比 PyOpenGL 更进化。它有一个很好的 API,它有一个完整的 OpenGL 包装器,可以通过 pyglet.gl 模块访问!PyOpenGL 甚至没有包含 OpenGL 的所有功能。Pyglet 也有一个很棒的库,可以通过 OpenGL 使用硬件加速来渲染 2D,而且它制作精良。
If you want a powerful ready made 3D engine you've got Ogreand such
如果你想要一个强大的现成 3D 引擎,你有Ogre 之类的
回答by Uchiha Madara
Hmm, i would suggest pyglet, it really provides everything needed by a game or application.
嗯,我建议使用 pyglet,它确实提供了游戏或应用程序所需的一切。
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)
请注意,pyglet 可以用 PyOpenGL 做很多事情,例如创建一个窗口,您需要做的就是:
glutInitWindow(title)
Although i think glutInitDisplayMode has to be called before that.
尽管我认为必须在此之前调用 glutInitDisplayMode。
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.netto read the docs on PyOpenGL and to http://pyglet.orgto read the docs on pyglet. Hope this was helpful!
简单总结:如果你不想哭到哭,就选择pyglet,但如果你想成为高手,就选择PyOpenGL。转至http://pyopengl.sourceforge.net阅读有关 PyOpenGL 的文档,并转至http://pyglet.org阅读有关 pyglet 的文档。希望这是有帮助的!
回答by Tony Arkles
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.
pyglet 包含许多不错的附加功能(如图像加载和声音)。如果你刚开始,我会先尝试 pyglet,如果你想更接近金属,然后切换到 PyOpenGL。
The real important question though is: what are you trying to accomplish?
但真正重要的问题是:你想完成什么?
回答by Cheery
I promote pyglet because it has the nicest API I've yet seen on stuff like this.
我推广 pyglet 是因为它拥有我见过的最好的 API。
Pyglet has opengl API as well. But it's often nicer to use the recently added vertex list support.
Pyglet 也有 opengl API。但是使用最近添加的顶点列表支持通常会更好。
pyglet.gl
pyglet.gl
回答by Kiv
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.
我会推荐 Pyglet,因为它很容易上手并且可以运行一些基本的东西,然后你可以按照自己的节奏添加更高级的技术。
回答by Richard Jones
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.
pyglet 的 GL API 远不及 PyOpenGL 的好 - pyglet 位于原始 ctypes 层,这意味着您还需要学习 ctypes。如果您打算进行大量 OpenGL 编程,则需要使用 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.
好处是你可以很好地混合两者。使用 pyglet 提供 GL 上下文、声音、事件、图像加载和纹理管理、文本渲染等。使用 PyOpenGL 进行您需要进行的实际 OpenGL 编程。