21 #include "qgsmeshterraingenerator.h"
26 #include <QDomDocument>
27 #include <QDomElement>
35 , mOrigin( other.mOrigin )
37 , mBackgroundColor( other.mBackgroundColor )
38 , mSelectionColor( other.mSelectionColor )
39 , mTerrainVerticalScale( other.mTerrainVerticalScale )
40 , mTerrainGenerator( other.mTerrainGenerator ? other.mTerrainGenerator->clone() : nullptr )
41 , mMapTileResolution( other.mMapTileResolution )
42 , mMaxTerrainScreenError( other.mMaxTerrainScreenError )
43 , mMaxTerrainGroundError( other.mMaxTerrainGroundError )
44 , mTerrainShadingEnabled( other.mTerrainShadingEnabled )
45 , mTerrainShadingMaterial( other.mTerrainShadingMaterial )
46 , mTerrainMapTheme( other.mTerrainMapTheme )
47 , mShowTerrainBoundingBoxes( other.mShowTerrainBoundingBoxes )
48 , mShowTerrainTileInfo( other.mShowTerrainTileInfo )
49 , mShowCameraViewCenter( other.mShowCameraViewCenter )
50 , mShowLightSources( other.mShowLightSources )
51 , mShowLabels( other.mShowLabels )
52 , mPointLights( other.mPointLights )
53 , mDirectionalLights( other.mDirectionalLights )
54 , mFieldOfView( other.mFieldOfView )
55 , mLayers( other.mLayers )
56 , mTerrainLayers( other.mTerrainLayers )
58 , mTransformContext( other.mTransformContext )
59 , mPathResolver( other.mPathResolver )
60 , mMapThemes( other.mMapThemes )
61 , mIsSkyboxEnabled( other.mIsSkyboxEnabled )
67 mRenderers << renderer->
clone();
73 qDeleteAll( mRenderers );
78 QDomElement elemOrigin = elem.firstChildElement( QStringLiteral(
"origin" ) );
80 elemOrigin.attribute( QStringLiteral(
"x" ) ).toDouble(),
81 elemOrigin.attribute( QStringLiteral(
"y" ) ).toDouble(),
82 elemOrigin.attribute( QStringLiteral(
"z" ) ).toDouble() );
84 QDomElement elemCamera = elem.firstChildElement( QStringLiteral(
"camera" ) );
85 if ( !elemCamera.isNull() )
87 mFieldOfView = elemCamera.attribute( QStringLiteral(
"field-of-view" ), QStringLiteral(
"45" ) ).toFloat();
90 QDomElement elemColor = elem.firstChildElement( QStringLiteral(
"color" ) );
91 if ( !elemColor.isNull() )
97 QDomElement elemCrs = elem.firstChildElement( QStringLiteral(
"crs" ) );
100 QDomElement elemTerrain = elem.firstChildElement( QStringLiteral(
"terrain" ) );
101 mTerrainVerticalScale = elemTerrain.attribute( QStringLiteral(
"exaggeration" ), QStringLiteral(
"1" ) ).toFloat();
102 mMapTileResolution = elemTerrain.attribute( QStringLiteral(
"texture-size" ), QStringLiteral(
"512" ) ).toInt();
103 mMaxTerrainScreenError = elemTerrain.attribute( QStringLiteral(
"max-terrain-error" ), QStringLiteral(
"3" ) ).toFloat();
104 mMaxTerrainGroundError = elemTerrain.attribute( QStringLiteral(
"max-ground-error" ), QStringLiteral(
"1" ) ).toFloat();
105 mTerrainShadingEnabled = elemTerrain.attribute( QStringLiteral(
"shading-enabled" ), QStringLiteral(
"0" ) ).toInt();
106 QDomElement elemTerrainShadingMaterial = elemTerrain.firstChildElement( QStringLiteral(
"shading-material" ) );
107 if ( !elemTerrainShadingMaterial.isNull() )
108 mTerrainShadingMaterial.
readXml( elemTerrainShadingMaterial, context );
109 mTerrainMapTheme = elemTerrain.attribute( QStringLiteral(
"map-theme" ) );
110 mShowLabels = elemTerrain.attribute( QStringLiteral(
"show-labels" ), QStringLiteral(
"0" ) ).toInt();
112 mPointLights.clear();
113 QDomElement elemPointLights = elem.firstChildElement( QStringLiteral(
"point-lights" ) );
114 if ( !elemPointLights.isNull() )
116 QDomElement elemPointLight = elemPointLights.firstChildElement( QStringLiteral(
"point-light" ) );
117 while ( !elemPointLight.isNull() )
120 pointLight.
readXml( elemPointLight );
121 mPointLights << pointLight;
122 elemPointLight = elemPointLight.nextSiblingElement( QStringLiteral(
"point-light" ) );
130 mPointLights << defaultLight;
133 mDirectionalLights.clear();
134 QDomElement elemDirectionalLights = elem.firstChildElement( QStringLiteral(
"directional-lights" ) );
135 if ( !elemDirectionalLights.isNull() )
137 QDomElement elemDirectionalLight = elemDirectionalLights.firstChildElement( QStringLiteral(
"directional-light" ) );
138 while ( !elemDirectionalLight.isNull() )
141 directionalLight.
readXml( elemDirectionalLight );
142 mDirectionalLights << directionalLight;
143 elemDirectionalLight = elemDirectionalLight.nextSiblingElement( QStringLiteral(
"directional-light" ) );
147 QDomElement elemMapLayers = elemTerrain.firstChildElement( QStringLiteral(
"layers" ) );
148 QDomElement elemMapLayer = elemMapLayers.firstChildElement( QStringLiteral(
"layer" ) );
149 QList<QgsMapLayerRef> mapLayers;
150 while ( !elemMapLayer.isNull() )
152 mapLayers <<
QgsMapLayerRef( elemMapLayer.attribute( QStringLiteral(
"id" ) ) );
153 elemMapLayer = elemMapLayer.nextSiblingElement( QStringLiteral(
"layer" ) );
157 QDomElement elemTerrainLayers = elemTerrain.firstChildElement( QStringLiteral(
"terrainLayers" ) );
158 if ( elemTerrainLayers.isNull() )
160 mTerrainLayers = mLayers;
164 QDomElement elemTerrainMapLayer = elemTerrainLayers.firstChildElement( QStringLiteral(
"layer" ) );
165 QList<QgsMapLayerRef> terrainMapLayers;
166 while ( !elemTerrainMapLayer.isNull() )
168 terrainMapLayers <<
QgsMapLayerRef( elemTerrainMapLayer.attribute( QStringLiteral(
"id" ) ) );
169 elemTerrainMapLayer = elemTerrainMapLayer.nextSiblingElement( QStringLiteral(
"layer" ) );
171 mTerrainLayers = mapLayers;
174 QDomElement elemTerrainGenerator = elemTerrain.firstChildElement( QStringLiteral(
"generator" ) );
175 QString terrainGenType = elemTerrainGenerator.attribute( QStringLiteral(
"type" ) );
176 if ( terrainGenType == QLatin1String(
"dem" ) )
179 demTerrainGenerator->
setCrs( mCrs, mTransformContext );
180 mTerrainGenerator.reset( demTerrainGenerator );
182 else if ( terrainGenType == QLatin1String(
"online" ) )
185 onlineTerrainGenerator->
setCrs( mCrs, mTransformContext );
186 mTerrainGenerator.reset( onlineTerrainGenerator );
188 else if ( terrainGenType == QLatin1String(
"mesh" ) )
190 QgsMeshTerrainGenerator *meshTerrainGenerator =
new QgsMeshTerrainGenerator;
191 meshTerrainGenerator->setCrs( mCrs, mTransformContext );
192 mTerrainGenerator.reset( meshTerrainGenerator );
198 mTerrainGenerator.reset( flatGen );
200 mTerrainGenerator->readXml( elemTerrainGenerator );
202 qDeleteAll( mRenderers );
205 QDomElement elemRenderers = elem.firstChildElement( QStringLiteral(
"renderers" ) );
206 QDomElement elemRenderer = elemRenderers.firstChildElement( QStringLiteral(
"renderer" ) );
207 while ( !elemRenderer.isNull() )
210 QString type = elemRenderer.attribute( QStringLiteral(
"type" ) );
211 if ( type == QLatin1String(
"vector" ) )
215 else if ( type == QLatin1String(
"mesh" ) )
222 renderer->
readXml( elemRenderer, context );
223 mRenderers.append( renderer );
225 elemRenderer = elemRenderer.nextSiblingElement( QStringLiteral(
"renderer" ) );
228 QDomElement elemSkybox = elem.firstChildElement( QStringLiteral(
"skybox" ) );
229 mIsSkyboxEnabled = elemSkybox.attribute( QStringLiteral(
"skybox-enabled" ) ).toInt();
230 mSkyboxSettings.
readXml( elemSkybox, context );
232 QDomElement elemShadows = elem.firstChildElement( QStringLiteral(
"shadow-rendering" ) );
233 mShadowSettings.
readXml( elemShadows, context );
235 QDomElement elemDebug = elem.firstChildElement( QStringLiteral(
"debug" ) );
236 mShowTerrainBoundingBoxes = elemDebug.attribute( QStringLiteral(
"bounding-boxes" ), QStringLiteral(
"0" ) ).toInt();
237 mShowTerrainTileInfo = elemDebug.attribute( QStringLiteral(
"terrain-tile-info" ), QStringLiteral(
"0" ) ).toInt();
238 mShowCameraViewCenter = elemDebug.attribute( QStringLiteral(
"camera-view-center" ), QStringLiteral(
"0" ) ).toInt();
239 mShowLightSources = elemDebug.attribute( QStringLiteral(
"show-light-sources" ), QStringLiteral(
"0" ) ).toInt();
241 QDomElement elemTemporalRange = elem.firstChildElement( QStringLiteral(
"temporal-range" ) );
242 QDateTime start = QDateTime::fromString( elemTemporalRange.attribute( QStringLiteral(
"start" ) ), Qt::ISODate );
243 QDateTime end = QDateTime::fromString( elemTemporalRange.attribute( QStringLiteral(
"end" ) ), Qt::ISODate );
249 QDomElement elem = doc.createElement( QStringLiteral(
"qgis3d" ) );
251 QDomElement elemOrigin = doc.createElement( QStringLiteral(
"origin" ) );
252 elemOrigin.setAttribute( QStringLiteral(
"x" ), QString::number( mOrigin.
x() ) );
253 elemOrigin.setAttribute( QStringLiteral(
"y" ), QString::number( mOrigin.
y() ) );
254 elemOrigin.setAttribute( QStringLiteral(
"z" ), QString::number( mOrigin.
z() ) );
255 elem.appendChild( elemOrigin );
257 QDomElement elemCamera = doc.createElement( QStringLiteral(
"camera" ) );
258 elemCamera.setAttribute( QStringLiteral(
"field-of-view" ), mFieldOfView );
259 elem.appendChild( elemCamera );
261 QDomElement elemColor = doc.createElement( QStringLiteral(
"color" ) );
264 elem.appendChild( elemColor );
266 QDomElement elemCrs = doc.createElement( QStringLiteral(
"crs" ) );
268 elem.appendChild( elemCrs );
270 QDomElement elemTerrain = doc.createElement( QStringLiteral(
"terrain" ) );
271 elemTerrain.setAttribute( QStringLiteral(
"exaggeration" ), QString::number( mTerrainVerticalScale ) );
272 elemTerrain.setAttribute( QStringLiteral(
"texture-size" ), mMapTileResolution );
273 elemTerrain.setAttribute( QStringLiteral(
"max-terrain-error" ), QString::number( mMaxTerrainScreenError ) );
274 elemTerrain.setAttribute( QStringLiteral(
"max-ground-error" ), QString::number( mMaxTerrainGroundError ) );
275 elemTerrain.setAttribute( QStringLiteral(
"shading-enabled" ), mTerrainShadingEnabled ? 1 : 0 );
276 QDomElement elemTerrainShadingMaterial = doc.createElement( QStringLiteral(
"shading-material" ) );
277 mTerrainShadingMaterial.
writeXml( elemTerrainShadingMaterial, context );
278 elemTerrain.appendChild( elemTerrainShadingMaterial );
279 elemTerrain.setAttribute( QStringLiteral(
"map-theme" ), mTerrainMapTheme );
280 elemTerrain.setAttribute( QStringLiteral(
"show-labels" ), mShowLabels ? 1 : 0 );
282 QDomElement elemPointLights = doc.createElement( QStringLiteral(
"point-lights" ) );
285 QDomElement elemPointLight = pointLight.writeXml( doc );
286 elemPointLights.appendChild( elemPointLight );
288 elem.appendChild( elemPointLights );
290 QDomElement elemDirectionalLights = doc.createElement( QStringLiteral(
"directional-lights" ) );
293 QDomElement elemDirectionalLight = directionalLight.writeXml( doc );
294 elemDirectionalLights.appendChild( elemDirectionalLight );
296 elem.appendChild( elemDirectionalLights );
298 QDomElement elemMapLayers = doc.createElement( QStringLiteral(
"layers" ) );
301 QDomElement elemMapLayer = doc.createElement( QStringLiteral(
"layer" ) );
302 elemMapLayer.setAttribute( QStringLiteral(
"id" ), layerRef.
layerId );
303 elemMapLayers.appendChild( elemMapLayer );
305 elemTerrain.appendChild( elemMapLayers );
307 QDomElement elemTerrainMapLayers = doc.createElement( QStringLiteral(
"terrainLayers" ) );
310 QDomElement elemMapLayer = doc.createElement( QStringLiteral(
"layer" ) );
311 elemMapLayer.setAttribute( QStringLiteral(
"id" ), layerRef.
layerId );
312 elemTerrainMapLayers.appendChild( elemMapLayer );
314 elemTerrain.appendChild( elemTerrainMapLayers );
316 QDomElement elemTerrainGenerator = doc.createElement( QStringLiteral(
"generator" ) );
318 mTerrainGenerator->writeXml( elemTerrainGenerator );
319 elemTerrain.appendChild( elemTerrainGenerator );
320 elem.appendChild( elemTerrain );
322 QDomElement elemRenderers = doc.createElement( QStringLiteral(
"renderers" ) );
325 QDomElement elemRenderer = doc.createElement( QStringLiteral(
"renderer" ) );
326 elemRenderer.setAttribute( QStringLiteral(
"type" ), renderer->
type() );
327 renderer->
writeXml( elemRenderer, context );
328 elemRenderers.appendChild( elemRenderer );
330 elem.appendChild( elemRenderers );
332 QDomElement elemSkybox = doc.createElement( QStringLiteral(
"skybox" ) );
333 elemSkybox.setAttribute( QStringLiteral(
"skybox-enabled" ), mIsSkyboxEnabled );
334 mSkyboxSettings.
writeXml( elemSkybox, context );
335 elem.appendChild( elemSkybox );
337 QDomElement elemShadows = doc.createElement( QStringLiteral(
"shadow-rendering" ) );
338 mShadowSettings.
writeXml( elemShadows, context );
339 elem.appendChild( elemShadows );
341 QDomElement elemDebug = doc.createElement( QStringLiteral(
"debug" ) );
342 elemDebug.setAttribute( QStringLiteral(
"bounding-boxes" ), mShowTerrainBoundingBoxes ? 1 : 0 );
343 elemDebug.setAttribute( QStringLiteral(
"terrain-tile-info" ), mShowTerrainTileInfo ? 1 : 0 );
344 elemDebug.setAttribute( QStringLiteral(
"camera-view-center" ), mShowCameraViewCenter ? 1 : 0 );
345 elemDebug.setAttribute( QStringLiteral(
"show-light-sources" ), mShowLightSources ? 1 : 0 );
346 elem.appendChild( elemDebug );
348 QDomElement elemTemporalRange = doc.createElement( QStringLiteral(
"temporal-range" ) );
349 elemTemporalRange.setAttribute( QStringLiteral(
"start" ),
temporalRange().begin().toString( Qt::ISODate ) );
350 elemTemporalRange.setAttribute( QStringLiteral(
"end" ),
temporalRange().end().toString( Qt::ISODate ) );
357 for (
int i = 0; i < mLayers.count(); ++i )
362 for (
int i = 0; i < mTerrainLayers.count(); ++i )
368 mTerrainGenerator->resolveReferences( project );
370 for (
int i = 0; i < mRenderers.count(); ++i )
394 return mTransformContext;
399 mTransformContext = context;
404 if ( color == mBackgroundColor )
407 mBackgroundColor = color;
413 return mBackgroundColor;
418 if ( color == mSelectionColor )
421 mSelectionColor = color;
427 return mSelectionColor;
432 if ( zScale == mTerrainVerticalScale )
435 mTerrainVerticalScale = zScale;
441 return mTerrainVerticalScale;
446 QList<QgsMapLayerRef> lst;
447 lst.reserve(
layers.count() );
453 if ( mLayers == lst )
462 QList<QgsMapLayer *> lst;
463 lst.reserve( mLayers.count() );
466 if ( layerRef.
layer )
467 lst.append( layerRef.
layer );
474 QList<QgsMapLayerRef> lst;
475 lst.reserve(
layers.count() );
481 if ( mTerrainLayers == lst )
484 mTerrainLayers = lst;
490 QList<QgsMapLayer *> lst;
491 lst.reserve( mTerrainLayers.count() );
494 if ( layerRef.
layer )
495 lst.append( layerRef.
layer );
502 if ( mMapTileResolution == res )
505 mMapTileResolution = res;
511 return mMapTileResolution;
516 if ( mMaxTerrainScreenError == error )
519 mMaxTerrainScreenError = error;
525 return mMaxTerrainScreenError;
530 if ( mMaxTerrainGroundError == error )
533 mMaxTerrainGroundError = error;
539 return mMaxTerrainGroundError;
544 mTerrainGenerator.reset( gen );
550 if ( mTerrainShadingEnabled == enabled )
553 mTerrainShadingEnabled = enabled;
559 if ( mTerrainShadingMaterial == material )
562 mTerrainShadingMaterial = material;
568 if ( mTerrainMapTheme == theme )
571 mTerrainMapTheme = theme;
577 qDeleteAll( mRenderers );
586 if ( mShowTerrainBoundingBoxes == enabled )
589 mShowTerrainBoundingBoxes = enabled;
595 if ( mShowTerrainTileInfo == enabled )
598 mShowTerrainTileInfo = enabled;
604 if ( mShowCameraViewCenter == enabled )
607 mShowCameraViewCenter = enabled;
613 if ( mShowLightSources == enabled )
616 mShowLightSources = enabled;
622 if ( mShowLabels == enabled )
625 mShowLabels = enabled;