QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgs3dutils.h"
19#include "qgscolorutils.h"
20
21#include <QString>
22#include <QUrl>
23#include <Qt3DCore/QAttribute>
24#include <Qt3DCore/QBuffer>
25#include <Qt3DCore/QGeometry>
26#include <Qt3DExtras/QGoochMaterial>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QGraphicsApiFilter>
29#include <Qt3DRender/QParameter>
30#include <Qt3DRender/QTechnique>
31
32using namespace Qt::StringLiterals;
33
35{
36 return u"gooch"_s;
37}
38
43
61
66
68{
69 const QgsGoochMaterialSettings *otherGooch = dynamic_cast<const QgsGoochMaterialSettings *>( other );
70 if ( !otherGooch )
71 return false;
72
73 return *this == *otherGooch;
74}
75
76void QgsGoochMaterialSettings::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
77{
78 mWarm = QgsColorUtils::colorFromString( elem.attribute( u"warm"_s, u"107,0,107"_s ) );
79 mCool = QgsColorUtils::colorFromString( elem.attribute( u"cool"_s, u"255,130,0"_s ) );
80 mDiffuse = QgsColorUtils::colorFromString( elem.attribute( u"diffuse"_s, u"178,178,178"_s ) );
81 mSpecular = QgsColorUtils::colorFromString( elem.attribute( u"specular"_s ) );
82 mShininess = elem.attribute( u"shininess2"_s, u"100"_s ).toDouble();
83 mAlpha = elem.attribute( u"alpha"_s, u"0.25"_s ).toDouble();
84 mBeta = elem.attribute( u"beta"_s, u"0.5"_s ).toDouble();
85
87}
88
89void QgsGoochMaterialSettings::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
90{
91 elem.setAttribute( u"warm"_s, QgsColorUtils::colorToString( mWarm ) );
92 elem.setAttribute( u"cool"_s, QgsColorUtils::colorToString( mCool ) );
93 elem.setAttribute( u"diffuse"_s, QgsColorUtils::colorToString( mDiffuse ) );
94 elem.setAttribute( u"specular"_s, QgsColorUtils::colorToString( mSpecular ) );
95 elem.setAttribute( u"shininess2"_s, mShininess );
96 elem.setAttribute( u"alpha"_s, mAlpha );
97 elem.setAttribute( u"beta"_s, mBeta );
98
100}
101
103{
104 return QMap<QString, QString>();
105}
106
126
127void QgsGoochMaterialSettings::addParametersToEffect( Qt3DRender::QEffect *, const QgsMaterialContext & ) const
128{
129}
130
132{
133 const QColor diffuse = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, mDiffuse );
134 const QColor warm = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Warm, expressionContext, mWarm );
135 const QColor cool = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Cool, expressionContext, mCool );
136 const QColor specular = dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, mSpecular );
137
138
139 QByteArray array;
140 array.resize( sizeof( unsigned char ) * 12 );
141 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
142
143 *fptr++ = static_cast<unsigned char>( diffuse.red() );
144 *fptr++ = static_cast<unsigned char>( diffuse.green() );
145 *fptr++ = static_cast<unsigned char>( diffuse.blue() );
146
147 *fptr++ = static_cast<unsigned char>( warm.red() );
148 *fptr++ = static_cast<unsigned char>( warm.green() );
149 *fptr++ = static_cast<unsigned char>( warm.blue() );
150
151 *fptr++ = static_cast<unsigned char>( cool.red() );
152 *fptr++ = static_cast<unsigned char>( cool.green() );
153 *fptr++ = static_cast<unsigned char>( cool.blue() );
154
155 *fptr++ = static_cast<unsigned char>( specular.red() );
156 *fptr++ = static_cast<unsigned char>( specular.green() );
157 *fptr++ = static_cast<unsigned char>( specular.blue() );
158
159 return array;
160}
161
163{
164 return 12 * sizeof( unsigned char );
165}
166
167void QgsGoochMaterialSettings::applyDataDefinedToGeometry( Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
168{
169 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
170
171 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
172 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
173 diffuseAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
174 diffuseAttribute->setVertexSize( 3 );
175 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
176 diffuseAttribute->setBuffer( dataBuffer );
177 diffuseAttribute->setByteStride( 12 * sizeof( unsigned char ) );
178 diffuseAttribute->setByteOffset( 0 );
179 diffuseAttribute->setCount( vertexCount );
180 geometry->addAttribute( diffuseAttribute );
181
182 Qt3DCore::QAttribute *warmAttribute = new Qt3DCore::QAttribute( geometry );
183 warmAttribute->setName( u"dataDefinedWarmColor"_s );
184 warmAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
185 warmAttribute->setVertexSize( 3 );
186 warmAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
187 warmAttribute->setBuffer( dataBuffer );
188 warmAttribute->setByteStride( 12 * sizeof( unsigned char ) );
189 warmAttribute->setByteOffset( 3 * sizeof( unsigned char ) );
190 warmAttribute->setCount( vertexCount );
191 geometry->addAttribute( warmAttribute );
192
193 Qt3DCore::QAttribute *coolAttribute = new Qt3DCore::QAttribute( geometry );
194 coolAttribute->setName( u"dataDefinedCoolColor"_s );
195 coolAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
196 coolAttribute->setVertexSize( 3 );
197 coolAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
198 coolAttribute->setBuffer( dataBuffer );
199 coolAttribute->setByteStride( 12 * sizeof( unsigned char ) );
200 coolAttribute->setByteOffset( 6 * sizeof( unsigned char ) );
201 coolAttribute->setCount( vertexCount );
202 geometry->addAttribute( coolAttribute );
203
204
205 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
206 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
207 specularAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
208 specularAttribute->setVertexSize( 3 );
209 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
210 specularAttribute->setBuffer( dataBuffer );
211 specularAttribute->setByteStride( 12 * sizeof( unsigned char ) );
212 specularAttribute->setByteOffset( 9 * sizeof( unsigned char ) );
213 specularAttribute->setCount( vertexCount );
214 geometry->addAttribute( specularAttribute );
215
216 dataBuffer->setData( data );
217}
218
219QgsMaterial *QgsGoochMaterialSettings::buildMaterial( const QgsMaterialContext &context ) const
220{
221 QgsMaterial *material = new QgsMaterial;
222
223 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
224
225 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
226 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
227 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
228 technique->graphicsApiFilter()->setMajorVersion( 3 );
229 technique->graphicsApiFilter()->setMinorVersion( 3 );
230 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
231 filterKey->setName( u"renderingStyle"_s );
232 filterKey->setValue( u"forward"_s );
233 technique->addFilterKey( filterKey );
234
235 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
236 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
237
238 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u"qrc:/shaders/gooch.frag"_s ) );
239
240 if ( dataDefinedProperties().hasActiveProperties() )
241 {
242 //Load shader programs
243 const QUrl urlVert( u"qrc:/shaders/goochDataDefined.vert"_s );
244 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
245
246 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
247 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
248 }
249 else
250 {
251 //Load shader programs
252 const QUrl urlVert( u"qrc:/shaders/default.vert"_s );
253 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
254 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
255
256 const QColor diffuseColor = context.isSelected() ? context.selectionColor() : mDiffuse;
257 effect->addParameter( new Qt3DRender::QParameter( u"kd"_s, diffuseColor ) );
258 effect->addParameter( new Qt3DRender::QParameter( u"ks"_s, mSpecular ) );
259 effect->addParameter( new Qt3DRender::QParameter( u"kblue"_s, mCool ) );
260 effect->addParameter( new Qt3DRender::QParameter( u"kyellow"_s, mWarm ) );
261 }
262
263 renderPass->setShaderProgram( shaderProgram );
264 technique->addRenderPass( renderPass );
265
266 technique->addParameter( new Qt3DRender::QParameter( u"shininess"_s, mShininess ) );
267 technique->addParameter( new Qt3DRender::QParameter( u"alpha"_s, mAlpha ) );
268 technique->addParameter( new Qt3DRender::QParameter( u"beta"_s, mBeta ) );
269
270 effect->addTechnique( technique );
271 material->setEffect( effect );
272
273 return material;
274}
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...
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.
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
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).