QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 #include <Qt3DExtras/QTextureMaterial>
29 #include <Qt3DExtras/QDiffuseMapMaterial>
30 
32 
34 
35 QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
36  : QgsChunkLoader( node )
37  , mTerrain( terrain )
38 {
39  const Qgs3DMapSettings &map = mTerrain->map3D();
40  int tx, ty, tz;
41 #if 0
42  if ( map.terrainGenerator->type() == TerrainGenerator::QuantizedMesh )
43  {
44  // TODO: sort out - should not be here
45  QuantizedMeshTerrainGenerator *generator = static_cast<QuantizedMeshTerrainGenerator *>( map.terrainGenerator.get() );
46  generator->quadTreeTileToBaseTile( node->x, node->y, node->z, tx, ty, tz );
47  }
48  else
49 #endif
50  {
51  tx = node->tileX();
52  ty = node->tileY();
53  tz = node->tileZ();
54  }
55 
56  QgsRectangle extentTerrainCrs = map.terrainGenerator()->tilingScheme().tileToExtent( tx, ty, tz );
57  mExtentMapCrs = terrain->terrainToMapTransform().transformBoundingBox( extentTerrainCrs );
58  mTileDebugText = QStringLiteral( "%1 | %2 | %3" ).arg( tx ).arg( ty ).arg( tz );
59 }
60 
61 void QgsTerrainTileLoader::loadTexture()
62 {
63  connect( mTerrain->textureGenerator(), &QgsTerrainTextureGenerator::tileReady, this, &QgsTerrainTileLoader::onImageReady );
64  mTextureJobId = mTerrain->textureGenerator()->render( mExtentMapCrs, mNode->tileId(), mTileDebugText );
65 }
66 
67 void QgsTerrainTileLoader::createTextureComponent( QgsTerrainTileEntity *entity, bool isShadingEnabled, const QgsPhongMaterialSettings &shadingMaterial )
68 {
69  Qt3DRender::QTexture2D *texture = createTexture( entity );
70 
71  Qt3DRender::QMaterial *material = nullptr;
72  if ( isShadingEnabled )
73  {
74  Qt3DExtras::QDiffuseMapMaterial *diffuseMapMaterial;
75  diffuseMapMaterial = new Qt3DExtras::QDiffuseMapMaterial;
76  diffuseMapMaterial->setDiffuse( texture );
77  diffuseMapMaterial->setAmbient( shadingMaterial.ambient() );
78  diffuseMapMaterial->setSpecular( shadingMaterial.specular() );
79  diffuseMapMaterial->setShininess( shadingMaterial.shininess() );
80  material = diffuseMapMaterial;
81  }
82  else
83  {
84  Qt3DExtras::QTextureMaterial *textureMaterial = new Qt3DExtras::QTextureMaterial;
85  textureMaterial->setTexture( texture );
86  material = textureMaterial;
87  }
88 
89  entity->addComponent( material ); // takes ownership if the component has no parent
90 }
91 
92 Qt3DRender::QTexture2D *QgsTerrainTileLoader::createTexture( QgsTerrainTileEntity *entity )
93 {
94  Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
95  QgsTerrainTextureImage *textureImage = new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
96  texture->addTextureImage( textureImage );//texture take the ownership of textureImage if has no parant
97  texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
98  texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
99 
100  entity->setTextureImage( textureImage );
101 
102  return texture;
103 }
104 
105 void QgsTerrainTileLoader::onImageReady( int jobId, const QImage &image )
106 {
107  if ( mTextureJobId == jobId )
108  {
109  mTextureImage = image;
110  mTextureJobId = -1;
111  emit finished(); // TODO: this should be left for derived class!
112  }
113 }
114 
A rectangle specified with double values.
Definition: qgsrectangle.h:41
float shininess() const
Returns shininess of the surface.
QColor specular() const
Returns specular color component.
virtual Type type() const =0
What texture generator implementation is this.
3 Basic shading material used for rendering based on the Phong shading model with three color compone...
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.
QColor ambient() const
Returns ambient color component.
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...