为什么glRotate只工作一次,而我没有为openGLWidget创建额外的类?

cld4siwp  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(184)

我正在使用Python编写一个GUI,用于显示6轴运动传感器的输出。窗口应该有一个OpenGL小部件,用于实时显示旋转。通常我会为我的openGLWidget编写一个单独的类,但由于我的UI有很多元素,从头开始构建它似乎不太合理。相反,我只是加载.ui文件,找到我需要的所有内容,就这样。然而,glRotate()函数只更新了一次,然后再也不更新了,我不知道是什么原因导致了这个问题。

from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import *
import OpenGL.GL as gl
import OpenGL.GLU as glu
import sys

class Ui(QtWidgets.QWidget):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('gl_test.ui', self)

        self.openGLWidget = self.findChild(QtWidgets.QOpenGLWidget, 'openGLWidget')
        self.openGLWidget.initializeGL()
        self.openGLWidget.paintGL = self.paintGL
        self.openGLWidget.initializeGL = self.initializeGL
        self.object = None

        self.rotation = 45.0

#       QTimer for updating the GL Viewport
        self.GLtimer = QTimer()
        self.GLtimer.setInterval(100)
        self.GLtimer.timeout.connect(self.openGLWidget.paintGL)
        self.GLtimer.start()

    def paintGL(self):
        try:
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
            gl.glMatrixMode(gl.GL_PROJECTION)
            gl.glLoadIdentity()
            x, y, width, height = gl.glGetDoublev(gl.GL_VIEWPORT)
            glu.gluPerspective(
                45,  # field of view in degrees
                width / float(height or 1),  # aspect ratio
                .25,  # near clipping plane
                200,  # far clipping plane
            )
            gl.glMatrixMode(gl.GL_MODELVIEW)
            gl.glLoadIdentity()
            gl.glTranslated(0.0, 0.0, -10.0)
            self.rotation += 1
            print(self.rotation)
            gl.glRotate(self.rotation, 1, 0, 0)  # works only once for some reason?
            gl.glCallList(self.object)
        except Exception as exc:
            print(exc)
            pass

    def initializeGL(self):
        try:
            # making an example cube
            self.object = gl.glGenLists(1)
            gl.glNewList(self.object, gl.GL_COMPILE)
            gl.glBegin(gl.GL_QUADS)

            gl.glColor3f(0.0, 1.0, 0.0)
            gl.glVertex3f(1.0, 1.0, -1.0)
            gl.glVertex3f(-1.0, 1.0, -1.0)
            gl.glVertex3f(-1.0, 1.0, 1.0)
            gl.glVertex3f(1.0, 1.0, 1.0)

            gl.glColor3f(0.0, 1.0, 0.0)
            gl.glVertex3f(1.0, -1.0, 1.0)
            gl.glVertex3f(-1.0, -1.0, 1.0)
            gl.glVertex3f(-1.0, -1.0, -1.0)
            gl.glVertex3f(1.0, -1.0, -1.0)

            gl.glColor3f(0.0, 1.0, 0.0)
            gl.glVertex3f(1.0, 1.0, 1.0)
            gl.glVertex3f(-1.0, 1.0, 1.0)
            gl.glVertex3f(-1.0, -1.0, 1.0)
            gl.glVertex3f(1.0, -1.0, 1.0)

            gl.glColor3f(0.0, 1.0, 0.0)
            gl.glVertex3f(1.0, -1.0, -1.0)
            gl.glVertex3f(-1.0, -1.0, -1.0)
            gl.glVertex3f(-1.0, 1.0, -1.0)
            gl.glVertex3f(1.0, 1.0, -1.0)

            gl.glColor3f(0.0, 1.0, 0.0)
            gl.glVertex3f(-1.0, 1.0, 1.0)
            gl.glVertex3f(-1.0, 1.0, -1.0)
            gl.glVertex3f(-1.0, -1.0, -1.0)
            gl.glVertex3f(-1.0, -1.0, 1.0)

            gl.glColor3f(0.0, 1.0, 0.0)
            gl.glVertex3f(1.0, 1.0, -1.0)
            gl.glVertex3f(1.0, 1.0, 1.0)
            gl.glVertex3f(1.0, -1.0, 1.0)
            gl.glVertex3f(1.0, -1.0, -1.0)

            gl.glEnd()
            gl.glEndList()

            gl.glEnable(gl.GL_CULL_FACE)
        except Exception as exc:
            print(exc)
            pass

def main():
    app = QtWidgets.QApplication(sys.argv)
    window = Ui()
    window.show()
    app.exec_()

main()

.ui file code
这是不起作用的部分。随着self.rotation每次paintGl()glRotate()被调用时都增加,我希望立方体旋转,但由于某种原因它没有旋转。

yhqotfr8

yhqotfr81#

问题在于计时器事件:

self.GLtimer.timeout.connect(self.openGLWidget.paintGL)

不建议直接调用.paintGL。注意,OpenGL不能绘制任何东西,绘制前后必须将OpenGL Context设置为当前,窗口必须更新。您可以看到初始绘图,因为第一次调用.paintGL是由框架完成的。以下调用,由定时器事件产生的未生成有效输出。您必须调用.update(),这将使上下文成为当前上下文并触发要调用的.paintGL(),例如:

class Ui(QtWidgets.QWidget):
    def __init__(self):

        # [...]

        self.GLtimer = QTimer()
        self.GLtimer.setInterval(100)
        self.GLtimer.timeout.connect(self.redraw) 
        self.GLtimer.start()

    def redraw(self):
        # updated the widget - triggers paintGL to be called
        self.openGLWidget.update()

动画是由你的原始代码生成的,除了我用glPolygonMode切换到了画线模式。

gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)

相关问题