Alpha values ignored in QOpenGLWidget



  • I'm trying to draw lines in a QOpenGLWidget. Here are my shaders:

    static const char *lineVertShaderSrc =
        "attribute highp vec4 posAttr;\n"
        "uniform highp mat4 matrix;\n"
        "void main() {\n"
        "   gl_Position = matrix * posAttr;\n"
        "}\n";
    
    static const char *lineFragShaderSrc =
        "void main() {\n"
        "   gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0);\n"
        "}\n";
    

    Even though I've set the alpha value on gl_FragColor to 1.0, when I move the window around I can see bits of the other windows behind it mixing in.

    Here's my GL state initialization immediately before the draw:

    	glEnable(GL_DEPTH_TEST);
    	glDepthMask(GL_TRUE);
    	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
    	glEnable(GL_BLEND);
    	glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
    	glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
    	glLineWidth(3);
    	glEnable(GL_LINE_SMOOTH);
    

    and here's the draw code itself:

        m_lineShader.program->bind();
    
        m_lineShader.program->setUniformValue(m_lineShader.matrixUniform, matrix);
        glVertexAttribPointer(m_lineShader.posAttr, 3, GL_FLOAT, GL_FALSE, 0, &m_grid.dimVerts[0][0]);
        glEnableVertexAttribArray(0);
        glDrawArrays(GL_LINES, 0, (int)m_grid.dimVerts.size());
        glDisableVertexAttribArray(0);
    
        m_lineShader.program->release();
    

    The lines all come out in the right places, so my handling of the vertex data is likely correct.

    If I draw a triangle using GL_TRIANGLES with vertex colors which are passed to the shaders as a varying attribute, it draws fine and has no problems with background windows showing through even though it's using the same GL state as the line draws.

    Any ideas as to what I might be doing wrong?

    EDIT: The draw code shown above is being called in my paintGL() override. The widget is double-buffered but I'm not setting which buffer I draw to, nor calling done() to swap the buffers because the docs for QOpenGLWidget says that Qt takes care of all of that for me. Perhaps I'm too trusting...

    EDIT: The problem was that glEnable(GL_LINE_SMOOTH) call. I don't need or want antialiasing on those lines and my blend mode is incompatible with it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.