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