pythonopenglpyopenglopengl-compat

Why the quads do not render?


Im using PyOpenGL with PyQt5 widgets. In my OpenGL widget I have the following code:

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)

Why the two planes are not rendered? I changed the background color to white, using the glClearColor and then a glClear functions before of doing that It worked. I can see the axis lines, but not the planes I draw.


Solution

  • You can`t "see" the planes because of the Blending.

    The blend function (see glBlendFunc)

    glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR)
    

    meas the following (R red, G green, B blue, A alphas, s source, d destintion) :

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

    In your case the alpha channel of the source is 0.6 and the destination color is the color which was used to clear the framebuffer (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
    

    So the result color is white in any case, because the result for each color channel is grater than 1.