QGIS API Documentation 4.1.0-Master (01362494303)
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 )
59{
60 Qt3DRender::QTexture2D *texture = useTexture || !isShadingEnabled ? createTexture( entity ) : nullptr;
61
62 QgsMaterial *material = nullptr;
63 if ( texture )
64 {
65 if ( isShadingEnabled )
66 {
67 QgsPhongTexturedMaterial *phongTexturedMaterial = new QgsPhongTexturedMaterial();
68 phongTexturedMaterial->setAmbient( shadingMaterial.ambient() );
69 phongTexturedMaterial->setSpecular( shadingMaterial.specular() );
70 phongTexturedMaterial->setShininess( static_cast<float>( shadingMaterial.shininess() ) );
71 phongTexturedMaterial->setDiffuseTexture( texture );
72 phongTexturedMaterial->setOpacity( static_cast<float>( shadingMaterial.opacity() ) );
73 material = phongTexturedMaterial;
74 }
75 else
76 {
77 QgsTextureMaterial *textureMaterial = new QgsTextureMaterial;
78 textureMaterial->setTexture( texture );
79 material = textureMaterial;
80 }
81 }
82 else
83 {
84 QgsMaterialContext materialContext;
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 )
106{
107 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
108 QgsTerrainTextureImage *textureImage = new QgsTerrainTextureImage( mTextureImage, mExtentMapCrs, mTileDebugText );
109 texture->addTextureImage( textureImage ); //texture take the ownership of textureImage if has no parant
110 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
111 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
112 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
113
114 entity->setTextureImage( textureImage );
115
116 return texture;
117}
118
119void QgsTerrainTileLoader::onTextureLoaded()
120{
121 emit finished();
122}
123
124void QgsTerrainTileLoader::onImageReady( int jobId, const QImage &image )
125{
126 if ( mTextureJobId == jobId )
127 {
128 mTextureImage = image;
129 mTextureJobId = -1;
130 onTextureLoaded();
131 }
132}
133
@ 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...
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:141
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: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.