QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "qgsapplication.h"
19#include "qgscolorutils.h"
20#include "qgsimagecache.h"
21#include "qgsimagetexture.h"
24#include <Qt3DRender/QPaintedTextureImage>
25#include <Qt3DRender/QTexture>
26#include <Qt3DRender/QParameter>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QTechnique>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <QMap>
31
32
34{
35 return QStringLiteral( "phongtextured" );
36}
37
55
60
65
67{
68 return mTextureRotation;
69}
70
71void QgsPhongTexturedMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
72{
73 mAmbient = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "ambient" ), QStringLiteral( "25,25,25" ) ) );
74 mSpecular = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "specular" ), QStringLiteral( "255,255,255" ) ) );
75 mShininess = elem.attribute( QStringLiteral( "shininess" ) ).toDouble();
76 mOpacity = elem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toDouble();
77 mDiffuseTexturePath = elem.attribute( QStringLiteral( "diffuse_texture_path" ), QString() );
78 mTextureScale = elem.attribute( QStringLiteral( "texture_scale" ), QString( "1.0" ) ).toFloat();
79 mTextureRotation = elem.attribute( QStringLiteral( "texture-rotation" ), QString( "0.0" ) ).toFloat();
80
82}
83
84void QgsPhongTexturedMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
85{
86 elem.setAttribute( QStringLiteral( "ambient" ), QgsColorUtils::colorToString( mAmbient ) );
87 elem.setAttribute( QStringLiteral( "specular" ), QgsColorUtils::colorToString( mSpecular ) );
88 elem.setAttribute( QStringLiteral( "shininess" ), mShininess );
89 elem.setAttribute( QStringLiteral( "opacity" ), mOpacity );
90 elem.setAttribute( QStringLiteral( "diffuse_texture_path" ), mDiffuseTexturePath );
91 elem.setAttribute( QStringLiteral( "texture_scale" ), mTextureScale );
92 elem.setAttribute( QStringLiteral( "texture-rotation" ), mTextureRotation );
93
95}
96
98{
99 switch ( technique )
100 {
107 {
108 bool fitsInCache = false;
109 const QImage textureSourceImage = QgsApplication::imageCache()->pathAsImage( mDiffuseTexturePath, QSize(), true, 1.0, fitsInCache );
110 ( void )fitsInCache;
111
112 // No texture image was provided.
113 // Fallback to QgsPhongMaterialSettings.
114 if ( textureSourceImage.isNull() )
115 {
117 phongSettings.setAmbient( mAmbient );
118 phongSettings.setDiffuse( QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) ); // default diffuse color from QDiffuseSpecularMaterial
119 phongSettings.setOpacity( mOpacity );
120 phongSettings.setShininess( mShininess );
121 phongSettings.setSpecular( mSpecular );
122 QgsMaterial *material = phongSettings.toMaterial( technique, context );
123 return material;
124 }
125
126 QgsPhongTexturedMaterial *material = new QgsPhongTexturedMaterial();
127
128 int opacity = static_cast<int>( mOpacity * 255.0 );
129 QColor ambient = context.isSelected() ? context.selectionColor().darker() : mAmbient;
130 material->setAmbient( QColor( ambient.red(), ambient.green(), ambient.blue(), opacity ) );
131 material->setSpecular( QColor( mSpecular.red(), mSpecular.green(), mSpecular.blue(), opacity ) );
132 material->setShininess( static_cast<float>( mShininess ) );
133 material->setOpacity( static_cast<float>( mOpacity ) );
134
135 // TODO : if ( context.isSelected() ) dampen the color of diffuse texture
136 // with context.map().selectionColor()
137 QgsImageTexture *textureImage = new QgsImageTexture( textureSourceImage );
138 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
139 texture->addTextureImage( textureImage );
140
141 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
142 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
143 texture->wrapMode()->setZ( Qt3DRender::QTextureWrapMode::Repeat );
144
145 texture->setSamples( 4 );
146
147 texture->setGenerateMipMaps( true );
148 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
149 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
150
151 material->setDiffuseTexture( texture );
152 material->setDiffuseTextureScale( mTextureScale );
153
154 return material;
155 }
156
158 return nullptr;
159
160 }
161 return nullptr;
162}
163
165{
166 QMap<QString, QString> parameters;
167 parameters[ QStringLiteral( "Ka" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
168 parameters[ QStringLiteral( "Ks" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
169 parameters[ QStringLiteral( "Ns" ) ] = QString::number( mShininess );
170 return parameters;
171}
172
173void QgsPhongTexturedMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext ) const
174{
175 const QColor ambientColor = materialContext.isSelected() ? materialContext.selectionColor().darker() : mAmbient;
176
177 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ), ambientColor );
178 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( QStringLiteral( "specularColor" ), mSpecular );
179 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast<float>( mShininess ) );
180
181 effect->addParameter( ambientParameter );
182 effect->addParameter( specularParameter );
183 effect->addParameter( shininessParameter );
184}
virtual void writeXml(QDomElement &element, const QgsReadWriteContext &) const
Writes settings to a DOM element.
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &)
Reads settings from a DOM element.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
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.
Holds an image that can be used as a texture in the 3D view.
QColor selectionColor() const
Returns the color for representing materials in a selected state.
bool isSelected() const
Returns true if the material should represent a selected state.
void setOpacity(double opacity)
Sets opacity of the surface.
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
void setDiffuse(const QColor &diffuse)
Sets diffuse color component.
void setShininess(double shininess)
Sets shininess of the surface.
void setAmbient(const QColor &ambient)
Sets ambient color component.
void setSpecular(const QColor &specular)
Sets specular color component.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
float textureRotation() const
Returns the texture rotation, in degrees.
QString type() const override
Returns the unique type name for the material.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
double opacity() const
Returns the opacity of the surface.
QgsPhongTexturedMaterialSettings * clone() const override
Clones the material settings.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongTexturedMaterialSettings.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
QColor ambient() const
Returns ambient color component.
The class is used as a container of context for various read/write operations on other objects.
QgsMaterialSettingsRenderingTechnique
Material rendering techniques 3.
@ Points
Point based rendering, requires point data.
@ Triangles
Triangle based rendering (default)
@ TrianglesFromModel
Triangle based rendering, using a model object source.
@ Lines
Line based rendering, requires line data.
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering)