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