25 #include <QDomDocument> 26 #include <QDomElement> 33 , mOrigin( other.mOrigin )
35 , mBackgroundColor( other.mBackgroundColor )
36 , mTerrainVerticalScale( other.mTerrainVerticalScale )
37 , mTerrainGenerator( other.mTerrainGenerator ? other.mTerrainGenerator->clone() : nullptr )
38 , mMapTileResolution( other.mMapTileResolution )
39 , mMaxTerrainScreenError( other.mMaxTerrainScreenError )
40 , mMaxTerrainGroundError( other.mMaxTerrainGroundError )
41 , mTerrainShadingEnabled( other.mTerrainShadingEnabled )
42 , mTerrainShadingMaterial( other.mTerrainShadingMaterial )
43 , mTerrainMapTheme( other.mTerrainMapTheme )
44 , mShowTerrainBoundingBoxes( other.mShowTerrainBoundingBoxes )
45 , mShowTerrainTileInfo( other.mShowTerrainTileInfo )
46 , mShowCameraViewCenter( other.mShowCameraViewCenter )
47 , mShowLabels( other.mShowLabels )
48 , mPointLights( other.mPointLights )
49 , mLayers( other.mLayers )
50 , mSkyboxEnabled( other.mSkyboxEnabled )
51 , mSkyboxFileBase( other.mSkyboxFileBase )
52 , mSkyboxFileExtension( other.mSkyboxFileExtension )
53 , mTransformContext( other.mTransformContext )
54 , mPathResolver( other.mPathResolver )
55 , mMapThemes( other.mMapThemes )
59 mRenderers << renderer->
clone();
65 qDeleteAll( mRenderers );
70 QDomElement elemOrigin = elem.firstChildElement( QStringLiteral(
"origin" ) );
72 elemOrigin.attribute( QStringLiteral(
"x" ) ).toDouble(),
73 elemOrigin.attribute( QStringLiteral(
"y" ) ).toDouble(),
74 elemOrigin.attribute( QStringLiteral(
"z" ) ).toDouble() );
76 QDomElement elemColor = elem.firstChildElement( QStringLiteral(
"color" ) );
77 if ( !elemColor.isNull() )
83 QDomElement elemCrs = elem.firstChildElement( QStringLiteral(
"crs" ) );
86 QDomElement elemTerrain = elem.firstChildElement( QStringLiteral(
"terrain" ) );
87 mTerrainVerticalScale = elemTerrain.attribute( QStringLiteral(
"exaggeration" ), QStringLiteral(
"1" ) ).toFloat();
88 mMapTileResolution = elemTerrain.attribute( QStringLiteral(
"texture-size" ), QStringLiteral(
"512" ) ).toInt();
89 mMaxTerrainScreenError = elemTerrain.attribute( QStringLiteral(
"max-terrain-error" ), QStringLiteral(
"3" ) ).toFloat();
90 mMaxTerrainGroundError = elemTerrain.attribute( QStringLiteral(
"max-ground-error" ), QStringLiteral(
"1" ) ).toFloat();
91 mTerrainShadingEnabled = elemTerrain.attribute( QStringLiteral(
"shading-enabled" ), QStringLiteral(
"0" ) ).toInt();
92 QDomElement elemTerrainShadingMaterial = elemTerrain.firstChildElement( QStringLiteral(
"shading-material" ) );
93 if ( !elemTerrainShadingMaterial.isNull() )
94 mTerrainShadingMaterial.
readXml( elemTerrainShadingMaterial );
95 mTerrainMapTheme = elemTerrain.attribute( QStringLiteral(
"map-theme" ) );
96 mShowLabels = elemTerrain.attribute( QStringLiteral(
"show-labels" ), QStringLiteral(
"0" ) ).toInt();
99 QDomElement elemPointLights = elem.firstChildElement( QStringLiteral(
"point-lights" ) );
100 if ( !elemPointLights.isNull() )
102 QDomElement elemPointLight = elemPointLights.firstChildElement( QStringLiteral(
"point-light" ) );
103 while ( !elemPointLight.isNull() )
106 pointLight.
readXml( elemPointLight );
107 mPointLights << pointLight;
108 elemPointLight = elemPointLight.nextSiblingElement( QStringLiteral(
"point-light" ) );
116 mPointLights << defaultLight;
119 QDomElement elemMapLayers = elemTerrain.firstChildElement( QStringLiteral(
"layers" ) );
120 QDomElement elemMapLayer = elemMapLayers.firstChildElement( QStringLiteral(
"layer" ) );
121 QList<QgsMapLayerRef> mapLayers;
122 while ( !elemMapLayer.isNull() )
124 mapLayers <<
QgsMapLayerRef( elemMapLayer.attribute( QStringLiteral(
"id" ) ) );
125 elemMapLayer = elemMapLayer.nextSiblingElement( QStringLiteral(
"layer" ) );
129 QDomElement elemTerrainGenerator = elemTerrain.firstChildElement( QStringLiteral(
"generator" ) );
130 QString terrainGenType = elemTerrainGenerator.attribute( QStringLiteral(
"type" ) );
131 if ( terrainGenType == QLatin1String(
"dem" ) )
134 demTerrainGenerator->
setCrs( mCrs, mTransformContext );
135 mTerrainGenerator.reset( demTerrainGenerator );
137 else if ( terrainGenType == QLatin1String(
"quantized-mesh" ) )
148 mTerrainGenerator.reset( flatGen );
150 mTerrainGenerator->readXml( elemTerrainGenerator );
152 qDeleteAll( mRenderers );
155 QDomElement elemRenderers = elem.firstChildElement( QStringLiteral(
"renderers" ) );
156 QDomElement elemRenderer = elemRenderers.firstChildElement( QStringLiteral(
"renderer" ) );
157 while ( !elemRenderer.isNull() )
160 QString type = elemRenderer.attribute( QStringLiteral(
"type" ) );
161 if ( type == QLatin1String(
"vector" ) )
165 else if ( type == QLatin1String(
"mesh" ) )
172 renderer->
readXml( elemRenderer, context );
173 mRenderers.append( renderer );
175 elemRenderer = elemRenderer.nextSiblingElement( QStringLiteral(
"renderer" ) );
178 QDomElement elemSkybox = elem.firstChildElement( QStringLiteral(
"skybox" ) );
179 mSkyboxEnabled = elemSkybox.attribute( QStringLiteral(
"enabled" ), QStringLiteral(
"0" ) ).toInt();
180 mSkyboxFileBase = elemSkybox.attribute( QStringLiteral(
"file-base" ) );
181 mSkyboxFileExtension = elemSkybox.attribute( QStringLiteral(
"file-ext" ) );
183 QDomElement elemDebug = elem.firstChildElement( QStringLiteral(
"debug" ) );
184 mShowTerrainBoundingBoxes = elemDebug.attribute( QStringLiteral(
"bounding-boxes" ), QStringLiteral(
"0" ) ).toInt();
185 mShowTerrainTileInfo = elemDebug.attribute( QStringLiteral(
"terrain-tile-info" ), QStringLiteral(
"0" ) ).toInt();
186 mShowCameraViewCenter = elemDebug.attribute( QStringLiteral(
"camera-view-center" ), QStringLiteral(
"0" ) ).toInt();
191 QDomElement elem = doc.createElement( QStringLiteral(
"qgis3d" ) );
193 QDomElement elemOrigin = doc.createElement( QStringLiteral(
"origin" ) );
194 elemOrigin.setAttribute( QStringLiteral(
"x" ), QString::number( mOrigin.
x() ) );
195 elemOrigin.setAttribute( QStringLiteral(
"y" ), QString::number( mOrigin.
y() ) );
196 elemOrigin.setAttribute( QStringLiteral(
"z" ), QString::number( mOrigin.
z() ) );
197 elem.appendChild( elemOrigin );
199 QDomElement elemColor = doc.createElement( QStringLiteral(
"color" ) );
202 elem.appendChild( elemColor );
204 QDomElement elemCrs = doc.createElement( QStringLiteral(
"crs" ) );
206 elem.appendChild( elemCrs );
208 QDomElement elemTerrain = doc.createElement( QStringLiteral(
"terrain" ) );
209 elemTerrain.setAttribute( QStringLiteral(
"exaggeration" ), QString::number( mTerrainVerticalScale ) );
210 elemTerrain.setAttribute( QStringLiteral(
"texture-size" ), mMapTileResolution );
211 elemTerrain.setAttribute( QStringLiteral(
"max-terrain-error" ), QString::number( mMaxTerrainScreenError ) );
212 elemTerrain.setAttribute( QStringLiteral(
"max-ground-error" ), QString::number( mMaxTerrainGroundError ) );
213 elemTerrain.setAttribute( QStringLiteral(
"shading-enabled" ), mTerrainShadingEnabled ? 1 : 0 );
214 QDomElement elemTerrainShadingMaterial = doc.createElement( QStringLiteral(
"shading-material" ) );
215 mTerrainShadingMaterial.
writeXml( elemTerrainShadingMaterial );
216 elemTerrain.appendChild( elemTerrainShadingMaterial );
217 elemTerrain.setAttribute( QStringLiteral(
"map-theme" ), mTerrainMapTheme );
218 elemTerrain.setAttribute( QStringLiteral(
"show-labels" ), mShowLabels ? 1 : 0 );
220 QDomElement elemPointLights = doc.createElement( QStringLiteral(
"point-lights" ) );
223 QDomElement elemPointLight = pointLight.writeXml( doc );
224 elemPointLights.appendChild( elemPointLight );
226 elem.appendChild( elemPointLights );
228 QDomElement elemMapLayers = doc.createElement( QStringLiteral(
"layers" ) );
231 QDomElement elemMapLayer = doc.createElement( QStringLiteral(
"layer" ) );
232 elemMapLayer.setAttribute( QStringLiteral(
"id" ), layerRef.
layerId );
233 elemMapLayers.appendChild( elemMapLayer );
235 elemTerrain.appendChild( elemMapLayers );
236 QDomElement elemTerrainGenerator = doc.createElement( QStringLiteral(
"generator" ) );
238 mTerrainGenerator->writeXml( elemTerrainGenerator );
239 elemTerrain.appendChild( elemTerrainGenerator );
240 elem.appendChild( elemTerrain );
242 QDomElement elemRenderers = doc.createElement( QStringLiteral(
"renderers" ) );
245 QDomElement elemRenderer = doc.createElement( QStringLiteral(
"renderer" ) );
246 elemRenderer.setAttribute( QStringLiteral(
"type" ), renderer->
type() );
247 renderer->
writeXml( elemRenderer, context );
248 elemRenderers.appendChild( elemRenderer );
250 elem.appendChild( elemRenderers );
252 QDomElement elemSkybox = doc.createElement( QStringLiteral(
"skybox" ) );
253 elemSkybox.setAttribute( QStringLiteral(
"enabled" ), mSkyboxEnabled ? 1 : 0 );
255 elemSkybox.setAttribute( QStringLiteral(
"file-base" ), mSkyboxFileBase );
256 elemSkybox.setAttribute( QStringLiteral(
"file-ext" ), mSkyboxFileExtension );
257 elem.appendChild( elemSkybox );
259 QDomElement elemDebug = doc.createElement( QStringLiteral(
"debug" ) );
260 elemDebug.setAttribute( QStringLiteral(
"bounding-boxes" ), mShowTerrainBoundingBoxes ? 1 : 0 );
261 elemDebug.setAttribute( QStringLiteral(
"terrain-tile-info" ), mShowTerrainTileInfo ? 1 : 0 );
262 elemDebug.setAttribute( QStringLiteral(
"camera-view-center" ), mShowCameraViewCenter ? 1 : 0 );
263 elem.appendChild( elemDebug );
270 for (
int i = 0; i < mLayers.count(); ++i )
276 mTerrainGenerator->resolveReferences( project );
278 for (
int i = 0; i < mRenderers.count(); ++i )
302 return mTransformContext;
307 mTransformContext = context;
312 if ( color == mBackgroundColor )
315 mBackgroundColor = color;
321 return mBackgroundColor;
326 if ( color == mSelectionColor )
329 mSelectionColor = color;
335 return mSelectionColor;
340 if ( zScale == mTerrainVerticalScale )
343 mTerrainVerticalScale = zScale;
349 return mTerrainVerticalScale;
354 QList<QgsMapLayerRef> lst;
355 lst.reserve( layers.count() );
361 if ( mLayers == lst )
370 QList<QgsMapLayer *> lst;
371 lst.reserve( mLayers.count() );
374 if ( layerRef.
layer )
375 lst.append( layerRef.
layer );
382 if ( mMapTileResolution == res )
385 mMapTileResolution = res;
391 return mMapTileResolution;
396 if ( mMaxTerrainScreenError == error )
399 mMaxTerrainScreenError = error;
405 return mMaxTerrainScreenError;
410 if ( mMaxTerrainGroundError == error )
413 mMaxTerrainGroundError = error;
419 return mMaxTerrainGroundError;
424 mTerrainGenerator.reset( gen );
430 if ( mTerrainShadingEnabled == enabled )
433 mTerrainShadingEnabled = enabled;
439 if ( mTerrainShadingMaterial == material )
442 mTerrainShadingMaterial = material;
448 if ( mTerrainMapTheme == theme )
451 mTerrainMapTheme = theme;
462 if ( mShowTerrainBoundingBoxes == enabled )
465 mShowTerrainBoundingBoxes = enabled;
471 if ( mShowTerrainTileInfo == enabled )
474 mShowTerrainTileInfo = enabled;
480 if ( mShowCameraViewCenter == enabled )
483 mShowCameraViewCenter = enabled;
489 if ( mShowLabels == enabled )
492 mShowLabels = enabled;
498 if ( mPointLights == pointLights )
float maxTerrainScreenError() const
Returns maximum allowed screen error of terrain tiles in pixels.
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.
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.
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
3 Terrain generator using downloaded terrain tiles using quantized mesh specification ...
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.
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 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.
Reads and writes project states.
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.
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.
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()
QgsTerrainGenerator * terrainGenerator() const
Returns terrain generator. It takes care of producing terrain tiles from the input data...