QGIS API Documentation 4.3.0-Master (bf28115e945)
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 "qgs3d.h"
19#include "qgs3dutils.h"
20#include "qgsphongmaterial.h"
22#include "qgsunlitmaterial.h"
23
24#include <QMap>
25#include <QString>
26#include <Qt3DCore/QAttribute>
27#include <Qt3DCore/QBuffer>
28#include <Qt3DCore/QGeometry>
29#include <Qt3DRender/QEffect>
30#include <Qt3DRender/QGraphicsApiFilter>
31#include <Qt3DRender/QParameter>
32#include <Qt3DRender/QTechnique>
33
34using namespace Qt::StringLiterals;
35
36
38{
39 switch ( technique )
40 {
42 {
43 Q_ASSERT( false );
44 return nullptr;
45 }
51 {
52 if ( context.isHighlighted() )
53 {
55 }
56
57 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
58 Q_ASSERT( phongSettings );
59
60 QgsPhongMaterial *material = new QgsPhongMaterial();
61 material->setObjectName( u"phongMaterial"_s );
62
63 const QgsPropertyCollection &dataDefinedProperties = phongSettings->dataDefinedProperties();
64 const bool dataDefined = dataDefinedProperties.isActive( QgsAbstractMaterialSettings::Property::Ambient )
67 if ( !dataDefined )
68 {
69 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
70 const QColor diffuse = context.isSelected() ? context.selectionColor() : phongSettings->diffuse();
71 material->setAmbient( ambient, static_cast<float>( phongSettings->ambientCoefficient() ) );
72 material->setDiffuse( diffuse, static_cast<float>( phongSettings->diffuseCoefficient() ) );
73 material->setSpecular( phongSettings->specular(), static_cast<float>( phongSettings->specularCoefficient() ) );
74 }
75 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
76 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
77 material->setDataDefinedEnabled( dataDefined );
78
79 return material;
80 }
81
84 return nullptr;
85 }
86 return nullptr;
87}
88
90{
91 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
92 Q_ASSERT( phongSettings );
93
94 QMap<QString, QString> parameters;
95 parameters[u"Kd"_s] = u"%1 %2 %3"_s.arg( phongSettings->diffuse().redF() ).arg( phongSettings->diffuse().greenF() ).arg( phongSettings->diffuse().blueF() );
96 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( phongSettings->ambient().redF() ).arg( phongSettings->ambient().greenF() ).arg( phongSettings->ambient().blueF() );
97 parameters[u"Ks"_s] = u"%1 %2 %3"_s.arg( phongSettings->specular().redF() ).arg( phongSettings->specular().greenF() ).arg( phongSettings->specular().blueF() );
98 parameters[u"Ns"_s] = QString::number( phongSettings->shininess() );
99 return parameters;
100}
101
103{
104 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
105 Q_ASSERT( phongSettings );
106
107 const QColor ambient = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, phongSettings->ambient() ) );
108 const QColor diffuse = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, phongSettings->diffuse() ) );
109 const QColor specular = Qgs3DUtils::srgbToLinear( phongSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, phongSettings->specular() ) );
110
111 const double diffuseCoefficient = phongSettings->diffuseCoefficient();
112 const double ambientCoefficient = phongSettings->ambientCoefficient();
113 const double specularCoefficient = phongSettings->specularCoefficient();
114
115 QByteArray array;
116 if ( diffuseCoefficient < 1 || ambientCoefficient < 1 || specularCoefficient < 1 )
117 {
118 // use floats if we are adjusting color component strength, bytes don't
119 // give us enough precision
120 array.resize( sizeof( float ) * 9 );
121 float *fptr = reinterpret_cast<float *>( array.data() );
122
123 *fptr++ = static_cast<float>( diffuse.redF() * diffuseCoefficient );
124 *fptr++ = static_cast<float>( diffuse.greenF() * diffuseCoefficient );
125 *fptr++ = static_cast<float>( diffuse.blueF() * diffuseCoefficient );
126
127 *fptr++ = static_cast<float>( ambient.redF() * ambientCoefficient );
128 *fptr++ = static_cast<float>( ambient.greenF() * ambientCoefficient );
129 *fptr++ = static_cast<float>( ambient.blueF() * ambientCoefficient );
130
131 *fptr++ = static_cast<float>( specular.redF() * specularCoefficient );
132 *fptr++ = static_cast<float>( specular.greenF() * specularCoefficient );
133 *fptr++ = static_cast<float>( specular.blueF() * specularCoefficient );
134 }
135 else
136 {
137 array.resize( sizeof( unsigned char ) * 9 );
138 unsigned char *ptr = reinterpret_cast<unsigned char *>( array.data() );
139
140 *ptr++ = static_cast<unsigned char>( diffuse.red() );
141 *ptr++ = static_cast<unsigned char>( diffuse.green() );
142 *ptr++ = static_cast<unsigned char>( diffuse.blue() );
143
144 *ptr++ = static_cast<unsigned char>( ambient.red() );
145 *ptr++ = static_cast<unsigned char>( ambient.green() );
146 *ptr++ = static_cast<unsigned char>( ambient.blue() );
147
148 *ptr++ = static_cast<unsigned char>( specular.red() );
149 *ptr++ = static_cast<unsigned char>( specular.green() );
150 *ptr++ = static_cast<unsigned char>( specular.blue() );
151 }
152
153 return array;
154}
155
156void QgsPhongMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
157{
158 const QgsPhongMaterialSettings *phongSettings = dynamic_cast< const QgsPhongMaterialSettings * >( settings );
159 Q_ASSERT( phongSettings );
160
161 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
162
163 // use floats if we are adjusting color component strength, bytes don't
164 // give us enough precision
165 const bool useFloats = phongSettings->diffuseCoefficient() < 1 || phongSettings->ambientCoefficient() < 1 || phongSettings->specularCoefficient() < 1;
166
167 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
168 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
169 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
170 diffuseAttribute->setVertexSize( 3 );
171 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
172 diffuseAttribute->setBuffer( dataBuffer );
173 diffuseAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
174 diffuseAttribute->setByteOffset( 0 );
175 diffuseAttribute->setCount( vertexCount );
176 geometry->addAttribute( diffuseAttribute );
177
178 Qt3DCore::QAttribute *ambientAttribute = new Qt3DCore::QAttribute( geometry );
179 ambientAttribute->setName( u"dataDefinedAmbiantColor"_s );
180 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
181 ambientAttribute->setVertexSize( 3 );
182 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
183 ambientAttribute->setBuffer( dataBuffer );
184 ambientAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
185 ambientAttribute->setByteOffset( 3 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
186 ambientAttribute->setCount( vertexCount );
187 geometry->addAttribute( ambientAttribute );
188
189 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
190 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
191 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
192 specularAttribute->setVertexSize( 3 );
193 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
194 specularAttribute->setBuffer( dataBuffer );
195 specularAttribute->setByteStride( 9 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
196 specularAttribute->setByteOffset( 6 * ( useFloats ? sizeof( float ) : sizeof( unsigned char ) ) );
197 specularAttribute->setCount( vertexCount );
198 geometry->addAttribute( specularAttribute );
199
200 dataBuffer->setData( data );
201}
202
203bool QgsPhongMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
204{
205 const QgsPhongMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongMaterialSettings * >( settings );
206
207 QgsPhongMaterial *material = sceneRoot->findChild<QgsPhongMaterial *>();
208 if ( !material || material->objectName() != "phongMaterial"_L1 )
209 return false;
210
211 material->setAmbient( phongSettings->ambient(), static_cast<float>( phongSettings->ambientCoefficient() ) );
212 material->setDiffuse( phongSettings->diffuse(), static_cast<float>( phongSettings->diffuseCoefficient() ) );
213 material->setSpecular( phongSettings->specular(), static_cast<float>( phongSettings->specularCoefficient() ) );
214 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
215 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
216
217 return true;
218}
219
221 const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform
222) const
223{
224 const QgsPhongMaterialSettings *phongSettings = qgis::down_cast< const QgsPhongMaterialSettings * >( settings );
225
226 QgsPhongMaterial *material = new QgsPhongMaterial();
227 material->setInstancingEnabled( true, flags );
228 material->setInstancingMeshTransform( transform );
229 material->setObjectName( u"phongMaterial"_s );
230
231 const QColor ambient = context.isSelected() ? context.selectionColor().darker() : phongSettings->ambient();
232 const QColor diffuse = context.isSelected() ? context.selectionColor() : phongSettings->diffuse();
233 material->setAmbient( ambient, static_cast<float>( phongSettings->ambientCoefficient() ) );
234 material->setDiffuse( diffuse, static_cast<float>( phongSettings->diffuseCoefficient() ) );
235 material->setSpecular( phongSettings->specular(), static_cast<float>( phongSettings->specularCoefficient() ) );
236 material->setShininess( static_cast<float>( phongSettings->shininess() ) );
237 material->setOpacity( static_cast<float>( phongSettings->opacity() ) );
238
239 return material;
240}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4388
@ Points
Point based rendering, requires point data.
Definition qgis.h:4392
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4389
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4394
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4390
@ Billboards
Flat billboard rendering.
Definition qgis.h:4396
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4395
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4391
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4393
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
Definition qgis.h:4411
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
static QgsUnlitMaterial * createHighlightMaterial()
Creates a new highlight material, consisting of an unlit material respecting the user's map highlight...
Definition qgs3d.cpp:163
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,...
QgsMaterial * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform=QMatrix4x4()) const override
Creates a QgsMaterial for instanced point rendering.
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.
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).
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.