QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgsphongmaterial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsphongmaterial3dhandler.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk 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 <QMap>
23#include <QString>
24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QGeometry>
27#include <Qt3DRender/QEffect>
28#include <Qt3DRender/QGraphicsApiFilter>
29#include <Qt3DRender/QParameter>
30#include <Qt3DRender/QTechnique>
31
32using namespace Qt::StringLiterals;
33
34
36{
37 switch ( technique )
38 {
45 {
46 if ( context.isHighlighted() )
47 {
48 return new QgsHighlightMaterial( technique );
49 }
50
51 return buildMaterial( settings, context );
52 }
53
56 return nullptr;
57 }
58 return nullptr;
59}
60
62{
63 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
64 Q_ASSERT( phongSettings );
65
66 QMap<QString, QString> parameters;
67 parameters[u"Kd"_s] = u"%1 %2 %3"_s.arg( phongSettings->diffuse().redF() ).arg( phongSettings->diffuse().greenF() ).arg( phongSettings->diffuse().blueF() );
68 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
69 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
70 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
71 return parameters;
72}
73
74void QgsPhongMaterial3DHandler::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext ) const
75{
76 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
77 Q_ASSERT( phongSettings );
78
79 const QColor ambient = materialContext.isSelected() ? materialContext.selectionColor().darker() : phongSettings->ambient();
80 const QColor diffuse = materialContext.isSelected() ? materialContext.selectionColor() : phongSettings->diffuse();
81
82 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter(
83 u"ambientColor"_s,
84 QColor::fromRgbF(
85 static_cast< float >( ambient.redF() * phongSettings->ambientCoefficient() ),
86 static_cast< float >( ambient.greenF() * phongSettings->ambientCoefficient() ),
87 static_cast< float >( ambient.blueF() * phongSettings->ambientCoefficient() )
88 )
89 );
90 Qt3DRender::QParameter *diffuseParameter = new Qt3DRender::QParameter(
91 u"diffuseColor"_s,
92 QColor::fromRgbF(
93 static_cast<float >( diffuse.redF() * phongSettings->diffuseCoefficient() ),
94 static_cast< float >( diffuse.greenF() * phongSettings->diffuseCoefficient() ),
95 static_cast< float >( diffuse.blueF() * phongSettings->diffuseCoefficient() )
96 )
97 );
98 Qt3DRender::QParameter *specularParameter = new Qt3DRender::QParameter(
99 u"specularColor"_s,
100 QColor::fromRgbF(
101 static_cast< float >( phongSettings->specular().redF() * phongSettings->specularCoefficient() ),
102 static_cast< float >( phongSettings->specular().greenF() * phongSettings->specularCoefficient() ),
103 static_cast< float >( phongSettings->specular().blueF() * phongSettings->specularCoefficient() )
104 )
105 );
106 Qt3DRender::QParameter *shininessParameter = new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( phongSettings->shininess() ) );
107 Qt3DRender::QParameter *opacityParameter = new Qt3DRender::QParameter( u"opacity"_s, static_cast<float>( phongSettings->opacity() ) );
108
109 effect->addParameter( ambientParameter );
110 effect->addParameter( diffuseParameter );
111 effect->addParameter( specularParameter );
112 effect->addParameter( shininessParameter );
113 effect->addParameter( opacityParameter );
114}
115
117{
118 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
119 Q_ASSERT( phongSettings );
120
121 const QColor ambient = phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, phongSettings->ambient() );
122 const QColor diffuse = phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, phongSettings->diffuse() );
123 const QColor specular = phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, phongSettings->specular() );
124
125 const double diffuseCoefficient = phongSettings->diffuseCoefficient();
126 const double ambientCoefficient = phongSettings->ambientCoefficient();
127 const double specularCoefficient = phongSettings->specularCoefficient();
128
129 QByteArray array;
130 if ( diffuseCoefficient < 1 || ambientCoefficient < 1 || specularCoefficient < 1 )
131 {
132 // use floats if we are adjusting color component strength, bytes don't
133 // give us enough precision
134 array.resize( sizeof( float ) * 9 );
135 float *fptr = reinterpret_cast<float *>( array.data() );
136
137 *fptr++ = static_cast<float>( diffuse.redF() * diffuseCoefficient );
138 *fptr++ = static_cast<float>( diffuse.greenF() * diffuseCoefficient );
139 *fptr++ = static_cast<float>( diffuse.blueF() * diffuseCoefficient );
140
141 *fptr++ = static_cast<float>( ambient.redF() * ambientCoefficient );
142 *fptr++ = static_cast<float>( ambient.greenF() * ambientCoefficient );
143 *fptr++ = static_cast<float>( ambient.blueF() * ambientCoefficient );
144
145 *fptr++ = static_cast<float>( specular.redF() * specularCoefficient );
146 *fptr++ = static_cast<float>( specular.greenF() * specularCoefficient );
147 *fptr++ = static_cast<float>( specular.blueF() * specularCoefficient );
148 }
149 else
150 {
151 array.resize( sizeof( unsigned char ) * 9 );
152 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
153
154 *ptr++ = static_cast<unsigned char>( diffuse.red() );
155 *ptr++ = static_cast<unsigned char>( diffuse.green() );
156 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
157
158 *ptr++ = static_cast<unsigned char>( ambient.red() );
159 *ptr++ = static_cast<unsigned char>( ambient.green() );
160 *ptr++ = static_cast<unsigned char>( ambient.blue() );
161
162 *ptr++ = static_cast<unsigned char>( specular.red() );
163 *ptr++ = static_cast<unsigned char>( specular.green() );
164 *ptr++ = static_cast<unsigned char>( specular.blue() );
165 }
166
167 return array;
168}
169
171{
172 return 9 * sizeof( unsigned char );
173}
174
175void QgsPhongMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
176{
177 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
178 Q_ASSERT( phongSettings );
179
180 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
181
182 // use floats if we are adjusting color component strength, bytes don't
183 // give us enough precision
184 const bool useFloats = phongSettings->diffuseCoefficient() < 1 || phongSettings->ambientCoefficient() < 1 || phongSettings->specularCoefficient() < 1;
185
186 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
187 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
188 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
189 diffuseAttribute->setVertexSize( 3 );
190 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
191 diffuseAttribute->setBuffer( dataBuffer );
192 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
193 diffuseAttribute->setByteOffset( 0 );
194 diffuseAttribute->setCount( vertexCount );
195 geometry->addAttribute( diffuseAttribute );
196
197 Qt3DCore::QAttribute *ambientAttribute = new Qt3DCore::QAttribute( geometry );
198 ambientAttribute->setName( u"dataDefinedAmbiantColor"_s );
199 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
200 ambientAttribute->setVertexSize( 3 );
201 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
202 ambientAttribute->setBuffer( dataBuffer );
203 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
204 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
205 ambientAttribute->setCount( vertexCount );
206 geometry->addAttribute( ambientAttribute );
207
208 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
209 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
210 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
211 specularAttribute->setVertexSize( 3 );
212 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
213 specularAttribute->setBuffer( dataBuffer );
214 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
215 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
216 specularAttribute->setCount( vertexCount );
217 geometry->addAttribute( specularAttribute );
218
219 dataBuffer->setData( data );
220}
221
222bool QgsPhongMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
223{
224 const QgsPhongMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongMaterialSettings * >( settings );
225
226 QgsMaterial *material = sceneRoot->findChild<QgsMaterial *>();
227 if ( material->objectName() != "phongMaterial"_L1 )
228 return false;
229
230 Qt3DRender::QEffect *effect = material->effect();
231
232 const QColor ambient = phongSettings->ambient();
233 if ( Qt3DRender::QParameter *p = findParameter( effect, u"ambientColor"_s ) )
234 {
235 p->setValue(
236 QColor::fromRgbF(
237 static_cast< float >( ambient.redF() * phongSettings->ambientCoefficient() ),
238 static_cast< float >( ambient.greenF() * phongSettings->ambientCoefficient() ),
239 static_cast< float >( ambient.blueF() * phongSettings->ambientCoefficient() )
240 )
241 );
242 }
243 const QColor diffuse = phongSettings->diffuse();
244 if ( Qt3DRender::QParameter *p = findParameter( effect, u"diffuseColor"_s ) )
245 {
246 p->setValue(
247 QColor::fromRgbF(
248 static_cast<float >( diffuse.redF() * phongSettings->diffuseCoefficient() ),
249 static_cast< float >( diffuse.greenF() * phongSettings->diffuseCoefficient() ),
250 static_cast< float >( diffuse.blueF() * phongSettings->diffuseCoefficient() )
251 )
252 );
253 }
254
255 const QColor specularColor = phongSettings->specular();
256 if ( Qt3DRender::QParameter *p = findParameter( effect, u"specularColor"_s ) )
257 {
258 p->setValue(
259 QColor::fromRgbF(
260 static_cast< float >( specularColor.redF() * phongSettings->specularCoefficient() ),
261 static_cast< float >( specularColor.greenF() * phongSettings->specularCoefficient() ),
262 static_cast< float >( specularColor.blueF() * phongSettings->specularCoefficient() )
263 )
264 );
265 }
266
267 if ( Qt3DRender::QParameter *p = findParameter( effect, u"shininess"_s ) )
268 p->setValue( static_cast<float>( phongSettings->shininess() ) );
269
270 if ( Qt3DRender::QParameter *p = findParameter( effect, u"opacity"_s ) )
271 p->setValue( static_cast<float>( phongSettings->opacity() ) );
272
273 return true;
274}
275
276QgsMaterial *QgsPhongMaterial3DHandler::buildMaterial( const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context ) const
277{
278 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
279 Q_ASSERT( phongSettings );
280
281 QgsMaterial *material = new QgsMaterial;
282 material->setObjectName( u"phongMaterial"_s );
283
284 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect( material );
285
286 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
287 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
288 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
289 technique->graphicsApiFilter()->setMajorVersion( 3 );
290 technique->graphicsApiFilter()->setMinorVersion( 3 );
291 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey();
292 filterKey->setName( u"renderingStyle"_s );
293 filterKey->setValue( u"forward"_s );
294 technique->addFilterKey( filterKey );
295
296 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
297 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram();
298
299 renderPass->setShaderProgram( shaderProgram );
300 technique->addRenderPass( renderPass );
301
302 const QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( u"qrc:/shaders/phong.frag"_s ) );
303
304 if ( phongSettings->dataDefinedProperties().hasActiveProperties() )
305 {
306 // Load shader programs
307 const QUrl urlVert( u"qrc:/shaders/phongDataDefined.vert"_s );
308 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
309 const QByteArray finalFragmentShaderCode = Qgs3DUtils::addDefinesToShaderCode( fragmentShaderCode, QStringList( { "DATA_DEFINED" } ) );
310 shaderProgram->setFragmentShaderCode( finalFragmentShaderCode );
311 }
312 else
313 {
314 // Load shader programs
315 const QUrl urlVert( u"qrc:/shaders/default.vert"_s );
316 shaderProgram->setShaderCode( Qt3DRender::QShaderProgram::Vertex, Qt3DRender::QShaderProgram::loadSource( urlVert ) );
317 shaderProgram->setFragmentShaderCode( fragmentShaderCode );
318
319 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
320 const QColor diffuse = context.isSelected() ? context.selectionColor() : phongSettings->diffuse();
321
322 effect->addParameter( new Qt3DRender::QParameter(
323 u"ambientColor"_s,
324 QColor::fromRgbF(
325 static_cast< float >( ambient.redF() * phongSettings->ambientCoefficient() ),
326 static_cast< float >( ambient.greenF() * phongSettings->ambientCoefficient() ),
327 static_cast< float >( ambient.blueF() * phongSettings->ambientCoefficient() )
328 )
329 ) );
330 effect->addParameter( new Qt3DRender::QParameter(
331 u"diffuseColor"_s,
332 QColor::fromRgbF(
333 static_cast< float >( diffuse.redF() * phongSettings->diffuseCoefficient() ),
334 static_cast< float >( diffuse.greenF() * phongSettings->diffuseCoefficient() ),
335 static_cast< float >( diffuse.blueF() * phongSettings->diffuseCoefficient() )
336 )
337 ) );
338 effect->addParameter( new Qt3DRender::QParameter(
339 u"specularColor"_s,
340 QColor::fromRgbF(
341 static_cast< float >( phongSettings->specular().redF() * phongSettings->specularCoefficient() ),
342 static_cast< float >( phongSettings->specular().greenF() * phongSettings->specularCoefficient() ),
343 static_cast< float >( phongSettings->specular().blueF() * phongSettings->specularCoefficient() )
344 )
345 ) );
346 }
347
348 effect->addParameter( new Qt3DRender::QParameter( u"shininess"_s, static_cast<float>( phongSettings->shininess() ) ) );
349 effect->addParameter( new Qt3DRender::QParameter( u"opacity"_s, static_cast<float>( phongSettings->opacity() ) ) );
350
351 effect->addTechnique( technique );
352 material->setEffect( effect );
353
354 return material;
355}
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...
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
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
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...
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
void addParametersToEffect(Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext) const override
Adds parameters from the material settings to a destination effect.
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...
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
Basic shading material used for rendering based on the Phong shading model with three color component...
double ambientCoefficient() const
Returns the coefficient for the ambient color contribution (ie strength factor of the ambient color).
QColor diffuse() const
Returns diffuse color component.
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
double specularCoefficient() const
Returns the coefficient for the specular color contribution (ie strength factor of the specular color...
double shininess() const
Returns shininess of the surface.
double diffuseCoefficient() const
Returns the coefficient for the diffuse color contribution (ie strength factor of the diffuse color).
bool hasActiveProperties() const final
Returns true if the collection has any active properties, or false if all properties within the colle...