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