QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsgoochmaterialsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgoochmaterialsettings.cpp
3 --------------------------------------
4 Date : July 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 "qgssymbollayerutils.h"
19#include <Qt3DExtras/QGoochMaterial>
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 <QUrl>
42
44{
45 return QStringLiteral( "gooch" );
46}
47
49{
50 return new QgsGoochMaterialSettings();
51}
52
54{
55 switch ( technique )
56 {
61 return true;
62
66 return false;
67 }
68 return false;
69}
70
72{
73 return new QgsGoochMaterialSettings( *this );
74}
75
76void QgsGoochMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
77{
78 mWarm = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "warm" ), QStringLiteral( "107,0,107" ) ) );
79 mCool = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "cool" ), QStringLiteral( "255,130,0" ) ) );
80 mDiffuse = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "diffuse" ), QStringLiteral( "178,178,178" ) ) );
81 mSpecular = QgsSymbolLayerUtils::decodeColor( elem.attribute( QStringLiteral( "specular" ) ) );
82 mShininess = elem.attribute( QStringLiteral( "shininess2" ), QStringLiteral( "100" ) ).toFloat();
83 mAlpha = elem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "0.25" ) ).toFloat();
84 mBeta = elem.attribute( QStringLiteral( "beta" ), QStringLiteral( "0.5" ) ).toFloat();
85
87}
88
89void QgsGoochMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
90{
91 elem.setAttribute( QStringLiteral( "warm" ), QgsSymbolLayerUtils::encodeColor( mWarm ) );
92 elem.setAttribute( QStringLiteral( "cool" ), QgsSymbolLayerUtils::encodeColor( mCool ) );
93 elem.setAttribute( QStringLiteral( "diffuse" ), QgsSymbolLayerUtils::encodeColor( mDiffuse ) );
94 elem.setAttribute( QStringLiteral( "specular" ), QgsSymbolLayerUtils::encodeColor( mSpecular ) );
95 elem.setAttribute( QStringLiteral( "shininess2" ), mShininess );
96 elem.setAttribute( QStringLiteral( "alpha" ), mAlpha );
97 elem.setAttribute( QStringLiteral( "beta" ), mBeta );
98
100}
101
103{
104 return QMap<QString, QString>();
105}
106
108{
109 switch ( technique )
110 {
115 {
116 if ( dataDefinedProperties().hasActiveProperties() )
117 return dataDefinedMaterial();
118 Qt3DExtras::QGoochMaterial *material = new Qt3DExtras::QGoochMaterial;
119 material->setDiffuse( mDiffuse );
120 material->setWarm( mWarm );
121 material->setCool( mCool );
122
123 material->setSpecular( mSpecular );
124 material->setShininess( mShininess );
125 material->setAlpha( mAlpha );
126 material->setBeta( mBeta );
127
128 if ( context.isSelected() )
129 {
130 // update the material with selection colors
131 material->setDiffuse( context.selectionColor() );
132 }
133 return material;
134 }
135
139 return nullptr;
140 }
141 return nullptr;
142}
143
144void QgsGoochMaterialSettings::addParametersToEffect( Qt3DRender::QEffect * ) const
145{
146}
147
149{
150
151 const QColor diffuse = dataDefinedProperties().valueAsColor( Diffuse, expressionContext, mDiffuse );
152 const QColor warm = dataDefinedProperties().valueAsColor( Warm, expressionContext, mWarm );
153 const QColor cool = dataDefinedProperties().valueAsColor( Cool, expressionContext, mCool );
154 const QColor specular = dataDefinedProperties().valueAsColor( Specular, expressionContext, mSpecular );
155
156
157 QByteArray array;
158 array.resize( sizeof( unsigned char ) * 12 );
159 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
160
161 *fptr++ = static_cast<unsigned char>( diffuse.red() );
162 *fptr++ = static_cast<unsigned char>( diffuse.green() );
163 *fptr++ = static_cast<unsigned char>( diffuse.blue() );
164
165 *fptr++ = static_cast<unsigned char>( warm.red() );
166 *fptr++ = static_cast<unsigned char>( warm.green() );
167 *fptr++ = static_cast<unsigned char>( warm.blue() );
168
169 *fptr++ = static_cast<unsigned char>( cool.red() );
170 *fptr++ = static_cast<unsigned char>( cool.green() );
171 *fptr++ = static_cast<unsigned char>( cool.blue() );
172
173 *fptr++ = static_cast<unsigned char>( specular.red() );
174 *fptr++ = static_cast<unsigned char>( specular.green() );
175 *fptr++ = static_cast<unsigned char>( specular.blue() );
176
177 return array;
178}
179
181{
182 return 12 * sizeof( unsigned char );
183}
184
185void QgsGoochMaterialSettings::applyDataDefinedToGeometry( Qt3DQGeometry *geometry, int vertexCount, const QByteArray &data ) const
186{
187 Qt3DQBuffer *dataBuffer = new Qt3DQBuffer( geometry );
188
189 Qt3DQAttribute *diffuseAttribute = new Qt3DQAttribute( geometry );
190 diffuseAttribute->setName( QStringLiteral( "dataDefinedDiffuseColor" ) );
191 diffuseAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
192 diffuseAttribute->setVertexSize( 3 );
193 diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
194 diffuseAttribute->setBuffer( dataBuffer );
195 diffuseAttribute->setByteStride( 12 * sizeof( unsigned char ) );
196 diffuseAttribute->setByteOffset( 0 );
197 diffuseAttribute->setCount( vertexCount );
198 geometry->addAttribute( diffuseAttribute );
199
200 Qt3DQAttribute *warmAttribute = new Qt3DQAttribute( geometry );
201 warmAttribute->setName( QStringLiteral( "dataDefinedWarmColor" ) );
202 warmAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
203 warmAttribute->setVertexSize( 3 );
204 warmAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
205 warmAttribute->setBuffer( dataBuffer );
206 warmAttribute->setByteStride( 12 * sizeof( unsigned char ) );
207 warmAttribute->setByteOffset( 3 * sizeof( unsigned char ) );
208 warmAttribute->setCount( vertexCount );
209 geometry->addAttribute( warmAttribute );
210
211 Qt3DQAttribute *coolAttribute = new Qt3DQAttribute( geometry );
212 coolAttribute->setName( QStringLiteral( "dataDefinedCoolColor" ) );
213 coolAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
214 coolAttribute->setVertexSize( 3 );
215 coolAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
216 coolAttribute->setBuffer( dataBuffer );
217 coolAttribute->setByteStride( 12 * sizeof( unsigned char ) );
218 coolAttribute->setByteOffset( 6 * sizeof( unsigned char ) );
219 coolAttribute->setCount( vertexCount );
220 geometry->addAttribute( coolAttribute );
221
222
223 Qt3DQAttribute *specularAttribute = new Qt3DQAttribute( geometry );
224 specularAttribute->setName( QStringLiteral( "dataDefinedSpecularColor" ) );
225 specularAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
226 specularAttribute->setVertexSize( 3 );
227 specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
228 specularAttribute->setBuffer( dataBuffer );
229 specularAttribute->setByteStride( 12 * sizeof( unsigned char ) );
230 specularAttribute->setByteOffset( 9 * sizeof( unsigned char ) );
231 specularAttribute->setCount( vertexCount );
232 geometry->addAttribute( specularAttribute );
233
234 dataBuffer->setData( data );
235}
236
237Qt3DRender::QMaterial *QgsGoochMaterialSettings::dataDefinedMaterial() const
238{
239 Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial;
240
241 Qt3DRender::QEffect *eff = new Qt3DRender::QEffect( material );
242
243 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
244 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
245 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
246 technique->graphicsApiFilter()->setMajorVersion( 3 );
247 technique->graphicsApiFilter()->setMinorVersion( 3 );
248 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
249 filterKey->setName( QStringLiteral( "renderingStyle" ) );
250 filterKey->setValue( QStringLiteral( "forward" ) );
251 technique->addFilterKey( filterKey );
252
253 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
254 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
255
256 //Load shader programs
257 const QUrl urlVert( QStringLiteral( "qrc:/shaders/goochDataDefined.vert" ) );
258 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
259 const QUrl urlFrag( QStringLiteral( "qrc:/shaders/goochDataDefined.frag" ) );
260 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Fragment, Qt3DRender::QShaderProgram::loadSource( urlFrag ) );
261
262 renderPass->setShaderProgram( shaderProgram );
263 technique->addRenderPass( renderPass );
264
265 technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "shininess" ), mShininess ) );
266 technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "alpha" ), mAlpha ) );
267 technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "beta" ), mBeta ) );
268
269 eff->addTechnique( technique );
270 material->setEffect( eff );
271
272 return material;
273}
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.
@ Warm
Warm color (gooch material)
@ Cool
Cool color (gooch 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.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsGoochMaterialSettings * clone() const override
Clones the material settings.
QColor specular() const
Returns specular color component.
QColor cool() const
Returns cool color component.
int dataDefinedByteStride() const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
QByteArray dataDefinedVertexColorsAsByte(const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes settings to a DOM element.
QMap< QString, QString > toExportParameters() const override
Returns the parameters to be exported to .mtl file.
QColor warm() const
Returns warm color component.
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...
QString type() const override
Returns the unique type name for the material.
QColor diffuse() const
Returns diffuse color component.
QgsGoochMaterialSettings()=default
Constructor for QgsGoochMaterialSettings.
void addParametersToEffect(Qt3DRender::QEffect *effect) const override
Adds parameters from the material to a destination effect.
static QgsAbstractMaterialSettings * create()
Returns a new instance of QgsGoochMaterialSettings.
static bool supportsTechnique(QgsMaterialSettingsRenderingTechnique technique)
Returns true if the specified technique is supported by the Gooch material.
Qt3DRender::QMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QMaterial object representing the material settings.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
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.
The class is used as a container of context for various read/write operations on other objects.
static QColor decodeColor(const QString &str)
static QString encodeColor(const QColor &color)
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
Definition: qgs3daxis.cpp:28
Qt3DCore::QBuffer Qt3DQBuffer
Definition: qgs3daxis.cpp:30
Qt3DCore::QGeometry Qt3DQGeometry
Definition: qgs3daxis.cpp:29
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry