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