为什么glRotate只在我没有为openGLWidget创建额外类的情况下工作一次?

2024-09-30 16:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个GUI,用于使用Python显示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()时增加,我希望立方体会旋转,但由于某些原因它不会


Tags: importselfobjectdefasglrotationqtwidgets
1条回答
网友
1楼 · 发布于 2024-09-30 16:32:11

问题是计时器事件:

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

不建议直接调用.paintGL。注意,OpenGL不能绘制任何东西,OpenGL Context必须在绘制之前和之后成为当前的,窗口必须更新。您可以看到初始图形,因为对.paintGL的第一次调用是由框架完成的。以下由计时器事件发出的调用不会生成有效的输出。 您必须调用^{},这使上下文成为当前上下文,并触发.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()

动画是由您的原始代码生成的,只是我通过^{}切换到了线条绘制模式

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

相关问题 更多 >