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