QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "qgscolorutils.h"
18#include "qgs3dutils.h"
19
20#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21#include <Qt3DRender/QAttribute>
22#include <Qt3DRender/QBuffer>
23#include <Qt3DRender/QGeometry>
24
25typedef Qt3DRender::QAttribute Qt3DQAttribute;
26typedef Qt3DRender::QBuffer Qt3DQBuffer;
27typedef Qt3DRender::QGeometry Qt3DQGeometry;
28#else
29#include <Qt3DCore/QAttribute>
30#include <Qt3DCore/QBuffer>
31#include <Qt3DCore/QGeometry>
32
33typedef Qt3DCore::QAttribute Qt3DQAttribute;
34typedef Qt3DCore::QBuffer Qt3DQBuffer;
35typedef Qt3DCore::QGeometry Qt3DQGeometry;
36#endif
37#include <Qt3DRender/QParameter>
38#include <Qt3DRender/QEffect>
39#include <Qt3DRender/QTechnique>
40#include <Qt3DRender/QGraphicsApiFilter>
41#include <QMap>
42
43
45{
46 return QStringLiteral( "phong" );
47}
48
66
71
76
77void QgsPhongMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
78{
79 mAmbient = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "ambient" ), QStringLiteral( "25,25,25" ) ) );
80 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "diffuse" ), QStringLiteral( "178,178,178" ) ) );
81 mSpecular = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "specular" ), QStringLiteral( "255,255,255" ) ) );
82 mShininess = elem.attribute( QStringLiteral( "shininess" ) ).toDouble();
83 mOpacity = elem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toDouble();
84 mAmbientCoefficient = elem.attribute( QStringLiteral( "ka" ), QStringLiteral( "1.0" ) ).toDouble();
85 mDiffuseCoefficient = elem.attribute( QStringLiteral( "kd" ), QStringLiteral( "1.0" ) ).toDouble();
86 mSpecularCoefficient = elem.attribute( QStringLiteral( "ks" ), QStringLiteral( "1.0" ) ).toDouble();
87
89}
90
91void QgsPhongMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
92{
93 elem.setAttribute( QStringLiteral( "ambient" ), QgsColorUtils::colorToString( mAmbient ) );
94 elem.setAttribute( QStringLiteral( "diffuse" ), QgsColorUtils::colorToString( mDiffuse ) );
95 elem.setAttribute( QStringLiteral( "specular" ), QgsColorUtils::colorToString( mSpecular ) );
96 elem.setAttribute( QStringLiteral( "shininess" ), mShininess );
97 elem.setAttribute( QStringLiteral( "opacity" ), mOpacity );
98 elem.setAttribute( QStringLiteral( "ka" ), mAmbientCoefficient );
99 elem.setAttribute( QStringLiteral( "kd" ), mDiffuseCoefficient );
100 elem.setAttribute( QStringLiteral( "ks" ), mSpecularCoefficient );
101
103}
104
105
125
127{
128 QMap<QString, QString> parameters;
129 parameters[ QStringLiteral( "Kd" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mDiffuse.redF() ).arg( mDiffuse.greenF() ).arg( mDiffuse.blueF() );
130 parameters[ QStringLiteral( "Ka" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mAmbient.redF() ).arg( mAmbient.greenF() ).arg( mAmbient.blueF() );
131 parameters[ QStringLiteral( "Ks" ) ] = QStringLiteral( "%1 %2 %3" ).arg( mSpecular.redF() ).arg( mSpecular.greenF() ).arg( mSpecular.blueF() );
132 parameters[ QStringLiteral( "Ns" ) ] = QString::number( mShininess );
133 return parameters;
134}
135
136void QgsPhongMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext ) const
137{
138 const QColor ambient = materialContext.isSelected() ? materialContext.selectionColor().darker() : mAmbient;
139 const QColor diffuse = materialContext.isSelected() ? materialContext.selectionColor() : mDiffuse;
140
141 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ),
142 QColor::fromRgbF( ambient.redF() * mAmbientCoefficient,
143 ambient.greenF() * mAmbientCoefficient,
144 ambient.blueF() * mAmbientCoefficient ) );
145 Qt3DRender::QParameter *diffuseParameter = new Qt3DRender::QParameter( QStringLiteral( "diffuseColor" ),
146 QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient,
147 diffuse.greenF() * mDiffuseCoefficient,
148 diffuse.blueF() * mDiffuseCoefficient ) );
149 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter( QStringLiteral( "specularColor" ),
150 QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient,
151 mSpecular.greenF() * mSpecularCoefficient,
152 mSpecular.blueF() * mSpecularCoefficient ) );
153 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast< float >( mShininess ) );
154 Qt3DRender::QParameter *opacityParameter = new Qt3DRender::QParameter( QStringLiteral( "opacity" ), static_cast< float >( mOpacity ) );
155
156 effect->addParameter( ambientParameter );
157 effect->addParameter( diffuseParameter );
158 effect->addParameter( specularParameter );
159 effect->addParameter( shininessParameter );
160 effect->addParameter( opacityParameter );
161}
162
164{
165 const QColor ambient = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, mAmbient );
166 const QColor diffuse = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, mDiffuse );
167 const QColor specular = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, mSpecular );
168
169 QByteArray array;
170 if ( mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1 )
171 {
172 // use floats if we are adjusting color component strength, bytes don't
173 // give us enough precision
174 array.resize( sizeof( float ) * 9 );
175 float *fptr = reinterpret_cast<float *>( array.data() );
176
177 *fptr++ = static_cast<float>( diffuse.redF() * mDiffuseCoefficient );
178 *fptr++ = static_cast<float>( diffuse.greenF() * mDiffuseCoefficient );
179 *fptr++ = static_cast<float>( diffuse.blueF() * mDiffuseCoefficient );
180
181 *fptr++ = static_cast<float>( ambient.redF() * mAmbientCoefficient );
182 *fptr++ = static_cast<float>( ambient.greenF() * mAmbientCoefficient );
183 *fptr++ = static_cast<float>( ambient.blueF() * mAmbientCoefficient );
184
185 *fptr++ = static_cast<float>( specular.redF() * mSpecularCoefficient );
186 *fptr++ = static_cast<float>( specular.greenF() * mSpecularCoefficient );
187 *fptr++ = static_cast<float>( specular.blueF() * mSpecularCoefficient );
188 }
189 else
190 {
191 array.resize( sizeof( unsigned char ) * 9 );
192 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
193
194 *ptr++ = static_cast<unsigned char>( diffuse.red() );
195 *ptr++ = static_cast<unsigned char>( diffuse.green() );
196 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
197
198 *ptr++ = static_cast<unsigned char>( ambient.red() );
199 *ptr++ = static_cast<unsigned char>( ambient.green() );
200 *ptr++ = static_cast<unsigned char>( ambient.blue() );
201
202 *ptr++ = static_cast<unsigned char>( specular.red() );
203 *ptr++ = static_cast<unsigned char>( specular.green() );
204 *ptr++ = static_cast<unsigned char>( specular.blue() );
205 }
206
207 return array;
208}
209
210int QgsPhongMaterialSettings::dataDefinedByteStride() const {return 9 * sizeof( unsigned char );}
211
212void QgsPhongMaterialSettings::applyDataDefinedToGeometry( Qt3DQGeometry *geometry, int vertexCount, const QByteArray &data ) const
213{
214 Qt3DQBuffer *dataBuffer = new Qt3DQBuffer( geometry );
215
216 // use floats if we are adjusting color component strength, bytes don't
217 // give us enough precision
218 const bool useFloats = mDiffuseCoefficient < 1 || mAmbientCoefficient < 1 || mSpecularCoefficient < 1;
219
220 Qt3DQAttribute *diffuseAttribute = new Qt3DQAttribute( geometry );
221 diffuseAttribute->setName( QStringLiteral( "dataDefinedDiffuseColor" ) );
222 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
223 diffuseAttribute->setVertexSize( 3 );
224 diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
225 diffuseAttribute->setBuffer( dataBuffer );
226 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
227 diffuseAttribute->setByteOffset( 0 );
228 diffuseAttribute->setCount( vertexCount );
229 geometry->addAttribute( diffuseAttribute );
230
231 Qt3DQAttribute *ambientAttribute = new Qt3DQAttribute( geometry );
232 ambientAttribute->setName( QStringLiteral( "dataDefinedAmbiantColor" ) );
233 ambientAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
234 ambientAttribute->setVertexSize( 3 );
235 ambientAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
236 ambientAttribute->setBuffer( dataBuffer );
237 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
238 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
239 ambientAttribute->setCount( vertexCount );
240 geometry->addAttribute( ambientAttribute );
241
242 Qt3DQAttribute *specularAttribute = new Qt3DQAttribute( geometry );
243 specularAttribute->setName( QStringLiteral( "dataDefinedSpecularColor" ) );
244 specularAttribute->setVertexBaseType( useFloats ? Qt3DQAttribute::Float : Qt3DQAttribute::UnsignedByte );
245 specularAttribute->setVertexSize( 3 );
246 specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
247 specularAttribute->setBuffer( dataBuffer );
248 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
249 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
250 specularAttribute->setCount( vertexCount );
251 geometry->addAttribute( specularAttribute );
252
253 dataBuffer->setData( data );
254}
255
256QgsMaterial *QgsPhongMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const
257{
258 QgsMaterial *material = new QgsMaterial;
259
260 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
261
262 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
263 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
264 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
265 technique->graphicsApiFilter()->setMajorVersion( 3 );
266 technique->graphicsApiFilter()->setMinorVersion( 3 );
267 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
268 filterKey->setName( QStringLiteral( "renderingStyle" ) );
269 filterKey->setValue( QStringLiteral( "forward" ) );
270 technique->addFilterKey( filterKey );
271
272 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
273 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
274
275 renderPass->setShaderProgram( shaderProgram );
276 technique->addRenderPass( renderPass );
277
278 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/phong.frag" ) ) );
279
280 if ( dataDefinedProperties().hasActiveProperties() )
281 {
282 // Load shader programs
283 const QUrl urlVert( QStringLiteral( "qrc:/shaders/phongDataDefined.vert" ) );
284 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
285 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( {"DATA_DEFINED"} ) );
286 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
287 }
288 else
289 {
290 // Load shader programs
291 const QUrl urlVert( QStringLiteral( "qrc:/shaders/default.vert" ) );
292 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
293 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
294
295 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : mAmbient;
296 const QColor diffuse = context.isSelected() ? context.selectionColor() : mDiffuse;
297
298 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "ambientColor" ),
299 QColor::fromRgbF( ambient.redF() * mAmbientCoefficient,
300 ambient.greenF() * mAmbientCoefficient,
301 ambient.blueF() * mAmbientCoefficient ) ) );
302 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "diffuseColor" ),
303 QColor::fromRgbF( diffuse.redF() * mDiffuseCoefficient,
304 diffuse.greenF() * mDiffuseCoefficient,
305 diffuse.blueF() * mDiffuseCoefficient ) ) );
306 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "specularColor" ),
307 QColor::fromRgbF( mSpecular.redF() * mSpecularCoefficient,
308 mSpecular.greenF() * mSpecularCoefficient,
309 mSpecular.blueF() * mSpecularCoefficient ) ) );
310 }
311
312 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "shininess" ), static_cast< float >( mShininess ) ) );
313 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "opacity" ), static_cast< float >( mOpacity ) ) );
314
315 effect->addTechnique( technique );
316 material->setEffect( effect );
317
318 return material;
319}
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
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.
@ Ambient
Ambient color (phong material)
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...
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.
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.
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.
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)
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry