Skip to content

Commit

Permalink
qgsmesh3dmaterial: Ensure to create valid textures for the fragment s…
Browse files Browse the repository at this point in the history
…hader

In the shader code, all the parameters need to be properly initialized
even if they are not used. Otherwise, this can create undefined
behaviors and some missing meshes in a 3d scene. In the current code,
there are two parameters which may not be properly initialized:
- "colorRampTexture" is not set if the color ramp mode is not set
- "arrowsGridTexture" texture image is invalid if the "display arrows"
  mode is disabled

The first issue ("colorRampTexture") is solved by creating a dummy
valid texture image if the color ramp mode is disabled and setting the
"colorRampTexture" parameter in all cases. This does not change
anything in the shader code. The parameter is not used. The shader
only needs a valid parameter.

The second issue ("arrowsGridTexture") is solved by setting proper
default values for the parameters which generate the arrows grid
texture. This ensure to generate a valid texture image . This also
does not change anything in the shader code. The parameter is not
used. The shader only needs a valid parameter.
  • Loading branch information
ptitjano authored and nyalldawson committed Sep 29, 2023
1 parent 8c57166 commit ec6e480
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/3d/mesh/qgsmesh3dmaterial_p.cpp
Expand Up @@ -132,10 +132,9 @@ QgsMesh3dMaterial::QgsMesh3dMaterial( QgsMeshLayer *layer,
void QgsMesh3dMaterial::configure()
{
// Create the texture to pass the color ramp
Qt3DRender::QTexture1D *colorRampTexture = nullptr;
Qt3DRender::QTexture1D *colorRampTexture = new Qt3DRender::QTexture1D( this );
if ( mSymbol->colorRampShader().colorRampItemList().count() > 0 )
{
colorRampTexture = new Qt3DRender::QTexture1D( this );
switch ( mMagnitudeType )
{
case QgsMesh3dMaterial::ZValue:
Expand All @@ -148,9 +147,15 @@ void QgsMesh3dMaterial::configure()
break;
}

colorRampTexture->setMinificationFilter( Qt3DRender::QTexture1D::Linear );
colorRampTexture->setMagnificationFilter( Qt3DRender::QTexture1D::Linear );
}
else
{
// create a simple texture to have a valid parameter in the shader and avoid undefined behaviors
colorRampTexture->addTextureImage( new QgsColorRampTexture( QgsColorRampShader(), 1 ) );
}

colorRampTexture->setMinificationFilter( Qt3DRender::QTexture1D::Linear );
colorRampTexture->setMagnificationFilter( Qt3DRender::QTexture1D::Linear );

// Create and configure technique
mTechnique = new Qt3DRender::QTechnique();
Expand Down Expand Up @@ -184,8 +189,7 @@ void QgsMesh3dMaterial::configure()
mTechnique->addParameter( new Qt3DRender::QParameter( "lineColor", QVector4D( wireframecolor.redF(), wireframecolor.greenF(), wireframecolor.blueF(), 1.0f ) ) );
mTechnique->addParameter( new Qt3DRender::QParameter( "wireframeEnabled", mSymbol->wireframeEnabled() ) );
mTechnique->addParameter( new Qt3DRender::QParameter( "textureType", int( mSymbol->renderingStyle() ) ) );
if ( colorRampTexture )
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampTexture", colorRampTexture ) ) ;
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampTexture", colorRampTexture ) ) ;
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampCount", mSymbol->colorRampShader().colorRampItemList().count() ) );
const int colorRampType = mSymbol->colorRampShader().colorRampType();
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampType", colorRampType ) );
Expand All @@ -203,8 +207,8 @@ void QgsMesh3dMaterial::configureArrows( QgsMeshLayer *layer, const QgsDateTimeR
if ( layer )
datasetIndex = layer->activeVectorDatasetAtTime( timeRange );

QVector<QgsVector> vectors;
QSize gridSize;
QVector<QgsVector> vectors( 1 );
QSize gridSize( 1, 1 );
QgsPointXY minCorner;
std::unique_ptr< Qt3DRender::QParameter > arrowsEnabledParameter = std::make_unique< Qt3DRender::QParameter >( "arrowsEnabled", nullptr );
if ( !layer || mMagnitudeType != MagnitudeType::ScalarDataSet || !mSymbol->arrowsEnabled() || meta.isScalar() || !datasetIndex.isValid() )
Expand Down

0 comments on commit ec6e480

Please sign in to comment.