python-3.x - PyQt5 QWindow + PyOpenGL 错误 1282 'invalid operation' 与每个 OpenGL 函数

标签 python-3.x pyopengl pyqt5

我已经没有想法了,我需要一些帮助。考虑以下代码段(修改后的 http://www.riverbankcomputing.com/pipermail/pyqt/2014-July/034542.html ):

from OpenGL import GL
from PyQt5 import Qt

class GLWindow(Qt.QWindow):
    def __init__(self):
        super().__init__()

        self.setSurfaceType(Qt.QWindow.OpenGLSurface)

        self.context = Qt.QOpenGLContext()
        self.context.setFormat(self.requestedFormat())
        if not self.context.create():
            raise Exception('self.context.create() failed')
        self.create()

    def exposeEvent(self, ev):
        ev.accept()
        if self.isExposed() and self.isVisible():
            self.update()

    def update(self):
        self.context.makeCurrent(self)
        GL.glClearColor(1.0, 0.0, 0.0, 0.0)
        GL.glClearDepth(1)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glFlush()
        self.context.swapBuffers(self)

app = Qt.QApplication([])
win = GLWindow()
widget = Qt.QWidget.createWindowContainer(win, None, Qt.Qt.Widget)
widget.show()
app.exec_()

无论我在 makeCurrent() 之后调用什么 OpenGL 函数,它们都会引发以下异常:
  File "errorchecker.pyx", line 53, in OpenGL_accelerate.errorchecker._ErrorChecker.glCheckError (src\errorchecker.c:1218)
OpenGL.error.GLError: GLError(
        err = 1282,
        description = b'nieprawid\xb3owa operacja',
        baseOperation = glClearColor,
        cArguments = (1.0, 0.0, 0.0, 0.0)
)

此外,除了 openglwindow.py 之外,没有一个 PyQt5 OpenGL 示例可以工作。

我使用的是 Python 3.4.2 win32、PyQt5 5.3.2 和 PyOpenGL 3.1.0。有任何想法吗?我发现 PyQt5 二进制文件可能是针对 OpenGL ES 构建的,但我不知道在使用 PyOpenGL 调用时这是否重要。

最佳答案

您应该使用 QtOpenGL.QGLWidgetQt.QWindow 未使用 OpenGL。

这是一个工作示例:

import struct

from PyQt5 import QtOpenGL, QtWidgets

import ModernGL


class QGLControllerWidget(QtOpenGL.QGLWidget):
    def __init__(self):
        fmt = QtOpenGL.QGLFormat()
        fmt.setVersion(3, 3)
        fmt.setProfile(QtOpenGL.QGLFormat.CoreProfile)
        fmt.setSampleBuffers(True)
        super(QGLControllerWidget, self).__init__(fmt, None)

    def initializeGL(self):
        self.ctx = ModernGL.create_context()

        prog = self.ctx.program([
            self.ctx.vertex_shader('''
                #version 330

                in vec2 vert;

                void main() {
                    gl_Position = vec4(vert, 0.0, 1.0);
                }
            '''),
            self.ctx.fragment_shader('''
                #version 330

                out vec4 color;

                void main() {
                    color = vec4(0.30, 0.50, 1.00, 1.0);
                }
            '''),
        ])

        vbo = self.ctx.buffer(struct.pack('6f', 0.0, 0.8, -0.6, -0.8, 0.6, -0.8))
        self.vao = self.ctx.simple_vertex_array(prog, vbo, ['vert'])

    def paintGL(self):
        self.ctx.viewport = (0, 0, self.width(), self.height())
        self.ctx.clear(0.9, 0.9, 0.9)
        self.vao.render()
        self.ctx.finish()


app = QtWidgets.QApplication([])
window = QGLControllerWidget()
window.move(QtWidgets.QDesktopWidget().rect().center() - window.rect().center())
window.show()
app.exec_()

关于python-3.x - PyQt5 QWindow + PyOpenGL 错误 1282 'invalid operation' 与每个 OpenGL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27407739/

相关文章:

python - 如何提取beautifulsoup中最后一段标签文本?

python - 如何添加随机颜色?

python - 如何使用表单上的按钮向 QTabWidget 添加选项卡?

python - 如何将 python csv.DictReader 与二进制文件一起使用? (对于一个babel自定义提取方法)

python-3.x - 如何修复迭代 pandas 分组 df 时的 ValueError?

python - 循环修改 OpenGL 小部件的按键输入

Python 从 float 组到纹理

python - 从 PyQt GUI 连接到串口

python - qlabel 不能触发鼠标释放事件

python-3.x - Pandas Groupby 结果中的列