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