QGIS API Documentation 4.1.0-Master (01362494303)
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 "qgs3dutils.h"
19#include "qgslinematerial_p.h"
20#include "qgslinestring.h"
21#include "qgslinevertexdata_p.h"
23
24#include <QMap>
25#include <QString>
26#include <Qt3DCore/QAttribute>
27#include <Qt3DCore/QBuffer>
28#include <Qt3DCore/QGeometry>
29#include <Qt3DExtras/Qt3DWindow>
30#include <Qt3DRender/QEffect>
31#include <Qt3DRender/QGeometryRenderer>
32#include <Qt3DRender/QParameter>
33#include <Qt3DRender/QTexture>
34
35using namespace Qt::StringLiterals;
36
37
39{
40 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
41 Q_ASSERT( lineSettings );
42
43 switch ( technique )
44 {
52 return nullptr;
53
55 {
56 if ( context.isHighlighted() )
57 {
58 // QgsHighlightMaterial does not support lines
59 return nullptr;
60 }
61
62 QgsLineMaterial *mat = new QgsLineMaterial;
63 if ( !context.isSelected() )
64 {
65 mat->setLineColor( lineSettings->ambient() );
66 mat->setUseVertexColors( lineSettings->dataDefinedProperties().isActive( QgsAbstractMaterialSettings::Property::Ambient ) );
67 }
68 else
69 {
70 // update the material with selection colors
71 mat->setLineColor( context.selectionColor() );
72 mat->setUseVertexColors( false );
73 }
74 return mat;
75 }
76 }
77 return nullptr;
78}
79
81{
82 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
83 Q_ASSERT( lineSettings );
84
85 QMap<QString, QString> parameters;
86 parameters[u"Ka"_s] = u"%1 %2 %3"_s.arg( lineSettings->ambient().redF() ).arg( lineSettings->ambient().greenF() ).arg( lineSettings->ambient().blueF() );
87 return parameters;
88}
89
90void QgsSimpleLineMaterial3DHandler::addParametersToEffect( Qt3DRender::QEffect *effect, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &materialContext ) const
91{
92 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
93 Q_ASSERT( lineSettings );
94
95 const QColor ambient = Qgs3DUtils::srgbToLinear( materialContext.isSelected() ? materialContext.selectionColor().darker() : lineSettings->ambient() );
96 Qt3DRender::QParameter *ambientParameter = new Qt3DRender::QParameter( u"ambientColor"_s, ambient );
97 effect->addParameter( ambientParameter );
98}
99
101{
102 const QgsSimpleLineMaterialSettings *lineSettings = dynamic_cast< const QgsSimpleLineMaterialSettings * >( settings );
103 Q_ASSERT( lineSettings );
104
105 const QColor ambient = Qgs3DUtils::srgbToLinear( lineSettings->dataDefinedProperties().valueAsColor( QgsAbstractMaterialSettings::Property::Ambient, expressionContext, lineSettings->ambient() ) );
106
107 QByteArray array;
108 array.resize( sizeof( unsigned char ) * 3 );
109 unsigned char *fptr = reinterpret_cast<unsigned char *>( array.data() );
110
111 *fptr++ = static_cast<unsigned char>( ambient.red() );
112 *fptr++ = static_cast<unsigned char>( ambient.green() );
113 *fptr++ = static_cast<unsigned char>( ambient.blue() );
114 return array;
115}
116
117void QgsSimpleLineMaterial3DHandler::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 *colorAttribute = new Qt3DCore::QAttribute( geometry );
122 colorAttribute->setName( u"dataDefinedColor"_s );
123 colorAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedByte );
124 colorAttribute->setVertexSize( 3 );
125 colorAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
126 colorAttribute->setBuffer( dataBuffer );
127 colorAttribute->setByteStride( 3 * sizeof( unsigned char ) );
128 colorAttribute->setByteOffset( 0 );
129 colorAttribute->setCount( vertexCount );
130 geometry->addAttribute( colorAttribute );
131
132 dataBuffer->setData( data );
133}
134
135QList<QgsAbstractMaterial3DHandler::PreviewMeshType> QgsSimpleLineMaterial3DHandler::previewMeshTypes() const
136{
137 PreviewMeshType lines;
138 lines.type = u"lines"_s;
139 lines.displayName = QObject::tr( "Lines" );
140 return { lines };
141}
142
143Qt3DCore::QEntity *QgsSimpleLineMaterial3DHandler::createPreviewMesh( const QString &, Qt3DCore::QEntity *parent ) const
144{
145 auto *entity = new Qt3DCore::QEntity( parent );
146 auto *renderer = new Qt3DRender::QGeometryRenderer( entity );
147 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
148
149 QgsLineVertexData lineVertexData;
150 lineVertexData.withAdjacency = true;
151 constexpr double s = 1.0;
152 // just a boring old flat square
153 lineVertexData.addLineString( QgsLineString( { -s, s, s, -s, -s }, { -s, -s, s, s, -s }, { 0, 0, 0, 0, 0 } ) );
154 Qt3DCore::QGeometry *geometry = lineVertexData.createGeometry( entity );
155 renderer->setGeometry( geometry );
156 renderer->setVertexCount( static_cast< int >( lineVertexData.indexes.count() ) );
157 renderer->setPrimitiveRestartEnabled( true );
158 renderer->setRestartIndexValue( 0 );
159
160 entity->addComponent( renderer );
161 return entity;
162}
163
165 const QgsAbstractMaterialSettings *settings, const QString &type, const QgsMaterialContext &context, Qt3DExtras::Qt3DWindow *window, Qt3DCore::QEntity *parent
166) const
167{
168 auto *root = new Qt3DCore::QEntity( parent );
169 Qt3DCore::QEntity *mesh = createPreviewMesh( type, root );
170
172 QgsLineMaterial *lineMaterial = qobject_cast<QgsLineMaterial *>( mat );
173 Q_ASSERT( lineMaterial );
174 lineMaterial->setLineWidth( 2 );
175 if ( window )
176 {
177 // ensure viewport size is updated if preview widget window size changes
178 auto updateViewport = [lineMaterial, window]() { lineMaterial->setViewportSize( QSizeF( window->width(), window->height() ) ); };
179 QObject::connect( window, &Qt3DExtras::Qt3DWindow::widthChanged, lineMaterial, updateViewport );
180 QObject::connect( window, &Qt3DExtras::Qt3DWindow::heightChanged, lineMaterial, updateViewport );
181 updateViewport();
182 }
183
184 mat->setParent( mesh );
185 mesh->addComponent( mat );
186 return root;
187}
188
189bool QgsSimpleLineMaterial3DHandler::updatePreviewScene( Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext & ) const
190{
191 const QgsSimpleLineMaterialSettings *lineSettings = qgis::down_cast< const QgsSimpleLineMaterialSettings * >( settings );
192
193 QgsLineMaterial *material = sceneRoot->findChild<QgsLineMaterial *>();
194 material->setLineColor( lineSettings->ambient() );
195
196 return true;
197}
MaterialRenderingTechnique
Material rendering techniques.
Definition qgis.h:4342
@ Points
Point based rendering, requires point data.
Definition qgis.h:4346
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4343
@ TrianglesFromModel
Triangle based rendering, using a model object source.
Definition qgis.h:4348
@ Lines
Line based rendering, requires line data.
Definition qgis.h:4344
@ Billboards
Flat billboard rendering.
Definition qgis.h:4350
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
Definition qgis.h:4349
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
Definition qgis.h:4345
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
Definition qgis.h:4347
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...
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.