加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-3.x – PyQt5 QWindow PyOpenGL错误1282’无效操作’与每

发布时间:2020-12-20 13:46:21 所属栏目:Python 来源:网络整理
导读:我已经没有想法了,我需要一些帮助.请考虑以下代码段(修改后的 http://www.riverbankcomputing.com/pipermail/pyqt/2014-July/034542.html): from OpenGL import GLfrom PyQt5 import Qtclass GLWindow(Qt.QWindow): def __init__(self): super().__init__()
我已经没有想法了,我需要一些帮助.请考虑以下代码段(修改后的 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)
        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 (srcerrorchecker.c:1218)
OpenGL.error.GLError: GLError(
        err = 1282,description = b'nieprawidxb3owa operacja',baSEOperation = glClearColor,cArguments = (1.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.QGLWidget. Qt.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,1.0);
                }
            '''),self.ctx.fragment_shader('''
                #version 330

                out vec4 color;

                void main() {
                    color = vec4(0.30,0.50,1.00,])

        vbo = self.ctx.buffer(struct.pack('6f',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,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_()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读