QGIS API Documentation 3.99.0-Master (d270888f95f)
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
25#include <QMap>
26#include <QString>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QGraphicsApiFilter>
29#include <Qt3DRender/QPaintedTextureImage>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
32#include <Qt3DRender/QTexture>
33
34using namespace Qt::StringLiterals;
35
37{
38 return u"phongtextured"_s;
39}
40
58
63
68
70{
71 const QgsPhongTexturedMaterialSettings *otherPhong = dynamic_cast<const QgsPhongTexturedMaterialSettings *>( other );
72 if ( !otherPhong )
73 return false;
74
75 return *this == *otherPhong;
76}
77
79{
80 return mTextureRotation;
81}
82
83void QgsPhongTexturedMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
84{
85 mAmbient = QgsColorUtils::colorFromString( elem.attribute( u"ambient"_s, u"25,25,25"_s ) );
86 mSpecular = QgsColorUtils::colorFromString( elem.attribute( u"specular"_s, u"255,255,255"_s ) );
87 mShininess = elem.attribute( u"shininess"_s ).toDouble();
88 mOpacity = elem.attribute( u"opacity"_s, u"1.0"_s ).toDouble();
89 mDiffuseTexturePath = elem.attribute( u"diffuse_texture_path"_s, QString() );
90 mTextureScale = elem.attribute( u"texture_scale"_s, QString( "1.0" ) ).toDouble();
91 mTextureRotation = elem.attribute( u"texture-rotation"_s, QString( "0.0" ) ).toDouble();
92
94}
95
96void QgsPhongTexturedMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
97{
98 elem.setAttribute( u"ambient"_s, QgsColorUtils::colorToString( mAmbient ) );
99 elem.setAttribute( u"specular"_s, QgsColorUtils::colorToString( mSpecular ) );
100 elem.setAttribute( u"shininess"_s, mShininess );
101 elem.setAttribute( u"opacity"_s, mOpacity );
102 elem.setAttribute( u"diffuse_texture_path"_s, mDiffuseTexturePath );
103 elem.setAttribute( u"texture_scale"_s, mTextureScale );
104 elem.setAttribute( u"texture-rotation"_s, mTextureRotation );
105
107}
108
110{
111 switch ( technique )
112 {
119 {
120 bool fitsInCache = false;
121 const QImage textureSourceImage = QgsApplication::imageCache()->pathAsImage( mDiffuseTexturePath, QSize(), true, 1.0, fitsInCache );
122 ( void ) fitsInCache;
123
124 // No texture image was provided.
125 // Fallback to QgsPhongMaterialSettings.
126 if ( textureSourceImage.isNull() )
127 {
129 phongSettings.setAmbient( mAmbient );
130 phongSettings.setDiffuse( QColor::fromRgbF( 0.7f, 0.7f, 0.7f, 1.0f ) ); // default diffuse color from QDiffuseSpecularMaterial
131 phongSettings.setOpacity( mOpacity );
132 phongSettings.setShininess( mShininess );
133 phongSettings.setSpecular( mSpecular );
134 QgsMaterial *material = phongSettings.toMaterial( technique, context );
135 return material;
136 }
137
138 QgsPhongTexturedMaterial *material = new QgsPhongTexturedMaterial();
139
140 int opacity = static_cast<int>( mOpacity * 255.0 );
141 QColor ambient = context.isSelected() ? context.selectionColor().darker() : mAmbient;
142 material->setAmbient( QColor( ambient.red(), ambient.green(), ambient.blue(), opacity ) );
143 material->setSpecular( QColor( mSpecular.red(), mSpecular.green(), mSpecular.blue(), opacity ) );
144 material->setShininess( static_cast<float>( mShininess ) );
145 material->setOpacity( static_cast<float>( mOpacity ) );
146
147 // TODO : if ( context.isSelected() ) dampen the color of diffuse texture
148 // with context.map().selectionColor()
149 QgsImageTexture *textureImage = new QgsImageTexture( textureSourceImage );
150 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D();
151 texture->addTextureImage( textureImage );
152
153 texture->wrapMode()->setX( Qt3DRender::QTextureWrapMode::Repeat );
154 texture->wrapMode()->setY( Qt3DRender::QTextureWrapMode::Repeat );
155 texture->wrapMode()->setZ( Qt3DRender::QTextureWrapMode::Repeat );
156
157 texture->setSamples( 4 );
158
159 texture->setGenerateMipMaps( true );
160 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
161 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
162
163 material->setDiffuseTexture( texture );
164 material->setDiffuseTextureScale( static_cast<float>( mTextureScale ) );
165
166 return material;
167 }
168
170 return nullptr;
171 }
172 return nullptr;
173}
174
176{
177 QMap<QString, QString> parameters;
178 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
179 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
180 parameters[u"Ns"_s] = QString::number( mShininess );
181 return parameters;
182}
183
184void QgsPhongTexturedMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext ) const
185{
186 const QColor ambientColor = materialContext.isSelected() ? materialContext.selectionColor().darker() : mAmbient;
187
188 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( u"ambientColor"_s, ambientColor );
189 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( u"specularColor"_s, mSpecular );
190 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( mShininess ) );
191
192 effect->addParameter( ambientParameter );
193 effect->addParameter( specularParameter );
194 effect->addParameter( shininessParameter );
195}
Abstract base class for material settings.
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.
Context settings for a material.
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.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:39
Basic shading material used for rendering based on the Phong shading model with three color component...
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.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
double 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.
A container for the context for various read/write operations on objects.
QgsMaterialSettingsRenderingTechnique
Material rendering techniques.
@ 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).