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 , mFieldOfView( other.mFieldOfView )
50 , mLayers( other.mLayers )
51 , mSkyboxEnabled( other.mSkyboxEnabled )
52 , mSkyboxFileBase( other.mSkyboxFileBase )
53 , mSkyboxFileExtension( other.mSkyboxFileExtension )
54 , mTransformContext( other.mTransformContext )
55 , mPathResolver( other.mPathResolver )
56 , mMapThemes( other.mMapThemes )
60 mRenderers << renderer->
clone();
66 qDeleteAll( mRenderers );
71 QDomElement elemOrigin = elem.firstChildElement( QStringLiteral(
"origin" ) );
73 elemOrigin.attribute( QStringLiteral(
"x" ) ).toDouble(),
74 elemOrigin.attribute( QStringLiteral(
"y" ) ).toDouble(),
75 elemOrigin.attribute( QStringLiteral(
"z" ) ).toDouble() );
77 QDomElement elemCamera = elem.firstChildElement( QStringLiteral(
"camera" ) );
78 if ( !elemCamera.isNull() )
80 mFieldOfView = elemCamera.attribute( QStringLiteral(
"field-of-view" ), QStringLiteral(
"45" ) ).toFloat();
83 QDomElement elemColor = elem.firstChildElement( QStringLiteral(
"color" ) );
84 if ( !elemColor.isNull() )
90 QDomElement elemCrs = elem.firstChildElement( QStringLiteral(
"crs" ) );
93 QDomElement elemTerrain = elem.firstChildElement( QStringLiteral(
"terrain" ) );
94 mTerrainVerticalScale = elemTerrain.attribute( QStringLiteral(
"exaggeration" ), QStringLiteral(
"1" ) ).toFloat();
95 mMapTileResolution = elemTerrain.attribute( QStringLiteral(
"texture-size" ), QStringLiteral(
"512" ) ).toInt();
96 mMaxTerrainScreenError = elemTerrain.attribute( QStringLiteral(
"max-terrain-error" ), QStringLiteral(
"3" ) ).toFloat();
97 mMaxTerrainGroundError = elemTerrain.attribute( QStringLiteral(
"max-ground-error" ), QStringLiteral(
"1" ) ).toFloat();
98 mTerrainShadingEnabled = elemTerrain.attribute( QStringLiteral(
"shading-enabled" ), QStringLiteral(
"0" ) ).toInt();
99 QDomElement elemTerrainShadingMaterial = elemTerrain.firstChildElement( QStringLiteral(
"shading-material" ) );
100 if ( !elemTerrainShadingMaterial.isNull() )
101 mTerrainShadingMaterial.
readXml( elemTerrainShadingMaterial );
102 mTerrainMapTheme = elemTerrain.attribute( QStringLiteral(
"map-theme" ) );
103 mShowLabels = elemTerrain.attribute( QStringLiteral(
"show-labels" ), QStringLiteral(
"0" ) ).toInt();
105 mPointLights.clear();
106 QDomElement elemPointLights = elem.firstChildElement( QStringLiteral(
"point-lights" ) );
107 if ( !elemPointLights.isNull() )
109 QDomElement elemPointLight = elemPointLights.firstChildElement( QStringLiteral(
"point-light" ) );
110 while ( !elemPointLight.isNull() )
113 pointLight.
readXml( elemPointLight );
114 mPointLights << pointLight;
115 elemPointLight = elemPointLight.nextSiblingElement( QStringLiteral(
"point-light" ) );
123 mPointLights << defaultLight;
126 QDomElement elemMapLayers = elemTerrain.firstChildElement( QStringLiteral(
"layers" ) );
127 QDomElement elemMapLayer = elemMapLayers.firstChildElement( QStringLiteral(
"layer" ) );
128 QList<QgsMapLayerRef> mapLayers;
129 while ( !elemMapLayer.isNull() )
131 mapLayers <<
QgsMapLayerRef( elemMapLayer.attribute( QStringLiteral(
"id" ) ) );
132 elemMapLayer = elemMapLayer.nextSiblingElement( QStringLiteral(
"layer" ) );
136 QDomElement elemTerrainGenerator = elemTerrain.firstChildElement( QStringLiteral(
"generator" ) );
137 QString terrainGenType = elemTerrainGenerator.attribute( QStringLiteral(
"type" ) );
138 if ( terrainGenType == QLatin1String(
"dem" ) )
141 demTerrainGenerator->
setCrs( mCrs, mTransformContext );
142 mTerrainGenerator.reset( demTerrainGenerator );
144 else if ( terrainGenType == QLatin1String(
"online" ) )
147 onlineTerrainGenerator->
setCrs( mCrs, mTransformContext );
148 mTerrainGenerator.reset( onlineTerrainGenerator );
154 mTerrainGenerator.reset( flatGen );
156 mTerrainGenerator->readXml( elemTerrainGenerator );
158 qDeleteAll( mRenderers );
161 QDomElement elemRenderers = elem.firstChildElement( QStringLiteral(
"renderers" ) );
162 QDomElement elemRenderer = elemRenderers.firstChildElement( QStringLiteral(
"renderer" ) );
163 while ( !elemRenderer.isNull() )
166 QString type = elemRenderer.attribute( QStringLiteral(
"type" ) );
167 if ( type == QLatin1String(
"vector" ) )
171 else if ( type == QLatin1String(
"mesh" ) )
178 renderer->
readXml( elemRenderer, context );
179 mRenderers.append( renderer );
181 elemRenderer = elemRenderer.nextSiblingElement( QStringLiteral(
"renderer" ) );
184 QDomElement elemSkybox = elem.firstChildElement( QStringLiteral(
"skybox" ) );
185 mSkyboxEnabled = elemSkybox.attribute( QStringLiteral(
"enabled" ), QStringLiteral(
"0" ) ).toInt();
186 mSkyboxFileBase = elemSkybox.attribute( QStringLiteral(
"file-base" ) );
187 mSkyboxFileExtension = elemSkybox.attribute( QStringLiteral(
"file-ext" ) );
189 QDomElement elemDebug = elem.firstChildElement( QStringLiteral(
"debug" ) );
190 mShowTerrainBoundingBoxes = elemDebug.attribute( QStringLiteral(
"bounding-boxes" ), QStringLiteral(
"0" ) ).toInt();
191 mShowTerrainTileInfo = elemDebug.attribute( QStringLiteral(
"terrain-tile-info" ), QStringLiteral(
"0" ) ).toInt();
192 mShowCameraViewCenter = elemDebug.attribute( QStringLiteral(
"camera-view-center" ), QStringLiteral(
"0" ) ).toInt();
197 QDomElement elem = doc.createElement( QStringLiteral(
"qgis3d" ) );
199 QDomElement elemOrigin = doc.createElement( QStringLiteral(
"origin" ) );
200 elemOrigin.setAttribute( QStringLiteral(
"x" ), QString::number( mOrigin.
x() ) );
201 elemOrigin.setAttribute( QStringLiteral(
"y" ), QString::number( mOrigin.
y() ) );
202 elemOrigin.setAttribute( QStringLiteral(
"z" ), QString::number( mOrigin.
z() ) );
203 elem.appendChild( elemOrigin );
205 QDomElement elemCamera = doc.createElement( QStringLiteral(
"camera" ) );
206 elemCamera.setAttribute( QStringLiteral(
"field-of-view" ), mFieldOfView );
207 elem.appendChild( elemCamera );
209 QDomElement elemColor = doc.createElement( QStringLiteral(
"color" ) );
212 elem.appendChild( elemColor );
214 QDomElement elemCrs = doc.createElement( QStringLiteral(
"crs" ) );
216 elem.appendChild( elemCrs );
218 QDomElement elemTerrain = doc.createElement( QStringLiteral(
"terrain" ) );
219 elemTerrain.setAttribute( QStringLiteral(
"exaggeration" ), QString::number( mTerrainVerticalScale ) );
220 elemTerrain.setAttribute( QStringLiteral(
"texture-size" ), mMapTileResolution );
221 elemTerrain.setAttribute( QStringLiteral(
"max-terrain-error" ), QString::number( mMaxTerrainScreenError ) );
222 elemTerrain.setAttribute( QStringLiteral(
"max-ground-error" ), QString::number( mMaxTerrainGroundError ) );
223 elemTerrain.setAttribute( QStringLiteral(
"shading-enabled" ), mTerrainShadingEnabled ? 1 : 0 );
224 QDomElement elemTerrainShadingMaterial = doc.createElement( QStringLiteral(
"shading-material" ) );
225 mTerrainShadingMaterial.
writeXml( elemTerrainShadingMaterial );
226 elemTerrain.appendChild( elemTerrainShadingMaterial );
227 elemTerrain.setAttribute( QStringLiteral(
"map-theme" ), mTerrainMapTheme );
228 elemTerrain.setAttribute( QStringLiteral(
"show-labels" ), mShowLabels ? 1 : 0 );
230 QDomElement elemPointLights = doc.createElement( QStringLiteral(
"point-lights" ) );
233 QDomElement elemPointLight = pointLight.writeXml( doc );
234 elemPointLights.appendChild( elemPointLight );
236 elem.appendChild( elemPointLights );
238 QDomElement elemMapLayers = doc.createElement( QStringLiteral(
"layers" ) );
241 QDomElement elemMapLayer = doc.createElement( QStringLiteral(
"layer" ) );
242 elemMapLayer.setAttribute( QStringLiteral(
"id" ), layerRef.
layerId );
243 elemMapLayers.appendChild( elemMapLayer );
245 elemTerrain.appendChild( elemMapLayers );
246 QDomElement elemTerrainGenerator = doc.createElement( QStringLiteral(
"generator" ) );
248 mTerrainGenerator->writeXml( elemTerrainGenerator );
249 elemTerrain.appendChild( elemTerrainGenerator );
250 elem.appendChild( elemTerrain );
252 QDomElement elemRenderers = doc.createElement( QStringLiteral(
"renderers" ) );
255 QDomElement elemRenderer = doc.createElement( QStringLiteral(
"renderer" ) );
256 elemRenderer.setAttribute( QStringLiteral(
"type" ), renderer->
type() );
257 renderer->
writeXml( elemRenderer, context );
258 elemRenderers.appendChild( elemRenderer );
260 elem.appendChild( elemRenderers );
262 QDomElement elemSkybox = doc.createElement( QStringLiteral(
"skybox" ) );
263 elemSkybox.setAttribute( QStringLiteral(
"enabled" ), mSkyboxEnabled ? 1 : 0 );
265 elemSkybox.setAttribute( QStringLiteral(
"file-base" ), mSkyboxFileBase );
266 elemSkybox.setAttribute( QStringLiteral(
"file-ext" ), mSkyboxFileExtension );
267 elem.appendChild( elemSkybox );
269 QDomElement elemDebug = doc.createElement( QStringLiteral(
"debug" ) );
270 elemDebug.setAttribute( QStringLiteral(
"bounding-boxes" ), mShowTerrainBoundingBoxes ? 1 : 0 );
271 elemDebug.setAttribute( QStringLiteral(
"terrain-tile-info" ), mShowTerrainTileInfo ? 1 : 0 );
272 elemDebug.setAttribute( QStringLiteral(
"camera-view-center" ), mShowCameraViewCenter ? 1 : 0 );
273 elem.appendChild( elemDebug );
280 for (
int i = 0; i < mLayers.count(); ++i )
286 mTerrainGenerator->resolveReferences( project );
288 for (
int i = 0; i < mRenderers.count(); ++i )
312 return mTransformContext;
317 mTransformContext = context;
322 if ( color == mBackgroundColor )
325 mBackgroundColor = color;
331 return mBackgroundColor;
336 if ( color == mSelectionColor )
339 mSelectionColor = color;
345 return mSelectionColor;
350 if ( zScale == mTerrainVerticalScale )
353 mTerrainVerticalScale = zScale;
359 return mTerrainVerticalScale;
364 QList<QgsMapLayerRef> lst;
365 lst.reserve( layers.count() );
371 if ( mLayers == lst )
380 QList<QgsMapLayer *> lst;
381 lst.reserve( mLayers.count() );
384 if ( layerRef.
layer )
385 lst.append( layerRef.
layer );
392 if ( mMapTileResolution == res )
395 mMapTileResolution = res;
401 return mMapTileResolution;
406 if ( mMaxTerrainScreenError == error )
409 mMaxTerrainScreenError = error;
415 return mMaxTerrainScreenError;
420 if ( mMaxTerrainGroundError == error )
423 mMaxTerrainGroundError = error;
429 return mMaxTerrainGroundError;
434 mTerrainGenerator.reset( gen );
440 if ( mTerrainShadingEnabled == enabled )
443 mTerrainShadingEnabled = enabled;
449 if ( mTerrainShadingMaterial == material )
452 mTerrainShadingMaterial = material;
458 if ( mTerrainMapTheme == theme )
461 mTerrainMapTheme = theme;
472 if ( mShowTerrainBoundingBoxes == enabled )
475 mShowTerrainBoundingBoxes = enabled;
481 if ( mShowTerrainTileInfo == enabled )
484 mShowTerrainTileInfo = enabled;
490 if ( mShowCameraViewCenter == enabled )
493 mShowCameraViewCenter = enabled;
499 if ( mShowLabels == enabled )
502 mShowLabels = enabled;
508 if ( mPointLights == pointLights )
517 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.
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 camer 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.
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.
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()