QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsflatterraingenerator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsflatterraingenerator.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
17
18#include <Qt3DRender/QGeometryRenderer>
19#include <Qt3DCore/QTransform>
20
21#include "qgs3dmapsettings.h"
22#include "qgschunknode_p.h"
23#include "qgsterrainentity_p.h"
25#include "qgs3dutils.h"
27
28
29//---------------
30
31
32FlatTerrainChunkLoader::FlatTerrainChunkLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
33 : QgsTerrainTileLoader( terrain, node )
34{
35 loadTexture();
36}
37
38
39Qt3DCore::QEntity *FlatTerrainChunkLoader::createEntity( Qt3DCore::QEntity *parent )
40{
41 QgsTerrainTileEntity *entity = new QgsTerrainTileEntity( mNode->tileId() );
42
43 // make geometry renderer
44
45 // simple quad geometry shared by all tiles
46 // QPlaneGeometry by default is 1x1 with mesh resolution QSize(2,2), centered at 0
47 // TODO: the geometry could be shared inside Terrain instance (within terrain-generator specific data?)
48 mTileGeometry = new Qt3DExtras::QPlaneGeometry;
49
50 Qt3DRender::QGeometryRenderer *mesh = new Qt3DRender::QGeometryRenderer;
51 mesh->setGeometry( mTileGeometry ); // takes ownership if the component has no parent
52 entity->addComponent( mesh ); // takes ownership if the component has no parent
53
54 // create material
55
56 const Qgs3DMapSettings &map = terrain()->map3D();
57
58 // create transform
59
60 Qt3DCore::QTransform *transform = nullptr;
61 transform = new Qt3DCore::QTransform();
62 entity->addComponent( transform );
63
64 // set up transform according to the extent covered by the quad geometry
65 const QgsAABB bbox = mNode->bbox();
66
67 const QgsAABB mapFullExtent = Qgs3DUtils::mapToWorldExtent( map.extent(), bbox.yMin, bbox.yMax, map.origin() );
68
69 const QgsAABB commonExtent = QgsAABB( std::max( bbox.xMin, mapFullExtent.xMin ),
70 bbox.yMin,
71 std::max( bbox.zMin, mapFullExtent.zMin ),
72 std::min( bbox.xMax, mapFullExtent.xMax ),
73 bbox.yMax,
74 std::min( bbox.zMax, mapFullExtent.zMax )
75 );
76 const double xSide = commonExtent.xExtent();
77 const double zSide = commonExtent.zExtent();
78
79 transform->setScale3D( QVector3D( xSide, 1, zSide ) );
80 transform->setTranslation( QVector3D( commonExtent.xMin + xSide / 2, 0, commonExtent.zMin + zSide / 2 ) );
81
82 createTextureComponent( entity, map.isTerrainShadingEnabled(), map.terrainShadingMaterial(), !map.layers().empty() );
83
84 entity->setParent( parent );
85 return entity;
86}
87
89
90// ---------------
91
92QgsChunkLoader *QgsFlatTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
93{
94 return new FlatTerrainChunkLoader( mTerrain, node );
95}
96
98{
100 cloned->mCrs = mCrs;
101 cloned->mExtent = mExtent;
102 cloned->updateTilingScheme();
103 return cloned;
104}
105
107{
109}
110
112{
113 return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
114}
115
116void QgsFlatTerrainGenerator::rootChunkHeightRange( float &hMin, float &hMax ) const
117{
118 hMin = 0;
119 hMax = 0;
120}
121
122void QgsFlatTerrainGenerator::writeXml( QDomElement &elem ) const
123{
124 Q_UNUSED( elem )
125}
126
127void QgsFlatTerrainGenerator::readXml( const QDomElement &elem )
128{
129 Q_UNUSED( elem )
130}
131
133{
134 mCrs = crs;
135 updateTilingScheme();
136}
137
139{
140 if ( mExtent == extent )
141 return;
142
143 mExtent = extent;
144 updateTilingScheme();
145}
146
147void QgsFlatTerrainGenerator::updateTilingScheme()
148{
149 if ( mExtent.isNull() )
150 {
152 }
153 else
154 {
155 // the real extent will be a square where the given extent fully fits
157 }
158}
QgsRectangle extent() const
Returns the 3D scene's 2D extent in project's CRS.
bool isTerrainShadingEnabled() const
Returns whether terrain shading is enabled.
QgsPhongMaterialSettings terrainShadingMaterial() const
Returns terrain shading material.
QList< QgsMapLayer * > layers() const
Returns the list of 3D map layers to be rendered in the scene.
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
static QgsAABB mapToWorldExtent(const QgsRectangle &extent, double zMin, double zMax, const QgsVector3D &mapOrigin)
Converts map extent to axis aligned bounding box in 3D world coordinates.
Definition: qgs3dutils.cpp:627
3
Definition: qgsaabb.h:33
float yMax
Definition: qgsaabb.h:90
float xMax
Definition: qgsaabb.h:89
float xExtent() const
Returns box width in X axis.
Definition: qgsaabb.h:42
float zExtent() const
Returns box width in Z axis.
Definition: qgsaabb.h:46
float xMin
Definition: qgsaabb.h:86
float zMax
Definition: qgsaabb.h:91
float yMin
Definition: qgsaabb.h:87
float zMin
Definition: qgsaabb.h:88
This class represents a coordinate reference system (CRS).
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override SIP_FACTORY
Type type() const override
What texture generator implementation is this.
QgsRectangle rootChunkExtent() const override
extent of the terrain's root chunk in terrain's CRS
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets CRS of the terrain.
void rootChunkHeightRange(float &hMin, float &hMax) const override
Returns height range of the root chunk in world coordinates.
QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
void writeXml(QDomElement &elem) const override
Write terrain generator's configuration to XML.
QgsTerrainGenerator * clone() const override SIP_FACTORY
Makes a copy of the current instance.
QgsFlatTerrainGenerator()=default
Creates flat terrain generator object.
void readXml(const QDomElement &elem) override
Read terrain generator's configuration from XML.
void setExtent(const QgsRectangle &extent) override
sets the extent of the terrain in terrain's CRS
A rectangle specified with double values.
Definition: qgsrectangle.h:42
bool isNull() const
Test if the rectangle is null (holding no spatial information).
Definition: qgsrectangle.h:505
Type
Enumeration of the available terrain generators.
@ Flat
The whole terrain is flat area.
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
virtual QgsRectangle extent() const
extent of the terrain in terrain's CRS, might be non-square and smaller than rootChunkExtent()
QgsTerrainEntity * mTerrain
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
const QgsCoordinateReferenceSystem & crs