34#include <QGeometryRenderer>
35#include <Qt3DCore/QEntity>
37#include "moc_qgsdemterraingenerator.cpp"
96 return std::numeric_limits<float>::quiet_NaN();
99static void heightMapMinMax(
const QByteArray &heightMap,
float &zMin,
float &zMax )
101 const float *zBits = (
const float * ) heightMap.constData();
102 int zCount = heightMap.count() /
sizeof( float );
105 zMin = zMax = std::numeric_limits<float>::quiet_NaN();
106 for (
int i = 0; i < zCount; ++i )
109 if ( std::isnan( z ) )
116 zMin = std::min( zMin, z );
117 zMax = std::max( zMax, z );
125 return QgsFutureUtils::combine( heightMapFuture, textureFuture ).then(
this, [
this, node]( std::tuple<QByteArray, QgsTerrainGenerator::TerrainTextureResources> results ) {
126 QByteArray heightMap = std::get<0>( results );
128 return QgsChunkLoaderResult { std::bind_front( &QgsDemTerrainGenerator::createEntity,
this, heightMap, textureResources, node ) };
132Qt3DCore::QEntity *QgsDemTerrainGenerator::createEntity( QByteArray heightMap, TerrainTextureResources textureResources, QgsChunkNode *node, Qt3DCore::QEntity *parent )
136 heightMapMinMax( heightMap, zMin, zMax );
138 if ( std::isnan( zMin ) || std::isnan( zMax ) )
144 Qgs3DMapSettings *map =
mTerrain->mapSettings();
146 QgsChunkNodeId nodeId = node->tileId();
148 double side = extent.
width();
150 QgsTerrainTileEntity *entity =
new QgsTerrainTileEntity( nodeId );
154 Qt3DRender::QGeometryRenderer *mesh =
new Qt3DRender::QGeometryRenderer;
156 entity->addComponent( mesh );
163 QgsGeoTransform *transform =
new QgsGeoTransform;
164 transform->setGeoTranslation( QgsVector3D( extent.
xMinimum(), extent.
yMinimum(), 0 ) );
165 entity->addComponent( transform );
173 node->updateParentBoundingBoxesRecursively();
175 entity->setParent( parent );
191 if ( dem &&
mCrs.isValid() )
const QgsAbstractTerrainSettings * terrainSettings() const
Returns the terrain settings.
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
bool isTerrainShadingEnabled() const
Returns whether terrain shading is enabled.
QgsPhongMaterialSettings terrainShadingMaterial() const
Returns terrain shading material.
QList< QgsMapLayer * > layers() const
Returns the list of 3D map layers to be rendered in the scene.
Rendering context for preparation of 3D entities.
static Qgs3DRenderContext fromMapSettings(const Qgs3DMapSettings *mapSettings)
Creates an initialized Qgs3DRenderContext instance from given Qgs3DMapSettings.
static QgsRectangle tryReprojectExtent2D(const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs1, const QgsCoordinateReferenceSystem &crs2, const QgsCoordinateTransformContext &context)
Reprojects extent from crs1 to crs2 coordinate reference system with context context.
double verticalScale() const
Returns the vertical scale (exaggeration) for terrain.
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
~QgsDemTerrainGenerator() override
std::unique_ptr< QgsDemHeightMapGenerator > mHeightMapGenerator
QgsRectangle rootChunkExtent() const override
extent of the terrain's root chunk in terrain's CRS
static QgsTerrainGenerator * create()
Creates a new instance of a QgsDemTerrainGenerator object.
float heightAt(double x, double y, const Qgs3DRenderContext &context) const override
Returns height at (x,y) in map's CRS.
Type type() const override
What texture generator implementation is this.
QgsTerrainGenerator * clone() const override
Makes a copy of the current instance.
float mSkirtHeight
height of the "skirts" at the edges of tiles to hide cracks between adjacent cracks
QgsCoordinateTransformContext mTransformContext
QgsTerrainGenerator::Capabilities capabilities() const override
Returns flags containing the supported capabilities.
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context) override
Sets the CRS associated with the terrain.
QgsRasterLayer * layer() const
Returns raster layer with elevation model to be used for terrain generation.
void setExtent(const QgsRectangle &extent) override
sets the extent of the terrain in terrain's CRS
QFuture< QgsChunkLoaderResult > loadChunk(QgsChunkNode *node) override
QPointer< QgsRasterLayer > mLayer
source layer for heights
virtual void updateGenerator()
(Re-)creates mHeightMapGenerator based on current params
QgsCoordinateReferenceSystem crs() const override
Returns CRS of the terrain.
QgsCoordinateReferenceSystem mCrs
int mResolution
how many vertices to place on one side of the tile
void setLayer(QgsRasterLayer *layer)
Sets raster layer with elevation model to be used for terrain generation.
static QFuture< std::tuple< Ts... > > combine(QFuture< Ts >... futures)
Converts multiple futures into a single future that will finish when all its parts have finished.
Represents a raster layer.
A rectangle specified with double values.
Type
Enumeration of the available terrain generators.
@ Dem
Terrain is built from raster layer with digital elevation model.
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
QgsTerrainGenerator()
Create new terrain generator.
void applyMaterial(const TerrainTextureResources &resources, QgsTerrainTileEntity *entity, bool isShadingEnabled, const QgsPhongMaterialSettings &shadingMaterial, bool useTexture, const Qgs3DRenderContext &context)
Creates material component for the entity with the rendered map as a texture.
QgsTerrainEntity * mTerrain
QFuture< TerrainTextureResources > loadTextureResources(QgsChunkNode *node)
Asynchronously loads resources for creating texture.
QFlags< Capability > Capabilities
@ SupportsTileResolution
Supports tile resolution.
virtual void setTerrain(QgsTerrainEntity *t)
Sets terrain entity for the generator (does not transfer ownership).
const QgsTilingScheme & tilingScheme() const
Returns tiling scheme of the terrain.
Encapsulates tiling schemes (just like with WMTS / TMS / XYZ layers).
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
#define QGIS_CHECK_MAIN_THREAD_ACCESS