用 Python 编写的 3D OpenGL 游戏可以看起来不错并且运行速度很快吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/641770/
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
Can 3D OpenGL game written in Python look good and run fast?
提问by praavDa
I am planning to write an simple 3d(isometric view) game in Java using jMonkeyEngine - nothing to fancy, I just want to learn something about OpenGL and writing efficient algorithms (random map generating ones).
我打算使用 jMonkeyEngine 在 Java 中编写一个简单的 3d(等距视图)游戏 - 没什么特别的,我只是想了解一些有关 OpenGL 的知识并编写有效的算法(随机地图生成算法)。
When I was planning what to do, I started wondering about switching to Python. I know that Python didn't come into existence to be a tool to write 3d games, but is it possible to write good looking games with this language?
当我计划要做什么时,我开始考虑是否要切换到 Python。我知道 Python 的出现并不是为了成为编写 3d 游戏的工具,但是否有可能用这种语言编写好看的游戏?
I have in mind 3d graphics, nice effects and free CPU time to power to rest of game engine? I had seen good looking java games - and too be honest, I was rather shocked when I saw level of detail achieved in Runescape HD.
我想到了 3d 图形、漂亮的效果和空闲的 CPU 时间来为游戏引擎的其余部分供电?我看过好看的 Java 游戏——老实说,当我看到 Runescape HD 达到的细节水平时,我感到相当震惊。
On the other hand, pygame.org has only 2d games, with some starting 3d projects. Are there any efficient 3d game engines for python? Is pyopengl the only alternative? Good looking games in python aren't popular or possible to achieve?
另一方面,pygame.org 只有 2d 游戏,还有一些开始的 3d 项目。有没有高效的python 3d 游戏引擎?pyopengl 是唯一的选择吗?python中好看的游戏不流行或不可能实现?
I would be grateful for any information / feedback.
我将不胜感激任何信息/反馈。
回答by sleske
If you are worried about 3D performance: Most of the performance-critical parts will be handled by OpenGL (in a C library or even in hardware), so the language you use to drive it should not matter too much.
如果您担心 3D 性能:大多数性能关键部分将由 OpenGL 处理(在 C 库中,甚至在硬件中),因此用于驱动它的语言应该无关紧要。
To really find out if performance is a problem, you'd have to try it. But there is no reason why it cannot work in principle.
要真正确定性能是否有问题,您必须尝试一下。但是没有理由它不能在原则上工作。
At any rate, you could still optimize the critical parts, either in Python or by dropping to C. You still gain Python's benefit for most of the game engine which is less performance-critical.
无论如何,您仍然可以优化关键部分,无论是在 Python 中还是使用 C 语言。您仍然可以获得 Python 对大多数性能不那么重要的游戏引擎的好处。
回答by S.Lott
Yes. Eve Online does it.
是的。Eve Online 做到了。
回答by Jonathan Hartley
I did a EuroPython talk about my amateur attempts to drive OpenGL from Python: http://pyvideo.org/video/381/pycon-2011--algorithmic-generation-of-opengl-geom
我做了一个 EuroPython 谈论我的业余尝试从 Python 驱动 OpenGL:http: //pyvideo.org/video/381/pycon-2011--algorithmic-generation-of-opengl-geom
The latest version of the code I'm talking about is here: https://github.com/tartley/gloopy
我正在谈论的代码的最新版本在这里:https: //github.com/tartley/gloopy
It's billed as a 'library', but that was naive of me: It's a bunch of personal experimental code.
它被标榜为“库”,但这对我来说太天真了:这是一堆个人实验代码。
Nevertheless, it demonstrates that you can move around hundreds of bits of geometry at 60fps from Python.
尽管如此,它表明您可以从 Python 以 60fps 的速度移动数百位几何图形。
Although the demo above is fairly bare-bones in that it uses simply geometry and untextured faces, one thing I found is that more detailed geometry, texture mapping or other more modern graphics effects don't substantially affect the framerate. Or at least they don't affect it any worse than using the same effects in a C program. These are run on the GPU, so it doesn't make any difference at all if your program is written in Python.
虽然上面的演示相当简单,因为它使用简单的几何图形和无纹理的面,但我发现的一件事是更详细的几何图形、纹理映射或其他更现代的图形效果不会显着影响帧率。或者至少它们不会比在 C 程序中使用相同的效果更糟。它们在 GPU 上运行,因此如果您的程序是用 Python 编写的,则根本没有任何区别。
One thing that is performance-sensitive from Python is if you are creating dynamic geometry on the CPU side, e.g. moving individual vertices within a shape, by bending or melting the shape. Doing this sort of per-vertex calculation in Python, then constructing a new ctypes array from the result, then shunting this geometry to the GPU, every frame, will be slow. Instead you should probably be doing this in a vertex shader.
Python 对性能敏感的一件事是,如果您在 CPU 端创建动态几何图形,例如通过弯曲或熔化形状来移动形状内的单个顶点。在 Python 中进行这种逐顶点计算,然后根据结果构造一个新的 ctypes 数组,然后将此几何图形分流到 GPU,每一帧都会很慢。相反,您可能应该在顶点着色器中执行此操作。
On the other hand, if you just want affine transformations (moving objects around, rotating them, opening chests of drawers, rotating car wheels, bending a jointed robot arm) then all of this can be done by the GPU and the fact your program is written in Python makes little difference to the performance.
另一方面,如果你只想要仿射变换(移动物体,旋转它们,打开抽屉,旋转车轮,弯曲一个有关节的机器人手臂)那么所有这些都可以由 GPU 完成,事实上你的程序是用 Python 编写对性能几乎没有影响。
回答by Kylotan
I would recommend pygletwhich is a similar system to pygame, but with full bindings to OpenGL. You can start with simple 2D games to get the hang of the system and work up to 3D later. It is a more modern system than PyGame which is built around SDL which itself is a bit long in the tooth these days.
我会推荐pyglet,它是一个与 pygame 类似的系统,但与 OpenGL 完全绑定。您可以从简单的 2D 游戏开始,以掌握系统的窍门,然后再进行 3D 游戏。它是一个比 PyGame 更现代的系统,它是围绕 SDL 构建的,如今 SDL 本身有点长。
回答by Miguel Moll
You might want to check out Python-Ogre. I just messed with it myself, nothing serious, but seems pretty good.
您可能想查看Python-Ogre。我只是自己搞砸了,没什么大不了的,但看起来还不错。
回答by Peter Lillevold
Perhaps a wee bit off topic but, if your goal is to learn Python, how about creating a game using IronPythonand XNA? XNA is not OpenGL though, yet I find it an extremely simple 2D/3D engine which is fast and supports Shader Model 3.0.
也许有点离题,但是,如果您的目标是学习 Python,那么如何使用IronPython和XNA创建一个游戏呢?虽然 XNA 不是 OpenGL,但我发现它是一个非常简单的 2D/3D 引擎,速度很快并且支持 Shader Model 3.0。
回答by DzinX
Check out the Frets on Fireproject -- an open source Guitar Hero alternative. It's written in Python and has decent 3D graphics in OpenGL. I would suggest checking out its sourcesfor hints on libraries etc.
查看Frets on Fire项目——一个开源的吉他英雄替代品。它是用 Python 编写的,并且在 OpenGL 中具有不错的 3D 图形。我建议查看其来源以获取有关图书馆等的提示。
回答by Alex Boschmans
There was a Vampires game out a few years ago where most if not all of the code was in Python. Not sure if the 3D routines were in them, but it worked fine.
几年前有一个吸血鬼游戏,其中大部分(如果不是全部)代码都是用 Python 编写的。不确定其中是否包含 3D 例程,但效果很好。