QGIS API Documentation 4.3.0-Master (bf28115e945)
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 "qgs3d.h"
19#include "qgs3dutils.h"
20#include "qgsgoochmaterial.h"
22#include "qgsunlitmaterial.h"
23
24#include <QString>
25#include <Qt3DCore/QAttribute>
26#include <Qt3DCore/QBuffer>
27#include <Qt3DCore/QGeometry>
28
29using namespace Qt::StringLiterals;
30
31
33{
34 return QMap<QString, QString>();
35}
36
38{
39 switch ( technique )
40 {
42 {
43 Q_ASSERT( false );
44 return nullptr;
45 }
46
52 {
53 if ( context.isHighlighted() )
54 {
56 }
57
58 const QgsGoochMaterialSettings *goochSettings = dynamic_cast< const QgsGoochMaterialSettings * >( settings );
59 Q_ASSERT( goochSettings );
60 const QgsPropertyCollection &dataDefinedProperties = goochSettings->dataDefinedProperties();
61
62 QgsGoochMaterial *material = new QgsGoochMaterial();
63 material->setObjectName( u"goochMaterial"_s );
64 applySettingsToMaterial( goochSettings, material );
65 if ( context.isSelected() )
66 material->setDiffuse( context.selectionColor() );
67 material->setDataDefinedEnabled(
72 );
73
74 return material;
75 }
76
79 return nullptr;
80 }
81 return nullptr;
82}
83
85{
86 const QgsGoochMaterialSettings *goochSettings = dynamic_cast< const QgsGoochMaterialSettings * >( settings );
87 Q_ASSERT( goochSettings );
88 const QgsPropertyCollection &dataDefinedProperties = goochSettings->dataDefinedProperties();
89 const QColor diffuse = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Diffuse, expressionContext, goochSettings->diffuse() ) );
90 const QColor warm = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Warm, expressionContext, goochSettings->warm() ) );
91 const QColor cool = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Cool, expressionContext, goochSettings->cool() ) );
92 const QColor specular = Qgs3DUtils::srgbToLinear( dataDefinedProperties.valueAsColor( QgsAbstractMaterialSettings::Property::Specular, expressionContext, goochSettings->specular() ) );
93
94 QByteArray array;
95 array.resize( sizeof( unsigned char ) * 12 );
96 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
97
98 *fptr++ = static_cast<unsigned char>( diffuse.red() );
99 *fptr++ = static_cast<unsigned char>( diffuse.green() );
100 *fptr++ = static_cast<unsigned char>( diffuse.blue() );
101
102 *fptr++ = static_cast<unsigned char>( warm.red() );
103 *fptr++ = static_cast<unsigned char>( warm.green() );
104 *fptr++ = static_cast<unsigned char>( warm.blue() );
105
106 *fptr++ = static_cast<unsigned char>( cool.red() );
107 *fptr++ = static_cast<unsigned char>( cool.green() );
108 *fptr++ = static_cast<unsigned char>( cool.blue() );
109
110 *fptr++ = static_cast<unsigned char>( specular.red() );
111 *fptr++ = static_cast<unsigned char>( specular.green() );
112 *fptr++ = static_cast<unsigned char>( specular.blue() );
113
114 return array;
115}
116
117void QgsGoochMaterial3DHandler::applyDataDefinedToGeometry( const QgsAbstractMaterialSettings *, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data ) const
118{
119 Qt3DCore::QBuffer *dataBuffer = new Qt3DCore::QBuffer( geometry );
120
121 Qt3DCore::QAttribute *diffuseAttribute = new Qt3DCore::QAttribute( geometry );
122 diffuseAttribute->setName( u"dataDefinedDiffuseColor"_s );
123 diffuseAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
124 diffuseAttribute->setVertexSize( 3 );
125 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
126 diffuseAttribute->setBuffer( dataBuffer );
127 diffuseAttribute->setByteStride( 12 * sizeof( unsigned char ) );
128 diffuseAttribute->setByteOffset( 0 );
129 diffuseAttribute->setCount( vertexCount );
130 geometry->addAttribute( diffuseAttribute );
131
132 Qt3DCore::QAttribute *warmAttribute = new Qt3DCore::QAttribute( geometry );
133 warmAttribute->setName( u"dataDefinedWarmColor"_s );
134 warmAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
135 warmAttribute->setVertexSize( 3 );
136 warmAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
137 warmAttribute->setBuffer( dataBuffer );
138 warmAttribute->setByteStride( 12 * sizeof( unsigned char ) );
139 warmAttribute->setByteOffset( 3 * sizeof( unsigned char ) );
140 warmAttribute->setCount( vertexCount );
141 geometry->addAttribute( warmAttribute );
142
143 Qt3DCore::QAttribute *coolAttribute = new Qt3DCore::QAttribute( geometry );
144 coolAttribute->setName( u"dataDefinedCoolColor"_s );
145 coolAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
146 coolAttribute->setVertexSize( 3 );
147 coolAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
148 coolAttribute->setBuffer( dataBuffer );
149 coolAttribute->setByteStride( 12 * sizeof( unsigned char ) );
150 coolAttribute->setByteOffset( 6 * sizeof( unsigned char ) );
151 coolAttribute->setCount( vertexCount );
152 geometry->addAttribute( coolAttribute );
153
154 Qt3DCore::QAttribute *specularAttribute = new Qt3DCore::QAttribute( geometry );
155 specularAttribute->setName( u"dataDefinedSpecularColor"_s );
156 specularAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
157 specularAttribute->setVertexSize( 3 );
158 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
159 specularAttribute->setBuffer( dataBuffer );
160 specularAttribute->setByteStride( 12 * sizeof( unsigned char ) );
161 specularAttribute->setByteOffset( 9 * sizeof( unsigned char ) );
162 specularAttribute->setCount( vertexCount );
163 geometry->addAttribute( specularAttribute );
164
165 dataBuffer->setData( data );
166}
167
168bool QgsGoochMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
169{
170 const QgsGoochMaterialSettings *goochSettings = qgis::down_cast< const QgsGoochMaterialSettings * >( settings );
171
172 QgsGoochMaterial *material = sceneRoot->findChild<QgsGoochMaterial *>();
173 if ( !material || material->objectName() != "goochMaterial"_L1 )
174 return false;
175
176 applySettingsToMaterial( goochSettings, material );
177
178 return true;
179}
180
182 const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform
183) const
184{
185 const QgsGoochMaterialSettings *goochSettings = qgis::down_cast< const QgsGoochMaterialSettings * >( settings );
186
187 QgsGoochMaterial *material = new QgsGoochMaterial();
188 material->setInstancingEnabled( true, flags );
189 material->setInstancingMeshTransform( transform );
190
191 material->setObjectName( u"goochMaterial"_s );
192 applySettingsToMaterial( goochSettings, material );
193 if ( context.isSelected() )
194 material->setDiffuse( context.selectionColor() );
195
196 return material;
197}
198
199void QgsGoochMaterial3DHandler::applySettingsToMaterial( const QgsGoochMaterialSettings *settings, QgsGoochMaterial *material )
200{
201 material->setDiffuse( settings->diffuse() );
202 material->setSpecular( settings->specular() );
203 material->setCool( settings->cool() );
204 material->setWarm( settings->warm() );
205 material->setShininess( static_cast<float>( settings->shininess() ) );
206 material->setAlpha( static_cast<float>( settings->alpha() ) );
207 material->setBeta( static_cast<float>( settings->beta() ) );
208}
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...
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.