Search code examples
qtopengltransparencyalphablendingmultisampling

Transparent Shader in QGLWidget leads to transparent background (see through)


I have a QGLWidget:

GlWidget::GlWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)

For antialiasing I resently implemented the Samplebuffer.

For some transparent meshes I used the GL_BLEND function:

void GlWidget::initializeGL()
{
...
glEnable(GL_MULTISAMPLE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

My widget also have a background painted in the painGL() function:

void GlWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glColor4f(0.67,0.67,0.75,1);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glColor4f(0.2,0.2,0.2,1);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();

// other object drawing stuff
...
}

The problem I have is the windows get transparent where the transparent meshes are rendered.

I want the BLEND to be applied to the colored background I drew.

I tried to clear the widget with none transparent white before drawing things but this did not help.

See this picture for more explanation: https://i.sstatic.net/AZx3f.png

The problem did not appear before I implemented the SampleBuffers.


Solution

  • I searched for hours and the simple solution is:

    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);