QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgsgoochmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgoochmaterial3dhandler.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"
21
22#include <QString>
23#include <QUrl>
24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QGeometry>
27#include <Qt3DExtras/QGoochMaterial>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
32
33using namespace Qt::StringLiterals;
34
35
37{
38 return QMap<QString, QString>();
39}
40
42{
43 switch ( technique )
44 {
49 {
50 if ( context.isHighlighted() )
51 {
52 return new QgsHighlightMaterial( technique );
53 }
54
55 return buildMaterial( settings, context );
56 }
57
62 return nullptr;
63 }
64 return nullptr;
65}
66
69
71{
72 const QgsGoochMaterialSettings *goochSettings = dynamic_cast< const QgsGoochMaterialSettings * >( settings );
73 Q_ASSERT( goochSettings );
74 const QgsPropertyCollection &dataDefinedProperties = goochSettings->dataDefinedProperties();
75 const QColor diffuse = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, goochSettings->diffuse() );
76 const QColor warm = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Warm, expressionContext, goochSettings->warm() );
77 const QColor cool = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Cool, expressionContext, goochSettings->cool() );
78 const QColor specular = dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, goochSettings->specular() );
79
80 QByteArray array;
81 array.resize( sizeof( unsigned char ) * 12 );
82 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
83
84 *fptr++ = static_cast<unsigned char>( diffuse.red() );
85 *fptr++ = static_cast<unsigned char>( diffuse.green() );
86 *fptr++ = static_cast<unsigned char>( diffuse.blue() );
87
88 *fptr++ = static_cast<unsigned char>( warm.red() );
89 *fptr++ = static_cast<unsigned char>( warm.green() );
90 *fptr++ = static_cast<unsigned char>( warm.blue() );
91
92 *fptr++ = static_cast<unsigned char>( cool.red() );
93 *fptr++ = static_cast<unsigned char>( cool.green() );
94 *fptr++ = static_cast<unsigned char>( cool.blue() );
95
96 *fptr++ = static_cast<unsigned char>( specular.red() );
97 *fptr++ = static_cast<unsigned char>( specular.green() );
98 *fptr++ = static_cast<unsigned char>( specular.blue() );
99
100 return array;
101}
102
104{
105 return 12 * sizeof( unsigned char );
106}
107
108void QgsGoochMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
109{
110 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
111
112 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
113 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
114 diffuseAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
115 diffuseAttribute->setVertexSize( 3 );
116 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
117 diffuseAttribute->setBuffer( dataBuffer );
118 diffuseAttribute->setByteStride( 12 * sizeof( unsigned char ) );
119 diffuseAttribute->setByteOffset( 0 );
120 diffuseAttribute->setCount( vertexCount );
121 geometry->addAttribute( diffuseAttribute );
122
123 Qt3DCore::QAttribute *warmAttribute = new Qt3DCore::QAttribute( geometry );
124 warmAttribute->setName( u"dataDefinedWarmColor"_s );
125 warmAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
126 warmAttribute->setVertexSize( 3 );
127 warmAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
128 warmAttribute->setBuffer( dataBuffer );
129 warmAttribute->setByteStride( 12 * sizeof( unsigned char ) );
130 warmAttribute->setByteOffset( 3 * sizeof( unsigned char ) );
131 warmAttribute->setCount( vertexCount );
132 geometry->addAttribute( warmAttribute );
133
134 Qt3DCore::QAttribute *coolAttribute = new Qt3DCore::QAttribute( geometry );
135 coolAttribute->setName( u"dataDefinedCoolColor"_s );
136 coolAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
137 coolAttribute->setVertexSize( 3 );
138 coolAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
139 coolAttribute->setBuffer( dataBuffer );
140 coolAttribute->setByteStride( 12 * sizeof( unsigned char ) );
141 coolAttribute->setByteOffset( 6 * sizeof( unsigned char ) );
142 coolAttribute->setCount( vertexCount );
143 geometry->addAttribute( coolAttribute );
144
145 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
146 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
147 specularAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
148 specularAttribute->setVertexSize( 3 );
149 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
150 specularAttribute->setBuffer( dataBuffer );
151 specularAttribute->setByteStride( 12 * sizeof( unsigned char ) );
152 specularAttribute->setByteOffset( 9 * sizeof( unsigned char ) );
153 specularAttribute->setCount( vertexCount );
154 geometry->addAttribute( specularAttribute );
155
156 dataBuffer->setData( data );
157}
158
159bool QgsGoochMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
160{
161 const QgsGoochMaterialSettings *goochSettings = qgis::down_cast< const QgsGoochMaterialSettings * >( settings );
162
163 QgsMaterial *material = sceneRoot->findChild<QgsMaterial *>();
164 if ( material->objectName() != "goochMaterial"_L1 )
165 return false;
166
167 Qt3DRender::QEffect *effect = material->effect();
168
169 if ( Qt3DRender::QParameter *p = findParameter( effect, u"kd"_s ) )
170 p->setValue( goochSettings->diffuse() );
171 if ( Qt3DRender::QParameter *p = findParameter( effect, u"ks"_s ) )
172 p->setValue( goochSettings->specular() );
173 if ( Qt3DRender::QParameter *p = findParameter( effect, u"kblue"_s ) )
174 p->setValue( goochSettings->cool() );
175 if ( Qt3DRender::QParameter *p = findParameter( effect, u"kyellow"_s ) )
176 p->setValue( goochSettings->warm() );
177 if ( Qt3DRender::QParameter *p = findParameter( effect, u"shininess"_s ) )
178 p->setValue( goochSettings->shininess() );
179 if ( Qt3DRender::QParameter *p = findParameter( effect, u"alpha"_s ) )
180 p->setValue( goochSettings->alpha() );
181 if ( Qt3DRender::QParameter *p = findParameter( effect, u"beta"_s ) )
182 p->setValue( goochSettings->beta() );
183
184 return true;
185}
186
187QgsMaterial *QgsGoochMaterial3DHandler::buildMaterial( const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
188{
189 const QgsGoochMaterialSettings *goochSettings = dynamic_cast< const QgsGoochMaterialSettings * >( settings );
190 Q_ASSERT( goochSettings );
191 const QgsPropertyCollection &dataDefinedProperties = goochSettings->dataDefinedProperties();
192
193 QgsMaterial *material = new QgsMaterial;
194 material->setObjectName( u"goochMaterial"_s );
195
196 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
197
198 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
199 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
200 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
201 technique->graphicsApiFilter()->setMajorVersion( 3 );
202 technique->graphicsApiFilter()->setMinorVersion( 3 );
203 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
204 filterKey->setName( u"renderingStyle"_s );
205 filterKey->setValue( u"forward"_s );
206 technique->addFilterKey( filterKey );
207
208 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
209 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
210
211 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u"qrc:/shaders/gooch.frag"_s ) );
212
213 if ( dataDefinedProperties.hasActiveProperties() )
214 {
215 //Load shader programs
216 const QUrl urlVert( u"qrc:/shaders/goochDataDefined.vert"_s );
217 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
218
219 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
220 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
221 }
222 else
223 {
224 //Load shader programs
225 const QUrl urlVert( u"qrc:/shaders/default.vert"_s );
226 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
227 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
228
229 const QColor diffuseColor = context.isSelected() ? context.selectionColor() : goochSettings->diffuse();
230 effect->addParameter( new Qt3DRender::QParameter( u"kd"_s, diffuseColor ) );
231 effect->addParameter( new Qt3DRender::QParameter( u"ks"_s, goochSettings->specular() ) );
232 effect->addParameter( new Qt3DRender::QParameter( u"kblue"_s, goochSettings->cool() ) );
233 effect->addParameter( new Qt3DRender::QParameter( u"kyellow"_s, goochSettings->warm() ) );
234 }
235
236 renderPass->setShaderProgram( shaderProgram );
237 technique->addRenderPass( renderPass );
238
239 technique->addParameter( new Qt3DRender::QParameter( u"shininess"_s, goochSettings->shininess() ) );
240 technique->addParameter( new Qt3DRender::QParameter( u"alpha"_s, goochSettings->alpha() ) );
241 technique->addParameter( new Qt3DRender::QParameter( u"beta"_s, goochSettings->beta() ) );
242
243 effect->addTechnique( technique );
244 material->setEffect( effect );
245
246 return material;
247}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4327
@ Points
Point based rendering, requires point data.
Definition qgis.h:4331
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4328
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4333
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4329
@ Billboards
Flat billboard rendering.
Definition qgis.h:4335
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4334
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4330
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4332
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.
static Qt3DRender::QParameter * findParameter(Qt3DRender::QEffect *effect, const QString &name)
Finds an existing parameter in an effect by name.
Abstract base class for material settings.
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...
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
void applyDataDefinedToGeometry(const QgsAbstractMaterialSettings *settings, 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...
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext) const override
Adds parameters from the material settings to a destination effect.
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
int dataDefinedByteStride(const QgsAbstractMaterialSettings *settings) const override
Returns byte stride of the data defined colors,used to fill the vertex colors data defined buffer for...
Basic shading material used for rendering based on the Phong shading model with three color component...
QColor specular() const
Returns specular color component.
double alpha() const
Returns the alpha value.
QColor cool() const
Returns cool color component.
QColor warm() const
Returns warm color component.
QColor diffuse() const
Returns diffuse color component.
double beta() const
Returns the beta value.
double shininess() const
Returns shininess of the surface.
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.
bool isHighlighted() const
Returns true if the material should represent a highlighted state.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:40
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
bool hasActiveProperties() const final
Returns true if the collection has any active properties, or false if all properties within the colle...