QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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#include "qgscolorutils.h"
18#include "qgs3dutils.h"
19
20#include <Qt3DExtras/QGoochMaterial>
21#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
22#include <Qt3DRender/QAttribute>
23#include <Qt3DRender/QBuffer>
24#include <Qt3DRender/QGeometry>
25
26typedef Qt3DRender::QAttribute Qt3DQAttribute;
27typedef Qt3DRender::QBuffer Qt3DQBuffer;
28typedef Qt3DRender::QGeometry Qt3DQGeometry;
29#else
30#include <Qt3DCore/QAttribute>
31#include <Qt3DCore/QBuffer>
32#include <Qt3DCore/QGeometry>
33
34typedef Qt3DCore::QAttribute Qt3DQAttribute;
35typedef Qt3DCore::QBuffer Qt3DQBuffer;
36typedef Qt3DCore::QGeometry Qt3DQGeometry;
37#endif
38#include <Qt3DRender/QParameter>
39#include <Qt3DRender/QEffect>
40#include <Qt3DRender/QTechnique>
41#include <Qt3DRender/QGraphicsApiFilter>
42#include <QUrl>
43
45{
46 return QStringLiteral( "gooch" );
47}
48
53
71
76
78{
79 const QgsGoochMaterialSettings *otherGooch = dynamic_cast<const QgsGoochMaterialSettings *>( other );
80 if ( !otherGooch )
81 return false;
82
83 return *this == *otherGooch;
84}
85
86void QgsGoochMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
87{
88 mWarm = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "warm" ), QStringLiteral( "107,0,107" ) ) );
89 mCool = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "cool" ), QStringLiteral( "255,130,0" ) ) );
90 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "diffuse" ), QStringLiteral( "178,178,178" ) ) );
91 mSpecular = QgsColorUtils::colorFromString( elem.attribute( QStringLiteral( "specular" ) ) );
92 mShininess = elem.attribute( QStringLiteral( "shininess2" ), QStringLiteral( "100" ) ).toDouble();
93 mAlpha = elem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "0.25" ) ).toDouble();
94 mBeta = elem.attribute( QStringLiteral( "beta" ), QStringLiteral( "0.5" ) ).toDouble();
95
97}
98
99void QgsGoochMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
100{
101 elem.setAttribute( QStringLiteral( "warm" ), QgsColorUtils::colorToString( mWarm ) );
102 elem.setAttribute( QStringLiteral( "cool" ), QgsColorUtils::colorToString( mCool ) );
103 elem.setAttribute( QStringLiteral( "diffuse" ), QgsColorUtils::colorToString( mDiffuse ) );
104 elem.setAttribute( QStringLiteral( "specular" ), QgsColorUtils::colorToString( mSpecular ) );
105 elem.setAttribute( QStringLiteral( "shininess2" ), mShininess );
106 elem.setAttribute( QStringLiteral( "alpha" ), mAlpha );
107 elem.setAttribute( QStringLiteral( "beta" ), mBeta );
108
110}
111
113{
114 return QMap<QString, QString>();
115}
116
136
137void QgsGoochMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *, const QgsMaterialContext & ) const
138{
139}
140
142{
143 const QColor diffuse = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, mDiffuse );
144 const QColor warm = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Warm, expressionContext, mWarm );
145 const QColor cool = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Cool, expressionContext, mCool );
146 const QColor specular = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, mSpecular );
147
148
149 QByteArray array;
150 array.resize( sizeof( unsigned char ) * 12 );
151 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
152
153 *fptr++ = static_cast<unsigned char>( diffuse.red() );
154 *fptr++ = static_cast<unsigned char>( diffuse.green() );
155 *fptr++ = static_cast<unsigned char>( diffuse.blue() );
156
157 *fptr++ = static_cast<unsigned char>( warm.red() );
158 *fptr++ = static_cast<unsigned char>( warm.green() );
159 *fptr++ = static_cast<unsigned char>( warm.blue() );
160
161 *fptr++ = static_cast<unsigned char>( cool.red() );
162 *fptr++ = static_cast<unsigned char>( cool.green() );
163 *fptr++ = static_cast<unsigned char>( cool.blue() );
164
165 *fptr++ = static_cast<unsigned char>( specular.red() );
166 *fptr++ = static_cast<unsigned char>( specular.green() );
167 *fptr++ = static_cast<unsigned char>( specular.blue() );
168
169 return array;
170}
171
173{
174 return 12 * sizeof( unsigned char );
175}
176
177void QgsGoochMaterialSettings::applyDataDefinedToGeometry( Qt3DQGeometry *geometry, int vertexCount, const QByteArray &data ) const
178{
179 Qt3DQBuffer *dataBuffer = new Qt3DQBuffer( geometry );
180
181 Qt3DQAttribute *diffuseAttribute = new Qt3DQAttribute( geometry );
182 diffuseAttribute->setName( QStringLiteral( "dataDefinedDiffuseColor" ) );
183 diffuseAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
184 diffuseAttribute->setVertexSize( 3 );
185 diffuseAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
186 diffuseAttribute->setBuffer( dataBuffer );
187 diffuseAttribute->setByteStride( 12 * sizeof( unsigned char ) );
188 diffuseAttribute->setByteOffset( 0 );
189 diffuseAttribute->setCount( vertexCount );
190 geometry->addAttribute( diffuseAttribute );
191
192 Qt3DQAttribute *warmAttribute = new Qt3DQAttribute( geometry );
193 warmAttribute->setName( QStringLiteral( "dataDefinedWarmColor" ) );
194 warmAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
195 warmAttribute->setVertexSize( 3 );
196 warmAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
197 warmAttribute->setBuffer( dataBuffer );
198 warmAttribute->setByteStride( 12 * sizeof( unsigned char ) );
199 warmAttribute->setByteOffset( 3 * sizeof( unsigned char ) );
200 warmAttribute->setCount( vertexCount );
201 geometry->addAttribute( warmAttribute );
202
203 Qt3DQAttribute *coolAttribute = new Qt3DQAttribute( geometry );
204 coolAttribute->setName( QStringLiteral( "dataDefinedCoolColor" ) );
205 coolAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
206 coolAttribute->setVertexSize( 3 );
207 coolAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
208 coolAttribute->setBuffer( dataBuffer );
209 coolAttribute->setByteStride( 12 * sizeof( unsigned char ) );
210 coolAttribute->setByteOffset( 6 * sizeof( unsigned char ) );
211 coolAttribute->setCount( vertexCount );
212 geometry->addAttribute( coolAttribute );
213
214
215 Qt3DQAttribute *specularAttribute = new Qt3DQAttribute( geometry );
216 specularAttribute->setName( QStringLiteral( "dataDefinedSpecularColor" ) );
217 specularAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedByte );
218 specularAttribute->setVertexSize( 3 );
219 specularAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
220 specularAttribute->setBuffer( dataBuffer );
221 specularAttribute->setByteStride( 12 * sizeof( unsigned char ) );
222 specularAttribute->setByteOffset( 9 * sizeof( unsigned char ) );
223 specularAttribute->setCount( vertexCount );
224 geometry->addAttribute( specularAttribute );
225
226 dataBuffer->setData( data );
227}
228
229QgsMaterial *QgsGoochMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const
230{
231 QgsMaterial *material = new QgsMaterial;
232
233 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
234
235 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
236 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
237 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
238 technique->graphicsApiFilter()->setMajorVersion( 3 );
239 technique->graphicsApiFilter()->setMinorVersion( 3 );
240 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
241 filterKey->setName( QStringLiteral( "renderingStyle" ) );
242 filterKey->setValue( QStringLiteral( "forward" ) );
243 technique->addFilterKey( filterKey );
244
245 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
246 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
247
248 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/gooch.frag" ) ) );
249
250 if ( dataDefinedProperties().hasActiveProperties() )
251 {
252 //Load shader programs
253 const QUrl urlVert( QStringLiteral( "qrc:/shaders/goochDataDefined.vert" ) );
254 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
255
256 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
257 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
258 }
259 else
260 {
261 //Load shader programs
262 const QUrl urlVert( QStringLiteral( "qrc:/shaders/default.vert" ) );
263 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
264 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
265
266 const QColor diffuseColor = context.isSelected() ? context.selectionColor() : mDiffuse;
267 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "kd" ), diffuseColor ) );
268 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "ks" ), mSpecular ) );
269 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "kblue" ), mCool ) );
270 effect->addParameter( new Qt3DRender::QParameter( QStringLiteral( "kyellow" ), mWarm ) );
271 }
272
273 renderPass->setShaderProgram( shaderProgram );
274 technique->addRenderPass( renderPass );
275
276 technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "shininess" ), mShininess ) );
277 technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "alpha" ), mAlpha ) );
278 technique->addParameter( new Qt3DRender::QParameter( QStringLiteral( "beta" ), mBeta ) );
279
280 effect->addTechnique( technique );
281 material->setEffect( effect );
282
283 return material;
284}
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.
@ 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.
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...
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.
bool equals(const QgsAbstractMaterialSettings *other) const override
Returns true if this settings exactly matches an other settings.
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
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.
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads settings from a DOM element.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsMaterialContext &materialContext) const override
Adds parameters from the material to a destination effect.
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.
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