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