13
13
#include < Qt3DExtras/QPlaneGeometry>
14
14
#include < Qt3DExtras/QSphereGeometry>
15
15
#include < Qt3DExtras/QTorusGeometry>
16
+ #include < Qt3DExtras/QPhongMaterial>
17
+ #include < Qt3DRender/QSceneLoader>
18
+
19
+ #include < Qt3DRender/QMesh>
20
+
16
21
#if QT_VERSION >= 0x050900
17
22
#include < Qt3DExtras/QExtrudedTextGeometry>
18
23
#endif
26
31
27
32
#include " qgsvectorlayer.h"
28
33
#include " qgspoint.h"
29
-
34
+ # include " utils.h "
30
35
31
36
32
37
PointEntity::PointEntity ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol, Qt3DCore::QNode *parent )
33
38
: Qt3DCore::QEntity( parent )
34
39
{
35
- addEntityForSelectedPoints ( map, layer, symbol );
36
- addEntityForNotSelectedPoints ( map, layer, symbol );
40
+ if ( symbol.shapeProperties [" shape" ].toString () == " model" ) {
41
+ Model3DPointEntityFactory::addEntitiesForSelectedPoints (map, layer, symbol, this );
42
+ Model3DPointEntityFactory::addEntitiesForNotSelectedPoints (map, layer, symbol, this );
43
+ } else {
44
+ InstancedPointEntityFactory::addEntityForNotSelectedPoints (map, layer, symbol, this );
45
+ InstancedPointEntityFactory::addEntityForSelectedPoints (map, layer, symbol, this );
46
+ }
37
47
}
38
48
39
- Qt3DRender::QMaterial *PointEntity::material ( const Point3DSymbol &symbol ) const
49
+ // * INSTANCED RENDERING *//
50
+
51
+ Qt3DRender::QMaterial *InstancedPointEntityFactory::material ( const Point3DSymbol &symbol )
40
52
{
41
53
Qt3DRender::QFilterKey *filterKey = new Qt3DRender::QFilterKey;
42
54
filterKey->setName ( " renderingStyle" );
@@ -105,7 +117,7 @@ Qt3DRender::QMaterial *PointEntity::material( const Point3DSymbol &symbol ) cons
105
117
return material;
106
118
}
107
119
108
- void PointEntity ::addEntityForSelectedPoints ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol )
120
+ void InstancedPointEntityFactory ::addEntityForSelectedPoints ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol, PointEntity* parent )
109
121
{
110
122
// build the default material
111
123
Qt3DRender::QMaterial *mat = material ( symbol );
@@ -125,12 +137,12 @@ void PointEntity::addEntityForSelectedPoints( const Map3D &map, QgsVectorLayer *
125
137
req.setFilterFids ( layer->selectedFeatureIds () );
126
138
127
139
// build the entity
128
- PointEntityNode *entity = new PointEntityNode ( map, layer, symbol, req );
140
+ InstancedPointEntityNode *entity = new InstancedPointEntityNode ( map, layer, symbol, req );
129
141
entity->addComponent ( mat );
130
- entity->setParent ( this );
142
+ entity->setParent ( parent );
131
143
}
132
144
133
- void PointEntity ::addEntityForNotSelectedPoints ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol )
145
+ void InstancedPointEntityFactory ::addEntityForNotSelectedPoints ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol, PointEntity* parent )
134
146
{
135
147
// build the default material
136
148
Qt3DRender::QMaterial *mat = material ( symbol );
@@ -144,19 +156,19 @@ void PointEntity::addEntityForNotSelectedPoints( const Map3D &map, QgsVectorLaye
144
156
req.setFilterFids ( notSelected );
145
157
146
158
// build the entity
147
- PointEntityNode *entity = new PointEntityNode ( map, layer, symbol, req );
159
+ InstancedPointEntityNode *entity = new InstancedPointEntityNode ( map, layer, symbol, req );
148
160
entity->addComponent ( mat );
149
- entity->setParent ( this );
161
+ entity->setParent ( parent );
150
162
}
151
163
152
- PointEntityNode::PointEntityNode ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent )
164
+ InstancedPointEntityNode::InstancedPointEntityNode ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent )
153
165
: Qt3DCore::QEntity( parent )
154
166
{
155
- QList<QVector3D> pos = positions ( map, layer, req );
167
+ QList<QVector3D> pos = Utils:: positions ( map, layer, req );
156
168
addComponent ( renderer ( symbol, pos ) );
157
169
}
158
170
159
- Qt3DRender::QGeometryRenderer *PointEntityNode ::renderer ( const Point3DSymbol &symbol, const QList<QVector3D> &positions ) const
171
+ Qt3DRender::QGeometryRenderer *InstancedPointEntityNode ::renderer ( const Point3DSymbol &symbol, const QList<QVector3D> &positions ) const
160
172
{
161
173
int count = positions.count ();
162
174
@@ -263,28 +275,91 @@ Qt3DRender::QGeometryRenderer *PointEntityNode::renderer( const Point3DSymbol &s
263
275
return renderer;
264
276
}
265
277
266
- QList<QVector3D> PointEntityNode::positions ( const Map3D &map, const QgsVectorLayer *layer, const QgsFeatureRequest &request ) const
278
+ // * 3D MODEL RENDERING *//
279
+
280
+ static Qt3DExtras::QPhongMaterial* phongMaterial (const Point3DSymbol &symbol) {
281
+ Qt3DExtras::QPhongMaterial* phong = new Qt3DExtras::QPhongMaterial;
282
+
283
+ phong->setAmbient (symbol.material .ambient ());
284
+ phong->setDiffuse (symbol.material .diffuse ());
285
+ phong->setSpecular (symbol.material .specular ());
286
+ phong->setShininess (symbol.material .shininess ());
287
+
288
+ return phong;
289
+ }
290
+
291
+ void Model3DPointEntityFactory::addEntitiesForSelectedPoints ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol, PointEntity* parent )
267
292
{
268
- QList<QVector3D> positions;
269
- QgsFeature f;
270
- QgsFeatureIterator fi = layer->getFeatures ( request );
271
- while ( fi.nextFeature ( f ) )
272
- {
273
- if ( f.geometry ().isNull () )
274
- continue ;
275
-
276
- QgsAbstractGeometry *g = f.geometry ().geometry ();
277
- if ( QgsWkbTypes::flatType ( g->wkbType () ) == QgsWkbTypes::Point )
278
- {
279
- QgsPoint *pt = static_cast <QgsPoint *>( g );
280
- // TODO: use Z coordinates if the point is 3D
281
- float h = map.terrainGenerator ()->heightAt ( pt->x (), pt->y (), map ) * map.terrainVerticalScale ();
282
- positions.append ( QVector3D ( pt->x () - map.originX , h, -( pt->y () - map.originY ) ) );
283
- // qDebug() << positions.last();
284
- }
285
- else
286
- qDebug () << " not a point" ;
293
+ QgsFeatureRequest req;
294
+ req.setDestinationCrs ( map.crs );
295
+ req.setFilterFids ( layer->selectedFeatureIds () );
296
+
297
+ addMeshEntities (map, layer, req, symbol, parent, true );
298
+ }
299
+
300
+
301
+
302
+ void Model3DPointEntityFactory::addEntitiesForNotSelectedPoints ( const Map3D &map, QgsVectorLayer *layer, const Point3DSymbol &symbol, PointEntity* parent )
303
+ {
304
+ // build the feature request to select features
305
+ QgsFeatureRequest req;
306
+ req.setDestinationCrs ( map.crs );
307
+ QgsFeatureIds notSelected = layer->allFeatureIds ();
308
+ notSelected.subtract ( layer->selectedFeatureIds () );
309
+ req.setFilterFids ( notSelected );
310
+
311
+ if (symbol.shapeProperties [" overwriteMaterial" ].toBool ()) {
312
+ addMeshEntities (map, layer, req, symbol, parent, false );
313
+ } else {
314
+ addSceneEntities (map, layer, req, symbol, parent);
287
315
}
316
+ }
317
+
318
+ void Model3DPointEntityFactory::addSceneEntities (const Map3D &map, QgsVectorLayer *layer, const QgsFeatureRequest &req, const Point3DSymbol &symbol, PointEntity* parent) {
319
+ QList<QVector3D> positions = Utils::positions ( map, layer, req );
320
+ Q_FOREACH (const QVector3D& position, positions) {
321
+ // build the entity
322
+ Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
323
+
324
+ QUrl url = QUrl::fromLocalFile (symbol.shapeProperties [" model" ].toString ());
325
+ Qt3DRender::QSceneLoader * modelLoader = new Qt3DRender::QSceneLoader;
326
+ modelLoader->setSource (url);
327
+
328
+ entity->addComponent ( modelLoader );
329
+ entity->addComponent (transform (position, symbol));
330
+ entity->setParent ( parent );
331
+ }
332
+ }
333
+
334
+ void Model3DPointEntityFactory::addMeshEntities (const Map3D &map, QgsVectorLayer *layer, const QgsFeatureRequest &req, const Point3DSymbol &symbol, PointEntity* parent, bool are_selected) {
335
+ // build the default material
336
+ Qt3DExtras::QPhongMaterial *mat = phongMaterial (symbol);
337
+
338
+ if (are_selected) {
339
+ mat->setDiffuse (map.selectionColor ());
340
+ mat->setAmbient (map.selectionColor ().darker ());
341
+ }
342
+
343
+ // get nodes
344
+ QList<QVector3D> positions = Utils::positions ( map, layer, req );
345
+ Q_FOREACH (const QVector3D& position, positions) {
346
+ // build the entity
347
+ Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
348
+
349
+ QUrl url = QUrl::fromLocalFile (symbol.shapeProperties [" model" ].toString ());
350
+ Qt3DRender::QMesh * mesh = new Qt3DRender::QMesh;
351
+ mesh->setSource (url);
352
+
353
+ entity->addComponent ( mesh );
354
+ entity->addComponent ( mat );
355
+ entity->addComponent (transform (position, symbol));
356
+ entity->setParent ( parent );
357
+ }
358
+ }
288
359
289
- return positions;
360
+ Qt3DCore::QTransform* Model3DPointEntityFactory::transform (const QVector3D& position, const Point3DSymbol &symbol) {
361
+ Qt3DCore::QTransform* tr = new Qt3DCore::QTransform;
362
+ tr->setMatrix (symbol.transform );
363
+ tr->setTranslation (position + tr->translation ());
364
+ return tr;
290
365
}
0 commit comments