QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgssimplelinematerial3dhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssimplelinematerial3dhandler.cpp
3 --------------------------------------
4 Date : August 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 "qgslinematerial_p.h"
19#include "qgslinestring.h"
20#include "qgslinevertexdata_p.h"
22
23#include <QMap>
24#include <QString>
25#include <Qt3DCore/QAttribute>
26#include <Qt3DCore/QBuffer>
27#include <Qt3DCore/QGeometry>
28#include <Qt3DExtras/Qt3DWindow>
29#include <Qt3DRender/QEffect>
30#include <Qt3DRender/QGeometryRenderer>
31#include <Qt3DRender/QParameter>
32#include <Qt3DRender/QTexture>
33
34using namespace Qt::StringLiterals;
35
36
38{
39 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
40 Q_ASSERT( lineSettings );
41
42 switch ( technique )
43 {
51 return nullptr;
52
54 {
55 if ( context.isHighlighted() )
56 {
57 // QgsHighlightMaterial does not support lines
58 return nullptr;
59 }
60
61 QgsLineMaterial *mat = new QgsLineMaterial;
62 if ( !context.isSelected() )
63 {
64 mat->setLineColor( lineSettings->ambient() );
65 mat->setUseVertexColors( lineSettings->dataDefinedProperties().isActive( QgsAbstractMaterialSettings::Property::Ambient ) );
66 }
67 else
68 {
69 // update the material with selection colors
70 mat->setLineColor( context.selectionColor() );
71 mat->setUseVertexColors( false );
72 }
73 return mat;
74 }
75 }
76 return nullptr;
77}
78
80{
81 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
82 Q_ASSERT( lineSettings );
83
84 QMap<QString, QString> parameters;
85 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( lineSettings->ambient().redF() ).arg( lineSettings->ambient().greenF() ).arg( lineSettings->ambient().blueF() );
86 return parameters;
87}
88
89void QgsSimpleLineMaterial3DHandler::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext ) const
90{
91 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
92 Q_ASSERT( lineSettings );
93
94 const QColor ambient = materialContext.isSelected() ? materialContext.selectionColor().darker() : lineSettings->ambient();
95 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( u"ambientColor"_s, ambient );
96 effect->addParameter( ambientParameter );
97}
98
100{
101 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
102 Q_ASSERT( lineSettings );
103
104 const QColor ambient = lineSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, lineSettings->ambient() );
105
106 QByteArray array;
107 array.resize( sizeof( unsigned char ) * 3 );
108 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
109
110 *fptr++ = static_cast<unsigned char>( ambient.red() );
111 *fptr++ = static_cast<unsigned char>( ambient.green() );
112 *fptr++ = static_cast<unsigned char>( ambient.blue() );
113 return array;
114}
115
116void QgsSimpleLineMaterial3DHandler::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 *colorAttribute = new Qt3DCore::QAttribute( geometry );
121 colorAttribute->setName( u"dataDefinedColor"_s );
122 colorAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
123 colorAttribute->setVertexSize( 3 );
124 colorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
125 colorAttribute->setBuffer( dataBuffer );
126 colorAttribute->setByteStride( 3 * sizeof( unsigned char ) );
127 colorAttribute->setByteOffset( 0 );
128 colorAttribute->setCount( vertexCount );
129 geometry->addAttribute( colorAttribute );
130
131 dataBuffer->setData( data );
132}
133
134QList<QgsAbstractMaterial3DHandler::PreviewMeshType> QgsSimpleLineMaterial3DHandler::previewMeshTypes() const
135{
136 PreviewMeshType lines;
137 lines.type = u"lines"_s;
138 lines.displayName = QObject::tr( "Lines" );
139 return { lines };
140}
141
142Qt3DCore::QEntity *QgsSimpleLineMaterial3DHandler::createPreviewMesh( const QString &, Qt3DCore::QEntity *parent ) const
143{
144 auto *entity = new Qt3DCore::QEntity( parent );
145 auto *renderer = new Qt3DRender::QGeometryRenderer( entity );
146 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
147
148 QgsLineVertexData lineVertexData;
149 lineVertexData.withAdjacency = true;
150 constexpr double s = 1.0;
151 // just a boring old flat square
152 lineVertexData.addLineString( QgsLineString( { -s, s, s, -s, -s }, { -s, -s, s, s, -s }, { 0, 0, 0, 0, 0 } ) );
153 Qt3DCore::QGeometry *geometry = lineVertexData.createGeometry( entity );
154 renderer->setGeometry( geometry );
155 renderer->setVertexCount( static_cast< int >( lineVertexData.indexes.count() ) );
156 renderer->setPrimitiveRestartEnabled( true );
157 renderer->setRestartIndexValue( 0 );
158
159 entity->addComponent( renderer );
160 return entity;
161}
162
164 const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, Qt3DExtras::Qt3DWindow *window, Qt3DCore::QEntity *parent
165) const
166{
167 auto *root = new Qt3DCore::QEntity( parent );
168 Qt3DCore::QEntity *mesh = createPreviewMesh( type, root );
169
171 QgsLineMaterial *lineMaterial = qobject_cast<QgsLineMaterial *>( mat );
172 Q_ASSERT( lineMaterial );
173 lineMaterial->setLineWidth( 2 );
174 if ( window )
175 {
176 // ensure viewport size is updated if preview widget window size changes
177 auto updateViewport = [lineMaterial, window]() { lineMaterial->setViewportSize( QSizeF( window->width(), window->height() ) ); };
178 QObject::connect( window, &Qt3DExtras::Qt3DWindow::widthChanged, lineMaterial, updateViewport );
179 QObject::connect( window, &Qt3DExtras::Qt3DWindow::heightChanged, lineMaterial, updateViewport );
180 updateViewport();
181 }
182
183 mat->setParent( mesh );
184 mesh->addComponent( mat );
185 return root;
186}
187
188bool QgsSimpleLineMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
189{
190 const QgsSimpleLineMaterialSettings *lineSettings = qgis::down_cast< const QgsSimpleLineMaterialSettings * >( settings );
191
192 QgsLineMaterial *material = sceneRoot->findChild<QgsLineMaterial *>();
193 material->setLineColor( lineSettings->ambient() );
194
195 return true;
196}
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
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...
Line string geometry type, with support for z-dimension and m-values.
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
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
QList< PreviewMeshType > previewMeshTypes() const override
Returns a list of available preview mesh types for the material.
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
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.
Qt3DCore::QEntity * createPreviewScene(const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, Qt3DExtras::Qt3DWindow *window, Qt3DCore::QEntity *parent) const override
Builds a complete self-contained scene for previewing the material, using the specified mesh type.
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...
Qt3DCore::QEntity * createPreviewMesh(const QString &type, Qt3DCore::QEntity *parent) const override
Creates a new entity representing a suitable preview mesh for this material type.
Basic shading material used for rendering simple lines as solid line components.
QColor ambient() const
Returns the ambient color component.
Encapsulates information about available preview meshes.
QString displayName
Translated, user-friendly name.