-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsterrainentity_p.cpp
217 lines (170 loc) · 6.98 KB
/
qgsterrainentity_p.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/***************************************************************************
qgsterrainentity_p.cpp
--------------------------------------
Date : July 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsterrainentity_p.h"
#include "qgsaabb.h"
#include "qgs3dmapsettings.h"
#include "qgschunknode_p.h"
#include "qgsdemterraintilegeometry_p.h"
#include "qgseventtracing.h"
#include "qgsraycastingutils_p.h"
#include "qgsterraingenerator.h"
#include "qgsterraintexturegenerator_p.h"
#include "qgsterraintextureimage_p.h"
#include "qgsterraintileentity_p.h"
#include "qgscoordinatetransform.h"
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QGeometryRenderer>
#include <Qt3DRender/QObjectPicker>
///@cond PRIVATE
//! Factory for map update jobs
class TerrainMapUpdateJobFactory : public QgsChunkQueueJobFactory
{
public:
TerrainMapUpdateJobFactory( QgsTerrainTextureGenerator *textureGenerator )
: mTextureGenerator( textureGenerator )
{
}
QgsChunkQueueJob *createJob( QgsChunkNode *chunk ) override
{
return new TerrainMapUpdateJob( mTextureGenerator, chunk );
}
private:
QgsTerrainTextureGenerator *mTextureGenerator = nullptr;
};
// -----------
QgsTerrainEntity::QgsTerrainEntity( int maxLevel, const Qgs3DMapSettings &map, Qt3DCore::QNode *parent )
: QgsChunkedEntity( map.terrainGenerator()->rootChunkBbox( map ),
map.terrainGenerator()->rootChunkError( map ),
map.maxTerrainScreenError(), maxLevel, map.terrainGenerator(), parent )
, mMap( map )
{
map.terrainGenerator()->setTerrain( this );
mIsValid = map.terrainGenerator()->isValid();
connect( &map, &Qgs3DMapSettings::showTerrainBoundingBoxesChanged, this, &QgsTerrainEntity::onShowBoundingBoxesChanged );
connect( &map, &Qgs3DMapSettings::showTerrainTilesInfoChanged, this, &QgsTerrainEntity::invalidateMapImages );
connect( &map, &Qgs3DMapSettings::showLabelsChanged, this, &QgsTerrainEntity::invalidateMapImages );
connect( &map, &Qgs3DMapSettings::layersChanged, this, &QgsTerrainEntity::onLayersChanged );
connect( &map, &Qgs3DMapSettings::backgroundColorChanged, this, &QgsTerrainEntity::invalidateMapImages );
connect( &map, &Qgs3DMapSettings::terrainMapThemeChanged, this, &QgsTerrainEntity::invalidateMapImages );
connectToLayersRepaintRequest();
mTerrainToMapTransform = new QgsCoordinateTransform( map.terrainGenerator()->crs(), map.crs(), map.transformContext() );
mTextureGenerator = new QgsTerrainTextureGenerator( map );
mUpdateJobFactory.reset( new TerrainMapUpdateJobFactory( mTextureGenerator ) );
mTerrainPicker = new Qt3DRender::QObjectPicker;
// add camera control's terrain picker as a component to be able to capture height where mouse was
// pressed in order to correctly pan camera when dragging mouse
addComponent( mTerrainPicker );
}
QgsTerrainEntity::~QgsTerrainEntity()
{
// cancel / wait for jobs
cancelActiveJobs();
delete mTextureGenerator;
delete mTerrainToMapTransform;
}
bool QgsTerrainEntity::rayIntersection( const QgsRayCastingUtils::Ray3D &ray, QVector3D &intersectionPoint )
{
if ( !rootNode() )
return false;
if ( mMap.terrainGenerator()->type() != QgsTerrainGenerator::Dem )
return false; // currently only working with DEM terrain
float minDist = -1;
QList<QgsChunkNode *> lst = activeNodes();
for ( QgsChunkNode *n : lst )
{
if ( n->entity() && ( minDist < 0 || n->bbox().distanceFromPoint( ray.origin() ) < minDist ) && QgsRayCastingUtils::rayBoxIntersection( ray, n->bbox() ) )
{
Qt3DRender::QGeometryRenderer *rend = n->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
Qt3DRender::QGeometry *geom = rend->geometry();
DemTerrainTileGeometry *demGeom = static_cast<DemTerrainTileGeometry *>( geom );
Qt3DCore::QTransform *tr = n->entity()->findChild<Qt3DCore::QTransform *>();
QVector3D nodeIntPoint;
if ( demGeom->rayIntersection( ray, tr->matrix(), nodeIntPoint ) )
{
float dist = ( ray.origin() - intersectionPoint ).length();
if ( minDist < 0 || dist < minDist )
{
minDist = dist;
intersectionPoint = nodeIntPoint;
}
}
}
}
return minDist >= 0;
}
void QgsTerrainEntity::onShowBoundingBoxesChanged()
{
setShowBoundingBoxes( mMap.showTerrainBoundingBoxes() );
}
void QgsTerrainEntity::invalidateMapImages()
{
QgsEventTracing::addEvent( QgsEventTracing::Instant, QStringLiteral( "3D" ), QStringLiteral( "Invalidate textures" ) );
// handle active nodes
updateNodes( mActiveNodes, mUpdateJobFactory.get() );
// handle inactive nodes afterwards
QList<QgsChunkNode *> inactiveNodes;
Q_FOREACH ( QgsChunkNode *node, mRootNode->descendants() )
{
if ( !node->entity() )
continue;
if ( mActiveNodes.contains( node ) )
continue;
inactiveNodes << node;
}
updateNodes( inactiveNodes, mUpdateJobFactory.get() );
setNeedsUpdate( true );
}
void QgsTerrainEntity::onLayersChanged()
{
connectToLayersRepaintRequest();
invalidateMapImages();
}
void QgsTerrainEntity::connectToLayersRepaintRequest()
{
Q_FOREACH ( QgsMapLayer *layer, mLayers )
{
disconnect( layer, &QgsMapLayer::repaintRequested, this, &QgsTerrainEntity::invalidateMapImages );
}
mLayers = mMap.layers();
Q_FOREACH ( QgsMapLayer *layer, mLayers )
{
connect( layer, &QgsMapLayer::repaintRequested, this, &QgsTerrainEntity::invalidateMapImages );
}
}
// -----------
TerrainMapUpdateJob::TerrainMapUpdateJob( QgsTerrainTextureGenerator *textureGenerator, QgsChunkNode *node )
: QgsChunkQueueJob( node )
, mTextureGenerator( textureGenerator )
{
QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( node->entity() );
connect( textureGenerator, &QgsTerrainTextureGenerator::tileReady, this, &TerrainMapUpdateJob::onTileReady );
mJobId = textureGenerator->render( entity->textureImage()->imageExtent(), node->tileId(), entity->textureImage()->imageDebugText() );
}
void TerrainMapUpdateJob::cancel()
{
if ( mJobId != -1 )
mTextureGenerator->cancelJob( mJobId );
}
void TerrainMapUpdateJob::onTileReady( int jobId, const QImage &image )
{
if ( mJobId == jobId )
{
QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( mNode->entity() );
entity->textureImage()->setImage( image );
mJobId = -1;
emit finished();
}
}
/// @endcond