QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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
18#include "qgs3dmapsettings.h"
19#include "qgs3dutils.h"
20#include "qgschunknode.h"
22#include "qgsterrainentity.h"
23#include "qgsterraingenerator.h"
28#include "qgsmaterial.h"
29#include "qgstexturematerial.h"
30
31#include <Qt3DRender/QTexture>
32#include <Qt3DRender/QTechnique>
33#include <Qt3DRender/QCullFace>
34
36
37QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkNode *node )
38 : QgsChunkLoader( node )
39 , mTerrain( terrain )
40{
41 const Qgs3DMapSettings *map = mTerrain->mapSettings();
42 const QgsChunkNodeId nodeId = node->tileId();
43 const QgsRectangle extentTerrainCrs = map->terrainGenerator()->tilingScheme().tileToExtent( nodeId );
44 mExtentMapCrs = Qgs3DUtils::tryReprojectExtent2D( extentTerrainCrs, map->terrainGenerator()->crs(), map->crs(), map->transformContext() );
45 mTileDebugText = nodeId.text();
46}
47
48void QgsTerrainTileLoader::loadTexture()
49{
50 connect( mTerrain->textureGenerator(), &QgsTerrainTextureGenerator::tileReady, this, &QgsTerrainTileLoader::onImageReady );
51 mTextureJobId = mTerrain->textureGenerator()->render( mExtentMapCrs, mNode->tileId(), mTileDebugText );
52}
53
54void QgsTerrainTileLoader::createTextureComponent( QgsTerrainTileEntity *entity, bool isShadingEnabled, const QgsPhongMaterialSettings &shadingMaterial, bool useTexture )
55{
56 Qt3DRender::QTexture2D *texture = useTexture || !isShadingEnabled ? createTexture( entity ) : nullptr;
57
58 QgsMaterial *material = nullptr;
59 if ( texture )
60 {
61 if ( isShadingEnabled )
62 {
63 QgsPhongTexturedMaterial *phongTexturedMaterial = new QgsPhongTexturedMaterial();
64 phongTexturedMaterial->setAmbient( shadingMaterial.ambient() );
65 phongTexturedMaterial->setSpecular( shadingMaterial.specular() );
66 phongTexturedMaterial->setShininess( static_cast<float>( shadingMaterial.shininess() ) );
67 phongTexturedMaterial->setDiffuseTexture( texture );
68 phongTexturedMaterial->setOpacity( static_cast<float>( shadingMaterial.opacity() ) );
69 material = phongTexturedMaterial;
70 }
71 else
72 {
73 QgsTextureMaterial *textureMaterial = new QgsTextureMaterial;
74 textureMaterial->setTexture( texture );
75 material = textureMaterial;
76 }
77 }
78 else
79 {
80 QgsMaterialContext materialContext;
81 materialContext.setIsSelected( false );
82 material = shadingMaterial.toMaterial( QgsMaterialSettingsRenderingTechnique::Triangles, materialContext );
83 }
84
85 // no backface culling on terrain, to allow terrain to be viewed from underground
86 const QVector<Qt3DRender::QTechnique *> techniques = material->effect()->techniques();
87 for ( Qt3DRender::QTechnique *technique : techniques )
88 {
89 const QVector<Qt3DRender::QRenderPass *> passes = technique->renderPasses();
90 for ( Qt3DRender::QRenderPass *pass : passes )
91 {
92 Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
93 cullFace->setMode( Qt3DRender::QCullFace::NoCulling );
94 pass->addRenderState( cullFace );
95 }
96 }
97
98 entity->addComponent( material ); // takes ownership if the component has no parent
99}
100
101Qt3DRender::QTexture2D *QgsTerrainTileLoader::createTexture( QgsTerrainTileEntity *entity )
102{
103 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
104 QgsTerrainTextureImage *textureImage = new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
105 texture->addTextureImage( textureImage );//texture take the ownership of textureImage if has no parant
106 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
107 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
108
109 entity->setTextureImage( textureImage );
110
111 return texture;
112}
113
114void QgsTerrainTileLoader::onTextureLoaded()
115{
116 emit finished();
117}
118
119void QgsTerrainTileLoader::onImageReady( int jobId, const QImage &image )
120{
121 if ( mTextureJobId == jobId )
122 {
123 mTextureImage = image;
124 mTextureJobId = -1;
125 onTextureLoaded();
126 }
127}
128
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)