Skip to content

Commit 6448000

Browse files
committed
Add 3D engine base class + window & offscreen implementations
1 parent 02ddffe commit 6448000

11 files changed

+541
-35
lines changed

src/3d/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
SET(QGIS_3D_SRCS
55
qgsaabb.cpp
6+
qgsabstract3dengine.cpp
67
qgs3dmapscene.cpp
78
qgs3dmapsettings.cpp
89
qgs3dutils.cpp
910
qgscameracontroller.cpp
11+
qgsoffscreen3dengine.cpp
1012
qgsphongmaterialsettings.cpp
1113
qgsraycastingutils_p.cpp
1214
qgstessellatedpolygongeometry.cpp
1315
qgstilingscheme.cpp
1416
qgsvectorlayer3drenderer.cpp
17+
qgswindow3dengine.cpp
1518

1619
chunks/qgschunkboundsentity_p.cpp
1720
chunks/qgschunkedentity_p.cpp
@@ -47,7 +50,10 @@ SET(QGIS_3D_MOC_HDRS
4750

4851
qgs3dmapscene.h
4952
qgs3dmapsettings.h
53+
qgsabstract3dengine.h
5054
qgscameracontroller.h
55+
qgsoffscreen3dengine.h
56+
qgswindow3dengine.h
5157

5258
chunks/qgschunkedentity_p.h
5359
chunks/qgschunkloader_p.h
@@ -70,15 +76,18 @@ QT5_ADD_RESOURCES(QGIS_3D_RCC_SRCS shaders.qrc)
7076

7177
SET(QGIS_3D_HDRS
7278
qgsaabb.h
79+
qgsabstract3dengine.h
7380
qgs3dmapscene.h
7481
qgs3dmapsettings.h
7582
qgs3dutils.h
7683
qgscameracontroller.h
84+
qgsoffscreen3dengine.h
7785
qgsphongmaterialsettings.h
7886
qgsraycastingutils_p.h
7987
qgstessellatedpolygongeometry.h
8088
qgstilingscheme.h
8189
qgsvectorlayer3drenderer.h
90+
qgswindow3dengine.h
8291

8392
chunks/qgschunkboundsentity_p.h
8493
chunks/qgschunkedentity_p.h

src/3d/qgs3dmapscene.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QTimer>
3030

3131
#include "qgsaabb.h"
32+
#include "qgsabstract3dengine.h"
3233
#include "qgs3dmapsettings.h"
3334
#include "qgs3dutils.h"
3435
#include "qgsabstract3drenderer.h"
@@ -41,27 +42,29 @@
4142
#include "qgsvectorlayer3drenderer.h"
4243

4344

44-
Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph, Qt3DRender::QRenderSettings *renderSettings, Qt3DRender::QCamera *camera, const QRect &viewportRect, Qt3DCore::QNode *parent )
45-
: Qt3DCore::QEntity( parent )
45+
Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, QgsAbstract3DEngine *engine )
46+
: Qt3DCore::QEntity()
4647
, mMap( map )
47-
, mForwardRenderer( defaultFrameGraph )
48+
, mEngine( engine )
4849
{
4950

5051
connect( &map, &Qgs3DMapSettings::backgroundColorChanged, this, &Qgs3DMapScene::onBackgroundColorChanged );
5152
onBackgroundColorChanged();
5253

5354
// TODO: strange - setting OnDemand render policy still keeps QGIS busy (Qt 5.9.0)
5455
// actually it is more busy than with the default "Always" policy although there are no changes in the scene.
55-
//renderSettings->setRenderPolicy( Qt3DRender::QRenderSettings::OnDemand );
56+
//mRenderer->renderSettings()->setRenderPolicy( Qt3DRender::QRenderSettings::OnDemand );
5657

5758
#if QT_VERSION >= 0x050900
5859
// we want precise picking of terrain (also bounding volume picking does not seem to work - not sure why)
59-
renderSettings->pickingSettings()->setPickMethod( Qt3DRender::QPickingSettings::TrianglePicking );
60+
mEngine->renderSettings()->pickingSettings()->setPickMethod( Qt3DRender::QPickingSettings::TrianglePicking );
6061
#endif
6162

63+
QRect viewportRect( QPoint( 0, 0 ), mEngine->size() );
64+
6265
// Camera
6366
float aspectRatio = ( float )viewportRect.width() / viewportRect.height();
64-
camera->lens()->setPerspectiveProjection( 45.0f, aspectRatio, 10.f, 10000.0f );
67+
mEngine->camera()->lens()->setPerspectiveProjection( 45.0f, aspectRatio, 10.f, 10000.0f );
6568

6669
mFrameAction = new Qt3DLogic::QFrameAction();
6770
connect( mFrameAction, &Qt3DLogic::QFrameAction::triggered,
@@ -71,10 +74,10 @@ Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, Qt3DExtras::QForwardR
7174
// Camera controlling
7275
mCameraController = new QgsCameraController( this ); // attaches to the scene
7376
mCameraController->setViewport( viewportRect );
74-
mCameraController->setCamera( camera );
77+
mCameraController->setCamera( mEngine->camera() );
7578
mCameraController->resetView( 1000 );
7679

77-
addCameraViewCenterEntity( camera );
80+
addCameraViewCenterEntity( mEngine->camera() );
7881

7982
// create terrain entity
8083

@@ -156,7 +159,7 @@ Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, Qt3DExtras::QForwardR
156159
// docs say frustum culling must be disabled for skybox.
157160
// it _somehow_ works even when frustum culling is enabled with some camera positions,
158161
// but then when zoomed in more it would disappear - so let's keep frustum culling disabled
159-
defaultFrameGraph->setFrustumCullingEnabled( false );
162+
mEngine->setFrustumCullingEnabled( false );
160163
}
161164

162165
// force initial update of chunked entities
@@ -333,7 +336,7 @@ void Qgs3DMapScene::createTerrainDeferred()
333336

334337
void Qgs3DMapScene::onBackgroundColorChanged()
335338
{
336-
mForwardRenderer->setClearColor( mMap.backgroundColor() );
339+
mEngine->setClearColor( mMap.backgroundColor() );
337340
}
338341

339342
void Qgs3DMapScene::onLayerRenderer3DChanged()

src/3d/qgs3dmapscene.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace Qt3DExtras
3636
class QForwardRenderer;
3737
}
3838

39+
class QgsAbstract3DEngine;
3940
class QgsMapLayer;
4041
class QgsCameraController;
4142
class Qgs3DMapSettings;
@@ -52,7 +53,7 @@ class _3D_EXPORT Qgs3DMapScene : public Qt3DCore::QEntity
5253
Q_OBJECT
5354
public:
5455
//! Constructs a 3D scene based on map settings and Qt 3D renderer configuration
55-
Qgs3DMapScene( const Qgs3DMapSettings &map, Qt3DExtras::QForwardRenderer *defaultFrameGraph, Qt3DRender::QRenderSettings *renderSettings, Qt3DRender::QCamera *camera, const QRect &viewportRect, Qt3DCore::QNode *parent = nullptr );
56+
Qgs3DMapScene( const Qgs3DMapSettings &map, QgsAbstract3DEngine *engine );
5657

5758
//! Returns camera controller
5859
QgsCameraController *cameraController() { return mCameraController; }
@@ -87,12 +88,11 @@ class _3D_EXPORT Qgs3DMapScene : public Qt3DCore::QEntity
8788

8889
private:
8990
const Qgs3DMapSettings &mMap;
91+
QgsAbstract3DEngine *mEngine = nullptr;
9092
//! Provides a way to have a synchronous function executed each frame
9193
Qt3DLogic::QFrameAction *mFrameAction = nullptr;
9294
QgsCameraController *mCameraController = nullptr;
9395
QgsTerrainEntity *mTerrain = nullptr;
94-
//! Forward renderer provided by 3D window
95-
Qt3DExtras::QForwardRenderer *mForwardRenderer = nullptr;
9696
QList<QgsChunkedEntity *> mChunkEntities;
9797
//! Entity that shows view center - useful for debugging camera issues
9898
Qt3DCore::QEntity *mEntityCameraViewCenter = nullptr;

src/3d/qgsabstract3dengine.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/***************************************************************************
2+
qgsabstract3dengine.cpp
3+
--------------------------------------
4+
Date : July 2018
5+
Copyright : (C) 2018 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+
16+
#include "qgsabstract3dengine.h"

src/3d/qgsabstract3dengine.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef QGSABSTRACT3DENGINE_H
2+
#define QGSABSTRACT3DENGINE_H
3+
4+
#include "qgis_3d.h"
5+
6+
#include <QObject>
7+
8+
class QColor;
9+
class QRect;
10+
11+
namespace Qt3DCore
12+
{
13+
class QEntity;
14+
}
15+
16+
namespace Qt3DRender
17+
{
18+
class QRenderSettings;
19+
class QCamera;
20+
}
21+
22+
/**
23+
* Base class for 3D engine implementation. A 3D engine is responsible for setting up
24+
* rendering with Qt3D. This means mainly:
25+
* - creating Qt3D aspect engine and registering rendering aspect
26+
* - setting up a camera, render settings and frame graph
27+
*
28+
* We have two implementations:
29+
* - QgsWindow3DEngine - used for rendering on display (has a QWindow that can be embedded into QWidget)
30+
* - QgsOffscreen3DEngine - renders scene to images
31+
*
32+
* \since QGIS 3.4
33+
*/
34+
class _3D_EXPORT QgsAbstract3DEngine : public QObject
35+
{
36+
Q_OBJECT
37+
public:
38+
39+
virtual void setClearColor( const QColor &color ) = 0;
40+
virtual void setFrustumCullingEnabled( bool enabled ) = 0;
41+
virtual void setRootEntity( Qt3DCore::QEntity *root ) = 0;
42+
43+
virtual Qt3DRender::QRenderSettings *renderSettings() = 0;
44+
virtual Qt3DRender::QCamera *camera() = 0;
45+
virtual QSize size() const = 0;
46+
47+
signals:
48+
void imageCaptured( const QImage &image );
49+
};
50+
51+
52+
#endif // QGSABSTRACT3DENGINE_H

0 commit comments

Comments
 (0)