QGIS API Documentation 3.38.0-Grenoble (exported)
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, const QgsMaterialContext &materialContext );
89 static Qt3DRender::QGeometryRenderer *renderer( const QgsPoint3DSymbol *symbol, const QVector<QVector3D> &positions );
90 static Qt3DQGeometry *symbolGeometry( const QgsPoint3DSymbol *symbol );
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->shapeProperty( QStringLiteral( "length" ) ).toFloat();
147 mZMin -= length * 0.5f;
148 mZMax += length * 0.5f;
149 break;
150 }
151
153 {
154 const float radius = mSymbol->shapeProperty( QStringLiteral( "radius" ) ).toFloat();
155 mZMin -= radius;
156 mZMax += radius;
157 break;
158 }
159
161 {
162 const float length = mSymbol->shapeProperty( QStringLiteral( "length" ) ).toFloat();
163 mZMin -= length * 0.5f;
164 mZMax += length * 0.5f;
165 break;
166 }
167
169 {
170 const float size = mSymbol->shapeProperty( QStringLiteral( "size" ) ).toFloat();
171 mZMin -= size * 0.5f;
172 mZMax += size * 0.5f;
173 break;
174 }
175
177 {
178 const float radius = mSymbol->shapeProperty( QStringLiteral( "radius" ) ).toFloat();
179 mZMin -= radius;
180 mZMax += radius;
181 break;
182 }
183
185 {
186 // worst case scenario -- even though planes are usually rotated so that they are flat,
187 // let's account for possible overridden rotation
188 const float size = mSymbol->shapeProperty( QStringLiteral( "size" ) ).toFloat();
189 mZMin -= size * 0.5f;
190 mZMax += size * 0.5f;
191 break;
192 }
193
197 break;
198 }
199
200 mZMin += symbolOffset;
201 mZMax += symbolOffset;
202}
203
204void QgsInstancedPoint3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PointData &out, bool selected )
205{
206 // build the default material
207 QgsMaterialContext materialContext;
208 materialContext.setIsSelected( selected );
209 materialContext.setSelectionColor( context.map().selectionColor() );
210 Qt3DRender::QMaterial *mat = material( mSymbol.get(), materialContext );
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, const QgsMaterialContext &materialContext )
225{
226 Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey;
227 filterKey->setName( QStringLiteral( "renderingStyle" ) );
228 filterKey->setValue( "forward" );
229
230 Qt3DRender::QShaderProgram *shaderProgram = new Qt3DRender::QShaderProgram;
231 shaderProgram->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/instanced.vert" ) ) ) );
232 shaderProgram->setFragmentShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral( "qrc:/shaders/phongConstant.frag" ) ) ) );
233
234 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass;
235 renderPass->setShaderProgram( shaderProgram );
236
237 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique;
238 technique->addFilterKey( filterKey );
239 technique->addRenderPass( renderPass );
240 technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
241 technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
242 technique->graphicsApiFilter()->setMajorVersion( 3 );
243 technique->graphicsApiFilter()->setMinorVersion( 2 );
244
245 const QMatrix4x4 transformMatrix = symbol->transform();
246 QMatrix3x3 normalMatrix = transformMatrix.normalMatrix(); // transponed inverse of 3x3 sub-matrix
247
248 // QMatrix3x3 is not supported for passing to shaders, so we pass QMatrix4x4
249 float *n = normalMatrix.data();
250 const QMatrix4x4 normalMatrix4(
251 n[0], n[3], n[6], 0,
252 n[1], n[4], n[7], 0,
253 n[2], n[5], n[8], 0,
254 0, 0, 0, 0 );
255
256 Qt3DRender::QParameter *paramInst = new Qt3DRender::QParameter;
257 paramInst->setName( QStringLiteral( "inst" ) );
258 paramInst->setValue( transformMatrix );
259
260 Qt3DRender::QParameter *paramInstNormal = new Qt3DRender::QParameter;
261 paramInstNormal->setName( QStringLiteral( "instNormal" ) );
262 paramInstNormal->setValue( normalMatrix4 );
263
264 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect;
265 effect->addTechnique( technique );
266 effect->addParameter( paramInst );
267 effect->addParameter( paramInstNormal );
268
269 symbol->materialSettings()->addParametersToEffect( effect, materialContext );
270
271 Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial;
272 material->setEffect( effect );
273
274 return material;
275}
276
277Qt3DRender::QGeometryRenderer *QgsInstancedPoint3DSymbolHandler::renderer( const QgsPoint3DSymbol *symbol, const QVector<QVector3D> &positions )
278{
279 const int count = positions.count();
280 const int byteCount = positions.count() * sizeof( QVector3D );
281 QByteArray ba;
282 ba.resize( byteCount );
283 memcpy( ba.data(), positions.constData(), byteCount );
284
285 Qt3DQBuffer *instanceBuffer = new Qt3DQBuffer();
286 instanceBuffer->setData( ba );
287
288 Qt3DQAttribute *instanceDataAttribute = new Qt3DQAttribute;
289 instanceDataAttribute->setName( QStringLiteral( "pos" ) );
290 instanceDataAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
291 instanceDataAttribute->setVertexBaseType( Qt3DQAttribute::Float );
292 instanceDataAttribute->setVertexSize( 3 );
293 instanceDataAttribute->setByteOffset( 0 );
294 instanceDataAttribute->setDivisor( 1 );
295 instanceDataAttribute->setBuffer( instanceBuffer );
296 instanceDataAttribute->setCount( count );
297 instanceDataAttribute->setByteStride( 3 * sizeof( float ) );
298
299 Qt3DQGeometry *geometry = symbolGeometry( symbol );
300 geometry->addAttribute( instanceDataAttribute );
301 geometry->setBoundingVolumePositionAttribute( instanceDataAttribute );
302
303 Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
304 renderer->setGeometry( geometry );
305 renderer->setInstanceCount( count );
306
307 return renderer;
308}
309
310Qt3DQGeometry *QgsInstancedPoint3DSymbolHandler::symbolGeometry( const QgsPoint3DSymbol *symbol )
311{
312 switch ( symbol->shape() )
313 {
315 {
316 const float radius = symbol->shapeProperty( QStringLiteral( "radius" ) ).toFloat();
317 const float length = symbol->shapeProperty( QStringLiteral( "length" ) ).toFloat();
318 Qt3DExtras::QCylinderGeometry *g = new Qt3DExtras::QCylinderGeometry;
319 //g->setRings(2); // how many vertices vertically
320 //g->setSlices(8); // how many vertices on circumference
321 g->setRadius( radius );
322 g->setLength( length );
323 return g;
324 }
325
327 {
328 const float radius = symbol->shapeProperty( QStringLiteral( "radius" ) ).toFloat();
329 Qt3DExtras::QSphereGeometry *g = new Qt3DExtras::QSphereGeometry;
330 g->setRadius( radius );
331 return g;
332 }
333
335 {
336 const float length = symbol->shapeProperty( QStringLiteral( "length" ) ).toFloat();
337 const float bottomRadius = symbol->shapeProperty( QStringLiteral( "bottomRadius" ) ).toFloat();
338 const float topRadius = symbol->shapeProperty( QStringLiteral( "topRadius" ) ).toFloat();
339
340 Qt3DExtras::QConeGeometry *g = new Qt3DExtras::QConeGeometry;
341 g->setLength( length );
342 g->setBottomRadius( bottomRadius );
343 g->setTopRadius( topRadius );
344 //g->setHasBottomEndcap(hasBottomEndcap);
345 //g->setHasTopEndcap(hasTopEndcap);
346 return g;
347 }
348
350 {
351 const float size = symbol->shapeProperty( QStringLiteral( "size" ) ).toFloat();
352 Qt3DExtras::QCuboidGeometry *g = new Qt3DExtras::QCuboidGeometry;
353 g->setXExtent( size );
354 g->setYExtent( size );
355 g->setZExtent( size );
356 return g;
357 }
358
360 {
361 const float radius = symbol->shapeProperty( QStringLiteral( "radius" ) ).toFloat();
362 const float minorRadius = symbol->shapeProperty( QStringLiteral( "minorRadius" ) ).toFloat();
363 Qt3DExtras::QTorusGeometry *g = new Qt3DExtras::QTorusGeometry;
364 g->setRadius( radius );
365 g->setMinorRadius( minorRadius );
366 return g;
367 }
368
370 {
371 const float size = symbol->shapeProperty( QStringLiteral( "size" ) ).toFloat();
372 Qt3DExtras::QPlaneGeometry *g = new Qt3DExtras::QPlaneGeometry;
373 g->setWidth( size );
374 g->setHeight( size );
375 return g;
376 }
377
379 {
380 const float depth = symbol->shapeProperty( QStringLiteral( "depth" ) ).toFloat();
381 const QString text = symbol->shapeProperty( QStringLiteral( "text" ) ).toString();
382 Qt3DExtras::QExtrudedTextGeometry *g = new Qt3DExtras::QExtrudedTextGeometry;
383 g->setDepth( depth );
384 g->setText( text );
385 return g;
386 }
387
390 break;
391 }
392 Q_ASSERT( false );
393 return nullptr;
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->shapeProperty( 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->shapeProperty( 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->shapeProperty( 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() == Qgis::Point3DShape::Model )
677 return new QgsModelPoint3DSymbolHandler( pointSymbol, layer->selectedFeatureIds() );
678 // Add proper handler for billboard
679 else if ( pointSymbol->shape() == Qgis::Point3DShape::Billboard )
680 return new QgsPoint3DBillboardSymbolHandler( pointSymbol, layer->selectedFeatureIds() );
681 else
682 return new QgsInstancedPoint3DSymbolHandler( pointSymbol, layer->selectedFeatureIds() );
683 }
684}
685
@ Plane
Flat plane.
@ Cylinder
Cylinder.
@ ExtrudedText
Extruded text.
@ Billboard
Billboard.
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 QgsMaterialContext &materialContext) 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:58
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
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.
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.
Qgis::Point3DShape shape() const
Returns 3D shape for points.
QgsAbstract3DSymbol * clone() const override
Returns a new instance of the symbol with the same settings.
QVariant shapeProperty(const QString &property) const
Returns the value for a specific shape property.
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
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry
QSet< QgsFeatureId > QgsFeatureIds
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
Qt3DCore::QGeometry Qt3DQGeometry