|
6 | 6 | #include "terraingenerator.h"
|
7 | 7 | #include "utils.h"
|
8 | 8 |
|
9 |
| -#include <Qt3DExtras/QPhongMaterial> |
10 |
| -#include <Qt3DRender/QGeometryRenderer> |
11 | 9 | #include <Qt3DCore/QTransform>
|
12 | 10 |
|
13 | 11 | #include "qgsvectorlayer.h"
|
|
18 | 16 | PolygonEntity::PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, Qt3DCore::QNode *parent )
|
19 | 17 | : Qt3DCore::QEntity( parent )
|
20 | 18 | {
|
21 |
| - QgsPointXY origin( map.originX, map.originY ); |
| 19 | + addEntityForSelectedPolygons( map, layer, symbol ); |
| 20 | + addEntityForNotSelectedPolygons( map, layer, symbol ); |
| 21 | +} |
| 22 | + |
| 23 | +void PolygonEntity::addEntityForSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol ) |
| 24 | +{ |
| 25 | + // build the default material |
| 26 | + Qt3DExtras::QPhongMaterial *mat = material( symbol ); |
| 27 | + |
| 28 | + // update the material with selection colors |
| 29 | + mat->setDiffuse( map.selectionColor() ); |
| 30 | + mat->setAmbient( map.selectionColor().darker() ); |
| 31 | + |
| 32 | + // build a transform function |
| 33 | + Qt3DCore::QTransform *tform = new Qt3DCore::QTransform; |
| 34 | + tform->setTranslation( QVector3D( 0, 0, 0 ) ); |
| 35 | + |
| 36 | + // build the feature request to select features |
| 37 | + QgsFeatureRequest req; |
| 38 | + req.setDestinationCrs( map.crs ); |
| 39 | + req.setFilterFids( layer->selectedFeatureIds() ); |
| 40 | + |
| 41 | + // build the entity |
| 42 | + PolygonEntityNode *entity = new PolygonEntityNode( map, layer, symbol, req ); |
| 43 | + entity->addComponent( mat ); |
| 44 | + entity->addComponent( tform ); |
| 45 | + entity->setParent( this ); |
| 46 | +} |
| 47 | + |
| 48 | +void PolygonEntity::addEntityForNotSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol ) |
| 49 | +{ |
| 50 | + // build the default material |
| 51 | + Qt3DExtras::QPhongMaterial *mat = material( symbol ); |
| 52 | + |
| 53 | + // build a transform function |
| 54 | + Qt3DCore::QTransform *tform = new Qt3DCore::QTransform; |
| 55 | + tform->setTranslation( QVector3D( 0, 0, 0 ) ); |
| 56 | + |
| 57 | + // build the feature request to select features |
| 58 | + QgsFeatureRequest req; |
| 59 | + req.setDestinationCrs( map.crs ); |
| 60 | + |
| 61 | + QgsFeatureIds notSelected = layer->allFeatureIds(); |
| 62 | + notSelected.subtract( layer->selectedFeatureIds() ); |
| 63 | + req.setFilterFids( notSelected ); |
| 64 | + |
| 65 | + // build the entity |
| 66 | + PolygonEntityNode *entity = new PolygonEntityNode( map, layer, symbol, req ); |
| 67 | + entity->addComponent( mat ); |
| 68 | + entity->addComponent( tform ); |
| 69 | + entity->setParent( this ); |
| 70 | +} |
22 | 71 |
|
| 72 | +Qt3DExtras::QPhongMaterial *PolygonEntity::material( const Polygon3DSymbol &symbol ) const |
| 73 | +{ |
23 | 74 | Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
|
24 | 75 | material->setAmbient( symbol.material.ambient() );
|
25 | 76 | material->setDiffuse( symbol.material.diffuse() );
|
26 | 77 | material->setSpecular( symbol.material.specular() );
|
27 | 78 | material->setShininess( symbol.material.shininess() );
|
28 |
| - addComponent( material ); |
| 79 | + return material; |
| 80 | +} |
29 | 81 |
|
| 82 | +PolygonEntityNode::PolygonEntityNode( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent ) |
| 83 | + : Qt3DCore::QEntity( parent ) |
| 84 | +{ |
| 85 | + addComponent( renderer( map, symbol, layer, req ) ); |
| 86 | +} |
| 87 | + |
| 88 | +Qt3DRender::QGeometryRenderer *PolygonEntityNode::renderer( const Map3D &map, const Polygon3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &request ) |
| 89 | +{ |
| 90 | + QgsPointXY origin( map.originX, map.originY ); |
30 | 91 | QList<QgsPolygonV2 *> polygons;
|
31 | 92 | QgsFeature f;
|
32 |
| - QgsFeatureRequest request; |
33 |
| - request.setDestinationCrs( map.crs ); |
34 | 93 | QgsFeatureIterator fi = layer->getFeatures( request );
|
35 | 94 | while ( fi.nextFeature( f ) )
|
36 | 95 | {
|
@@ -62,14 +121,11 @@ PolygonEntity::PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Pol
|
62 | 121 | qDebug() << "not a polygon";
|
63 | 122 | }
|
64 | 123 |
|
65 |
| - geometry = new PolygonGeometry; |
66 |
| - geometry->setPolygons( polygons, origin, symbol.extrusionHeight ); |
| 124 | + mGeometry = new PolygonGeometry; |
| 125 | + mGeometry->setPolygons( polygons, origin, symbol.extrusionHeight ); |
67 | 126 |
|
68 | 127 | Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
|
69 |
| - renderer->setGeometry( geometry ); |
70 |
| - addComponent( renderer ); |
| 128 | + renderer->setGeometry( mGeometry ); |
71 | 129 |
|
72 |
| - Qt3DCore::QTransform *tform = new Qt3DCore::QTransform; |
73 |
| - tform->setTranslation( QVector3D( 0, 0, 0 ) ); |
74 |
| - addComponent( tform ); |
| 130 | + return renderer; |
75 | 131 | }
|
0 commit comments