From ada2c601fb7c4ef2b9db6dd045c0fd96dbc8adef Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 30 Jul 2020 14:27:28 +1000 Subject: [PATCH] Move some texture handling methods to base class --- .../qgsabstractmaterialsettings.sip.in | 12 +++++++++++ .../qgsphongmaterialsettings.sip.in | 20 ++++--------------- src/3d/qgsabstractmaterialsettings.h | 12 +++++++++++ src/3d/qgsphongmaterialsettings.cpp | 12 ++++++++++- src/3d/qgsphongmaterialsettings.h | 15 ++------------ src/3d/symbols/qgsmesh3dsymbol_p.cpp | 2 +- src/3d/symbols/qgspolygon3dsymbol_p.cpp | 10 +++++----- 7 files changed, 47 insertions(+), 36 deletions(-) diff --git a/python/3d/auto_generated/qgsabstractmaterialsettings.sip.in b/python/3d/auto_generated/qgsabstractmaterialsettings.sip.in index 7ee7a09de11..08b8f16052c 100644 --- a/python/3d/auto_generated/qgsabstractmaterialsettings.sip.in +++ b/python/3d/auto_generated/qgsabstractmaterialsettings.sip.in @@ -111,6 +111,18 @@ Reads settings from a DOM ``element`` Writes settings to a DOM ``element`` %End + virtual bool requiresTextureCoordinates() const; +%Docstring +Returns true if the material requires texture coordinates to be generated +during triangulation. +%End + + virtual float textureRotation() const; +%Docstring +Returns the texture rotation (in degrees), if texture coordinates to be generated +during triangulation. +%End + }; diff --git a/python/3d/auto_generated/qgsphongmaterialsettings.sip.in b/python/3d/auto_generated/qgsphongmaterialsettings.sip.in index 3a0b13a1541..fa7a76233f5 100644 --- a/python/3d/auto_generated/qgsphongmaterialsettings.sip.in +++ b/python/3d/auto_generated/qgsphongmaterialsettings.sip.in @@ -83,18 +83,6 @@ Returns whether the diffuse texture is used. .. seealso:: :py:func:`setDiffuseTextureEnabled` -.. seealso:: :py:func:`texturePath` -%End - - bool shouldUseDiffuseTexture() const; -%Docstring -Returns whether the diffuse texture should be used during rendering. - -Diffuse textures will only be used at render time if :py:func:`~QgsPhongMaterialSettings.diffuseTextureEnabled` is ``True`` -and a :py:func:`~QgsPhongMaterialSettings.texturePath` is non-empty. - -.. seealso:: :py:func:`diffuseTextureEnabled` - .. seealso:: :py:func:`texturePath` %End @@ -119,10 +107,10 @@ The texture scale changes the size of the displayed texture in the 3D scene If the texture scale is less than 1 the texture will be stretched %End - float textureRotation() const; -%Docstring -Returns the texture's rotation in degrees -%End + virtual bool requiresTextureCoordinates() const; + + virtual float textureRotation() const; + void setAmbient( const QColor &ambient ); %Docstring diff --git a/src/3d/qgsabstractmaterialsettings.h b/src/3d/qgsabstractmaterialsettings.h index c8da5b02fdc..eff3bfdfefb 100644 --- a/src/3d/qgsabstractmaterialsettings.h +++ b/src/3d/qgsabstractmaterialsettings.h @@ -124,6 +124,18 @@ class _3D_EXPORT QgsAbstractMaterialSettings SIP_ABSTRACT //! Writes settings to a DOM \a element virtual void writeXml( QDomElement &element, const QgsReadWriteContext &context ) const = 0; + /** + * Returns true if the material requires texture coordinates to be generated + * during triangulation. + */ + virtual bool requiresTextureCoordinates() const { return false; } + + /** + * Returns the texture rotation (in degrees), if texture coordinates to be generated + * during triangulation. + */ + virtual float textureRotation() const { return 0.f; } + #ifndef SIP_RUN /** diff --git a/src/3d/qgsphongmaterialsettings.cpp b/src/3d/qgsphongmaterialsettings.cpp index 325dca7b7e6..7babbc4d987 100644 --- a/src/3d/qgsphongmaterialsettings.cpp +++ b/src/3d/qgsphongmaterialsettings.cpp @@ -56,6 +56,16 @@ QgsPhongMaterialSettings *QgsPhongMaterialSettings::clone() const return new QgsPhongMaterialSettings( *this ); } +bool QgsPhongMaterialSettings::requiresTextureCoordinates() const +{ + return mDiffuseTextureEnabled && !mTexturePath.isEmpty(); +} + +float QgsPhongMaterialSettings::textureRotation() const +{ + return mTextureRotation; +} + void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext & ) { mAmbient = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "ambient" ), QStringLiteral( "25,25,25" ) ) ); @@ -115,7 +125,7 @@ Qt3DRender::QMaterial *QgsPhongMaterialSettings::toMaterial( QgsMaterialSettings bool fitsInCache = false; QImage textureSourceImage; - if ( shouldUseDiffuseTexture() ) + if ( requiresTextureCoordinates() ) textureSourceImage = QgsApplication::imageCache()->pathAsImage( mTexturePath, QSize(), true, 1.0, fitsInCache ); ( void )fitsInCache; diff --git a/src/3d/qgsphongmaterialsettings.h b/src/3d/qgsphongmaterialsettings.h index c957d4f5454..ba8ee1ff86f 100644 --- a/src/3d/qgsphongmaterialsettings.h +++ b/src/3d/qgsphongmaterialsettings.h @@ -79,17 +79,6 @@ class _3D_EXPORT QgsPhongMaterialSettings : public QgsAbstractMaterialSettings */ bool diffuseTextureEnabled() const { return mDiffuseTextureEnabled; } - /** - * Returns whether the diffuse texture should be used during rendering. - * - * Diffuse textures will only be used at render time if diffuseTextureEnabled() is TRUE - * and a texturePath() is non-empty. - * - * \see diffuseTextureEnabled() - * \see texturePath() - */ - bool shouldUseDiffuseTexture() const { return mDiffuseTextureEnabled && !mTexturePath.isEmpty(); } - /** * Returns the diffuse texture path. * @@ -108,8 +97,8 @@ class _3D_EXPORT QgsPhongMaterialSettings : public QgsAbstractMaterialSettings */ float textureScale() const { return mTextureScale; } - //! Returns the texture's rotation in degrees - float textureRotation() const { return mTextureRotation; } + bool requiresTextureCoordinates() const override; + float textureRotation() const override; //! Sets ambient color component void setAmbient( const QColor &ambient ) { mAmbient = ambient; } diff --git a/src/3d/symbols/qgsmesh3dsymbol_p.cpp b/src/3d/symbols/qgsmesh3dsymbol_p.cpp index 831c297e193..66277279fad 100644 --- a/src/3d/symbols/qgsmesh3dsymbol_p.cpp +++ b/src/3d/symbols/qgsmesh3dsymbol_p.cpp @@ -133,7 +133,7 @@ Qt3DRender::QGeometryRenderer *QgsMesh3DSymbolEntityNode::renderer( const Qgs3DM // Polygons from mesh are already triangles, but // call QgsTessellatedPolygonGeometry to // use symbol settings for back faces, normals, etc - mGeometry = new QgsTessellatedPolygonGeometry( true, false, symbol.addBackFaces(), dynamic_cast< QgsPhongMaterialSettings * >( symbol.material() ) ? dynamic_cast< QgsPhongMaterialSettings * >( symbol.material() )->shouldUseDiffuseTexture() : false ); + mGeometry = new QgsTessellatedPolygonGeometry( true, false, symbol.addBackFaces(), symbol.material()->requiresTextureCoordinates() ); QList extrusionHeightPerPolygon; mGeometry->setPolygons( polygons, fids, origin, 0.0, extrusionHeightPerPolygon ); diff --git a/src/3d/symbols/qgspolygon3dsymbol_p.cpp b/src/3d/symbols/qgspolygon3dsymbol_p.cpp index d13fbaa218e..245542c696b 100644 --- a/src/3d/symbols/qgspolygon3dsymbol_p.cpp +++ b/src/3d/symbols/qgspolygon3dsymbol_p.cpp @@ -89,14 +89,14 @@ bool QgsPolygon3DSymbolHandler::prepare( const Qgs3DRenderContext &context, QSet outEdges.init( mSymbol.altitudeClamping(), mSymbol.altitudeBinding(), 0, &context.map() ); outNormal.tessellator.reset( new QgsTessellator( context.map().origin().x(), context.map().origin().y(), true, mSymbol.invertNormals(), mSymbol.addBackFaces(), false, - dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() ) ? dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() )->shouldUseDiffuseTexture() : false, + mSymbol.material()->requiresTextureCoordinates(), mSymbol.renderedFacade(), - dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() ) ? dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() )->textureRotation() : 0 ) ); + mSymbol.material()->textureRotation() ) ); outSelected.tessellator.reset( new QgsTessellator( context.map().origin().x(), context.map().origin().y(), true, mSymbol.invertNormals(), mSymbol.addBackFaces(), false, - dynamic_cast< QgsPhongMaterialSettings *>( mSymbol.material() ) ? dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() )->shouldUseDiffuseTexture() : false, + mSymbol.material()->requiresTextureCoordinates(), mSymbol.renderedFacade(), - dynamic_cast< QgsPhongMaterialSettings *>( mSymbol.material() ) ? dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() )->textureRotation() : 0 ) ); + mSymbol.material()->textureRotation() ) ); QSet attrs = mSymbol.dataDefinedProperties().referencedFields( context.expressionContext() ); attributeNames.unite( attrs ); @@ -241,7 +241,7 @@ void QgsPolygon3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const Qgs int nVerts = data.count() / out.tessellator->stride(); QgsTessellatedPolygonGeometry *geometry = new QgsTessellatedPolygonGeometry( true, mSymbol.invertNormals(), mSymbol.addBackFaces(), - dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() ) ? dynamic_cast< QgsPhongMaterialSettings * >( mSymbol.material() )->shouldUseDiffuseTexture() : false ); + mSymbol.material()->requiresTextureCoordinates() ); geometry->setData( data, nVerts, out.triangleIndexFids, out.triangleIndexStartingIndices ); Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;