QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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#include "moc_qgsflatterraingenerator.cpp"
18
19#include <Qt3DRender/QGeometryRenderer>
20#include <Qt3DCore/QTransform>
21
22#include "qgs3dmapsettings.h"
23#include "qgschunknode.h"
24#include "qgsterrainentity.h"
26#include "qgs3dutils.h"
28
29
30//---------------
31
32
33FlatTerrainChunkLoader::FlatTerrainChunkLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
34 : QgsTerrainTileLoader( terrain, node )
35{
36 loadTexture();
37}
38
39
40Qt3DCore::QEntity *FlatTerrainChunkLoader::createEntity( Qt3DCore::QEntity *parent )
41{
42 QgsTerrainTileEntity *entity = new QgsTerrainTileEntity( mNode->tileId() );
43
44 // make geometry renderer
45
46 // simple quad geometry shared by all tiles
47 // QPlaneGeometry by default is 1x1 with mesh resolution QSize(2,2), centered at 0
48 // TODO: the geometry could be shared inside Terrain instance (within terrain-generator specific data?)
49 mTileGeometry = new Qt3DExtras::QPlaneGeometry;
50
51 Qt3DRender::QGeometryRenderer *mesh = new Qt3DRender::QGeometryRenderer;
52 mesh->setGeometry( mTileGeometry ); // takes ownership if the component has no parent
53 entity->addComponent( mesh ); // takes ownership if the component has no parent
54
55 // create material
56
57 const Qgs3DMapSettings *map = terrain()->mapSettings();
58
59 // create transform
60
61 Qt3DCore::QTransform *transform = nullptr;
62 transform = new Qt3DCore::QTransform();
63 entity->addComponent( transform );
64
65 // set up transform according to the extent covered by the quad geometry
66 const QgsBox3D box3D = mNode->box3D();
67 const QgsBox3D mapFullBox3D( map->extent(), box3D.zMinimum(), box3D.zMaximum() );
68
69 const QgsBox3D commonExtent( std::max( box3D.xMinimum(), mapFullBox3D.xMinimum() ),
70 std::max( box3D.yMinimum(), mapFullBox3D.yMinimum() ),
71 box3D.zMinimum(),
72 std::min( box3D.xMaximum(), mapFullBox3D.xMaximum() ),
73 std::min( box3D.yMaximum(), mapFullBox3D.yMaximum() ),
74 box3D.zMaximum() );
75 const double xSide = commonExtent.width();
76 const double ySide = commonExtent.height();
77 const double xMin = commonExtent.xMinimum() - map->origin().x();
78 const double yMin = commonExtent.yMinimum() - map->origin().y();
79
80 transform->setRotation( QQuaternion::fromAxisAndAngle( QVector3D( 1, 0, 0 ), 90 ) ); // QPlaneGeometry uses XZ as the base plane
81 transform->setScale3D( QVector3D( static_cast<float>( xSide ), 1, static_cast<float>( ySide ) ) );
82 transform->setTranslation( QVector3D( static_cast<float>( xMin + xSide / 2 ), static_cast<float>( yMin + ySide / 2 ), 0 ) );
83
84 createTextureComponent( entity, map->isTerrainShadingEnabled(), map->terrainShadingMaterial(), !map->layers().empty() );
85
86 entity->setParent( parent );
87 return entity;
88}
89
91
92// ---------------
93
94QgsChunkLoader *QgsFlatTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
95{
96 return new FlatTerrainChunkLoader( mTerrain, node );
97}
98
100{
102 cloned->mCrs = mCrs;
103 cloned->mExtent = mExtent;
104 cloned->updateTilingScheme();
105 return cloned;
106}
107
112
117
118void QgsFlatTerrainGenerator::rootChunkHeightRange( float &hMin, float &hMax ) const
119{
120 hMin = 0;
121 hMax = 0;
122}
123
124void QgsFlatTerrainGenerator::writeXml( QDomElement &elem ) const
125{
126 Q_UNUSED( elem )
127}
128
129void QgsFlatTerrainGenerator::readXml( const QDomElement &elem )
130{
131 Q_UNUSED( elem )
132}
133
135{
136 mCrs = crs;
137 updateTilingScheme();
138}
139
141{
142 if ( mExtent == extent )
143 return;
144
145 mExtent = extent;
146 updateTilingScheme();
147}
148
149void QgsFlatTerrainGenerator::updateTilingScheme()
150{
151 // the real extent will be a square where the given extent fully fits
153}
QgsRectangle extent() const
Returns the 3D scene's 2D extent in the 3D scene'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).
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
double yMaximum() const
Returns the maximum y value.
Definition qgsbox3d.h:246
double xMinimum() const
Returns the minimum x value.
Definition qgsbox3d.h:211
double zMaximum() const
Returns the maximum z value.
Definition qgsbox3d.h:274
double xMaximum() const
Returns the maximum x value.
Definition qgsbox3d.h:218
double zMinimum() const
Returns the minimum z value.
Definition qgsbox3d.h:267
double yMinimum() const
Returns the minimum y value.
Definition qgsbox3d.h:239
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.
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
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
QgsCoordinateReferenceSystem crs() const override
Returns CRS of the terrain.
A rectangle specified with double values.
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.
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:50
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48
const QgsCoordinateReferenceSystem & crs