无法清除OpenGL视口

时间:2014-07-07 19:06:35

标签: python-2.7 opengl pyopengl

我复制了this script (pasted below)并运行了它。不幸的是,图像出现乱码,如下所示。任何人都可以帮我摆脱它吗?

配置:

  • Ubuntu 13.10
  • Pyopengl 3.0.1b1
  • 显卡AMD Radeon HD 6650M
  • OpenGL版本字符串:3.0 Mesa 10.3.0-devel(git-32c5544 saucy-oibaf-ppa)

    from OpenGL.GL import *
    from PyQt4 import QtGui
    from PyQt4.QtOpenGL import *
    
        class WfWidget(QGLWidget):
            def __init__(self, parent = None):
                super(WfWidget, self).__init__(parent)
    
            def paintGL(self):
                glClearColor(0,0,0,0)
                glColor3f(0.0, 0.0, 1.0)
                glRectf(-5, -5, 5, 5)
                glColor3f(1.0, 0.0, 0.0)
                glBegin(GL_LINES)
                glVertex3f(0, 0, 0)
                glVertex3f(20, 20, 0)
                glEnd()
    
            def resizeGL(self, w, h):
                glMatrixMode(GL_PROJECTION)
                glLoadIdentity()
                glOrtho(-50, 50, -50, 50, -50.0, 50.0)
                glViewport(0, 0, w, h)
    
            def initializeGL(self):
                glClearColor(0.0, 0.0, 0.0, 1.0)
                glClear(GL_COLOR_BUFFER_BIT)
    
    if __name__ == '__main__':
        app = QtGui.QApplication(["Winfred's PyQt OpenGL"])
        widget = WfWidget()
        widget.show()
        app.exec_()
    

enter image description here

1 个答案:

答案 0 :(得分:3)

glClear移至paint方法:

def paintGL(self):
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0.0, 0.0, 1.0)
    glRectf(-5, -5, 5, 5)
    glColor3f(1.0, 0.0, 0.0)
    glBegin(GL_LINES)
    glVertex3f(0, 0, 0)
    glVertex3f(20, 20, 0)
    glEnd()

同样在这里:

    def initializeGL(self):
        glColor3f(0.0, 0.0, 1.0)

这里你不需要glClear。 生产:

enter image description here