QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsphongmaterialsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsphongmaterialsettings.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk 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 "qgs3dutils.h"
19#include "qgscolorutils.h"
20
21#include <QMap>
22#include <QString>
23#include <Qt3DCore/QAttribute>
24#include <Qt3DCore/QBuffer>
25#include <Qt3DCore/QGeometry>
26#include <Qt3DRender/QEffect>
27#include <Qt3DRender/QGraphicsApiFilter>
28#include <Qt3DRender/QParameter>
29#include <Qt3DRender/QTechnique>
30
31using namespace Qt::StringLiterals;
32
34{
35 return u"phong"_s;
36}
37
55
60
65
67{
68 const QgsPhongMaterialSettings *otherPhong = dynamic_cast<const QgsPhongMaterialSettings *>( other );
69 if ( !otherPhong )
70 return false;
71
72 return *this == *otherPhong;
73}
74
75void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
76{
77 mAmbient = QgsColorUtils::colorFromString( elem.attribute( u"ambient"_s, u"25,25,25"_s ) );
78 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( u"diffuse"_s, u"178,178,178"_s ) );
79 mSpecular = QgsColorUtils::colorFromString( elem.attribute( u"specular"_s, u"255,255,255"_s ) );
80 mShininess = elem.attribute( u"shininess"_s ).toDouble();
81 mOpacity = elem.attribute( u"opacity"_s, u"1.0"_s ).toDouble();
82 mAmbientCoefficient = elem.attribute( u"ka"_s, u"1.0"_s ).toDouble();
83 mDiffuseCoefficient = elem.attribute( u"kd"_s, u"1.0"_s ).toDouble();
84 mSpecularCoefficient = elem.attribute( u"ks"_s, u"1.0"_s ).toDouble();
85
87}
88
89void QgsPhongMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
90{
91 elem.setAttribute( u"ambient"_s, QgsColorUtils::colorToString( mAmbient ) );
92 elem.setAttribute( u"diffuse"_s, QgsColorUtils::colorToString( mDiffuse ) );
93 elem.setAttribute( u"specular"_s, QgsColorUtils::colorToString( mSpecular ) );
94 elem.setAttribute( u"shininess"_s, mShininess );
95 elem.setAttribute( u"opacity"_s, mOpacity );
96 elem.setAttribute( u"ka"_s, mAmbientCoefficient );
97 elem.setAttribute( u"kd"_s, mDiffuseCoefficient );
98 elem.setAttribute( u"ks"_s, mSpecularCoefficient );
99
101}
102
103
123
125{
126 QMap<QString, QString> parameters;
127 parameters[u"Kd"_s] = u"%1 %2 %3"_s.arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
128 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
129 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
130 parameters[u"Ns"_s] = QString::number( mShininess );
131 return parameters;
132}
133
134void QgsPhongMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext ) const
135{
136 const QColor ambient = materialContext.isSelected() ? materialContext.selectionColor().darker() : mAmbient;
137 const QColor diffuse = materialContext.isSelected() ? materialContext.selectionColor() : mDiffuse;
138
139 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( u"ambientColor"_s, QColor::fromRgbF( ambient.redF() * mAmbientCoefficient, ambient.greenF() * mAmbientCoefficient, ambient.blueF() * mAmbientCoefficient ) );
140 Qt3DRender::QParameter *diffuseParameter = new Qt3DRender::QParameter( u"diffuseColor"_s, QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient, diffuse.greenF() * mDiffuseCoefficient, diffuse.blueF() * mDiffuseCoefficient ) );
141 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( u"specularColor"_s, QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) );
142 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( mShininess ) );
143 Qt3DRender::QParameter *opacityParameter = new Qt3DRender::QParameter( u"opacity"_s, static_cast<float>( mOpacity ) );
144
145 effect->addParameter( ambientParameter );
146 effect->addParameter( diffuseParameter );
147 effect->addParameter( specularParameter );
148 effect->addParameter( shininessParameter );
149 effect->addParameter( opacityParameter );
150}
151
153{
154 const QColor ambient = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, mAmbient );
155 const QColor diffuse = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, mDiffuse );
156 const QColor specular = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, mSpecular );
157
158 QByteArray array;
159 if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
160 {
161 // use floats if we are adjusting color component strength, bytes don't
162 // give us enough precision
163 array.resize( sizeof( float ) * 9 );
164 float *fptr = reinterpret_cast<float *>( array.data() );
165
166 *fptr++ = static_cast<float>( diffuse.redF() * mDiffuseCoefficient );
167 *fptr++ = static_cast<float>( diffuse.greenF() * mDiffuseCoefficient );
168 *fptr++ = static_cast<float>( diffuse.blueF() * mDiffuseCoefficient );
169
170 *fptr++ = static_cast<float>( ambient.redF() * mAmbientCoefficient );
171 *fptr++ = static_cast<float>( ambient.greenF() * mAmbientCoefficient );
172 *fptr++ = static_cast<float>( ambient.blueF() * mAmbientCoefficient );
173
174 *fptr++ = static_cast<float>( specular.redF() * mSpecularCoefficient );
175 *fptr++ = static_cast<float>( specular.greenF() * mSpecularCoefficient );
176 *fptr++ = static_cast<float>( specular.blueF() * mSpecularCoefficient );
177 }
178 else
179 {
180 array.resize( sizeof( unsigned char ) * 9 );
181 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
182
183 *ptr++ = static_cast<unsigned char>( diffuse.red() );
184 *ptr++ = static_cast<unsigned char>( diffuse.green() );
185 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
186
187 *ptr++ = static_cast<unsigned char>( ambient.red() );
188 *ptr++ = static_cast<unsigned char>( ambient.green() );
189 *ptr++ = static_cast<unsigned char>( ambient.blue() );
190
191 *ptr++ = static_cast<unsigned char>( specular.red() );
192 *ptr++ = static_cast<unsigned char>( specular.green() );
193 *ptr++ = static_cast<unsigned char>( specular.blue() );
194 }
195
196 return array;
197}
198
199int QgsPhongMaterialSettings::dataDefinedByteStride() const { return 9 * sizeof( unsigned char ); }
200
201void QgsPhongMaterialSettings::applyDataDefinedToGeometry( Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
202{
203 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
204
205 // use floats if we are adjusting color component strength, bytes don't
206 // give us enough precision
207 const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
208
209 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
210 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
211 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
212 diffuseAttribute->setVertexSize( 3 );
213 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
214 diffuseAttribute->setBuffer( dataBuffer );
215 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
216 diffuseAttribute->setByteOffset( 0 );
217 diffuseAttribute->setCount( vertexCount );
218 geometry->addAttribute( diffuseAttribute );
219
220 Qt3DCore::QAttribute *ambientAttribute = new Qt3DCore::QAttribute( geometry );
221 ambientAttribute->setName( u"dataDefinedAmbiantColor"_s );
222 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
223 ambientAttribute->setVertexSize( 3 );
224 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
225 ambientAttribute->setBuffer( dataBuffer );
226 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
227 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
228 ambientAttribute->setCount( vertexCount );
229 geometry->addAttribute( ambientAttribute );
230
231 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
232 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
233 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
234 specularAttribute->setVertexSize( 3 );
235 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
236 specularAttribute->setBuffer( dataBuffer );
237 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
238 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
239 specularAttribute->setCount( vertexCount );
240 geometry->addAttribute( specularAttribute );
241
242 dataBuffer->setData( data );
243}
244
245QgsMaterial *QgsPhongMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const
246{
247 QgsMaterial *material = new QgsMaterial;
248
249 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
250
251 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
252 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
253 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
254 technique->graphicsApiFilter()->setMajorVersion( 3 );
255 technique->graphicsApiFilter()->setMinorVersion( 3 );
256 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
257 filterKey->setName( u"renderingStyle"_s );
258 filterKey->setValue( u"forward"_s );
259 technique->addFilterKey( filterKey );
260
261 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
262 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
263
264 renderPass->setShaderProgram( shaderProgram );
265 technique->addRenderPass( renderPass );
266
267 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u"qrc:/shaders/phong.frag"_s ) );
268
269 if ( dataDefinedProperties().hasActiveProperties() )
270 {
271 // Load shader programs
272 const QUrl urlVert( u"qrc:/shaders/phongDataDefined.vert"_s );
273 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
274 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
275 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
276 }
277 else
278 {
279 // Load shader programs
280 const QUrl urlVert( u"qrc:/shaders/default.vert"_s );
281 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
282 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
283
284 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : mAmbient;
285 const QColor diffuse = context.isSelected() ? context.selectionColor() : mDiffuse;
286
287 effect->addParameter( new Qt3DRender::QParameter( u"ambientColor"_s, QColor::fromRgbF( ambient.redF() * mAmbientCoefficient, ambient.greenF() * mAmbientCoefficient, ambient.blueF() * mAmbientCoefficient ) ) );
288 effect->addParameter( new Qt3DRender::QParameter( u"diffuseColor"_s, QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient, diffuse.greenF() * mDiffuseCoefficient, diffuse.blueF() * mDiffuseCoefficient ) ) );
289 effect->addParameter( new Qt3DRender::QParameter( u"specularColor"_s, QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient, mSpecular.greenF() * mSpecularCoefficient, mSpecular.blueF() * mSpecularCoefficient ) ) );
290 }
291
292 effect->addParameter( new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( mShininess ) ) );
293 effect->addParameter( new Qt3DRender::QParameter( u"opacity"_s, static_cast<float>( mOpacity ) ) );
294
295 effect->addTechnique( technique );
296 material->setEffect( effect );
297
298 return material;
299}
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
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.
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
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.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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
int dataDefinedByteStride() const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
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.
QString type() const override
Returns the unique type name for the material.
QColor diffuse() const
Returns diffuse color component.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
void applyDataDefinedToGeometry(Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data) const override
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsPhongMaterialSettings.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Phong material.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
QgsPhongMaterialSettings * clone() const override
Clones the material settings.
QgsPhongMaterialSettings()=default
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
QByteArray dataDefinedVertexColorsAsByte(const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
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).