关于python 3.x:每个OpenGL函数的PyQt5 QWindow + PyOpenGL错误1282’无效操作’

PyQt5 QWindow + PyOpenGL error 1282 'invalid operation' with every OpenGL function

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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函数,它们都会引发以下异常:

1
2
3
4
5
6
7
  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。

这是一个工作示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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_()


我只能提供一种解决方法。

我也可以在Windows上重现该问题。 glClearColor调用不是问题。 该错误发生在context.create()self.create()中,但是pyOpenGL仅在第一次调用后检查错误时才注意到它。

我不知道是什么真正导致了问题,但如果我只是忽略该错误,它就可以正常工作。 在__init__的末尾添加:

1
2
    self.context.makeCurrent(self)
    GL.glGetError()  # Ignore openGL error if one occured