Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec6e480

Browse files
ptitjanonyalldawson
authored andcommittedSep 29, 2023
qgsmesh3dmaterial: Ensure to create valid textures for the fragment shader
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.
1 parent 8c57166 commit ec6e480

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed
 

‎src/3d/mesh/qgsmesh3dmaterial_p.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ QgsMesh3dMaterial::QgsMesh3dMaterial( QgsMeshLayer *layer,
132132
void QgsMesh3dMaterial::configure()
133133
{
134134
// Create the texture to pass the color ramp
135-
Qt3DRender::QTexture1D *colorRampTexture = nullptr;
135+
Qt3DRender::QTexture1D *colorRampTexture = new Qt3DRender::QTexture1D( this );
136136
if ( mSymbol->colorRampShader().colorRampItemList().count() > 0 )
137137
{
138-
colorRampTexture = new Qt3DRender::QTexture1D( this );
139138
switch ( mMagnitudeType )
140139
{
141140
case QgsMesh3dMaterial::ZValue:
@@ -148,9 +147,15 @@ void QgsMesh3dMaterial::configure()
148147
break;
149148
}
150149

151-
colorRampTexture->setMinificationFilter( Qt3DRender::QTexture1D::Linear );
152-
colorRampTexture->setMagnificationFilter( Qt3DRender::QTexture1D::Linear );
153150
}
151+
else
152+
{
153+
// create a simple texture to have a valid parameter in the shader and avoid undefined behaviors
154+
colorRampTexture->addTextureImage( new QgsColorRampTexture( QgsColorRampShader(), 1 ) );
155+
}
156+
157+
colorRampTexture->setMinificationFilter( Qt3DRender::QTexture1D::Linear );
158+
colorRampTexture->setMagnificationFilter( Qt3DRender::QTexture1D::Linear );
154159

155160
// Create and configure technique
156161
mTechnique = new Qt3DRender::QTechnique();
@@ -184,8 +189,7 @@ void QgsMesh3dMaterial::configure()
184189
mTechnique->addParameter( new Qt3DRender::QParameter( "lineColor", QVector4D( wireframecolor.redF(), wireframecolor.greenF(), wireframecolor.blueF(), 1.0f ) ) );
185190
mTechnique->addParameter( new Qt3DRender::QParameter( "wireframeEnabled", mSymbol->wireframeEnabled() ) );
186191
mTechnique->addParameter( new Qt3DRender::QParameter( "textureType", int( mSymbol->renderingStyle() ) ) );
187-
if ( colorRampTexture )
188-
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampTexture", colorRampTexture ) ) ;
192+
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampTexture", colorRampTexture ) ) ;
189193
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampCount", mSymbol->colorRampShader().colorRampItemList().count() ) );
190194
const int colorRampType = mSymbol->colorRampShader().colorRampType();
191195
mTechnique->addParameter( new Qt3DRender::QParameter( "colorRampType", colorRampType ) );
@@ -203,8 +207,8 @@ void QgsMesh3dMaterial::configureArrows( QgsMeshLayer *layer, const QgsDateTimeR
203207
if ( layer )
204208
datasetIndex = layer->activeVectorDatasetAtTime( timeRange );
205209

206-
QVector<QgsVector> vectors;
207-
QSize gridSize;
210+
QVector<QgsVector> vectors( 1 );
211+
QSize gridSize( 1, 1 );
208212
QgsPointXY minCorner;
209213
std::unique_ptr< Qt3DRender::QParameter > arrowsEnabledParameter = std::make_unique< Qt3DRender::QParameter >( "arrowsEnabled", nullptr );
210214
if ( !layer || mMagnitudeType != MagnitudeType::ScalarDataSet || !mSymbol->arrowsEnabled() || meta.isScalar() || !datasetIndex.isValid() )

0 commit comments

Comments
 (0)
Please sign in to comment.