使用 Python 的 OpenGL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/242059/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:43:15  来源:igfitidea点击:

OpenGl with Python

pythonopenglfedora

提问by corymathews

I am currently in a course that is using OpenGL and I have been using C for all the programs so far. I have Python installed on Fedora as well as OpenGL, however the minute I call an OpenGL command in my Python code, I get a segmentation fault. I have no idea why this is.

我目前正在学习一门使用 OpenGL 的课程,到目前为止,我一直在为所有程序使用 C。我在 Fedora 和 OpenGL 上安装了 Python,但是在我在 Python 代码中调用 OpenGL 命令的那一刻,我得到了一个段错误。我不知道这是为什么。

Just to avoid the "just use C" comments, here is why I want to use Python:

只是为了避免“只使用 C”注释,这就是我想使用 Python 的原因:

There are a couple reasons I am wanting to switch from C to Python, but the main one is because we are about to start writing a raytracer and I would like to use classes to make it easier on me. Since I hate classes in C++ and structs in C seems a little crazy, I thought I would give Python a try at it. I have also been looking for a reason to use Python again as it has been a while.

我想从 C 切换到 Python 有几个原因,但主要是因为我们即将开始编写光线跟踪器,我想使用类来简化我的工作。由于我讨厌 C++ 中的类,而 C 中的结构似乎有点疯狂,我想我会尝试一下 Python。我也一直在寻找再次使用 Python 的理由,因为它已经有一段时间了。

Thanks for any help.

谢谢你的帮助。

回答by technomalogical

You may also want to consider using Pygletinstead of PyOpenGL. It's a ctypes-wrapper around the native OpenGL libs on the local platform, along with windowing support (should handle most of the stuff you want to use GLUT for.) The pyglet-userslist is pretty active and very helpful.

您可能还想考虑使用Pyglet而不是 PyOpenGL。它是一个围绕本地平台上的原生 OpenGL 库的 ctypes 包装器,以及窗口支持(应该处理您想要使用 GLUT 的大部分内容。)pyglet-users列表非常活跃并且非常有用。

回答by Adam Luchjenbroers

Well, I don't know if these are the libs the original poster are using but I saw identical issues in a pet project I'm working on (Graphics Engine using C++ and Python) using PyOpenGL.

好吧,我不知道这些是否是原始海报使用的库,但我在使用 PyOpenGL 的宠物项目(使用 C++ 和 Python 的图形引擎)中看到了相同的问题。

PyOpenGL didn't correctly pick up the rendering context if it was created after the python script had been loaded (I was loading the script first, then calling Python methods in it from my C++ code).

如果在加载 python 脚本之后创建了渲染上下文,PyOpenGL 无法正确获取渲染上下文(我首先加载脚本,然后从我的 C++ 代码中调用其中的 Python 方法)。

The problem doesn't appear if you initialize the display and create the OpenGL rendering context before loading the Python script.

如果在加载 Python 脚本之前初始化显示并创建 OpenGL 渲染上下文,则不会出现此问题。

回答by Cody Brocious

What OpenGL library are you using? What windowing library? What version of Python?

您使用的是什么 OpenGL 库?什么窗口库?什么版本的Python?

Most likely cause I can think of is that your windowing library (SDL or whatever you're using) isn't initializing OpenGL before you start calling into it.

我能想到的最可能的原因是您的窗口库(SDL 或您使用的任何东西)在您开始调用它之前没有初始化 OpenGL。

回答by Cheery

We have neither ideas about random segmentation faults. There is not enough information. What python libraries are you using for opengl? How do you use them? Can you show us your code? It's probably something trivial but my god -skill ends up to telling me just and only that.

我们对随机分段错误没有任何想法。没有足够的信息。你为 opengl 使用了哪些 python 库?你如何使用它们?你能告诉我们你的代码吗?这可能是一些微不足道的事情,但我的上帝技能最终只会告诉我这些。

Raytracer in python? I'd prefer just doing that in C with those structs. But then, I'm assuming you aren't going to do a realtime raytracer so that may be ok.

python中的光线追踪器?我更喜欢在 C 中使用这些结构来做这件事。但是,我假设你不会做实时光线追踪器,所以这可能没问题。

回答by unwind

Perhaps you are calling an OpenGL function that requires an active OpenGL context, without having one? That shouldn't necessarily crash, but I guess it might. How to set up such a context depends on the platform, and it's been a while since I used GL from Python (and when I did, I also used GTK+ which complicates matters).

也许您正在调用一个需要活动 OpenGL 上下文的 OpenGL 函数,而没有一个?这不一定会崩溃,但我想可能会。如何设置这样的上下文取决于平台,自从我使用 Python 中的 GL 已经有一段时间了(当我这样做时,我还使用了 GTK+,这使问题变得复杂)。

回答by C?t?lin George Fe?til?

Scripts never cause segmentation faults. But first see if your kernel and kmod video driver working property ... Extension modules can cause "segmentation fault".

脚本永远不会导致分段错误。但首先看看您的内核和 kmod 视频驱动程序是否工作属性...扩展模块会导致“分段错误”。