更改 PyOpenGL 小部件的透视图

Change perspective of a PyOpenGL widget

我正在尝试在 PyQt5 OpenGL 小部件中更改场景的视角。我知道我必须覆盖一些方法,但我不知道我应该使用哪个。

def initializeGL(self):
    glClear(GL_COLOR_BUFFER_BIT)
    glEnable(GL_DEPTH_TEST)

def paintGL(self):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glBegin(GL_LINES)
    # More code

我应该在哪里使用glOrtho功能?我在哪里可以找到有关覆盖此方法的信息? 当我去声明这个方法时,它们只有一个 pass 语句,没有别的,它们是如何以及何时执行的?我应该使用 QPainter 而不是 OpenGL 吗?

    def __init__(self, parent=None):
        super().__init__(parent)
        self._x = 0
        self._y = -0.3
        self._z = 0.5
        self._rz = 0
        self._ry = -0.5
        self.vertices_vertical = [[1000, 1000, 000], [1000, -1000, 000],
                                  [-1000, -1000, 000], [-1000, 1000, 000]]
        self.vertices_horizontal = [[1000, 000, -1000], [1000, 000, 1000],
                                    [-1000, 000, 1000], [-1000, 000, -1000]]
    def initializeGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glEnable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, 1000, 750, 0, -1, 1)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glBegin(GL_LINES)

        glColor3d(1, 0, 0)
        glVertex3d(0, 0, 0)
        glVertex3d(1, 0, 0)

        glColor3d(0, 1, 0)
        glVertex3d(0, 0, 0)
        glVertex3d(0, 1, 0)

        glColor3d(0, 0, 1)
        glVertex3d(0, 0, 0)
        glVertex3d(0, 0, 1)

        glEnd()
        # glLoadIdentity()

        glTranslate(self._x, self._y, self._z)
        glRotate(self._ry, 0, 1, 0)
        glRotate(self._rz, 0, 0, 1)

        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR)
        glBegin(GL_QUADS)
        glColor4fv((0, 1, 0, 0.6))
        for vertex in range(4):
            glVertex3fv(self.vertices_vertical[vertex])
        glColor4fv((1, 0, 0, 0.6))
        for vertex in range(4):
            glVertex3fv(self.vertices_horizontal[vertex])
        glEnd()
        glDisable(GL_BLEND)

Legacy OpenGL the current matrix is a global state. There are different kind of matrices, for each kind of matrix exists a matrix stack. The top of the matrix stack is the current matrix. The matrix stack which is the target for subsequent matrix operations like glOrtho, glPushMatrix/glPopMatrix, glLoadIdentity,etc. can be chosen by glMatrixMode.

每个顶点坐标(glVertex)由模型视图矩阵(GL_MODELVIEW)和投影矩阵(GL_PROJECTION)变换。

initializeGL中选择投影矩阵模式。设置 Identity matrix and set the orthographic projection by glOrtho. Note, glOrtho dose not only set a matrix, it defines a Orthographic projection 矩阵并将当前矩阵乘以新矩阵。

例如设置正射投影,将 "window" 坐标转换为标准化设备 space。下面的(windowdWidth,windowHeight)是window的大小:

def initializeGL(self):
    glClear(GL_COLOR_BUFFER_BIT)
    glEnable(GL_DEPTH_TEST)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, windowdWidth, windowHeight, 0, -1, 1) 

在绘制模型之前,使用模型视图矩阵模式设置模型视图变换:

def paintGL(self):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    # model view transformations
    # [...]

    glBegin(GL_LINES)
    # [...]
    glEnd()