QGIS API Documentation 4.1.0-Master (64dc32379c2)
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
18#include "qgs3dmapsettings.h"
19#include "qgs3drendercontext.h"
20#include "qgschunknode.h"
21#include "qgsgeotransform.h"
22#include "qgsterrainentity.h"
24
25#include <Qt3DRender/QGeometryRenderer>
26
27#include "moc_qgsflatterraingenerator.cpp"
28
30
31
32//---------------
33
34
35FlatTerrainChunkLoader::FlatTerrainChunkLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
36 : QgsTerrainTileLoader( terrain, node )
37{}
38
39void FlatTerrainChunkLoader::start()
40{
41 loadTexture();
42}
43
44
45Qt3DCore::QEntity *FlatTerrainChunkLoader::createEntity( Qt3DCore::QEntity *parent )
46{
47 QgsTerrainTileEntity *entity = new QgsTerrainTileEntity( mNode->tileId() );
48
49 // make geometry renderer
50
51 // simple quad geometry shared by all tiles
52 // QPlaneGeometry by default is 1x1 with mesh resolution QSize(2,2), centered at 0
53 // TODO: the geometry could be shared inside Terrain instance (within terrain-generator specific data?)
54 mTileGeometry = new Qt3DExtras::QPlaneGeometry;
55
56 Qt3DRender::QGeometryRenderer *mesh = new Qt3DRender::QGeometryRenderer;
57 mesh->setGeometry( mTileGeometry ); // takes ownership if the component has no parent
58 entity->addComponent( mesh ); // takes ownership if the component has no parent
59
60 // create material
61
62 const Qgs3DMapSettings *map = terrain()->mapSettings();
63
64 // create transform
65
66 QgsGeoTransform *transform = nullptr;
67 transform = new QgsGeoTransform();
68 entity->addComponent( transform );
69
70 // set up transform according to the extent covered by the quad geometry
71 const QgsBox3D box3D = mNode->box3D();
72 const QgsBox3D mapFullBox3D( map->extent(), box3D.zMinimum(), box3D.zMaximum() );
73
74 const QgsBox3D commonExtent(
75 std::max( box3D.xMinimum(), mapFullBox3D.xMinimum() ),
76 std::max( box3D.yMinimum(), mapFullBox3D.yMinimum() ),
77 box3D.zMinimum(),
78 std::min( box3D.xMaximum(), mapFullBox3D.xMaximum() ),
79 std::min( box3D.yMaximum(), mapFullBox3D.yMaximum() ),
80 box3D.zMaximum()
81 );
82 const double xSide = commonExtent.width();
83 const double ySide = commonExtent.height();
84 const double xMin = commonExtent.xMinimum();
85 const double yMin = commonExtent.yMinimum();
86
87 transform->setRotation( QQuaternion::fromAxisAndAngle( QVector3D( 1, 0, 0 ), 90 ) ); // QPlaneGeometry uses XZ as the base plane
88 transform->setScale3D( QVector3D( static_cast<float>( xSide ), 1, static_cast<float>( ySide ) ) );
89 transform->setGeoTranslation( QgsVector3D( xMin + xSide / 2, yMin + ySide / 2, 0 ) );
90
91 createTextureComponent( entity, map->isTerrainShadingEnabled(), map->terrainShadingMaterial(), !map->layers().empty(), Qgs3DRenderContext::fromMapSettings( map ) );
92
93 entity->setParent( parent );
94 return entity;
95}
96
98
99// ---------------
100
105
106QgsChunkLoader *QgsFlatTerrainGenerator::createChunkLoader( QgsChunkNode *node ) const
107{
108 return new FlatTerrainChunkLoader( mTerrain, node );
109}
110
112{
114 cloned->mCrs = mCrs;
115 cloned->mExtent = mExtent;
116 cloned->updateTilingScheme();
117 return cloned;
118}
119
124
126{
127 return mTerrainTilingScheme.tileToExtent( 0, 0, 0 );
128}
129
130void QgsFlatTerrainGenerator::rootChunkHeightRange( float &hMin, float &hMax ) const
131{
132 hMin = 0;
133 hMax = 0;
134}
135
137{
138 mCrs = crs;
139 updateTilingScheme();
140}
141
143{
144 if ( mExtent == extent )
145 return;
146
147 mExtent = extent;
148 updateTilingScheme();
149}
150
151float QgsFlatTerrainGenerator::heightAt( double x, double y, const Qgs3DRenderContext &context ) const
152{
153 Q_UNUSED( x )
154 Q_UNUSED( y )
155 Q_UNUSED( context )
156 return 0;
157}
158
159void QgsFlatTerrainGenerator::updateTilingScheme()
160{
161 // the real extent will be a square where the given extent fully fits
163}
Definition of the world.
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.
Rendering context for preparation of 3D entities.
static Qgs3DRenderContext fromMapSettings(const Qgs3DMapSettings *mapSettings)
Creates an initialized Qgs3DRenderContext instance from given Qgs3DMapSettings.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
double yMaximum() const
Returns the maximum y value.
Definition qgsbox3d.h:240
double xMinimum() const
Returns the minimum x value.
Definition qgsbox3d.h:205
double zMaximum() const
Returns the maximum z value.
Definition qgsbox3d.h:268
double xMaximum() const
Returns the maximum x value.
Definition qgsbox3d.h:212
double zMinimum() const
Returns the minimum z value.
Definition qgsbox3d.h:261
double yMinimum() const
Returns the minimum y value.
Definition qgsbox3d.h:233
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
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 rootChunkHeightRange(float &hMin, float &hMax) const override
Returns height range of the root chunk in world coordinates.
static QgsTerrainGenerator * create()
Creates a new instance of a QgsFlatTerrainGenerator object.
QgsChunkLoader * createChunkLoader(QgsChunkNode *node) const override
float heightAt(double x, double y, const Qgs3DRenderContext &context) const override
Returns height at (x,y) in map's CRS.
QgsFlatTerrainGenerator()=default
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.
QgsTerrainGenerator * clone() const override
Makes a copy of the current instance.
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context) override
Sets the CRS associated with the terrain.
A rectangle specified with double values.
Base class for generators of terrain.
Type
Enumeration of the available terrain generators.
@ Flat
The whole terrain is flat area.
QgsTilingScheme mTerrainTilingScheme
Tiling scheme of the terrain.
QgsTerrainEntity * mTerrain
Encapsulates tiling schemes (just like with WMTS / TMS / XYZ layers).
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33