QGIS API Documentation 3.43.0-Master (b60ef06885e)
qgsoffscreen3dengine.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsoffscreen3dengine.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
17#include "moc_qgsoffscreen3dengine.cpp"
18
19#include "qgsframegraph.h"
20
21#include <QCoreApplication>
22#include <QOffscreenSurface>
23#include <QSurfaceFormat>
24#include <QOpenGLFunctions>
25
26#include <Qt3DCore/QAspectEngine>
27#include <Qt3DLogic/QLogicAspect>
28#include <Qt3DRender/QCamera>
29#include <Qt3DRender/QCameraSelector>
30#include <Qt3DRender/QClearBuffers>
31#include <Qt3DRender/QRenderAspect>
32#include <Qt3DRender/QRenderSettings>
33#include <Qt3DRender/QRenderTarget>
34#include <Qt3DRender/QRenderTargetOutput>
35#include <Qt3DRender/QRenderTargetSelector>
36#include <Qt3DRender/QRenderSurfaceSelector>
37#include <Qt3DRender/QTexture>
38#include <Qt3DRender/QViewport>
40#include "qgsshadowrenderview.h"
42
44{
45 // Set up a camera to point at the shapes.
46 mCamera = new Qt3DRender::QCamera;
47 mCamera->lens()->setPerspectiveProjection( 45.0f, float( mSize.width() ) / float( mSize.height() ), 0.1f, 1000.0f );
48 mCamera->setPosition( QVector3D( 0, 0, 20.0f ) );
49 mCamera->setUpVector( QVector3D( 0, 1, 0 ) );
50 mCamera->setViewCenter( QVector3D( 0, 0, 0 ) );
51
52 // Set up the engine and the aspects that we want to use.
53 mAspectEngine = new Qt3DCore::QAspectEngine();
54
55#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
56 mRenderAspect = new Qt3DRender::QRenderAspect( Qt3DRender::QRenderAspect::Threaded ); // Only threaded mode seems to work right now.
57#else
58 mRenderAspect = new Qt3DRender::QRenderAspect();
59#endif
60
61 mLogicAspect = new Qt3DLogic::QLogicAspect();
62
63 mAspectEngine->registerAspect( mRenderAspect );
64 mAspectEngine->registerAspect( mLogicAspect );
65
66 // Create the root entity of the engine.
67 // This is not the same as the 3D scene root: the QRenderSettings
68 // component must be held by the root of the QEntity tree,
69 // so it is added to this one. The 3D scene is added as a subtree later,
70 // in setRootEntity().
71 mRoot = new Qt3DCore::QEntity;
72 mRenderSettings = new Qt3DRender::QRenderSettings( mRoot );
73 mRoot->addComponent( mRenderSettings );
74
75 mCamera->setParent( mRoot );
76
77 // Create the offscreen frame graph, which will manage all of the resources required
78 // for rendering without a QWindow.
79 mOffscreenSurface = new QOffscreenSurface();
80
81 QSurfaceFormat format;
82
83 //Use default format when shared OpenGL context is enabled
84 if ( QCoreApplication::instance() && QCoreApplication::instance()->testAttribute( Qt::AA_ShareOpenGLContexts ) )
85 {
86 format = QSurfaceFormat::defaultFormat();
87 }
88 //Set the surface format when used outside of QGIS application
89 else
90 {
91 format.setRenderableType( QSurfaceFormat::OpenGL );
92#ifdef Q_OS_MAC
93 format.setVersion( 4, 1 ); //OpenGL is deprecated on MacOS, use last supported version
94 format.setProfile( QSurfaceFormat::CoreProfile );
95#else
96 format.setVersion( 4, 3 );
97 format.setProfile( QSurfaceFormat::CoreProfile );
98#endif
99 format.setDepthBufferSize( 24 );
100 format.setSamples( 4 );
101 format.setStencilBufferSize( 8 );
102 }
103
104 mOffscreenSurface->setFormat( format );
105 mOffscreenSurface->create();
106
107 mFrameGraph = new QgsFrameGraph( mOffscreenSurface, mSize, mCamera, mRoot );
110 // Set this frame graph to be in use.
111 // the render settings also sets itself as the parent of mSurfaceSelector
112 mRenderSettings->setActiveFrameGraph( mFrameGraph->frameGraphRoot() );
113
114 // Set the root entity of the engine. This causes the engine to begin running.
115 mAspectEngine->setRootEntity( Qt3DCore::QEntityPtr( mRoot ) );
116}
117
119{
120 delete mAspectEngine;
121 delete mOffscreenSurface;
122}
123
125{
126 mSize = s;
127
128 mFrameGraph->setSize( mSize );
129 mCamera->setAspectRatio( float( mSize.width() ) / float( mSize.height() ) );
130 emit sizeChanged();
131}
132
133void QgsOffscreen3DEngine::setClearColor( const QColor &color )
134{
135 mFrameGraph->setClearColor( color );
136}
137
142
143void QgsOffscreen3DEngine::setRootEntity( Qt3DCore::QEntity *root )
144{
145 // Make sure any existing scene root is unparented.
146 if ( mSceneRoot )
147 {
148 mSceneRoot->setParent( static_cast<Qt3DCore::QNode *>( nullptr ) );
149 }
150
151 // Parent the incoming scene root to our current root entity.
152 mSceneRoot = root;
153 mSceneRoot->setParent( mRoot );
154 root->addComponent( mFrameGraph->forwardRenderView().renderLayer() );
155 root->addComponent( mFrameGraph->shadowRenderView().entityCastingShadowsLayer() );
156}
157
158Qt3DRender::QRenderSettings *QgsOffscreen3DEngine::renderSettings()
159{
160 return mRenderSettings;
161}
162
163Qt3DRender::QCamera *QgsOffscreen3DEngine::camera()
164{
165 return mCamera;
166}
167
169{
170 return mSize;
171}
172
174{
175 return mOffscreenSurface;
176}
void sizeChanged()
Emitted after a call to setSize()
QgsFrameGraph * mFrameGraph
Qt3DRender::QLayer * renderLayer()
Returns a layer object used to indicate that the object is transparent.
Container class that holds different objects related to frame graphs of 3D scenes.
void setRenderCaptureEnabled(bool enabled)
Sets whether it will be possible to render to an image.
Qt3DRender::QFrameGraphNode * frameGraphRoot()
Returns the root of the frame graph object.
void setClearColor(const QColor &clearColor)
Sets the clear color of the scene (background color)
void setFrustumCullingEnabled(bool enabled)
Sets whether frustum culling is enabled.
QgsForwardRenderView & forwardRenderView()
Returns forward renderview.
QgsShadowRenderView & shadowRenderView()
Returns shadow renderview.
void setSize(QSize s)
Sets the size of the buffers used for rendering.
void setSize(QSize s) override
Sets the size of the rendering area (in pixels)
void setClearColor(const QColor &color) override
Sets background color of the scene.
QSize size() const override
Returns size of the engine's rendering area in pixels.
void setRootEntity(Qt3DCore::QEntity *root) override
Sets root entity of the 3D scene.
void setFrustumCullingEnabled(bool enabled) override
Sets whether frustum culling is enabled (this should make rendering faster by not rendering entities ...
Qt3DRender::QCamera * camera() override
Returns pointer to the engine's camera entity.
Qt3DRender::QRenderSettings * renderSettings() override
Returns access to the engine's render settings (the frame graph can be accessed from here)
QSurface * surface() const override
Returns the surface of the engine.
Qt3DRender::QLayer * entityCastingShadowsLayer() const
Returns the layer to be used by entities to be included in this renderview.
virtual void setEnabled(bool enable) override
Enable or disable via enable the renderview sub tree.