Commit b80febc0 authored by Paul Lemire's avatar Paul Lemire
Browse files

Scene3D: only show SGNode once Qt3D has first rendered into it



Avoids seeing garbage for the first frame (with planets-qml)

Change-Id: I3445ce47f27bb921c15861e580a8cc6c7d8cc645
Reviewed-by: Mike Krus's avatarMike Krus <mike.krus@kdab.com>
parent 0ff2215b
......@@ -419,6 +419,10 @@ void Scene3DRenderer::render()
// Restore QtQuick FBO
QOpenGLFramebufferObject::bindDefault();
// Only show the node once Qt3D has rendered to it
// Avoids showing garbage on the first frame
m_node->show();
}
// Reset the state used by the Qt Quick scenegraph to avoid any
......
......@@ -60,6 +60,7 @@ namespace Qt3DRender {
Scene3DSGMaterial::Scene3DSGMaterial()
: QSGMaterial()
, m_texture(nullptr)
, m_visible(false)
{
}
......
......@@ -75,8 +75,12 @@ public:
QSGMaterialType *type() const final { return &Scene3DSGMaterialShader::type; }
QSGMaterialShader *createShader() const final { return new Scene3DSGMaterialShader(); }
void show() { m_visible = true; }
bool visible() const { return m_visible; }
private:
QSGTexture *m_texture;
bool m_visible;
};
} // namespace Qt3DRender
......
......@@ -118,21 +118,25 @@ const char *Scene3DSGMaterialShader::fragmentShader() const
if (ctx->format().version() >= qMakePair(3, 2) && ctx->format().profile() == QSurfaceFormat::CoreProfile) {
return ""
"#version 150 core \n"
"uniform bool visible; \n"
"uniform sampler2D source; \n"
"uniform float qt_Opacity; \n"
"in vec2 qt_TexCoord; \n"
"out vec4 fragColor; \n"
"void main() { \n"
" vec4 p = texture(source, qt_TexCoord); \n"
" if (!visible) discard; \n"
" vec4 p = texture(source, qt_TexCoord); \n"
" float a = qt_Opacity * p.a; \n"
" fragColor = vec4(p.rgb * a, a); \n"
"}";
} else {
return ""
"uniform bool visible; \n"
"uniform highp sampler2D source; \n"
"uniform highp float qt_Opacity; \n"
"varying highp vec2 qt_TexCoord; \n"
"void main() { \n"
" if (!visible) discard; \n"
" highp vec4 p = texture2D(source, qt_TexCoord); \n"
" highp float a = qt_Opacity * p.a; \n"
" gl_FragColor = vec4(p.rgb * a, a); \n"
......@@ -144,6 +148,7 @@ void Scene3DSGMaterialShader::initialize()
{
m_matrixId = program()->uniformLocation("qt_Matrix");
m_opacityId = program()->uniformLocation("qt_Opacity");
m_visibleId = program()->uniformLocation("visible");
}
void Scene3DSGMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
......@@ -172,6 +177,9 @@ void Scene3DSGMaterialShader::updateState(const RenderState &state, QSGMaterial
t->updateBindOptions();
}
if (oldTx == nullptr || oldTx->visible() != tx->visible())
program()->setUniformValue(m_visibleId, tx->visible());
if (state.isMatrixDirty())
program()->setUniformValue(m_matrixId, state.combinedMatrix());
......
......@@ -74,6 +74,7 @@ protected:
private:
int m_matrixId;
int m_opacityId;
int m_visibleId;
};
} // namespace Qt3DRender
......
......@@ -84,6 +84,12 @@ void Scene3DSGNode::setRect(const QRectF &rect, const QRectF textureRect)
}
}
void Scene3DSGNode::show()
{
m_material.show();
m_opaqueMaterial.show();
}
} // namespace Qt3DRender
QT_END_NAMESPACE
......@@ -78,6 +78,8 @@ public:
void setRect(const QRectF &rect, const QRectF textureRect = QRectF(0.0f, 1.0f, 1.0f, -1.0f));
QRectF rect() const Q_DECL_NOTHROW { return m_rect; }
void show();
private:
Scene3DSGMaterial m_material;
Scene3DSGMaterial m_opaqueMaterial;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment