30 #include <Qt3DCore/QTransform> 31 #include <Qt3DRender/QGeometryRenderer> 32 #include <Qt3DRender/QObjectPicker> 38 class TerrainMapUpdateJobFactory :
public QgsChunkQueueJobFactory
41 TerrainMapUpdateJobFactory( QgsTerrainTextureGenerator *textureGenerator )
42 : mTextureGenerator( textureGenerator )
46 QgsChunkQueueJob *createJob( QgsChunkNode *chunk )
override 48 return new TerrainMapUpdateJob( mTextureGenerator, chunk );
52 QgsTerrainTextureGenerator *mTextureGenerator =
nullptr;
59 QgsTerrainEntity::QgsTerrainEntity(
int maxLevel,
const Qgs3DMapSettings &map, Qt3DCore::QNode *parent )
60 : QgsChunkedEntity( map.terrainGenerator()->rootChunkBbox( map ),
61 map.terrainGenerator()->rootChunkError( map ),
62 map.maxTerrainScreenError(), maxLevel, map.terrainGenerator(), parent )
74 connectToLayersRepaintRequest();
78 mTextureGenerator =
new QgsTerrainTextureGenerator( map );
80 mUpdateJobFactory.reset(
new TerrainMapUpdateJobFactory( mTextureGenerator ) );
82 mTerrainPicker =
new Qt3DRender::QObjectPicker;
85 addComponent( mTerrainPicker );
88 QgsTerrainEntity::~QgsTerrainEntity()
94 delete mTextureGenerator;
95 delete mTerrainToMapTransform;
98 bool QgsTerrainEntity::rayIntersection(
const QgsRayCastingUtils::Ray3D &ray, QVector3D &intersectionPoint )
108 QList<QgsChunkNode *> lst = activeNodes();
109 for ( QgsChunkNode *n : lst )
111 if ( n->entity() && ( minDist < 0 || n->bbox().distanceFromPoint( ray.origin() ) < minDist ) && QgsRayCastingUtils::rayBoxIntersection( ray, n->bbox() ) )
113 Qt3DRender::QGeometryRenderer *rend = n->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
114 Qt3DRender::QGeometry *geom = rend->geometry();
115 DemTerrainTileGeometry *demGeom =
static_cast<DemTerrainTileGeometry *
>( geom );
116 Qt3DCore::QTransform *tr = n->entity()->findChild<Qt3DCore::QTransform *>();
117 QVector3D nodeIntPoint;
118 if ( demGeom->rayIntersection( ray, tr->matrix(), nodeIntPoint ) )
120 float dist = ( ray.origin() - intersectionPoint ).length();
121 if ( minDist < 0 || dist < minDist )
124 intersectionPoint = nodeIntPoint;
133 void QgsTerrainEntity::onShowBoundingBoxesChanged()
135 setShowBoundingBoxes( mMap.showTerrainBoundingBoxes() );
139 void QgsTerrainEntity::invalidateMapImages()
143 updateNodes( mActiveNodes, mUpdateJobFactory.get() );
147 QList<QgsChunkNode *> inactiveNodes;
148 Q_FOREACH ( QgsChunkNode *node, mRootNode->descendants() )
150 if ( !node->entity() )
152 if ( mActiveNodes.contains( node ) )
154 inactiveNodes << node;
157 updateNodes( inactiveNodes, mUpdateJobFactory.get() );
159 setNeedsUpdate(
true );
162 void QgsTerrainEntity::onLayersChanged()
164 connectToLayersRepaintRequest();
165 invalidateMapImages();
168 void QgsTerrainEntity::connectToLayersRepaintRequest()
175 mLayers = mMap.layers();
187 TerrainMapUpdateJob::TerrainMapUpdateJob( QgsTerrainTextureGenerator *textureGenerator, QgsChunkNode *node )
188 : QgsChunkQueueJob( node )
189 , mTextureGenerator( textureGenerator )
191 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( node->entity() );
192 connect( textureGenerator, &QgsTerrainTextureGenerator::tileReady,
this, &TerrainMapUpdateJob::onTileReady );
193 mJobId = textureGenerator->render( entity->textureImage()->imageExtent(), entity->textureImage()->imageDebugText() );
196 void TerrainMapUpdateJob::cancel()
199 mTextureGenerator->cancelJob( mJobId );
203 void TerrainMapUpdateJob::onTileReady(
int jobId,
const QImage &image )
205 if ( mJobId == jobId )
207 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( mNode->entity() );
208 entity->textureImage()->setImage( image );
Base class for all map layer types.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
void layersChanged()
Emitted when the list of map layers for terrain texture has changed.
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
3 Definition of the world
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Terrain is built from raster layer with digital elevation model.
void terrainMapThemeChanged()
Emitted when terrain's map theme has changed.
void showTerrainBoundingBoxesChanged()
Emitted when the flag whether terrain's bounding boxes are shown has changed.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
void showTerrainTilesInfoChanged()
Emitted when the flag whether terrain's tile info is shown has changed.
void showLabelsChanged()
Emitted when the flag whether labels are displayed on terrain tiles has changed.
void setTerrain(QgsTerrainEntity *t)
Sets terrain entity for the generator (does not transfer ownership)
void backgroundColorChanged()
Emitted when the background color has changed.
QgsTerrainGenerator * terrainGenerator() const
Returns terrain generator. It takes care of producing terrain tiles from the input data...