Skip to content
Permalink
Browse files

Update list of 3D entities when the list of active layers is changed

  • Loading branch information
wonder-sk committed Jul 28, 2017
1 parent 82f608e commit 2e03f5d36bea007efa9f860dca6ecb3995dcb334
Showing with 54 additions and 10 deletions.
  1. +49 −10 src/3d/scene.cpp
  2. +5 −0 src/3d/scene.h
@@ -69,16 +69,13 @@ Scene::Scene( const Map3D &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph,

Q_FOREACH ( QgsMapLayer *layer, map.layers() )
{
if ( layer->renderer3D() )
{
Qt3DCore::QEntity *p = layer->renderer3D()->createEntity( map );
p->setParent( this );
mLayerEntities.insert( layer, p );
}
connect( layer, &QgsMapLayer::renderer3DChanged, this, &Scene::onLayerRenderer3DChanged );
addLayerEntity( layer );
// TODO: connect( layer, &QgsMapLayer::willBeDeleted, this, &Scene::onLayerWillBeDeleted );
}

// listen to changes of layers in order to add/remove 3D renderer entities
connect( &map, &Map3D::layersChanged, this, &Scene::onLayersChanged );

Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity;
Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform;
lightTransform->setTranslation( QVector3D( 0, 1000, 0 ) );
@@ -211,16 +208,58 @@ void Scene::onLayerRenderer3DChanged()
Q_ASSERT( layer );

// remove old entity - if any
Qt3DCore::QEntity *entity = mLayerEntities.take( layer );
if ( entity )
entity->deleteLater();
removeLayerEntity( layer );

// add new entity - if any 3D renderer
addLayerEntity( layer );
}

void Scene::onLayersChanged()
{
QSet<QgsMapLayer *> layersBefore = QSet<QgsMapLayer *>::fromList( mLayerEntities.keys() );
QList<QgsMapLayer *> layersAdded;
Q_FOREACH ( QgsMapLayer *layer, mMap.layers() )
{
if ( !layersBefore.contains( layer ) )
{
layersAdded << layer;
}
else
{
layersBefore.remove( layer );
}
}

// what is left in layersBefore are layers that have been removed
Q_FOREACH ( QgsMapLayer *layer, layersBefore )
{
removeLayerEntity( layer );
}

Q_FOREACH ( QgsMapLayer *layer, layersAdded )
{
addLayerEntity( layer );
}
}

void Scene::addLayerEntity( QgsMapLayer *layer )
{
QgsAbstract3DRenderer *renderer = layer->renderer3D();
if ( renderer )
{
Qt3DCore::QEntity *newEntity = renderer->createEntity( mMap );
newEntity->setParent( this );
mLayerEntities.insert( layer, newEntity );
}

connect( layer, &QgsMapLayer::renderer3DChanged, this, &Scene::onLayerRenderer3DChanged );
}

void Scene::removeLayerEntity( QgsMapLayer *layer )
{
Qt3DCore::QEntity *entity = mLayerEntities.take( layer );
if ( entity )
entity->deleteLater();

disconnect( layer, &QgsMapLayer::renderer3DChanged, this, &Scene::onLayerRenderer3DChanged );
}
@@ -44,6 +44,11 @@ class _3D_EXPORT Scene : public Qt3DCore::QEntity
void onFrameTriggered( float dt );
void createTerrain();
void onLayerRenderer3DChanged();
void onLayersChanged();

private:
void addLayerEntity( QgsMapLayer *layer );
void removeLayerEntity( QgsMapLayer *layer );

private:
const Map3D &mMap;

0 comments on commit 2e03f5d

Please sign in to comment.
You can’t perform that action at this time.