QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
19#include "qgsframegraph.h"
20#include "qgsshadowrenderview.h"
21
22#include <QCoreApplication>
23#include <QOffscreenSurface>
24#include <QOpenGLFunctions>
25#include <QSurfaceFormat>
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/QRenderSurfaceSelector>
34#include <Qt3DRender/QRenderTarget>
35#include <Qt3DRender/QRenderTargetOutput>
36#include <Qt3DRender/QRenderTargetSelector>
37#include <Qt3DRender/QTexture>
38#include <Qt3DRender/QViewport>
39
40#include "moc_qgsoffscreen3dengine.cpp"
41
43{
44 // Set up a camera to point at the shapes.
45 mCamera = new Qt3DRender::QCamera;
46 mCamera->lens()->setPerspectiveProjection( 45.0f, float( mSize.width() ) / float( mSize.height() ), 0.1f, 1000.0f );
47 mCamera->setPosition( QVector3D( 0, 0, 20.0f ) );
48 mCamera->setUpVector( QVector3D( 0, 1, 0 ) );
49 mCamera->setViewCenter( QVector3D( 0, 0, 0 ) );
50
51 // Set up the engine and the aspects that we want to use.
52 mAspectEngine = new Qt3DCore::QAspectEngine();
53
54 mRenderAspect = new Qt3DRender::QRenderAspect();
55
56 mLogicAspect = new Qt3DLogic::QLogicAspect();
57
58 mAspectEngine->registerAspect( mRenderAspect );
59 mAspectEngine->registerAspect( mLogicAspect );
60
61 // Create the root entity of the engine.
62 // This is not the same as the 3D scene root: the QRenderSettings
63 // component must be held by the root of the QEntity tree,
64 // so it is added to this one. The 3D scene is added as a subtree later,
65 // in setRootEntity().
66 mRoot = new Qt3DCore::QEntity;
67 mRenderSettings = new Qt3DRender::QRenderSettings( mRoot );
68 mRoot->addComponent( mRenderSettings );
69
70 mCamera->setParent( mRoot );
71
72 // Create the offscreen frame graph, which will manage all of the resources required
73 // for rendering without a QWindow.
74 mOffscreenSurface = new QOffscreenSurface();
75
76 QSurfaceFormat format;
77
78 //Use default format when shared OpenGL context is enabled
79 if ( QCoreApplication::instance() && QCoreApplication::instance()->testAttribute( Qt::AA_ShareOpenGLContexts ) )
80 {
81 format = QSurfaceFormat::defaultFormat();
82 }
83 //Set the surface format when used outside of QGIS application
84 else
85 {
86 format.setRenderableType( QSurfaceFormat::OpenGL );
87#ifdef Q_OS_MAC
88 format.setVersion( 4, 1 ); //OpenGL is deprecated on MacOS, use last supported version
89 format.setProfile( QSurfaceFormat::CoreProfile );
90#else
91 format.setVersion( 4, 3 );
92 format.setProfile( QSurfaceFormat::CoreProfile );
93#endif
94 format.setDepthBufferSize( 24 );
95 format.setSamples( 4 );
96 format.setStencilBufferSize( 8 );
97 }
98
99 mOffscreenSurface->setFormat( format );
100 mOffscreenSurface->create();
101
102 mFrameGraph = new QgsFrameGraph( mOffscreenSurface, mSize, mCamera, mRoot );
103 mFrameGraph->shadowRenderView().setEnabled( false );
104 // Set this frame graph to be in use.
105 // the render settings also sets itself as the parent of mSurfaceSelector
106 mRenderSettings->setActiveFrameGraph( mFrameGraph->frameGraphRoot() );
107
108 // Set the root entity of the engine. This causes the engine to begin running.
109 mAspectEngine->setRootEntity( Qt3DCore::QEntityPtr( mRoot ) );
110}
111
113{
114 delete mAspectEngine;
115 delete mOffscreenSurface;
116}
117
119{
120 mSize = s;
121
122 mFrameGraph->setSize( mSize );
123 mCamera->setAspectRatio( float( mSize.width() ) / float( mSize.height() ) );
124 emit sizeChanged();
125}
126
127void QgsOffscreen3DEngine::setClearColor( const QColor &color )
128{
129 mFrameGraph->setClearColor( color );
130}
131
133{
134 mFrameGraph->setFrustumCullingEnabled( enabled );
135}
136
137void QgsOffscreen3DEngine::setRootEntity( Qt3DCore::QEntity *root )
138{
139 // Make sure any existing scene root is unparented.
140 if ( mSceneRoot )
141 {
142 mSceneRoot->setParent( static_cast<Qt3DCore::QNode *>( nullptr ) );
143 }
144
145 // Parent the incoming scene root to our current root entity.
146 mSceneRoot = root;
147 mSceneRoot->setParent( mRoot );
148 root->addComponent( mFrameGraph->forwardRenderView().renderLayer() );
149 root->addComponent( mFrameGraph->shadowRenderView().entityCastingShadowsLayer() );
150}
151
152Qt3DRender::QRenderSettings *QgsOffscreen3DEngine::renderSettings()
153{
154 return mRenderSettings;
155}
156
157Qt3DRender::QCamera *QgsOffscreen3DEngine::camera()
158{
159 return mCamera;
160}
161
163{
164 return mSize;
165}
166
168{
169 return mOffscreenSurface;
170}
void sizeChanged()
Emitted after a call to setSize().
QgsFrameGraph * mFrameGraph
Container class that holds different objects related to frame graphs of 3D scenes.
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.