Skip to content

Commit

Permalink
Move some texture handling methods to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 3, 2020
1 parent 201ab0d commit ada2c60
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
12 changes: 12 additions & 0 deletions python/3d/auto_generated/qgsabstractmaterialsettings.sip.in
Expand Up @@ -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


};

Expand Down
20 changes: 4 additions & 16 deletions python/3d/auto_generated/qgsphongmaterialsettings.sip.in
Expand Up @@ -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

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/3d/qgsabstractmaterialsettings.h
Expand Up @@ -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

/**
Expand Down
12 changes: 11 additions & 1 deletion src/3d/qgsphongmaterialsettings.cpp
Expand Up @@ -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" ) ) );
Expand Down Expand Up @@ -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;

Expand Down
15 changes: 2 additions & 13 deletions src/3d/qgsphongmaterialsettings.h
Expand Up @@ -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.
*
Expand All @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion src/3d/symbols/qgsmesh3dsymbol_p.cpp
Expand Up @@ -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 );

Expand Down
10 changes: 5 additions & 5 deletions src/3d/symbols/qgspolygon3dsymbol_p.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ada2c60

Please sign in to comment.