[SOLVED] Use glBlendFunc in QOpenGLWidget

Issue

I’m trying to use glBlendFunc in QOpenGLWidget (in paintGL), but objects do not mix (alpha is works).

My code:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);

glBlendFunc(blenFunc, GL_ONE);

m_world.setToIdentity();
m_world.rotate((m_xRot / 16.0f), 1, 0, 0);
m_world.rotate(m_yRot / 16.0f, 0, 1, 0);
m_world.rotate(m_zRot / 16.0f, 0, 0, 1);

QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_program->bind();
m_tex->bind();
fillYoffsetLightning();

const GLfloat scaleFactor = 0.05f;
m_world.scale(scaleFactor, scaleFactor, 0.0f);
m_world.translate(0.f, 0.0f, 0.0f);

const GLfloat fact = 1 / scaleFactor;
const uint8_t X = 0, Y = 1;

for(int i = 0; i < maxElem; ++i) {
    const GLfloat offX = m_ELECT[i][X] * fact;
    const GLfloat offY = m_ELECT[i][Y] * fact;
    m_world.translate(offX, offY);

    m_program->setUniformValue(m_projMatrixLoc, m_proj);
    m_program->setUniformValue(m_mvMatrixLoc, m_camera * m_world);
    QMatrix3x3 normalMatrix = m_world.normalMatrix();
    m_program->setUniformValue(m_normalMatrixLoc, normalMatrix);

glDrawArrays(GL_TRIANGLE_FAN, 0, m_logo.vertexCount());

    update();
    m_world.translate(-offX, -offY);
}

m_program->release();

shaders are simple:

// vertex
"attribute highp vec4 color;\n"
"varying highp vec4 colorVertex;\n"
//......... main:
"colorVertex = color;\n"

// fragment
"varying highp vec4 colorVertex;\n"
//......... main:
"gl_FragColor = colorVertex;\n"

Color is:
a pentagon with a gradient from white from center to blue to the edges is drawn (center color is 1,1,1, edges is 0,0,0.5)

screenshoot

Why is this happening?

Solution

If you want to achieve a blending effect, the you have to disable the depth test:

glDisable(GL_DEPTH_TEST);

Note, the default depth test function is GL_LESS. If a fragment is draw on a place of a previous fragment, then it is discarded by the depth test, because this condition is not full filled.
If the depth test is disabled, then the fragments are “blended” by the blending function (glBlendFunc) and equation (glBlendEquation).

I recommend to use the following blending function:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Answered By – Rabbid76

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *