Search code examples
c++qtopenglglslqt3d

Phong-Alpha material transparency


I'm using a pre-built material of Qt3D:

Qt3DRender::QMaterial *MyClass::createMaterial()
{
    Qt3DExtras::QPhongAlphaMaterial *mat = new Qt3DExtras::QPhongAlphaMaterial();
    mat->setAmbient(QColor("#576675"));
    mat->setDiffuse(QColor("#5F6E7D"));
    mat->setSpecular(QColor("#61707F"));
    mat->setShininess(0.0f);
    mat->setAlpha(0.5f);
    return mat;
}

I set alpha to 0.5f, so I expect the material be semi-transparent. But the model looks mostly white except some regions:

Result

When I check the source code I see this settings for alpha-blending:

m_blendState->setSourceRgb(QBlendEquationArguments::SourceAlpha);
m_blendState->setDestinationRgb(QBlendEquationArguments::OneMinusSourceAlpha);
m_blendEquation->setBlendFunction(QBlendEquation::Add);

I wonder why the model looks white?


As suggested by @Macke, the object on black background looks fine!

black background

When I set alpha to 1.0, I observe this:

alpha = 1.0


UPDATE

As pointed out by @Macke, one issue was related to depth test. On the source code, depth mask is disabled by default:

// ...
, m_noDepthMask(new QNoDepthMask())
// ...

m_phongAlphaGL3RenderPass->addRenderState(m_noDepthMask);

m_phongAlphaGL2RenderPass->addRenderState(m_noDepthMask);

m_phongAlphaES2RenderPass->addRenderState(m_noDepthMask);

I enabled depth mask by removing QNoDepthMask stuff, and now with alpha = 1.0 the rendering result is fine:

enable depth mask, alpha = 1.0


UPDATE

Suggested by @EddyAlleman, I added such lines of code:

blendState->setSourceAlpha(Qt3DRender::QBlendEquationArguments::Zero);
blendState->setDestinationAlpha(Qt3DRender::QBlendEquationArguments::One);

Then, the transparency (alpha = 0.4) is fine even on gray background:

alpha = 0.4


Solution

  • try this to set the blendequation state

    set sourceAlphaArg to Qt3DRender::QBlendEquationArguments::Zero
    set destinationAlphaArg to Qt3DRender::QBlendEquationArguments::One
    

    info from enum QBlendEquationArguments::Blending

    Constant Value OpenGL Qt3DRender::QBlendEquationArguments::Zero 0 GL_ZERO Qt3DRender::QBlendEquationArguments::One 1 GL_ONE

    EDIT: a good explanation can be found here: learnopengl.com/Advanced-OpenGL/Blending