QGIS API Documentation 4.1.0-Master (3fcefe620d1)
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"
19#include "qgsgoochmaterial.h"
22
23#include <QString>
24#include <Qt3DCore/QAttribute>
25#include <Qt3DCore/QBuffer>
26#include <Qt3DCore/QGeometry>
27
28using namespace Qt::StringLiterals;
29
30
32{
33 return QMap<QString, QString>();
34}
35
37{
38 switch ( technique )
39 {
41 {
42 Q_ASSERT( false );
43 return nullptr;
44 }
45
51 {
52 if ( context.isHighlighted() )
53 {
54 return new QgsHighlightMaterial();
55 }
56
57 const QgsGoochMaterialSettings *goochSettings = dynamic_cast< const QgsGoochMaterialSettings * >( settings );
58 Q_ASSERT( goochSettings );
59 const QgsPropertyCollection &dataDefinedProperties = goochSettings->dataDefinedProperties();
60
61 QgsGoochMaterial *material = new QgsGoochMaterial();
62 material->setObjectName( u"goochMaterial"_s );
63 applySettingsToMaterial( goochSettings, material );
64 if ( context.isSelected() )
65 material->setDiffuse( context.selectionColor() );
66 material->setDataDefinedEnabled(
71 );
72
73 return material;
74 }
75
78 return nullptr;
79 }
80 return nullptr;
81}
82
84{
85 const QgsGoochMaterialSettings *goochSettings = dynamic_cast< const QgsGoochMaterialSettings * >( settings );
86 Q_ASSERT( goochSettings );
87 const QgsPropertyCollection &dataDefinedProperties = goochSettings->dataDefinedProperties();
88 const QColor diffuse = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, goochSettings->diffuse() ) );
89 const QColor warm = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Warm, expressionContext, goochSettings->warm() ) );
90 const QColor cool = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Cool, expressionContext, goochSettings->cool() ) );
91 const QColor specular = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, goochSettings->specular() ) );
92
93 QByteArray array;
94 array.resize( sizeof( unsigned char ) * 12 );
95 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
96
97 *fptr++ = static_cast<unsigned char>( diffuse.red() );
98 *fptr++ = static_cast<unsigned char>( diffuse.green() );
99 *fptr++ = static_cast<unsigned char>( diffuse.blue() );
100
101 *fptr++ = static_cast<unsigned char>( warm.red() );
102 *fptr++ = static_cast<unsigned char>( warm.green() );
103 *fptr++ = static_cast<unsigned char>( warm.blue() );
104
105 *fptr++ = static_cast<unsigned char>( cool.red() );
106 *fptr++ = static_cast<unsigned char>( cool.green() );
107 *fptr++ = static_cast<unsigned char>( cool.blue() );
108
109 *fptr++ = static_cast<unsigned char>( specular.red() );
110 *fptr++ = static_cast<unsigned char>( specular.green() );
111 *fptr++ = static_cast<unsigned char>( specular.blue() );
112
113 return array;
114}
115
116void QgsGoochMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
117{
118 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
119
120 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
121 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
122 diffuseAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
123 diffuseAttribute->setVertexSize( 3 );
124 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
125 diffuseAttribute->setBuffer( dataBuffer );
126 diffuseAttribute->setByteStride( 12 * sizeof( unsigned char ) );
127 diffuseAttribute->setByteOffset( 0 );
128 diffuseAttribute->setCount( vertexCount );
129 geometry->addAttribute( diffuseAttribute );
130
131 Qt3DCore::QAttribute *warmAttribute = new Qt3DCore::QAttribute( geometry );
132 warmAttribute->setName( u"dataDefinedWarmColor"_s );
133 warmAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
134 warmAttribute->setVertexSize( 3 );
135 warmAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
136 warmAttribute->setBuffer( dataBuffer );
137 warmAttribute->setByteStride( 12 * sizeof( unsigned char ) );
138 warmAttribute->setByteOffset( 3 * sizeof( unsigned char ) );
139 warmAttribute->setCount( vertexCount );
140 geometry->addAttribute( warmAttribute );
141
142 Qt3DCore::QAttribute *coolAttribute = new Qt3DCore::QAttribute( geometry );
143 coolAttribute->setName( u"dataDefinedCoolColor"_s );
144 coolAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
145 coolAttribute->setVertexSize( 3 );
146 coolAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
147 coolAttribute->setBuffer( dataBuffer );
148 coolAttribute->setByteStride( 12 * sizeof( unsigned char ) );
149 coolAttribute->setByteOffset( 6 * sizeof( unsigned char ) );
150 coolAttribute->setCount( vertexCount );
151 geometry->addAttribute( coolAttribute );
152
153 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
154 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
155 specularAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
156 specularAttribute->setVertexSize( 3 );
157 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
158 specularAttribute->setBuffer( dataBuffer );
159 specularAttribute->setByteStride( 12 * sizeof( unsigned char ) );
160 specularAttribute->setByteOffset( 9 * sizeof( unsigned char ) );
161 specularAttribute->setCount( vertexCount );
162 geometry->addAttribute( specularAttribute );
163
164 dataBuffer->setData( data );
165}
166
167bool QgsGoochMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
168{
169 const QgsGoochMaterialSettings *goochSettings = qgis::down_cast< const QgsGoochMaterialSettings * >( settings );
170
171 QgsGoochMaterial *material = sceneRoot->findChild<QgsGoochMaterial *>();
172 if ( !material || material->objectName() != "goochMaterial"_L1 )
173 return false;
174
175 applySettingsToMaterial( goochSettings, material );
176
177 return true;
178}
179
181 const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform
182) const
183{
184 const QgsGoochMaterialSettings *goochSettings = qgis::down_cast< const QgsGoochMaterialSettings * >( settings );
185
186 QgsGoochMaterial *material = new QgsGoochMaterial();
187 material->setInstancingEnabled( true, flags );
188 material->setInstancingMeshTransform( transform );
189
190 material->setObjectName( u"goochMaterial"_s );
191 applySettingsToMaterial( goochSettings, material );
192 if ( context.isSelected() )
193 material->setDiffuse( context.selectionColor() );
194
195 return material;
196}
197
198void QgsGoochMaterial3DHandler::applySettingsToMaterial( const QgsGoochMaterialSettings *settings, QgsGoochMaterial *material )
199{
200 material->setDiffuse( settings->diffuse() );
201 material->setSpecular( settings->specular() );
202 material->setCool( settings->cool() );
203 material->setWarm( settings->warm() );
204 material->setShininess( static_cast<float>( settings->shininess() ) );
205 material->setAlpha( static_cast<float>( settings->alpha() ) );
206 material->setBeta( static_cast<float>( settings->beta() ) );
207}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4375
@ Points
Point based rendering, requires point data.
Definition qgis.h:4379
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4376
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4381
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4377
@ Billboards
Flat billboard rendering.
Definition qgis.h:4383
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4382
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4378
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4380
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
Definition qgis.h:4398
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
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 * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform=QMatrix4x4()) const override
Creates a QgsMaterial for instanced point rendering.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
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,...
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 isActive(int key) const final
Returns true if the collection contains an active property with the specified key.