QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsphongtexturedmaterialsettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsphongtexturedmaterialsettings.cpp
3  --------------------------------------
4  Date : August 2020
5  Copyright : (C) 2020 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgssymbollayerutils.h"
19 #include "qgsapplication.h"
20 #include "qgsimagecache.h"
21 #include "qgsimagetexture.h"
22 #include <Qt3DExtras/QDiffuseSpecularMaterial>
23 #include <Qt3DExtras/QPhongMaterial>
24 #include <Qt3DRender/QPaintedTextureImage>
25 #include <Qt3DRender/QTexture>
26 #include <Qt3DRender/QParameter>
27 #include <Qt3DRender/QEffect>
28 #include <QMap>
29 
30 
32 {
33  return QStringLiteral( "phongtextured" );
34 }
35 
37 {
38  switch ( technique )
39  {
41  case QgsMaterialSettingsRenderingTechnique::TrianglesDataDefined: //technique is supported but color can't be datadefined
42  return true;
43 
49  return false;
50  }
51  return false;
52 }
53 
55 {
57 }
58 
60 {
61  return new QgsPhongTexturedMaterialSettings( *this );
62 }
63 
65 {
66  return mTextureRotation;
67 }
68 
69 void QgsPhongTexturedMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
70 {
71  mAmbient = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "ambient" ), QStringLiteral( "25,25,25" ) ) );
72  mSpecular = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "specular" ), QStringLiteral( "255,255,255" ) ) );
73  mShininess = elem.attribute( QStringLiteral( "shininess" ) ).toFloat();
74  mDiffuseTexturePath = elem.attribute( QStringLiteral( "diffuse_texture_path" ), QString() );
75  mTextureScale = elem.attribute( QStringLiteral( "texture_scale" ), QString( "1.0" ) ).toFloat();
76  mTextureRotation = elem.attribute( QStringLiteral( "texture-rotation" ), QString( "0.0" ) ).toFloat();
77 
78  QgsAbstractMaterialSettings::readXml( elem, context );
79 }
80 
81 void QgsPhongTexturedMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
82 {
83  elem.setAttribute( QStringLiteral( "ambient" ), QgsSymbolLayerUtils::encodeColor( mAmbient ) );
84  elem.setAttribute( QStringLiteral( "specular" ), QgsSymbolLayerUtils::encodeColor( mSpecular ) );
85  elem.setAttribute( QStringLiteral( "shininess" ), mShininess );
86  elem.setAttribute( QStringLiteral( "diffuse_texture_path" ), mDiffuseTexturePath );
87  elem.setAttribute( QStringLiteral( "texture_scale" ), mTextureScale );
88  elem.setAttribute( QStringLiteral( "texture-rotation" ), mTextureRotation );
89 
91 }
92 
95 {
96  switch ( technique )
97  {
104  {
105  bool fitsInCache = false;
106  const QImage textureSourceImage = QgsApplication::imageCache()->pathAsImage( mDiffuseTexturePath, QSize(), true, 1.0, fitsInCache );
107  ( void )fitsInCache;
108 
109  if ( !textureSourceImage.isNull() )
110  {
111  QgsImageTexture *textureImage = new QgsImageTexture( textureSourceImage );
112  Qt3DExtras::QDiffuseSpecularMaterial *material = new Qt3DExtras::QDiffuseSpecularMaterial;
113 
114  Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
115  texture->addTextureImage( textureImage );
116 
117  texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
118  texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
119  texture->wrapMode()->setZ( Qt3DRender::QTextureWrapMode::Repeat );
120 
121  texture->setSamples( 4 );
122 
123  texture->setGenerateMipMaps( true );
124  texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
125  texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
126 
127  material->setDiffuse( QVariant::fromValue( texture ) );
128 
129  material->setSpecular( mSpecular );
130  material->setAmbient( mAmbient );
131  material->setShininess( mShininess );
132  material->setTextureScale( mTextureScale );
133 
134  if ( context.isSelected() )
135  {
136  // update the material with selection colors
137  // TODO : dampen the color of diffuse texture
138  // mat->setDiffuse( context.map().selectionColor() );
139  material->setAmbient( context.selectionColor().darker() );
140  }
141 
142  return material;
143  }
144  else
145  {
146  Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
147  material->setAmbient( mAmbient );
148  material->setSpecular( mSpecular );
149  material->setShininess( mShininess );
150 
151  if ( context.isSelected() )
152  {
153  // update the material with selection colors
154  material->setDiffuse( context.selectionColor() );
155  material->setAmbient( context.selectionColor().darker() );
156  }
157  return material;
158  }
159  }
160 
162  return nullptr;
163 
164  }
165  return nullptr;
166 }
167 
169 {
170  QMap<QString, QString> parameters;
171  parameters[ QStringLiteral( "Ka" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
172  parameters[ QStringLiteral( "Ks" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
173  parameters[ QStringLiteral( "Ns" ) ] = QString::number( mShininess );
174  return parameters;
175 }
176 
177 void QgsPhongTexturedMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect ) const
178 {
179  Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( QStringLiteral( "ka" ), QColor::fromRgbF( 0.05f, 0.05f, 0.05f, 1.0f ) );
180  Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( QStringLiteral( "ks" ), QColor::fromRgbF( 0.01f, 0.01f, 0.01f, 1.0f ) );
181  Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( QStringLiteral( "shininess" ), 150.0f );
182 
183  ambientParameter->setValue( mAmbient );
184  specularParameter->setValue( mSpecular );
185  shininessParameter->setValue( mShininess );
186 
187  effect->addParameter( ambientParameter );
188  effect->addParameter( specularParameter );
189  effect->addParameter( shininessParameter );
190 }
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:64
QgsPhongTexturedMaterialSettings::supportsTechnique
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
Definition: qgsphongtexturedmaterialsettings.cpp:36
QgsPhongTexturedMaterialSettings::QgsPhongTexturedMaterialSettings
QgsPhongTexturedMaterialSettings()=default
Constructor for QgsPhongTexturedMaterialSettings.
QgsPhongTexturedMaterialSettings::textureRotation
float textureRotation() const
Returns the texture rotation, in degrees.
Definition: qgsphongtexturedmaterialsettings.cpp:64
QgsImageCache::pathAsImage
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
Definition: qgsimagecache.cpp:137
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsPhongTexturedMaterialSettings::create
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongTexturedMaterialSettings.
Definition: qgsphongtexturedmaterialsettings.cpp:54
qgsphongtexturedmaterialsettings.h
qgssymbollayerutils.h
QgsMaterialSettingsRenderingTechnique::InstancedPoints
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:69
QgsMaterialContext::selectionColor
QColor selectionColor() const
Returns the color for representing materials in a selected state.
Definition: qgsabstractmaterialsettings.h:87
QgsPhongTexturedMaterialSettings::toExportParameters
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
Definition: qgsphongtexturedmaterialsettings.cpp:168
qgsapplication.h
QgsMaterialContext
Context settings for a material.
Definition: qgsabstractmaterialsettings.h:64
QgsPhongTexturedMaterialSettings::clone
QgsPhongTexturedMaterialSettings * clone() const override
Clones the material settings.
Definition: qgsphongtexturedmaterialsettings.cpp:59
QgsAbstractMaterialSettings
Abstract base class for material settings.
Definition: qgsabstractmaterialsettings.h:114
QgsMaterialSettingsRenderingTechnique::Lines
@ Lines
Line based rendering, requires line data.
QgsImageTexture
Holds an image that can be used as a texture in the 3D view.
Definition: qgsimagetexture.h:36
QgsPhongTexturedMaterialSettings::addParametersToEffect
void addParametersToEffect(Qt3DRender::QEffect *effect) const override
Adds parameters from the material to a destination effect.
Definition: qgsphongtexturedmaterialsettings.cpp:177
QgsPhongTexturedMaterialSettings::writeXml
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
Definition: qgsphongtexturedmaterialsettings.cpp:81
QgsApplication::imageCache
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
Definition: qgsapplication.cpp:2365
QgsMaterialSettingsRenderingTechnique::Triangles
@ Triangles
Triangle based rendering (default)
QgsMaterialSettingsRenderingTechnique::TrianglesWithFixedTexture
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering)
QgsPhongTexturedMaterialSettings::toMaterial
Qt3DRender::QMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QMaterial object representing the material settings.
Definition: qgsphongtexturedmaterialsettings.cpp:94
QgsAbstractMaterialSettings::writeXml
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &) const
Writes settings to a DOM element.
Definition: qgsabstractmaterialsettings.cpp:27
QgsMaterialSettingsRenderingTechnique
QgsMaterialSettingsRenderingTechnique
Material rendering techniques.
Definition: qgsabstractmaterialsettings.h:43
QgsPhongTexturedMaterialSettings
A phong shading model with diffuse texture map.
Definition: qgsphongtexturedmaterialsettings.h:35
qgsimagecache.h
QgsMaterialSettingsRenderingTechnique::TrianglesDataDefined
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
QgsMaterialSettingsRenderingTechnique::TrianglesFromModel
@ TrianglesFromModel
Triangle based rendering, using a model object source.
qgsimagetexture.h
QgsPhongTexturedMaterialSettings::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
Definition: qgsphongtexturedmaterialsettings.cpp:69
QgsAbstractMaterialSettings::readXml
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &)
Reads settings from a DOM element.
Definition: qgsabstractmaterialsettings.cpp:20
QgsMaterialSettingsRenderingTechnique::Points
@ Points
Point based rendering, requires point data.
QgsMaterialContext::isSelected
bool isSelected() const
Returns true if the material should represent a selected state.
Definition: qgsabstractmaterialsettings.h:73
QgsPhongTexturedMaterialSettings::type
QString type() const override
Returns the unique type name for the material.
Definition: qgsphongtexturedmaterialsettings.cpp:31