QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsterraintileloader.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsterraintileloader.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_qgsterraintileloader.cpp"
18
19#include "qgs3dmapsettings.h"
20#include "qgs3dutils.h"
21#include "qgschunknode.h"
23#include "qgsterrainentity.h"
24#include "qgsterraingenerator.h"
29#include "qgsmaterial.h"
30#include "qgstexturematerial.h"
31
32#include <Qt3DRender/QTexture>
33#include <Qt3DRender/QTechnique>
34#include <Qt3DRender/QCullFace>
35
37
38QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
39 : QgsChunkLoader( node )
40 , mTerrain( terrain )
41{
42 const Qgs3DMapSettings *map = mTerrain->mapSettings();
43 const QgsChunkNodeId nodeId = node->tileId();
44 const QgsRectangle extentTerrainCrs = map->terrainGenerator()->tilingScheme().tileToExtent( nodeId );
45 mExtentMapCrs = Qgs3DUtils::tryReprojectExtent2D( extentTerrainCrs, map->terrainGenerator()->crs(), map->crs(), map->transformContext() );
46 mTileDebugText = nodeId.text();
47}
48
49void QgsTerrainTileLoader::loadTexture()
50{
51 connect( mTerrain->textureGenerator(), &QgsTerrainTextureGenerator::tileReady, this, &QgsTerrainTileLoader::onImageReady );
52 mTextureJobId = mTerrain->textureGenerator()->render( mExtentMapCrs, mNode->tileId(), mTileDebugText );
53}
54
55void QgsTerrainTileLoader::createTextureComponent( QgsTerrainTileEntity *entity, bool isShadingEnabled, const QgsPhongMaterialSettings &shadingMaterial, bool useTexture )
56{
57 Qt3DRender::QTexture2D *texture = useTexture || !isShadingEnabled ? createTexture( entity ) : nullptr;
58
59 QgsMaterial *material = nullptr;
60 if ( texture )
61 {
62 if ( isShadingEnabled )
63 {
64 QgsPhongTexturedMaterial *phongTexturedMaterial = new QgsPhongTexturedMaterial();
65 phongTexturedMaterial->setAmbient( shadingMaterial.ambient() );
66 phongTexturedMaterial->setSpecular( shadingMaterial.specular() );
67 phongTexturedMaterial->setShininess( static_cast<float>( shadingMaterial.shininess() ) );
68 phongTexturedMaterial->setDiffuseTexture( texture );
69 phongTexturedMaterial->setOpacity( static_cast<float>( shadingMaterial.opacity() ) );
70 material = phongTexturedMaterial;
71 }
72 else
73 {
74 QgsTextureMaterial *textureMaterial = new QgsTextureMaterial;
75 textureMaterial->setTexture( texture );
76 material = textureMaterial;
77 }
78 }
79 else
80 {
81 QgsMaterialContext materialContext;
82 materialContext.setIsSelected( false );
83 material = shadingMaterial.toMaterial( QgsMaterialSettingsRenderingTechnique::Triangles, materialContext );
84 }
85
86 // no backface culling on terrain, to allow terrain to be viewed from underground
87 const QVector<Qt3DRender::QTechnique *> techniques = material->effect()->techniques();
88 for ( Qt3DRender::QTechnique *technique : techniques )
89 {
90 const QVector<Qt3DRender::QRenderPass *> passes = technique->renderPasses();
91 for ( Qt3DRender::QRenderPass *pass : passes )
92 {
93 Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
94 cullFace->setMode( Qt3DRender::QCullFace::NoCulling );
95 pass->addRenderState( cullFace );
96 }
97 }
98
99 entity->addComponent( material ); // takes ownership if the component has no parent
100}
101
102Qt3DRender::QTexture2D *QgsTerrainTileLoader::createTexture( QgsTerrainTileEntity *entity )
103{
104 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
105 QgsTerrainTextureImage *textureImage = new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
106 texture->addTextureImage( textureImage );//texture take the ownership of textureImage if has no parant
107 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
108 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
109
110 entity->setTextureImage( textureImage );
111
112 return texture;
113}
114
115void QgsTerrainTileLoader::onTextureLoaded()
116{
117 emit finished();
118}
119
120void QgsTerrainTileLoader::onImageReady( int jobId, const QImage &image )
121{
122 if ( mTextureJobId == jobId )
123 {
124 mTextureImage = image;
125 mTextureJobId = -1;
126 onTextureLoaded();
127 }
128}
129
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
static QgsRectangle tryReprojectExtent2D(const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs1, const QgsCoordinateReferenceSystem &crs2, const QgsCoordinateTransformContext &context)
Reprojects extent from crs1 to crs2 coordinate reference system with context context.
void setIsSelected(bool isSelected)
Sets whether the material should represent a selected state.
QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
double shininess() const
Returns shininess of the surface.
A rectangle specified with double values.
virtual QgsCoordinateReferenceSystem crs() const
Returns CRS of the terrain.
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.
@ Triangles
Triangle based rendering (default)