opengl 为什么四边形不渲染?

3df52oht  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(129)

我使用PyOpenGL与PyQt5部件。在我的OpenGL widget中,我有以下代码:

class Renderizador(QOpenGLWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        self._x = -0.1
        self._y = -0.1
        self._z = -0.1
        self._rx = 45
        self._ry = 45
        self._rz = -45
        self.vertices_vertical = [[100, 100, 0], [100, -100, 0],
                                  [-100, -100, 0], [-100, 100, 0]]
        self.vertices_horizontal = [[100, 0, -100], [100, 0, 100],
                                    [-100, 0, 100], [-100, 0, -100]]

    def initializeGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glEnable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-1, 1, 1, -1, -15, 1)
        glTranslate(self._x, self._y, self._z)
        glRotate(self._rx, 1, 0, 0)
        glRotate(self._ry, 0, 1, 0)
        glRotate(self._rz, 0, 0, 0)

    def paintGL(self):
        glClearColor(1, 1, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

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

        # Axis lines
        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()

        # Plane drawing, does not work
        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)

为什么这两个平面没有渲染?我把背景色改成了白色,用了glClearColor,然后用了glClear函数,然后才这样做。我能看到轴线,但看不到我画的平面。

ruoxqz4g

ruoxqz4g1#

因为Blending,你不能“看到”飞机。
混合函数(参见glBlendFunc

glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR)

测量以下内容(R红色、G绿色、B蓝色、A阿尔法、s源、d目的地):

R = Rs * As + Rd * Rd
G = Gs * As + Gd * Gd
B = Bs * As + Bd * Bd
A = As * As + Ad * Ad

在您的示例中,源的alpha通道为0.6,目标颜色为用于清除帧缓冲区的颜色(1,1,1,1):

R = Rs * 0.6 + 1 * 1
G = Gs * 0.6 + 1 * 1
B = Bs * 0.6 + 1 * 1
A = As * 0.6 + 1 * 1

因此,在任何情况下,结果颜色都是白色,因为每个颜色通道的结果都大于1。

相关问题