Skip to content

Commit 0469d44

Browse files
committed
Initial work on integration of 3D map into QGIS application
One can create a 3D map view from menu similar to a new 3D map view
1 parent 8f76477 commit 0469d44

15 files changed

+396
-7
lines changed

src/3d/cameracontroller.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ void CameraController::frameTriggered( float dt )
217217
}
218218
}
219219

220+
void CameraController::resetView()
221+
{
222+
setCameraData( 0, 0, 1000 );
223+
224+
emit cameraChanged();
225+
}
226+
220227
void CameraController::onPositionChanged( Qt3DInput::QMouseEvent *mouse )
221228
{
222229
mMousePos = QPoint( mouse->x(), mouse->y() );

src/3d/cameracontroller.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class _3D_EXPORT CameraController : public Qt3DCore::QEntity
2727

2828
void frameTriggered( float dt );
2929

30+
//! Move camera back to the initial position (looking down towards origin of world's coordinates)
31+
void resetView();
32+
3033
signals:
3134
void cameraChanged();
3235
void viewportChanged();

src/3d/map3d.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ Map3D::Map3D()
8686
{
8787
}
8888

89+
Map3D::~Map3D()
90+
{
91+
}
92+
8993
void Map3D::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
9094
{
9195
Q_UNUSED( context );

src/3d/map3d.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class _3D_EXPORT Map3D
151151
{
152152
public:
153153
Map3D();
154+
~Map3D();
154155

155156
void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
156157

src/3d/scene.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Scene::Scene( const Map3D &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph,
4545
mCameraController = new CameraController( this ); // attaches to the scene
4646
mCameraController->setViewport( viewportRect );
4747
mCameraController->setCamera( camera );
48-
mCameraController->setCameraData( 0, 0, 1000 );
48+
mCameraController->resetView();
4949

5050
// create terrain entity
5151
mTerrain = new Terrain( 3, map );

src/3d/terrain/maptexturegenerator.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "maptexturegenerator.h"
22

3+
#include <qgsmaprenderercustompainterjob.h>
34
#include <qgsmaprenderersequentialjob.h>
45
#include <qgsmapsettings.h>
56
#include <qgsproject.h>
@@ -54,22 +55,26 @@ QImage MapTextureGenerator::renderSynchronously( const QgsRectangle &extent, con
5455
QgsMapSettings mapSettings( baseMapSettings() );
5556
mapSettings.setExtent( extent );
5657

57-
QgsMapRendererSequentialJob job( mapSettings );
58-
job.start();
59-
job.waitForFinished();
58+
QImage img = QImage( mapSettings.outputSize(), mapSettings.outputImageFormat() );
59+
img.setDotsPerMeterX( 1000 * mapSettings.outputDpi() / 25.4 );
60+
img.setDotsPerMeterY( 1000 * mapSettings.outputDpi() / 25.4 );
61+
img.fill( Qt::transparent );
6062

61-
QImage img = job.renderedImage();
63+
QPainter p( &img );
64+
65+
QgsMapRendererCustomPainterJob job( mapSettings, &p );
66+
job.renderSynchronously();
6267

6368
if ( !debugText.isEmpty() )
6469
{
6570
// extra tile information for debugging
66-
QPainter p( &img );
6771
p.setPen( Qt::white );
6872
p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
6973
p.drawText( img.rect(), debugText, QTextOption( Qt::AlignCenter ) );
70-
p.end();
7174
}
7275

76+
p.end();
77+
7378
return img;
7479
}
7580

src/app/3d/qgs3dmapcanvas.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "qgs3dmapcanvas.h"
2+
3+
#include <QBoxLayout>
4+
#include <Qt3DExtras/Qt3DWindow>
5+
6+
#include "cameracontroller.h"
7+
#include "map3d.h"
8+
#include "scene.h"
9+
10+
11+
Qgs3DMapCanvas::Qgs3DMapCanvas( QWidget *parent )
12+
: QWidget( parent )
13+
, mWindow3D( nullptr )
14+
, mContainer( nullptr )
15+
, mMap( nullptr )
16+
, mScene( nullptr )
17+
{
18+
mWindow3D = new Qt3DExtras::Qt3DWindow;
19+
mContainer = QWidget::createWindowContainer( mWindow3D );
20+
21+
QHBoxLayout *hLayout = new QHBoxLayout( this );
22+
hLayout->setMargin( 0 );
23+
hLayout->addWidget( mContainer, 1 );
24+
}
25+
26+
Qgs3DMapCanvas::~Qgs3DMapCanvas()
27+
{
28+
delete mMap;
29+
}
30+
31+
void Qgs3DMapCanvas::resizeEvent( QResizeEvent *ev )
32+
{
33+
QWidget::resizeEvent( ev );
34+
35+
QRect viewportRect( QPoint( 0, 0 ), size() );
36+
mScene->cameraController()->setViewport( viewportRect );
37+
}
38+
39+
void Qgs3DMapCanvas::setMap( Map3D *map )
40+
{
41+
QRect viewportRect( QPoint( 0, 0 ), size() );
42+
Scene *newScene = new Scene( *map, mWindow3D->defaultFrameGraph(), mWindow3D->renderSettings(), mWindow3D->camera(), viewportRect );
43+
44+
mWindow3D->setRootEntity( newScene );
45+
46+
if ( mScene )
47+
mScene->deleteLater();
48+
mScene = newScene;
49+
50+
delete mMap;
51+
mMap = map;
52+
}
53+
54+
void Qgs3DMapCanvas::resetView()
55+
{
56+
mScene->cameraController()->resetView();
57+
}

src/app/3d/qgs3dmapcanvas.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef QGS3DMAPWIDGET_H
2+
#define QGS3DMAPWIDGET_H
3+
4+
#include <QWidget>
5+
6+
namespace Qt3DExtras
7+
{
8+
class Qt3DWindow;
9+
}
10+
11+
class Map3D;
12+
class Scene;
13+
14+
15+
class Qgs3DMapCanvas : public QWidget
16+
{
17+
Q_OBJECT
18+
public:
19+
Qgs3DMapCanvas( QWidget *parent = nullptr );
20+
~Qgs3DMapCanvas();
21+
22+
//! Configure map scene being displayed. Takes ownership.
23+
void setMap( Map3D *map );
24+
25+
//! Resets camera position to the default: looking down at the origin of world coordinates
26+
void resetView();
27+
28+
protected:
29+
void resizeEvent( QResizeEvent *ev ) override;
30+
31+
private:
32+
//! 3D window with all the 3D magic inside
33+
Qt3DExtras::Qt3DWindow *mWindow3D;
34+
//! Container QWidget that encapsulates mWindow3D so we can use it embedded in ordinary widgets app
35+
QWidget *mContainer;
36+
//! Description of the 3D scene
37+
Map3D *mMap;
38+
//! Root entity of the 3D scene
39+
Scene *mScene;
40+
};
41+
42+
#endif // QGS3DMAPWIDGET_H
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "qgs3dmapcanvasdockwidget.h"
2+
3+
#include "qgs3dmapcanvas.h"
4+
5+
#include <QBoxLayout>
6+
#include <QToolBar>
7+
8+
Qgs3DMapCanvasDockWidget::Qgs3DMapCanvasDockWidget( QWidget *parent )
9+
: QgsDockWidget( parent )
10+
{
11+
QWidget *contentsWidget = new QWidget( this );
12+
13+
QToolBar *toolBar = new QToolBar( contentsWidget );
14+
toolBar->addAction( "Reset view", this, &Qgs3DMapCanvasDockWidget::resetView );
15+
16+
mCanvas = new Qgs3DMapCanvas( contentsWidget );
17+
mCanvas->setMinimumSize( QSize( 200, 200 ) );
18+
19+
QVBoxLayout *layout = new QVBoxLayout( contentsWidget );
20+
layout->setContentsMargins( 0, 0, 0, 0 );
21+
layout->setSpacing( 0 );
22+
layout->addWidget( toolBar );
23+
layout->addWidget( mCanvas, 1 );
24+
25+
setWidget( contentsWidget );
26+
}
27+
28+
void Qgs3DMapCanvasDockWidget::setMap( Map3D *map )
29+
{
30+
mCanvas->setMap( map );
31+
}
32+
33+
void Qgs3DMapCanvasDockWidget::resetView()
34+
{
35+
mCanvas->resetView();
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef QGS3DMAPCANVASDOCKWIDGET_H
2+
#define QGS3DMAPCANVASDOCKWIDGET_H
3+
4+
#include "qgsdockwidget.h"
5+
6+
class Qgs3DMapCanvas;
7+
8+
class Map3D;
9+
10+
11+
class Qgs3DMapCanvasDockWidget : public QgsDockWidget
12+
{
13+
Q_OBJECT
14+
public:
15+
Qgs3DMapCanvasDockWidget( QWidget *parent = nullptr );
16+
17+
//! takes ownership
18+
void setMap( Map3D *map );
19+
20+
private slots:
21+
void resetView();
22+
23+
private:
24+
Qgs3DMapCanvas *mCanvas;
25+
};
26+
27+
#endif // QGS3DMAPCANVASDOCKWIDGET_H

0 commit comments

Comments
 (0)