Search code examples
python-2.7openglpyopengl

Unable to clear OpenGL viewport


I copied this script (pasted below) and ran it. Unfortunately, the image appears scrambled, as below. Can anyone help me get rid of that?

Config:

  • Ubuntu 13.10
  • Pyopengl 3.0.1b1
  • graphic card AMD Radeon HD 6650M
  • OpenGL version string: 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


Solution

  • Move, glClear to the paint method:

    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()
    

    Also here:

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

    you don't need glClear here. Produces:

    enter image description here