Qt3d blending
-
I am now using Qt3D to draw some 3D object. I draw an object with semi transparent effect with the following code,
m_pWorkspaceEntity = new Qt3DCore::QEntity(pRootEntity);auto cubeMesh12 = new Qt3DExtras::QCuboidMesh(); cubeMesh12->setXExtent(200); cubeMesh12->setYExtent(800); cubeMesh12->setZExtent(800); auto pWorkspaceMaterial = new Qt3DExtras::QDiffuseSpecularMaterial(); pWorkspaceMaterial->setDiffuse(QColor(255,0,0,200)); pWorkspaceMaterial->setAlphaBlendingEnabled(true); m_pWorkspaceTransform = new Qt3DCore::QTransform(); m_pWorkspaceTransform->setTranslation(QVector3D(500,0,0)); m_pWorkspaceEntity->addComponent(cubeMesh12); m_pWorkspaceEntity->addComponent(pWorkspaceMaterial); m_pWorkspaceEntity->addComponent(m_pWorkspaceTransform);As you can see, this cuboid is in semi trasnsparent state because QColor(255,0,0,200) is used. However, what I get is
(
The object behind the cuboid is totally displayed.
I have turn on the blending functionality of Qt3D in the framegrpah, as shown below,
auto pCullFaceRenderState = new Qt3DRender::QCullFace();
pCullFaceRenderState->setMode(Qt3DRender::QCullFace::Back);auto pFrontFaceRenderState = new Qt3DRender::QFrontFace(); pFrontFaceRenderState->setDirection(Qt3DRender::QFrontFace::CounterClockWise); auto PDepthTestRenderState = new Qt3DRender::QDepthTest(); PDepthTestRenderState->setDepthFunction(Qt3DRender::QDepthTest::Less); auto pBlendEquation = new Qt3DRender::QBlendEquation(); pBlendEquation->setBlendFunction(Qt3DRender::QBlendEquation::Add); auto pBlendEquationArguments = new Qt3DRender::QBlendEquationArguments(); pBlendEquationArguments->setSourceRgba(Qt3DRender::QBlendEquationArguments::SourceAlpha); pBlendEquationArguments->setDestinationRgba(Qt3DRender::QBlendEquationArguments::OneMinusSourceAlpha); pBlendEquationArguments->setBufferIndex(-1); //One viewport m_pSurfaceSelectorOneViewPort = new Qt3DRender::QRenderSurfaceSelector(); m_pSurfaceSelectorOneViewPort->setSurface(this); //Clear Buffer auto pClearBufferOneViewPort = new Qt3DRender::QClearBuffers(m_pSurfaceSelectorOneViewPort); auto PNoDrawOneViewPort = new Qt3DRender::QNoDraw(pClearBufferOneViewPort); pClearBufferOneViewPort->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer); pClearBufferOneViewPort->setClearColor(QColor(95,105,115));//change the background color //View LeftUp auto pViewPortLeftUpOneViewPort = new Qt3DRender::QViewport(m_pSurfaceSelectorOneViewPort); auto pCameraSelectorLeftUpOneViewPort = new Qt3DRender::QCameraSelector(pViewPortLeftUpOneViewPort); pViewPortLeftUpOneViewPort->setNormalizedRect(QRectF(0.0f,0.0f,1.0f,1.0f)); pCameraSelectorLeftUpOneViewPort->setCamera(m_pCameraLeftUp); auto PRenderStateSet= new Qt3DRender::QRenderStateSet(pCameraSelectorLeftUpOneViewPort); PRenderStateSet->addRenderState(pCullFaceRenderState); PRenderStateSet->addRenderState(pFrontFaceRenderState); PRenderStateSet->addRenderState(PDepthTestRenderState); PRenderStateSet->addRenderState(pBlendEquation); PRenderStateSet->addRenderState(pBlendEquationArguments);I have spent lots of time trying to solve this problem, but I still get no result.
Thanks for your help in advance. -
Are you setting your render surface selector to be a child of the active framegraph somewhere? If not, you have to do this.
E.g. if you are using a Qt3DWindow you can retrieve its render settings (which are only there to hold the active framegrahp) by calling
renderSettings(). Then you can set your custom framegraph (i.e. you only have to set your render surface selector, as that's the root node of your graph) using thesetActiveFramegraph(QFramegraphNode*)function.