Move some texture handling methods to base class

This commit is contained in:
Nyall Dawson 2020-07-30 14:27:28 +10:00
parent 201ab0d05c
commit ada2c601fb
7 changed files with 47 additions and 36 deletions

View File

@ -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
};

View File

@ -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

View File

@ -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
/**

View File

@ -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;

View File

@ -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; }

View File

@ -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<float> extrusionHeightPerPolygon;
mGeometry->setPolygons( polygons, fids, origin, 0.0, extrusionHeightPerPolygon );

View File

@ -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<QString> 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;