QGIS API Documentation 4.3.0-Master (16eddb2670e)
Loading...
Searching...
No Matches
qgsterraingenerator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsterraingenerator.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsterraingenerator.h"
17
18#include "qgs3d.h"
19#include "qgs3dmapsettings.h"
20#include "qgs3dutils.h"
24#include "qgsterrainentity.h"
28#include "qgstexturematerial.h"
29#include "qgsthreadingutils.h"
30
31#include <QString>
32#include <QTechnique>
33
34#include "moc_qgsterraingenerator.cpp"
35
36using namespace Qt::StringLiterals;
37
40
42{
44
45 float hMin, hMax;
46 rootChunkHeightRange( hMin, hMax );
47 return QgsBox3D( te.xMinimum(), te.yMinimum(), hMin, te.xMaximum(), te.yMaximum(), hMax );
48}
49
51{
53
54 // use texel size as the error
55 return te.width() / map.terrainSettings()->mapTileResolution();
56}
57
58void QgsTerrainGenerator::rootChunkHeightRange( float &hMin, float &hMax ) const
59{
60 // TODO: makes sense to have kind of default implementation?
63}
64
66{
67 switch ( type )
68 {
70 return u"flat"_s;
72 return u"dem"_s;
74 return u"online"_s;
76 return u"mesh"_s;
78 return u"quantizedmesh"_s;
79 }
80 return QString();
81}
82
85
87{
88 return mIsValid;
89}
90
91QFuture<QgsChunkLoaderResult> QgsTerrainGenerator::updateChunk( QgsChunkNode *node )
92{
93 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( node->entity() );
94 return mTerrain->textureGenerator()->render( entity->textureImage()->imageExtent(), node->tileId(), entity->textureImage()->imageDebugText() ).then( this, [entity]( QImage image ) {
95 return QgsChunkLoaderResult { [entity, image]( Qt3DCore::QEntity *parent ) {
97 entity->textureImage()->setImage( image );
98 entity->setParent( parent );
99 return entity;
100 } };
101 } );
102}
103
108
109QFuture<QgsTerrainGenerator::TerrainTextureResources> QgsTerrainGenerator::loadTextureResources( QgsChunkNode *node )
110{
111 const Qgs3DMapSettings *map = mTerrain->mapSettings();
112 const QgsChunkNodeId nodeId = node->tileId();
113 const QgsRectangle extentTerrainCrs = map->terrainGenerator()->tilingScheme().tileToExtent( nodeId );
114 const QgsRectangle extentMapCrs = Qgs3DUtils::tryReprojectExtent2D( extentTerrainCrs, map->terrainGenerator()->crs(), map->crs(), map->transformContext() );
115 const QString tileDebugText = node->tileId().text();
116
117 return mTerrain->textureGenerator()->render( extentMapCrs, node->tileId(), tileDebugText ).then( this, [extentTerrainCrs, extentMapCrs, tileDebugText]( QImage img ) {
119 img,
120 extentTerrainCrs,
121 extentMapCrs,
122 tileDebugText,
123 };
124 } );
125}
126
128 const TerrainTextureResources &resources, QgsTerrainTileEntity *entity, bool isShadingEnabled, const QgsPhongMaterialSettings &shadingMaterial, bool useTexture, const Qgs3DRenderContext &context
129)
130{
132 Qt3DRender::QTexture2D *texture = useTexture || !isShadingEnabled ? applyTexture( entity, materialContext, resources ) : nullptr;
133
134 QgsMaterial *material = nullptr;
135 if ( texture )
136 {
137 if ( isShadingEnabled )
138 {
139 QgsPhongTexturedMaterial *phongTexturedMaterial = new QgsPhongTexturedMaterial();
140 phongTexturedMaterial->setAmbient( shadingMaterial.ambient() );
141 phongTexturedMaterial->setSpecular( shadingMaterial.specular() );
142 phongTexturedMaterial->setShininess( static_cast<float>( shadingMaterial.shininess() ) );
143 phongTexturedMaterial->setDiffuseTexture( texture );
144 phongTexturedMaterial->setOpacity( static_cast<float>( shadingMaterial.opacity() ) );
145 material = phongTexturedMaterial;
146 }
147 else
148 {
149 QgsTextureMaterial *textureMaterial = new QgsTextureMaterial;
150 textureMaterial->setTexture( texture );
151 material = textureMaterial;
152 }
153 }
154 else
155 {
156 materialContext.setIsSelected( false );
157 material = Qgs3D::toMaterial( &shadingMaterial, Qgis::MaterialRenderingTechnique::Triangles, materialContext );
158 }
159
160 // no backface culling on terrain, to allow terrain to be viewed from underground
161 const QVector<Qt3DRender::QTechnique *> techniques = material->effect()->techniques();
162 for ( Qt3DRender::QTechnique *technique : techniques )
163 {
164 const QVector<Qt3DRender::QRenderPass *> passes = technique->renderPasses();
165 for ( Qt3DRender::QRenderPass *pass : passes )
166 {
167 Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
168 cullFace->setMode( Qt3DRender::QCullFace::NoCulling );
169 pass->addRenderState( cullFace );
170 }
171 }
172
173 entity->addComponent( material ); // takes ownership if the component has no parent
174}
175
176Qt3DRender::QTexture2D *QgsTerrainGenerator::applyTexture( QgsTerrainTileEntity *entity, const QgsMaterialContext &context, const QgsTerrainGenerator::TerrainTextureResources &resources ) const
177{
178 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
179 QgsTerrainTextureImage *textureImage = new QgsTerrainTextureImage( resources.image, resources.extentMapCrs, resources.tileDebugText );
180 Qgs3DUtils::setTextureFiltering( texture, context );
181 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
182 texture->addTextureImage( textureImage ); //texture take the ownership of textureImage if has no parant
183
184 entity->setTextureImage( textureImage );
185
186 return texture;
187}
@ Triangles
Triangle based rendering (default).
Definition qgis.h:4454
Definition of the world.
const QgsAbstractTerrainSettings * terrainSettings() const
Returns the terrain settings.
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Rendering context for preparation of 3D entities.
static void setTextureFiltering(Qt3DRender::QAbstractTexture *texture, const QgsMaterialContext &context)
Sets the default filtering options for a texture.
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.
static constexpr double MINIMUM_VECTOR_Z_ESTIMATE
Definition qgs3dutils.h:553
static constexpr double MAXIMUM_VECTOR_Z_ESTIMATE
Definition qgs3dutils.h:554
static QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context)
Creates a new QgsMaterial object representing the material settings.
Definition qgs3d.cpp:159
int mapTileResolution() const
Returns the resolution (in pixels) of the texture of a terrain tile.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Context settings for a material.
void setIsSelected(bool isSelected)
Sets whether the material should represent a selected state.
static QgsMaterialContext fromRenderContext(const Qgs3DRenderContext &context)
Constructs a material context from the settings in a 3D render context.
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:40
Basic shading material used for rendering based on the Phong shading model with three color component...
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
double shininess() const
Returns shininess of the surface.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
Type
Enumeration of the available terrain generators.
@ QuantizedMesh
Terrain is built from quantized mesh tiles.
@ Dem
Terrain is built from raster layer with digital elevation model.
@ Online
Terrain is built from downloaded tiles with digital elevation model.
@ Mesh
Terrain is built from mesh layer with z value on vertices.
@ Flat
The whole terrain is flat area.
QgsTerrainGenerator()
Create new terrain generator.
virtual Type type() const =0
What texture generator implementation is this.
~QgsTerrainGenerator() override
virtual QgsBox3D rootChunkBox3D(const Qgs3DMapSettings &map) const
Returns 3D box (in map coordinates) of the root chunk.
virtual float rootChunkError(const Qgs3DMapSettings &map) const
Returns error of the root chunk in world coordinates.
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.
static QString typeToString(Type type)
Converts terrain generator type enumeration into a string.
virtual QgsRectangle rootChunkExtent() const =0
extent of the terrain's root chunk in terrain's CRS
QgsTerrainEntity * mTerrain
QFuture< TerrainTextureResources > loadTextureResources(QgsChunkNode *node)
Asynchronously loads resources for creating texture.
QFuture< QgsChunkLoaderResult > updateChunk(QgsChunkNode *node) override
virtual void rootChunkHeightRange(float &hMin, float &hMax) const
Returns height range of the root chunk in world coordinates.
QFlags< Capability > Capabilities
Qt3DRender::QTexture2D * applyTexture(QgsTerrainTileEntity *entity, const QgsMaterialContext &context, const TerrainTextureResources &resources) const
Creates a new texture that is linked to the entity.
@ NoCapabilities
No specific capabilities.
virtual QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
const QgsTilingScheme & tilingScheme() const
Returns tiling scheme of the terrain.
virtual QgsTerrainGenerator::Capabilities capabilities() const
Returns flags containing the supported capabilities.
bool isValid() const
Returns whether the terrain generator is valid.
virtual void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the CRS associated with the terrain.
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
#define QGIS_CHECK_MAIN_THREAD_ACCESS