21 #include "qgsmeshterraingenerator.h" 26 #include <QDomDocument> 27 #include <QDomElement> 34 , mOrigin( other.mOrigin )
36 , mBackgroundColor( other.mBackgroundColor )
37 , mTerrainVerticalScale( other.mTerrainVerticalScale )
38 , mTerrainGenerator( other.mTerrainGenerator ? other.mTerrainGenerator->clone() : nullptr )
39 , mMapTileResolution( other.mMapTileResolution )
40 , mMaxTerrainScreenError( other.mMaxTerrainScreenError )
41 , mMaxTerrainGroundError( other.mMaxTerrainGroundError )
42 , mTerrainShadingEnabled( other.mTerrainShadingEnabled )
43 , mTerrainShadingMaterial( other.mTerrainShadingMaterial )
44 , mTerrainMapTheme( other.mTerrainMapTheme )
45 , mShowTerrainBoundingBoxes( other.mShowTerrainBoundingBoxes )
46 , mShowTerrainTileInfo( other.mShowTerrainTileInfo )
47 , mShowCameraViewCenter( other.mShowCameraViewCenter )
48 , mShowLabels( other.mShowLabels )
49 , mPointLights( other.mPointLights )
50 , mFieldOfView( other.mFieldOfView )
51 , mLayers( other.mLayers )
52 , mSkyboxEnabled( other.mSkyboxEnabled )
53 , mSkyboxFileBase( other.mSkyboxFileBase )
54 , mSkyboxFileExtension( other.mSkyboxFileExtension )
55 , mTransformContext( other.mTransformContext )
56 , mPathResolver( other.mPathResolver )
57 , mMapThemes( other.mMapThemes )
61 mRenderers << renderer->
clone();
67 qDeleteAll( mRenderers );
72 QDomElement elemOrigin = elem.firstChildElement( QStringLiteral(
"origin" ) );
74 elemOrigin.attribute( QStringLiteral(
"x" ) ).toDouble(),
75 elemOrigin.attribute( QStringLiteral(
"y" ) ).toDouble(),
76 elemOrigin.attribute( QStringLiteral(
"z" ) ).toDouble() );
78 QDomElement elemCamera = elem.firstChildElement( QStringLiteral(
"camera" ) );
79 if ( !elemCamera.isNull() )
81 mFieldOfView = elemCamera.attribute( QStringLiteral(
"field-of-view" ), QStringLiteral(
"45" ) ).toFloat();
84 QDomElement elemColor = elem.firstChildElement( QStringLiteral(
"color" ) );
85 if ( !elemColor.isNull() )
91 QDomElement elemCrs = elem.firstChildElement( QStringLiteral(
"crs" ) );
94 QDomElement elemTerrain = elem.firstChildElement( QStringLiteral(
"terrain" ) );
95 mTerrainVerticalScale = elemTerrain.attribute( QStringLiteral(
"exaggeration" ), QStringLiteral(
"1" ) ).toFloat();
96 mMapTileResolution = elemTerrain.attribute( QStringLiteral(
"texture-size" ), QStringLiteral(
"512" ) ).toInt();
97 mMaxTerrainScreenError = elemTerrain.attribute( QStringLiteral(
"max-terrain-error" ), QStringLiteral(
"3" ) ).toFloat();
98 mMaxTerrainGroundError = elemTerrain.attribute( QStringLiteral(
"max-ground-error" ), QStringLiteral(
"1" ) ).toFloat();
99 mTerrainShadingEnabled = elemTerrain.attribute( QStringLiteral(
"shading-enabled" ), QStringLiteral(
"0" ) ).toInt();
100 QDomElement elemTerrainShadingMaterial = elemTerrain.firstChildElement( QStringLiteral(
"shading-material" ) );
101 if ( !elemTerrainShadingMaterial.isNull() )
102 mTerrainShadingMaterial.
readXml( elemTerrainShadingMaterial );
103 mTerrainMapTheme = elemTerrain.attribute( QStringLiteral(
"map-theme" ) );
104 mShowLabels = elemTerrain.attribute( QStringLiteral(
"show-labels" ), QStringLiteral(
"0" ) ).toInt();
106 mPointLights.clear();
107 QDomElement elemPointLights = elem.firstChildElement( QStringLiteral(
"point-lights" ) );
108 if ( !elemPointLights.isNull() )
110 QDomElement elemPointLight = elemPointLights.firstChildElement( QStringLiteral(
"point-light" ) );
111 while ( !elemPointLight.isNull() )
114 pointLight.
readXml( elemPointLight );
115 mPointLights << pointLight;
116 elemPointLight = elemPointLight.nextSiblingElement( QStringLiteral(
"point-light" ) );
124 mPointLights << defaultLight;
127 QDomElement elemMapLayers = elemTerrain.firstChildElement( QStringLiteral(
"layers" ) );
128 QDomElement elemMapLayer = elemMapLayers.firstChildElement( QStringLiteral(
"layer" ) );
129 QList<QgsMapLayerRef> mapLayers;
130 while ( !elemMapLayer.isNull() )
132 mapLayers <<
QgsMapLayerRef( elemMapLayer.attribute( QStringLiteral(
"id" ) ) );
133 elemMapLayer = elemMapLayer.nextSiblingElement( QStringLiteral(
"layer" ) );
137 QDomElement elemTerrainGenerator = elemTerrain.firstChildElement( QStringLiteral(
"generator" ) );
138 QString terrainGenType = elemTerrainGenerator.attribute( QStringLiteral(
"type" ) );
139 if ( terrainGenType == QLatin1String(
"dem" ) )
142 demTerrainGenerator->
setCrs( mCrs, mTransformContext );
143 mTerrainGenerator.reset( demTerrainGenerator );
145 else if ( terrainGenType == QLatin1String(
"online" ) )
148 onlineTerrainGenerator->
setCrs( mCrs, mTransformContext );
149 mTerrainGenerator.reset( onlineTerrainGenerator );
151 else if ( terrainGenType == QLatin1String(
"mesh" ) )
153 QgsMeshTerrainGenerator *meshTerrainGenerator =
new QgsMeshTerrainGenerator;
154 meshTerrainGenerator->setCrs( mCrs, mTransformContext );
155 mTerrainGenerator.reset( meshTerrainGenerator );
161 mTerrainGenerator.reset( flatGen );
163 mTerrainGenerator->readXml( elemTerrainGenerator );
165 qDeleteAll( mRenderers );
168 QDomElement elemRenderers = elem.firstChildElement( QStringLiteral(
"renderers" ) );
169 QDomElement elemRenderer = elemRenderers.firstChildElement( QStringLiteral(
"renderer" ) );
170 while ( !elemRenderer.isNull() )
173 QString type = elemRenderer.attribute( QStringLiteral(
"type" ) );
174 if ( type == QLatin1String(
"vector" ) )
178 else if ( type == QLatin1String(
"mesh" ) )
185 renderer->
readXml( elemRenderer, context );
186 mRenderers.append( renderer );
188 elemRenderer = elemRenderer.nextSiblingElement( QStringLiteral(
"renderer" ) );
191 QDomElement elemSkybox = elem.firstChildElement( QStringLiteral(
"skybox" ) );
192 mSkyboxEnabled = elemSkybox.attribute( QStringLiteral(
"enabled" ), QStringLiteral(
"0" ) ).toInt();
193 mSkyboxFileBase = elemSkybox.attribute( QStringLiteral(
"file-base" ) );
194 mSkyboxFileExtension = elemSkybox.attribute( QStringLiteral(
"file-ext" ) );
196 QDomElement elemDebug = elem.firstChildElement( QStringLiteral(
"debug" ) );
197 mShowTerrainBoundingBoxes = elemDebug.attribute( QStringLiteral(
"bounding-boxes" ), QStringLiteral(
"0" ) ).toInt();
198 mShowTerrainTileInfo = elemDebug.attribute( QStringLiteral(
"terrain-tile-info" ), QStringLiteral(
"0" ) ).toInt();
199 mShowCameraViewCenter = elemDebug.attribute( QStringLiteral(
"camera-view-center" ), QStringLiteral(
"0" ) ).toInt();
204 QDomElement elem = doc.createElement( QStringLiteral(
"qgis3d" ) );
206 QDomElement elemOrigin = doc.createElement( QStringLiteral(
"origin" ) );
207 elemOrigin.setAttribute( QStringLiteral(
"x" ), QString::number( mOrigin.
x() ) );
208 elemOrigin.setAttribute( QStringLiteral(
"y" ), QString::number( mOrigin.
y() ) );
209 elemOrigin.setAttribute( QStringLiteral(
"z" ), QString::number( mOrigin.
z() ) );
210 elem.appendChild( elemOrigin );
212 QDomElement elemCamera = doc.createElement( QStringLiteral(
"camera" ) );
213 elemCamera.setAttribute( QStringLiteral(
"field-of-view" ), mFieldOfView );
214 elem.appendChild( elemCamera );
216 QDomElement elemColor = doc.createElement( QStringLiteral(
"color" ) );
219 elem.appendChild( elemColor );
221 QDomElement elemCrs = doc.createElement( QStringLiteral(
"crs" ) );
223 elem.appendChild( elemCrs );
225 QDomElement elemTerrain = doc.createElement( QStringLiteral(
"terrain" ) );
226 elemTerrain.setAttribute( QStringLiteral(
"exaggeration" ), QString::number( mTerrainVerticalScale ) );
227 elemTerrain.setAttribute( QStringLiteral(
"texture-size" ), mMapTileResolution );
228 elemTerrain.setAttribute( QStringLiteral(
"max-terrain-error" ), QString::number( mMaxTerrainScreenError ) );
229 elemTerrain.setAttribute( QStringLiteral(
"max-ground-error" ), QString::number( mMaxTerrainGroundError ) );
230 elemTerrain.setAttribute( QStringLiteral(
"shading-enabled" ), mTerrainShadingEnabled ? 1 : 0 );
231 QDomElement elemTerrainShadingMaterial = doc.createElement( QStringLiteral(
"shading-material" ) );
232 mTerrainShadingMaterial.
writeXml( elemTerrainShadingMaterial );
233 elemTerrain.appendChild( elemTerrainShadingMaterial );
234 elemTerrain.setAttribute( QStringLiteral(
"map-theme" ), mTerrainMapTheme );
235 elemTerrain.setAttribute( QStringLiteral(
"show-labels" ), mShowLabels ? 1 : 0 );
237 QDomElement elemPointLights = doc.createElement( QStringLiteral(
"point-lights" ) );
240 QDomElement elemPointLight = pointLight.writeXml( doc );
241 elemPointLights.appendChild( elemPointLight );
243 elem.appendChild( elemPointLights );
245 QDomElement elemMapLayers = doc.createElement( QStringLiteral(
"layers" ) );
248 QDomElement elemMapLayer = doc.createElement( QStringLiteral(
"layer" ) );
249 elemMapLayer.setAttribute( QStringLiteral(
"id" ), layerRef.
layerId );
250 elemMapLayers.appendChild( elemMapLayer );
252 elemTerrain.appendChild( elemMapLayers );
253 QDomElement elemTerrainGenerator = doc.createElement( QStringLiteral(
"generator" ) );
255 mTerrainGenerator->writeXml( elemTerrainGenerator );
256 elemTerrain.appendChild( elemTerrainGenerator );
257 elem.appendChild( elemTerrain );
259 QDomElement elemRenderers = doc.createElement( QStringLiteral(
"renderers" ) );
262 QDomElement elemRenderer = doc.createElement( QStringLiteral(
"renderer" ) );
263 elemRenderer.setAttribute( QStringLiteral(
"type" ), renderer->
type() );
264 renderer->
writeXml( elemRenderer, context );
265 elemRenderers.appendChild( elemRenderer );
267 elem.appendChild( elemRenderers );
269 QDomElement elemSkybox = doc.createElement( QStringLiteral(
"skybox" ) );
270 elemSkybox.setAttribute( QStringLiteral(
"enabled" ), mSkyboxEnabled ? 1 : 0 );
272 elemSkybox.setAttribute( QStringLiteral(
"file-base" ), mSkyboxFileBase );
273 elemSkybox.setAttribute( QStringLiteral(
"file-ext" ), mSkyboxFileExtension );
274 elem.appendChild( elemSkybox );
276 QDomElement elemDebug = doc.createElement( QStringLiteral(
"debug" ) );
277 elemDebug.setAttribute( QStringLiteral(
"bounding-boxes" ), mShowTerrainBoundingBoxes ? 1 : 0 );
278 elemDebug.setAttribute( QStringLiteral(
"terrain-tile-info" ), mShowTerrainTileInfo ? 1 : 0 );
279 elemDebug.setAttribute( QStringLiteral(
"camera-view-center" ), mShowCameraViewCenter ? 1 : 0 );
280 elem.appendChild( elemDebug );
287 for (
int i = 0; i < mLayers.count(); ++i )
293 mTerrainGenerator->resolveReferences( project );
295 for (
int i = 0; i < mRenderers.count(); ++i )
319 return mTransformContext;
324 mTransformContext = context;
329 if ( color == mBackgroundColor )
332 mBackgroundColor = color;
338 return mBackgroundColor;
343 if ( color == mSelectionColor )
346 mSelectionColor = color;
352 return mSelectionColor;
357 if ( zScale == mTerrainVerticalScale )
360 mTerrainVerticalScale = zScale;
366 return mTerrainVerticalScale;
371 QList<QgsMapLayerRef> lst;
372 lst.reserve( layers.count() );
378 if ( mLayers == lst )
387 QList<QgsMapLayer *> lst;
388 lst.reserve( mLayers.count() );
391 if ( layerRef.
layer )
392 lst.append( layerRef.
layer );
399 if ( mMapTileResolution == res )
402 mMapTileResolution = res;
408 return mMapTileResolution;
413 if ( mMaxTerrainScreenError == error )
416 mMaxTerrainScreenError = error;
422 return mMaxTerrainScreenError;
427 if ( mMaxTerrainGroundError == error )
430 mMaxTerrainGroundError = error;
436 return mMaxTerrainGroundError;
441 mTerrainGenerator.reset( gen );
447 if ( mTerrainShadingEnabled == enabled )
450 mTerrainShadingEnabled = enabled;
456 if ( mTerrainShadingMaterial == material )
459 mTerrainShadingMaterial = material;
465 if ( mTerrainMapTheme == theme )
468 mTerrainMapTheme = theme;
474 qDeleteAll( mRenderers );
483 if ( mShowTerrainBoundingBoxes == enabled )
486 mShowTerrainBoundingBoxes = enabled;
492 if ( mShowTerrainTileInfo == enabled )
495 mShowTerrainTileInfo = enabled;
501 if ( mShowCameraViewCenter == enabled )
504 mShowCameraViewCenter = enabled;
510 if ( mShowLabels == enabled )
513 mShowLabels = enabled;
519 if ( mPointLights == pointLights )
528 if ( mFieldOfView == fieldOfView )
float maxTerrainScreenError() const
Returns maximum allowed screen error of terrain tiles in pixels.
void setFieldOfView(const float fieldOfView)
Sets the camera lens' field of view.
QList< QgsMapLayer * > layers() const
Returns the list of map layers to be rendered as a texture of the terrain.
The class is used as a container of context for various read/write operations on other objects...
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a DOM element, to be used later with readXml()
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets coordinate reference system used in the 3D scene.
3 Terrain generator that creates a simple square flat area.
void renderersChanged()
Emitted when the list of map's extra renderers have been modified.
Base class for all map layer types.
void maxTerrainGroundErrorChanged()
Emitted when the maximum terrain ground error has changed.
void setTerrainShadingMaterial(const QgsPhongMaterialSettings &material)
Sets terrain shading material.
void setShowCameraViewCenter(bool enabled)
Sets whether to show camera's view center as a sphere (for debugging)
virtual void resolveReferences(const QgsProject &project)
Resolves references to other objects - second phase of loading - after readXml()
void setTerrainGenerator(QgsTerrainGenerator *gen)
Sets terrain generator.
QColor selectionColor() const
Returns color used for selected features.
_LayerRef< QgsMapLayer > QgsMapLayerRef
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
Base class for all renderers that may to participate in 3D view.
void layersChanged()
Emitted when the list of map layers for terrain texture has changed.
void setShowTerrainTilesInfo(bool enabled)
Sets whether to display extra tile info on top of terrain tiles (for debugging)
void setPointLights(const QList< QgsPointLightSettings > &pointLights)
Sets list of point lights defined in the scene.
3D renderer that renders all mesh triangles of a mesh layer.
3 Implementation of terrain generator that uses online resources to download heightmaps.
void setTerrainShadingEnabled(bool enabled)
Sets whether terrain shading is enabled.
QList< QgsPointLightSettings > pointLights() const
Returns list of point lights defined in the scene.
QgsVector3D worldToMapCoordinates(const QgsVector3D &worldCoords) const
Converts 3D world coordinates to map coordinates (applies offset and turns (x,y,z) into (x...
void terrainVerticalScaleChanged()
Emitted when the vertical scale of the terrain has changed.
3 Basic shading material used for rendering based on the Phong shading model with three color compone...
double y() const
Returns Y coordinate.
3 Definition of the world
static QString encodeColor(const QColor &color)
3 Definition of a point light in a 3D map scene
void pointLightsChanged()
Emitted when the list of point lights changes.
float maxTerrainGroundError() const
Returns maximum ground error of terrain tiles in world units.
virtual void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const =0
Writes renderer's properties to given XML element.
double z() const
Returns Z coordinate.
static QgsVector3D worldToMapCoordinates(const QgsVector3D &worldCoords, const QgsVector3D &origin)
Converts 3D world coordinates to map coordinates (applies offset and turns (x,y,z) into (x...
QString layerId
Original layer ID.
void setRenderers(const QList< QgsAbstract3DRenderer *> &renderers)
Sets list of extra 3D renderers to use in the scene. Takes ownership of the objects.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
virtual QString type() const =0
Returns unique identifier of the renderer class (used to identify subclass)
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets CRS of the terrain.
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets CRS of the terrain.
QPointer< TYPE > layer
Weak pointer to map layer.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void terrainMapThemeChanged()
Emitted when terrain's map theme has changed.
void fieldOfViewChanged()
Emitted when the camera lens field of view changes.
void readXml(const QDomElement &elem)
Reads settings from a DOM element.
void showCameraViewCenterChanged()
Emitted when the flag whether camera's view center is shown has changed.
int mapTileResolution() const
Returns resolution (in pixels) of the texture of a terrain tile.
void selectionColorChanged()
Emitted when the selection color has changed.
static QgsVector3D mapToWorldCoordinates(const QgsVector3D &mapCoords, const QgsVector3D &origin)
Converts map coordinates to 3D world coordinates (applies offset and turns (x,y,z) into (x...
void terrainGeneratorChanged()
Emitted when the terrain generator has changed.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
QColor backgroundColor() const
Returns background color of the 3D map view.
void showTerrainBoundingBoxesChanged()
Emitted when the flag whether terrain's bounding boxes are shown has changed.
Contains information about the context in which a coordinate transform is executed.
static QString typeToString(Type type)
Converts terrain generator type enumeration into a string.
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
void setMaxTerrainGroundError(float error)
Returns maximum ground error of terrain tiles in world units.
void mapTileResolutionChanged()
Emitted when the map tile resoulution has changed.
void setTerrainVerticalScale(double zScale)
Sets vertical scale (exaggeration) of terrain (1 = true scale, > 1 = hills get more pronounced) ...
QgsVector3D mapToWorldCoordinates(const QgsVector3D &mapCoords) const
Converts map coordinates to 3D world coordinates (applies offset and turns (x,y,z) into (x...
3D renderer that renders all features of a vector layer with the same 3D symbol.
void showTerrainTilesInfoChanged()
Emitted when the flag whether terrain's tile info is shown has changed.
double terrainVerticalScale() const
Returns vertical scale (exaggeration) of terrain.
void writeXml(QDomElement &elem) const
Writes settings to a DOM element.
void setMapTileResolution(int res)
Sets resolution (in pixels) of the texture of a terrain tile.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads renderer's properties from given XML element.
Qgs3DMapSettings()=default
Constructor for Qgs3DMapSettings.
void setPosition(const QgsVector3D &pos)
Sets position of the light (in 3D world coordinates)
void showLabelsChanged()
Emitted when the flag whether labels are displayed on terrain tiles has changed.
void maxTerrainScreenErrorChanged()
Emitted when the maximum terrain screen error has changed.
3 Base class for generators of terrain.
QList< QgsAbstract3DRenderer * > renderers() const
Returns list of extra 3D renderers.
void setBackgroundColor(const QColor &color)
Sets background color of the 3D map view.
3 Implementation of terrain generator that uses a raster layer with DEM to build terrain.
This class represents a coordinate reference system (CRS).
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets CRS of the terrain.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
~Qgs3DMapSettings() override
void setShowLabels(bool enabled)
Sets whether to display labels on terrain tiles.
float fieldOfView() const
Returns the camera lens' field of view.
virtual QgsAbstract3DRenderer * clone() const =0
Returns a cloned instance.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from a DOM element previously written by writeXml()
void backgroundColorChanged()
Emitted when the background color has changed.
void setLayers(const QList< QgsMapLayer *> &layers)
Sets the list of map layers to be rendered as a texture of the terrain.
void setMaxTerrainScreenError(float error)
Sets maximum allowed screen error of terrain tiles in pixels.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
void resolveReferences(const QgsProject &project)
Resolves references to other objects (map layers) after the call to readXml()
void setShowTerrainBoundingBoxes(bool enabled)
Sets whether to display bounding boxes of terrain tiles (for debugging)
void setSelectionColor(const QColor &color)
Sets color used for selected features.
void setTerrainMapTheme(const QString &theme)
Sets name of the map theme.
void terrainShadingChanged()
Emitted when terrain shading enabled flag or terrain shading material has changed.
double x() const
Returns X coordinate.
static QColor decodeColor(const QString &str)
void readXml(const QDomElement &elem)
Reads configuration from a DOM element previously written using writeXml()