QGIS API Documentation  3.0.2-Girona (307d082)
qgsterraintileloader_p.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsterraintileloader_p.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 
16 #include "qgsterraintileloader_p.h"
17 
18 #include "qgs3dmapsettings.h"
19 #include "qgschunknode_p.h"
20 #include "qgsterrainentity_p.h"
21 #include "qgsterraingenerator.h"
24 #include "qgsterraintileentity_p.h"
25 
26 #include <Qt3DRender/QTexture>
27 
28 #if QT_VERSION >= 0x050900
29 #include <Qt3DExtras/QTextureMaterial>
30 #else
31 #include <Qt3DExtras/QDiffuseMapMaterial>
32 #endif
33 
35 
37 
38 QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
39  : QgsChunkLoader( node )
40  , mTerrain( terrain )
41 {
42  const Qgs3DMapSettings &map = mTerrain->map3D();
43  int tx, ty, tz;
44 #if 0
45  if ( map.terrainGenerator->type() == TerrainGenerator::QuantizedMesh )
46  {
47  // TODO: sort out - should not be here
48  QuantizedMeshTerrainGenerator *generator = static_cast<QuantizedMeshTerrainGenerator *>( map.terrainGenerator.get() );
49  generator->quadTreeTileToBaseTile( node->x, node->y, node->z, tx, ty, tz );
50  }
51  else
52 #endif
53  {
54  tx = node->tileX();
55  ty = node->tileY();
56  tz = node->tileZ();
57  }
58 
59  QgsRectangle extentTerrainCrs = map.terrainGenerator()->tilingScheme().tileToExtent( tx, ty, tz );
60  mExtentMapCrs = terrain->terrainToMapTransform().transformBoundingBox( extentTerrainCrs );
61  mTileDebugText = QString( "%1 | %2 | %3" ).arg( tx ).arg( ty ).arg( tz );
62 }
63 
64 void QgsTerrainTileLoader::loadTexture()
65 {
66  connect( mTerrain->textureGenerator(), &QgsTerrainTextureGenerator::tileReady, this, &QgsTerrainTileLoader::onImageReady );
67  mTextureJobId = mTerrain->textureGenerator()->render( mExtentMapCrs, mTileDebugText );
68 }
69 
70 void QgsTerrainTileLoader::createTextureComponent( QgsTerrainTileEntity *entity )
71 {
72  Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D( entity );
73  QgsTerrainTextureImage *textureImage = new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
74  texture->addTextureImage( textureImage );
75  texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
76  texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
77  Qt3DExtras::QTextureMaterial *material = nullptr;
78 #if QT_VERSION >= 0x050900
79  material = new Qt3DExtras::QTextureMaterial;
80  material->setTexture( texture );
81 #else
82  material = new Qt3DExtras::QDiffuseMapMaterial;
83  material->setDiffuse( texture );
84  material->setShininess( 1 );
85  material->setAmbient( Qt::white );
86 #endif
87  entity->setTextureImage( textureImage );
88  entity->addComponent( material ); // takes ownership if the component has no parent
89 }
90 
91 void QgsTerrainTileLoader::onImageReady( int jobId, const QImage &image )
92 {
93  if ( mTextureJobId == jobId )
94  {
95  mTextureImage = image;
96  mTextureJobId = -1;
97  emit finished(); // TODO: this should be left for derived class!
98  }
99 }
100 
A rectangle specified with double values.
Definition: qgsrectangle.h:39
virtual Type type() const =0
What texture generator implementation is this.
3 Definition of the world
void quadTreeTileToBaseTile(int x, int y, int z, int &tx, int &ty, int &tz) const
Converts tile coordinates (x,y,z) in our quadtree to tile coordinates of quantized mesh tree...
3 Terrain generator using downloaded terrain tiles using quantized mesh specification ...
QgsTerrainEntity * mTerrain
const QgsTilingScheme & tilingScheme() const
Returns tiling scheme of the terrain.
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
QgsTerrainGenerator * terrainGenerator() const
Returns terrain generator. It takes care of producing terrain tiles from the input data...