QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 #include <Qt3DExtras/QPhongMaterial>
31 
33 
35 
36 QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
37  : QgsChunkLoader( node )
38  , mTerrain( terrain )
39 {
40  const Qgs3DMapSettings &map = mTerrain->map3D();
41  int tx, ty, tz;
42 #if 0
43  if ( map.terrainGenerator->type() == TerrainGenerator::QuantizedMesh )
44  {
45  // TODO: sort out - should not be here
46  QuantizedMeshTerrainGenerator *generator = static_cast<QuantizedMeshTerrainGenerator *>( map.terrainGenerator.get() );
47  generator->quadTreeTileToBaseTile( node->x, node->y, node->z, tx, ty, tz );
48  }
49  else
50 #endif
51  {
52  tx = node->tileX();
53  ty = node->tileY();
54  tz = node->tileZ();
55  }
56 
57  QgsRectangle extentTerrainCrs = map.terrainGenerator()->tilingScheme().tileToExtent( tx, ty, tz );
58  mExtentMapCrs = terrain->terrainToMapTransform().transformBoundingBox( extentTerrainCrs );
59  mTileDebugText = QStringLiteral( "%1 | %2 | %3" ).arg( tx ).arg( ty ).arg( tz );
60 }
61 
62 void QgsTerrainTileLoader::loadTexture()
63 {
64  connect( mTerrain->textureGenerator(), &QgsTerrainTextureGenerator::tileReady, this, &QgsTerrainTileLoader::onImageReady );
65  mTextureJobId = mTerrain->textureGenerator()->render( mExtentMapCrs, mNode->tileId(), mTileDebugText );
66 }
67 
68 void QgsTerrainTileLoader::createTextureComponent( QgsTerrainTileEntity *entity, bool isShadingEnabled, const QgsPhongMaterialSettings &shadingMaterial, bool useTexture )
69 {
70  Qt3DRender::QTexture2D *texture = useTexture || !isShadingEnabled ? createTexture( entity ) : nullptr;
71 
72  Qt3DRender::QMaterial *material = nullptr;
73  if ( texture )
74  {
75  if ( isShadingEnabled )
76  {
77  Qt3DExtras::QDiffuseMapMaterial *diffuseMapMaterial;
78  diffuseMapMaterial = new Qt3DExtras::QDiffuseMapMaterial;
79  diffuseMapMaterial->setDiffuse( texture );
80  diffuseMapMaterial->setAmbient( shadingMaterial.ambient() );
81  diffuseMapMaterial->setSpecular( shadingMaterial.specular() );
82  diffuseMapMaterial->setShininess( shadingMaterial.shininess() );
83  material = diffuseMapMaterial;
84  }
85  else
86  {
87  Qt3DExtras::QTextureMaterial *textureMaterial = new Qt3DExtras::QTextureMaterial;
88  textureMaterial->setTexture( texture );
89  material = textureMaterial;
90  }
91  }
92  else
93  {
94  Qt3DExtras::QPhongMaterial *phongMaterial = new Qt3DExtras::QPhongMaterial;
95  phongMaterial->setDiffuse( shadingMaterial.diffuse() );
96  phongMaterial->setAmbient( shadingMaterial.ambient() );
97  phongMaterial->setSpecular( shadingMaterial.specular() );
98  phongMaterial->setShininess( shadingMaterial.shininess() );
99  material = phongMaterial;
100  }
101 
102  entity->addComponent( material ); // takes ownership if the component has no parent
103 }
104 
105 Qt3DRender::QTexture2D *QgsTerrainTileLoader::createTexture( QgsTerrainTileEntity *entity )
106 {
107  Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
108  QgsTerrainTextureImage *textureImage = new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
109  texture->addTextureImage( textureImage );//texture take the ownership of textureImage if has no parant
110  texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
111  texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
112 
113  entity->setTextureImage( textureImage );
114 
115  return texture;
116 }
117 
118 void QgsTerrainTileLoader::onImageReady( int jobId, const QImage &image )
119 {
120  if ( mTextureJobId == jobId )
121  {
122  mTextureImage = image;
123  mTextureJobId = -1;
124  emit finished(); // TODO: this should be left for derived class!
125  }
126 }
127 
QgsPhongMaterialSettings::ambient
QColor ambient() const
Returns ambient color component.
Definition: qgsphongmaterialsettings.h:60
QgsPhongMaterialSettings::diffuse
QColor diffuse() const
Returns diffuse color component.
Definition: qgsphongmaterialsettings.h:62
QgsPhongMaterialSettings::specular
QColor specular() const
Returns specular color component.
Definition: qgsphongmaterialsettings.h:64
QgsTerrainGenerator::type
virtual Type type() const =0
What texture generator implementation is this.
quantizedmeshterraingenerator.h
qgschunknode_p.h
qgsterraintileentity_p.h
QuantizedMeshTerrainGenerator::quadTreeTileToBaseTile
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.
Definition: quantizedmeshterraingenerator.cpp:124
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsPhongMaterialSettings
3 Basic shading material used for rendering based on the Phong shading model with three color compone...
Definition: qgsphongmaterialsettings.h:37
QgsTerrainGenerator::mTerrain
QgsTerrainEntity * mTerrain
Definition: qgsterraingenerator.h:107
qgsterraingenerator.h
qgsterraintextureimage_p.h
Qgs3DMapSettings
3 Definition of the world
Definition: qgs3dmapsettings.h:54
qgsterraintileloader_p.h
QgsTilingScheme::tileToExtent
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
Definition: qgstilingscheme.cpp:42
QuantizedMeshTerrainGenerator
3 Terrain generator using downloaded terrain tiles using quantized mesh specification
Definition: quantizedmeshterraingenerator.h:27
Qgs3DMapSettings::terrainGenerator
QgsTerrainGenerator * terrainGenerator() const
Returns terrain generator. It takes care of producing terrain tiles from the input data.
Definition: qgs3dmapsettings.h:272
qgsterrainentity_p.h
qgs3dmapsettings.h
QgsPhongMaterialSettings::shininess
float shininess() const
Returns shininess of the surface.
Definition: qgsphongmaterialsettings.h:66
qgsterraintexturegenerator_p.h
QgsTerrainGenerator::tilingScheme
const QgsTilingScheme & tilingScheme() const
Returns tiling scheme of the terrain.
Definition: qgsterraingenerator.h:97