QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgspoint3dsymbol_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspoint3dsymbol_p.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
16#include "qgspoint3dsymbol_p.h"
17
18#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
19#include <Qt3DRender/QAttribute>
20#include <Qt3DRender/QBuffer>
21#include <Qt3DRender/QGeometry>
22#include <Qt3DCore/QTransform>
23
24typedef Qt3DRender::QAttribute Qt3DQAttribute;
25typedef Qt3DRender::QBuffer Qt3DQBuffer;
26typedef Qt3DRender::QGeometry Qt3DQGeometry;
27#else
28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
30#include <Qt3DCore/QGeometry>
31
32typedef Qt3DCore::QAttribute Qt3DQAttribute;
33typedef Qt3DCore::QBuffer Qt3DQBuffer;
34typedef Qt3DCore::QGeometry Qt3DQGeometry;
35#endif
36
37#include <Qt3DRender/QEffect>
38#include <Qt3DRender/QGraphicsApiFilter>
39#include <Qt3DRender/QParameter>
40#include <Qt3DRender/QTechnique>
41
42#include <Qt3DExtras/QCylinderGeometry>
43#include <Qt3DExtras/QConeGeometry>
44#include <Qt3DExtras/QCuboidGeometry>
45#include <Qt3DExtras/QPlaneGeometry>
46#include <Qt3DExtras/QSphereGeometry>
47#include <Qt3DExtras/QTorusGeometry>
48#include <Qt3DExtras/QPhongMaterial>
49#include <Qt3DRender/QSceneLoader>
50#include <Qt3DRender/QPaintedTextureImage>
51
52#include <Qt3DRender/QMesh>
53
54#include <Qt3DExtras/QExtrudedTextGeometry>
55
56#include <QUrl>
57#include <QVector3D>
58
59#include "qgspoint3dsymbol.h"
60#include "qgs3dmapsettings.h"
61
62#include "qgsapplication.h"
63#include "qgsvectorlayer.h"
64#include "qgs3dutils.h"
67#include "qgssourcecache.h"
68
70
71
72//* INSTANCED RENDERING *//
73
74
75class QgsInstancedPoint3DSymbolHandler : public QgsFeature3DHandler
76{
77 public:
78 QgsInstancedPoint3DSymbolHandler( const QgsPoint3DSymbol *symbol, const QgsFeatureIds &selectedIds )
79 : mSymbol( static_cast< QgsPoint3DSymbol *>( symbol->clone() ) )
80 , mSelectedIds( selectedIds ) {}
81
82 bool prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames ) override;
83 void processFeature( const QgsFeature &feature, const Qgs3DRenderContext &context ) override;
84 void finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context ) override;
85
86 private:
87
88 static Qt3DRender::QMaterial *material( const QgsPoint3DSymbol *symbol );
89 static Qt3DRender::QGeometryRenderer *renderer( const QgsPoint3DSymbol *symbol, const QVector<QVector3D> &positions );
90 static Qt3DQGeometry *symbolGeometry( QgsPoint3DSymbol::Shape shape, const QVariantMap &shapeProperties );
91
93 struct PointData
94 {
95 QVector<QVector3D> positions; // contains triplets of float x,y,z for each point
96 };
97
98 void makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected );
99
100 // input specific for this class
101 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
102 // inputs - generic
103 QgsFeatureIds mSelectedIds;
104
105 // outputs
106 PointData outNormal;
107 PointData outSelected;
108};
109
110
111bool QgsInstancedPoint3DSymbolHandler::prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
112{
113 Q_UNUSED( context )
114 Q_UNUSED( attributeNames )
115 return true;
116}
117
118void QgsInstancedPoint3DSymbolHandler::processFeature( const QgsFeature &feature, const Qgs3DRenderContext &context )
119{
120 PointData &out = mSelectedIds.contains( feature.id() ) ? outSelected : outNormal;
121
122 if ( feature.geometry().isNull() )
123 return;
124
125 Qgs3DUtils::extractPointPositions( feature, context.map(), mSymbol->altitudeClamping(), out.positions );
126 mFeatureCount++;
127}
128
129void QgsInstancedPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context )
130{
131 makeEntity( parent, context, outNormal, false );
132 makeEntity( parent, context, outSelected, true );
133
134 updateZRangeFromPositions( outNormal.positions );
135 updateZRangeFromPositions( outSelected.positions );
136
137 // the elevation offset is applied in the vertex shader so let's account for it as well
138 const float symbolOffset = mSymbol->transform().data()[13];
139
140 // also account for the actual height of the objects themselves
141 // NOTE -- these calculations are naive, and assume no rotation or scaling of the symbol!
142 switch ( mSymbol->shape() )
143 {
145 {
146 const float length = mSymbol->shapeProperties().value( QStringLiteral( "length" ), 10 ).toFloat();
147 mZMin -= length * 0.5f;
148 mZMax += length * 0.5f;
149 break;
150 }
151
153 {
154 const float radius = mSymbol->shapeProperties().value( QStringLiteral( "radius" ), 10 ).toFloat();
155 mZMin -= radius;
156 mZMax += radius;
157 break;
158 }
159
161 {
162 const float length = mSymbol->shapeProperties().value( QStringLiteral( "length" ), 10 ).toFloat();
163 mZMin -= length * 0.5f;
164 mZMax += length * 0.5f;
165 break;
166 }
167
169 {
170 const float size = mSymbol->shapeProperties().value( QStringLiteral( "size" ) ).toFloat();
171 mZMin -= size * 0.5f;
172 mZMax += size * 0.5f;
173 break;
174 }
175
177 {
178 const float radius = mSymbol->shapeProperties().value( QStringLiteral( "radius" ), 10 ).toFloat();
179 mZMin -= radius;
180 mZMax += radius;
181 break;
182 }
183
188 break;
189 }
190
191 mZMin += symbolOffset;
192 mZMax += symbolOffset;
193}
194
195void QgsInstancedPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected )
196{
197 // build the default material
198 Qt3DRender::QMaterial *mat = material( mSymbol.get() );
199
200 if ( selected )
201 {
202 // update the material with selection colors
203 for ( Qt3DRender::QParameter *param : mat->effect()->parameters() )
204 {
205 if ( param->name() == QLatin1String( "kd" ) ) // diffuse
206 param->setValue( context.map().selectionColor() );
207 else if ( param->name() == QLatin1String( "ka" ) ) // ambient
208 param->setValue( context.map().selectionColor().darker() );
209 }
210 }
211
212 // build the entity
213 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
214 entity->addComponent( renderer( mSymbol.get(), out.positions ) );
215 entity->addComponent( mat );
216 entity->setParent( parent );
217
218// cppcheck wrongly believes entity will leak
219// cppcheck-suppress memleak
220}
221
222
223
224Qt3DRender::QMaterial *QgsInstancedPoint3DSymbolHandler::material( const QgsPoint3DSymbol *symbol )
225{
226 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey;
227 filterKey->setName( QStringLiteral( "renderingStyle" ) );
228 filterKey->setValue( "forward" );
229
230 // the fragment shader implements a simplified version of phong shading that uses hardcoded light
231 // (instead of whatever light we have defined in the scene)
232 // TODO: use phong shading that respects lights from the scene
233 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram;
234 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/instanced.vert" ) ) ) );
235 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/instanced.frag" ) ) ) );
236
237 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass;
238 renderPass->setShaderProgram( shaderProgram );
239
240 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
241 technique->addFilterKey( filterKey );
242 technique->addRenderPass( renderPass );
243 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
244 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
245 technique->graphicsApiFilter()->setMajorVersion( 3 );
246 technique->graphicsApiFilter()->setMinorVersion( 2 );
247
248 const QMatrix4x4 transformMatrix = symbol->transform();
249 QMatrix3x3 normalMatrix = transformMatrix.normalMatrix(); // transponed inverse of 3x3 sub-matrix
250
251 // QMatrix3x3 is not supported for passing to shaders, so we pass QMatrix4x4
252 float *n = normalMatrix.data();
253 const QMatrix4x4 normalMatrix4(
254 n[0], n[3], n[6], 0,
255 n[1], n[4], n[7], 0,
256 n[2], n[5], n[8], 0,
257 0, 0, 0, 0 );
258
259 Qt3DRender::QParameter *paramInst = new Qt3DRender::QParameter;
260 paramInst->setName( QStringLiteral( "inst" ) );
261 paramInst->setValue( transformMatrix );
262
263 Qt3DRender::QParameter *paramInstNormal = new Qt3DRender::QParameter;
264 paramInstNormal->setName( QStringLiteral( "instNormal" ) );
265 paramInstNormal->setValue( normalMatrix4 );
266
267 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect;
268 effect->addTechnique( technique );
269 effect->addParameter( paramInst );
270 effect->addParameter( paramInstNormal );
271
272 symbol->materialSettings()->addParametersToEffect( effect );
273
274 Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial;
275 material->setEffect( effect );
276
277 return material;
278}
279
280Qt3DRender::QGeometryRenderer *QgsInstancedPoint3DSymbolHandler::renderer( const QgsPoint3DSymbol *symbol, const QVector<QVector3D> &positions )
281{
282 const int count = positions.count();
283 const int byteCount = positions.count() * sizeof( QVector3D );
284 QByteArray ba;
285 ba.resize( byteCount );
286 memcpy( ba.data(), positions.constData(), byteCount );
287
288 Qt3DQBuffer *instanceBuffer = new Qt3DQBuffer();
289 instanceBuffer->setData( ba );
290
291 Qt3DQAttribute *instanceDataAttribute = new Qt3DQAttribute;
292 instanceDataAttribute->setName( QStringLiteral( "pos" ) );
293 instanceDataAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
294 instanceDataAttribute->setVertexBaseType( Qt3DQAttribute::Float );
295 instanceDataAttribute->setVertexSize( 3 );
296 instanceDataAttribute->setByteOffset( 0 );
297 instanceDataAttribute->setDivisor( 1 );
298 instanceDataAttribute->setBuffer( instanceBuffer );
299 instanceDataAttribute->setCount( count );
300 instanceDataAttribute->setByteStride( 3 * sizeof( float ) );
301
302 Qt3DQGeometry *geometry = symbolGeometry( symbol->shape(), symbol->shapeProperties() );
303 geometry->addAttribute( instanceDataAttribute );
304 geometry->setBoundingVolumePositionAttribute( instanceDataAttribute );
305
306 Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
307 renderer->setGeometry( geometry );
308 renderer->setInstanceCount( count );
309
310 return renderer;
311}
312
313Qt3DQGeometry *QgsInstancedPoint3DSymbolHandler::symbolGeometry( QgsPoint3DSymbol::Shape shape, const QVariantMap &shapeProperties )
314{
315 switch ( shape )
316 {
318 {
319 const float radius = shapeProperties[QStringLiteral( "radius" )].toFloat();
320 const float length = shapeProperties[QStringLiteral( "length" )].toFloat();
321 Qt3DExtras::QCylinderGeometry *g = new Qt3DExtras::QCylinderGeometry;
322 //g->setRings(2); // how many vertices vertically
323 //g->setSlices(8); // how many vertices on circumference
324 g->setRadius( radius ? radius : 10 );
325 g->setLength( length ? length : 10 );
326 return g;
327 }
328
330 {
331 const float radius = shapeProperties[QStringLiteral( "radius" )].toFloat();
332 Qt3DExtras::QSphereGeometry *g = new Qt3DExtras::QSphereGeometry;
333 g->setRadius( radius ? radius : 10 );
334 return g;
335 }
336
338 {
339 const float length = shapeProperties[QStringLiteral( "length" )].toFloat();
340 const float bottomRadius = shapeProperties[QStringLiteral( "bottomRadius" )].toFloat();
341 const float topRadius = shapeProperties[QStringLiteral( "topRadius" )].toFloat();
342 Qt3DExtras::QConeGeometry *g = new Qt3DExtras::QConeGeometry;
343 g->setLength( length ? length : 10 );
344 g->setBottomRadius( bottomRadius );
345 g->setTopRadius( topRadius );
346 //g->setHasBottomEndcap(hasBottomEndcap);
347 //g->setHasTopEndcap(hasTopEndcap);
348 return g;
349 }
350
352 {
353 const float size = shapeProperties[QStringLiteral( "size" )].toFloat();
354 Qt3DExtras::QCuboidGeometry *g = new Qt3DExtras::QCuboidGeometry;
355 g->setXExtent( size ? size : 10 );
356 g->setYExtent( size ? size : 10 );
357 g->setZExtent( size ? size : 10 );
358 return g;
359 }
360
362 {
363 const float radius = shapeProperties[QStringLiteral( "radius" )].toFloat();
364 const float minorRadius = shapeProperties[QStringLiteral( "minorRadius" )].toFloat();
365 Qt3DExtras::QTorusGeometry *g = new Qt3DExtras::QTorusGeometry;
366 g->setRadius( radius ? radius : 10 );
367 g->setMinorRadius( minorRadius ? minorRadius : 5 );
368 return g;
369 }
370
372 {
373 const float size = shapeProperties[QStringLiteral( "size" )].toFloat();
374 Qt3DExtras::QPlaneGeometry *g = new Qt3DExtras::QPlaneGeometry;
375 g->setWidth( size ? size : 10 );
376 g->setHeight( size ? size : 10 );
377 return g;
378 }
379
381 {
382 const float depth = shapeProperties[QStringLiteral( "depth" )].toFloat();
383 const QString text = shapeProperties[QStringLiteral( "text" )].toString();
384 Qt3DExtras::QExtrudedTextGeometry *g = new Qt3DExtras::QExtrudedTextGeometry;
385 g->setDepth( depth ? depth : 1 );
386 g->setText( text );
387 return g;
388 }
389
390 default:
391 Q_ASSERT( false );
392 return nullptr;
393 }
394}
395
396//* 3D MODEL RENDERING *//
397
398
399class QgsModelPoint3DSymbolHandler : public QgsFeature3DHandler
400{
401 public:
402 QgsModelPoint3DSymbolHandler( const QgsPoint3DSymbol *symbol, const QgsFeatureIds &selectedIds )
403 : mSymbol( static_cast< QgsPoint3DSymbol * >( symbol->clone() ) )
404 , mSelectedIds( selectedIds ) {}
405
406 bool prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames ) override;
407 void processFeature( const QgsFeature &feature, const Qgs3DRenderContext &context ) override;
408 void finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context ) override;
409
410 private:
411
412 static void addSceneEntities( const Qgs3DMapSettings &map, const QVector<QVector3D> &positions, const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent );
413 static void addMeshEntities( const Qgs3DMapSettings &map, const QVector<QVector3D> &positions, const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent, bool are_selected );
414 static Qt3DCore::QTransform *transform( QVector3D position, const QgsPoint3DSymbol *symbol );
415
417 struct PointData
418 {
419 QVector<QVector3D> positions; // contains triplets of float x,y,z for each point
420 };
421
422 void makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected );
423
424 // input specific for this class
425 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
426 // inputs - generic
427 QgsFeatureIds mSelectedIds;
428
429 // outputs
430 PointData outNormal;
431 PointData outSelected;
432};
433
434bool QgsModelPoint3DSymbolHandler::prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
435{
436 Q_UNUSED( context )
437 Q_UNUSED( attributeNames )
438 return true;
439}
440
441void QgsModelPoint3DSymbolHandler::processFeature( const QgsFeature &feature, const Qgs3DRenderContext &context )
442{
443 PointData &out = mSelectedIds.contains( feature.id() ) ? outSelected : outNormal;
444
445 if ( feature.geometry().isNull() )
446 return;
447
448 Qgs3DUtils::extractPointPositions( feature, context.map(), mSymbol->altitudeClamping(), out.positions );
449 mFeatureCount++;
450}
451
452void QgsModelPoint3DSymbolHandler::finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context )
453{
454 makeEntity( parent, context, outNormal, false );
455 makeEntity( parent, context, outSelected, true );
456
457 updateZRangeFromPositions( outNormal.positions );
458 updateZRangeFromPositions( outSelected.positions );
459
460 // the elevation offset is applied separately in QTransform added to sub-entities
461 const float symbolHeight = mSymbol->transform().data()[13];
462 mZMin += symbolHeight;
463 mZMax += symbolHeight;
464}
465
466void QgsModelPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected )
467{
468 if ( selected )
469 {
470 addMeshEntities( context.map(), out.positions, mSymbol.get(), parent, true );
471 }
472 else
473 {
474 // "overwriteMaterial" is a legacy setting indicating that non-embedded material should be used
475 if ( mSymbol->shapeProperties()[QStringLiteral( "overwriteMaterial" )].toBool()
476 || ( mSymbol->materialSettings() && mSymbol->materialSettings()->type() != QLatin1String( "null" ) ) )
477 {
478 addMeshEntities( context.map(), out.positions, mSymbol.get(), parent, false );
479 }
480 else
481 {
482 addSceneEntities( context.map(), out.positions, mSymbol.get(), parent );
483 }
484 }
485}
486
487
488
489void QgsModelPoint3DSymbolHandler::addSceneEntities( const Qgs3DMapSettings &map, const QVector<QVector3D> &positions, const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent )
490{
491 Q_UNUSED( map )
492 for ( const QVector3D &position : positions )
493 {
494 const QString source = QgsApplication::sourceCache()->localFilePath( symbol->shapeProperties()[QStringLiteral( "model" )].toString() );
495 // if the source is remote, the Qgs3DMapScene will take care of refreshing this 3D symbol when the source is fetched
496 if ( !source.isEmpty() )
497 {
498 // build the entity
499 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
500
501 const QUrl url = QUrl::fromLocalFile( source );
502 Qt3DRender::QSceneLoader *modelLoader = new Qt3DRender::QSceneLoader;
503 modelLoader->setSource( url );
504
505 entity->addComponent( modelLoader );
506 entity->addComponent( transform( position, symbol ) );
507 entity->setParent( parent );
508
509// cppcheck wrongly believes entity will leak
510// cppcheck-suppress memleak
511 }
512 }
513}
514
515void QgsModelPoint3DSymbolHandler::addMeshEntities( const Qgs3DMapSettings &map, const QVector<QVector3D> &positions, const QgsPoint3DSymbol *symbol, Qt3DCore::QEntity *parent, bool are_selected )
516{
517 if ( positions.empty() )
518 return;
519
520 // build the default material
521 QgsMaterialContext materialContext;
522 materialContext.setIsSelected( are_selected );
523 materialContext.setSelectionColor( map.selectionColor() );
524 Qt3DRender::QMaterial *mat = symbol->materialSettings()->toMaterial( QgsMaterialSettingsRenderingTechnique::Triangles, materialContext );
525
526 // get nodes
527 for ( const QVector3D &position : positions )
528 {
529 const QString source = QgsApplication::sourceCache()->localFilePath( symbol->shapeProperties()[QStringLiteral( "model" )].toString() );
530 if ( !source.isEmpty() )
531 {
532 // build the entity
533 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
534
535 const QUrl url = QUrl::fromLocalFile( source );
536 Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh;
537 mesh->setSource( url );
538
539 entity->addComponent( mesh );
540 entity->addComponent( mat );
541 entity->addComponent( transform( position, symbol ) );
542 entity->setParent( parent );
543
544// cppcheck wrongly believes entity will leak
545// cppcheck-suppress memleak
546 }
547 }
548}
549
550Qt3DCore::QTransform *QgsModelPoint3DSymbolHandler::transform( QVector3D position, const QgsPoint3DSymbol *symbol )
551{
552 Qt3DCore::QTransform *tr = new Qt3DCore::QTransform;
553 tr->setMatrix( symbol->transform() );
554 tr->setTranslation( position + tr->translation() );
555 return tr;
556}
557
558// --------------
559
560//* BILLBOARD RENDERING *//
561
562class QgsPoint3DBillboardSymbolHandler : public QgsFeature3DHandler
563{
564 public:
565 QgsPoint3DBillboardSymbolHandler( const QgsPoint3DSymbol *symbol, const QgsFeatureIds &selectedIds )
566 : mSymbol( static_cast< QgsPoint3DSymbol * >( symbol->clone() ) )
567 , mSelectedIds( selectedIds ) {}
568
569 bool prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames ) override;
570 void processFeature( const QgsFeature &feature, const Qgs3DRenderContext &context ) override;
571 void finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context ) override;
572
573 private:
574
576 struct PointData
577 {
578 QVector<QVector3D> positions; // contains triplets of float x,y,z for each point
579 };
580
581 void makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected );
582
583 // input specific for this class
584 std::unique_ptr< QgsPoint3DSymbol > mSymbol;
585 // inputs - generic
586 QgsFeatureIds mSelectedIds;
587
588 // outputs
589 PointData outNormal;
590 PointData outSelected;
591};
592
593bool QgsPoint3DBillboardSymbolHandler::prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
594{
595 Q_UNUSED( context )
596 Q_UNUSED( attributeNames )
597 return true;
598}
599
600void QgsPoint3DBillboardSymbolHandler::processFeature( const QgsFeature &feature, const Qgs3DRenderContext &context )
601{
602 PointData &out = mSelectedIds.contains( feature.id() ) ? outSelected : outNormal;
603
604 if ( feature.geometry().isNull() )
605 return;
606
607 Qgs3DUtils::extractPointPositions( feature, context.map(), mSymbol->altitudeClamping(), out.positions );
608 mFeatureCount++;
609}
610
611void QgsPoint3DBillboardSymbolHandler::finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context )
612{
613 makeEntity( parent, context, outNormal, false );
614 makeEntity( parent, context, outSelected, true );
615
616 updateZRangeFromPositions( outNormal.positions );
617 updateZRangeFromPositions( outSelected.positions );
618
619 // the elevation offset is applied externally through a QTransform of QEntity so let's account for it
620 const float billboardHeight = mSymbol->transform().data()[13];
621 mZMin += billboardHeight;
622 mZMax += billboardHeight;
623}
624
625void QgsPoint3DBillboardSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected )
626{
627 // Billboard Geometry
628 QgsBillboardGeometry *billboardGeometry = new QgsBillboardGeometry();
629 billboardGeometry->setPoints( out.positions );
630
631 // Billboard Geometry Renderer
632 Qt3DRender::QGeometryRenderer *billboardGeometryRenderer = new Qt3DRender::QGeometryRenderer;
633 billboardGeometryRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Points );
634 billboardGeometryRenderer->setGeometry( billboardGeometry );
635 billboardGeometryRenderer->setVertexCount( billboardGeometry->count() );
636
637 // Billboard Material
639 QgsMarkerSymbol *symbol = mSymbol->billboardSymbol();
640
641 if ( symbol )
642 {
643 billboardMaterial->setTexture2DFromSymbol( symbol, context.map(), selected );
644 }
645 else
646 {
647 billboardMaterial->useDefaultSymbol( context.map(), selected );
648 }
649
650 // Billboard Transform
651 Qt3DCore::QTransform *billboardTransform = new Qt3DCore::QTransform();
652 billboardTransform->setMatrix( mSymbol->billboardTransform() );
653
654 // Build the entity
655 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
656
657 entity->addComponent( billboardMaterial );
658 entity->addComponent( billboardTransform );
659 entity->addComponent( billboardGeometryRenderer );
660 entity->setParent( parent );
661
662// cppcheck wrongly believes entity will leak
663// cppcheck-suppress memleak
664}
665
666
667namespace Qgs3DSymbolImpl
668{
669
670 QgsFeature3DHandler *handlerForPoint3DSymbol( QgsVectorLayer *layer, const QgsAbstract3DSymbol *symbol )
671 {
672 const QgsPoint3DSymbol *pointSymbol = dynamic_cast< const QgsPoint3DSymbol * >( symbol );
673 if ( !pointSymbol )
674 return nullptr;
675
676 if ( pointSymbol->shape() == QgsPoint3DSymbol::Model )
677 return new QgsModelPoint3DSymbolHandler( pointSymbol, layer->selectedFeatureIds() );
678 // Add proper handler for billboard
679 else if ( pointSymbol->shape() == QgsPoint3DSymbol::Billboard )
680 return new QgsPoint3DBillboardSymbolHandler( pointSymbol, layer->selectedFeatureIds() );
681 else
682 return new QgsInstancedPoint3DSymbolHandler( pointSymbol, layer->selectedFeatureIds() );
683 }
684
685 Qt3DCore::QEntity *entityForPoint3DSymbol( const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsPoint3DSymbol &symbol )
686 {
687 QgsFeature3DHandler *handler = handlerForPoint3DSymbol( layer, &symbol );
688 Qt3DCore::QEntity *e = entityFromHandler( handler, map, layer );
689 delete handler;
690 return e;
691 }
692}
693
QColor selectionColor() const
Returns color used for selected features.
static void extractPointPositions(const QgsFeature &f, const Qgs3DMapSettings &map, Qgis::AltitudeClamping altClamp, QVector< QVector3D > &positions)
Calculates (x,y,z) positions of (multi)point from the given feature.
virtual Qt3DRender::QMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const =0
Creates a new QMaterial object representing the material settings.
virtual void addParametersToEffect(Qt3DRender::QEffect *effect) const =0
Adds parameters from the material to a destination effect.
static QgsSourceCache * sourceCache()
Returns the application's source cache, used for caching embedded and remote source strings as local ...
void setPoints(const QVector< QVector3D > &vertices)
Set the points for the billboard with vertices.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
QgsFeatureId id
Definition qgsfeature.h:64
QgsGeometry geometry
Definition qgsfeature.h:67
A marker symbol type, for rendering Point and MultiPoint geometries.
void setIsSelected(bool isSelected)
Sets whether the material should represent a selected state.
void setSelectionColor(const QColor &color)
Sets the color for representing materials in a selected state.
void useDefaultSymbol(const Qgs3DMapSettings &map, bool selected=false)
Set default symbol for the texture with map and selected parameter for rendering.
void setTexture2DFromSymbol(QgsMarkerSymbol *markerSymbol, const Qgs3DMapSettings &map, bool selected=false)
Set markerSymbol for the texture with map and selected parameter for rendering.
Shape
3D shape types supported by the symbol
@ ExtrudedText
Supported in Qt 5.9+.
QgsAbstractMaterialSettings * materialSettings() const
Returns material settings used for shading of the symbol.
QMatrix4x4 transform() const
Returns transform for individual objects represented by the symbol.
QgsMarkerSymbol * billboardSymbol() const
Returns a symbol for billboard.
QgsAbstract3DSymbol * clone() const override SIP_FACTORY
Shape shape() const
Returns 3D shape for points.
QVariantMap shapeProperties() const
Returns a key-value dictionary of point shape properties.
QString localFilePath(const QString &path, bool blocking=false)
Returns a local file path reflecting the content of a specified source path.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
@ Triangles
Triangle based rendering (default)
Qt3DCore::QAttribute Qt3DQAttribute
Definition qgs3daxis.cpp:28
Qt3DCore::QBuffer Qt3DQBuffer
Definition qgs3daxis.cpp:30
Qt3DCore::QGeometry Qt3DQGeometry
Definition qgs3daxis.cpp:29
QSet< QgsFeatureId > QgsFeatureIds
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry