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