1

I m trying to build a gui with Pyqt5. and withing the gui there is an openGLwidget, that should contain a rotating cube. But i cannot figure out how to make the cube rotate. Thank you. this is the setup function

def setupUI(self):
    self.openGLWidget.initializeGL()
    self.openGLWidget.resizeGL(651,551)
    self.openGLWidget.paintGL = self.paintGL
    self.rotX=10.0
    self.rotY=0.0
    self.rotZ=0.0
    timer = QTimer(self)
    timer.timeout.connect(self.Update) 
    timer.start(1000)

and here is the paintGL and update functions:

def paintGL(self):
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0,1,0)
    self.Cube()
    gluPerspective(45, 651/551, 1, 100.0)
    glTranslatef(0.0,0.0, -5)
    glRotate(self.rotX, 1.0, 0.0, 0.0)
def Update(self):
    glClear(GL_COLOR_BUFFER_BIT)
    self.rotX+=10
    self.openGLWidget.paintGL = self.paintGL

1 Answer 1

2

There are different current matrices, see glMatrixMode. The projection matrix should be set to the current GL_PROJECTION and the model view matrix to GL_MODELVIEW.
The operations which manipulate the current matrix (like gluPerspective, glTranslate, glRotate), do not just set a matrix, they specify a matrix and multiply the current matrix by the new matrix. Thus you have to set the Identity matrix at the begin of every frame, by glLoadIdentity:

def paintGL(self):
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0,1,0)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 651/551, 1, 100.0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glTranslatef(0, 0, -7)
    glRotate(self.rotX, 1, 0, 0)
    self.Cube()

Invoke update() to update respectively repaint a QOpenGLWidget:

timer = QTimer(self)
timer.timeout.connect(self.Update) 
timer.start(10)
def Update(self):
    self.rotX += 1
    self.openGLWidget.update()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.