5

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

Megidd
  • 7,089
  • 6
  • 65
  • 142

2 Answers2

2

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

Eddy Alleman
  • 1,096
  • 2
  • 10
  • 21
  • It worked =) I did `setSourceAlpha` to `Zero` and did `setDestinationAlpha` to `One`. Now transparency works on gray background. I have no idea what's going on with these settings though! – Megidd Aug 15 '19 at 08:45
1

Try not using blendequation, seems like the color computed by blendstate is added to the gray background. (maybe good for got particle sparks, less so for objects)

How does it look with black background?

Macke
  • 24,812
  • 7
  • 82
  • 118
  • Wow! Thanks =) with black background the object looks fine. Do you know how to make it work with any background color? – Megidd Aug 14 '19 at 08:24
  • Remove the blendequation state? I'm not an expert on Qt3D, but I know my OpenGL. :) – Macke Aug 14 '19 at 13:01
  • Thanks! if I remove that, I guess OpenGL would use it's default blend function/equation. Right? – Megidd Aug 14 '19 at 13:33
  • Yes. The default is to replace the color in the framebuffer with that what your blend state computes. So, since you already mix it with the data, there, you don't want to do another add. – Macke Aug 15 '19 at 08:28