24#include <Qt3DRender/QCamera>
25#include <Qt3DRender/QCameraSelector>
26#include <Qt3DRender/QClearBuffers>
27#include <Qt3DRender/QLayer>
28#include <Qt3DRender/QLayerFilter>
29#include <Qt3DRender/QSortPolicy>
30#include <Qt3DRender/QViewport>
31#include <Qt3DRender/qsubtreeenabler.h>
38 , mMapSettings( settings )
42 mViewport->setObjectName(
mViewName +
"::Viewport" );
44 mObjectLayer =
new Qt3DRender::QLayer;
45 mObjectLayer->setObjectName(
mViewName +
"::ObjectLayer" );
46 mObjectLayer->setRecursive(
true );
48 mLabelLayer =
new Qt3DRender::QLayer;
49 mLabelLayer->setObjectName(
mViewName +
"::LabelLayer" );
50 mLabelLayer->setRecursive(
true );
53 Qt3DRender::QLayerFilter *objectFilter =
new Qt3DRender::QLayerFilter( mViewport );
54 objectFilter->addLayer( mObjectLayer );
56 mObjectCamera =
new Qt3DRender::QCamera;
57 mObjectCamera->setObjectName(
mViewName +
"::ObjectCamera" );
58 mObjectCamera->setProjectionType( cameraCtrl->
camera()->projectionType() );
59 mObjectCamera->lens()->setFieldOfView( cameraCtrl->
camera()->lens()->fieldOfView() * 0.5f );
61 Qt3DRender::QCameraSelector *cameraSelector =
new Qt3DRender::QCameraSelector( objectFilter );
62 cameraSelector->setCamera( mObjectCamera );
66 Qt3DRender::QSortPolicy *objectSortPolicy =
new Qt3DRender::QSortPolicy( cameraSelector );
67 QVector<Qt3DRender::QSortPolicy::SortType> objectSortTypes = QVector<Qt3DRender::QSortPolicy::SortType>();
68 objectSortTypes << Qt3DRender::QSortPolicy::BackToFront;
69 objectSortPolicy->setSortTypes( objectSortTypes );
71 Qt3DRender::QClearBuffers *objectClearBuffers =
new Qt3DRender::QClearBuffers( objectSortPolicy );
72 objectClearBuffers->setBuffers( Qt3DRender::QClearBuffers::DepthBuffer );
74 Qt3DRender::QLayerFilter *labelFilter =
new Qt3DRender::QLayerFilter( mViewport );
75 labelFilter->addLayer( mLabelLayer );
77 mLabelCamera =
new Qt3DRender::QCamera;
78 mLabelCamera->setObjectName(
mViewName +
"::LabelCamera" );
79 mLabelCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
81 Qt3DRender::QCameraSelector *labelCameraSelector =
new Qt3DRender::QCameraSelector( labelFilter );
82 labelCameraSelector->setCamera( mLabelCamera );
86 Qt3DRender::QSortPolicy *labelSortPolicy =
new Qt3DRender::QSortPolicy( labelCameraSelector );
87 QVector<Qt3DRender::QSortPolicy::SortType> labelSortTypes = QVector<Qt3DRender::QSortPolicy::SortType>();
88 labelSortTypes << Qt3DRender::QSortPolicy::BackToFront;
89 labelSortPolicy->setSortTypes( labelSortTypes );
91 Qt3DRender::QClearBuffers *clearBuffers =
new Qt3DRender::QClearBuffers( labelSortPolicy );
92 clearBuffers->setBuffers( Qt3DRender::QClearBuffers::DepthBuffer );
115 return mObjectCamera;
132 double windowWidth =
static_cast<double>( width < 0 ? mCanvas->width() : width );
133 double windowHeight =
static_cast<double>( height < 0 ? mCanvas->height() : height );
138 QgsDebugMsgLevel( QString(
"onViewportSizeUpdate window w/h: %1px / %2px" ).arg( windowWidth ).arg( windowHeight ), 2 );
139 QgsDebugMsgLevel( QString(
"onViewportSizeUpdate window physicalDpi %1 (%2, %3)" ).arg( mCanvas->screen()->physicalDotsPerInch() ).arg( mCanvas->screen()->physicalDotsPerInchX() ).arg( mCanvas->screen()->physicalDotsPerInchY() ), 2 );
140 QgsDebugMsgLevel( QString(
"onViewportSizeUpdate window logicalDotsPerInch %1 (%2, %3)" ).arg( mCanvas->screen()->logicalDotsPerInch() ).arg( mCanvas->screen()->logicalDotsPerInchX() ).arg( mCanvas->screen()->logicalDotsPerInchY() ), 2 );
142 QgsDebugMsgLevel( QString(
"onViewportSizeUpdate window pixel ratio %1" ).arg( mCanvas->screen()->devicePixelRatio() ), 2 );
150 double defaultViewportPixelSize = ( ( double ) settings.
defaultViewportSize() / 25.4 ) * 92.0;
154 double viewportPixelSize = defaultViewportPixelSize + ( ( double ) settings.
defaultViewportSize() / 25.4 ) * ( mCanvas->screen()->physicalDotsPerInch() - 92.0 ) * 0.7;
155 QgsDebugMsgLevel( QString(
"onViewportSizeUpdate viewportPixelSize %1" ).arg( viewportPixelSize ), 2 );
156 double widthRatio = viewportPixelSize / windowWidth;
157 double heightRatio = widthRatio * windowWidth / windowHeight;
159 QgsDebugMsgLevel( QString(
"3DAxis viewport ratios width: %1% / height: %2%" ).arg( widthRatio * 100.0 ).arg( heightRatio * 100.0 ), 2 );
161 if ( heightRatio * windowHeight < viewportPixelSize )
163 heightRatio = viewportPixelSize / windowHeight;
164 widthRatio = heightRatio * windowHeight / windowWidth;
165 QgsDebugMsgLevel( QString(
"3DAxis viewport, height too small, ratios adjusted to width: %1% / height: %2%" ).arg( widthRatio * 100.0 ).arg( heightRatio * 100.0 ), 2 );
170 QgsDebugMsgLevel( QString(
"3DAxis viewport takes too much place into the 3d view, disabling it (maxViewportRatio: %1)." ).arg( settings.
maxViewportRatio() ), 2 );
172 mViewport->setEnabled(
false );
173 m3DAxis->onViewportScaleFactorChanged( 0.0 );
177 if ( !mViewport->isEnabled() )
180 m3DAxis->onViewportScaleFactorChanged( viewportPixelSize / defaultViewportPixelSize );
182 mViewport->setEnabled(
true );
189 xRatio = 0.5f -
static_cast<float>( widthRatio ) / 2.0f;
191 xRatio = 1.0f -
static_cast<float>( widthRatio );
195 else if ( settings.
verticalPosition() == Qt::AnchorPoint::AnchorVerticalCenter )
196 yRatio = 0.5f -
static_cast<float>( heightRatio ) / 2.0f;
198 yRatio = 1.0f -
static_cast<float>( heightRatio );
200 QgsDebugMsgLevel( QString(
"Qgs3DAxis: update viewport: %1 x %2 x %3 x %4" ).arg( xRatio ).arg( yRatio ).arg( widthRatio ).arg( heightRatio ), 2 );
201 mViewport->setNormalizedRect( QRectF( xRatio, yRatio, widthRatio, heightRatio ) );
205 const float halfWidthSize =
static_cast<float>( windowWidth * widthRatio / 2.0 );
206 const float halfHeightSize =
static_cast<float>( windowWidth * widthRatio / 2.0 );
207 mLabelCamera->lens()->setOrthographicProjection(
208 -halfWidthSize, halfWidthSize,
209 -halfHeightSize, halfHeightSize,
210 mLabelCamera->lens()->nearPlane(), mLabelCamera->lens()->farPlane()
220 mMapSettings->set3DAxisSettings( axisSettings );
228 mMapSettings->set3DAxisSettings( axisSettings );
Qt3DRender::QLayer * labelLayer() const
Returns the layer to be used by entities to be included in the label renderpass.
void onVerticalPositionChanged(Qt::AnchorPoint position)
Updates viewport vertical position.
Qt3DRender::QCamera * labelCamera() const
Returns camera used for billboarded labels.
Qt3DRender::QLayer * objectLayer() const
Returns main object layer.
Qgs3DAxisRenderView(const QString &viewName, Qgs3DMapCanvas *canvas, QgsCameraController *cameraCtrl, Qgs3DMapSettings *settings, Qgs3DAxis *axis3D)
Constructor for Qgs3DAxisRenderView with the specified parent object.
Qt3DRender::QViewport * viewport() const
Returns the viewport associated to this renderview.
void onViewportSizeUpdate(int width=-1, int height=-1)
Updates viewport size. Uses canvas size by default.
Qt3DRender::QCamera * objectCamera() const
Returns main object camera (used for axis or cube).
void updateWindowResize(int width, int height) override
Called when 3D window is resized.
void onHorizontalPositionChanged(Qt::AnchorPoint position)
Updates viewport horizontal position.
Contains the configuration of a 3d axis.
double maxViewportRatio() const
Returns the maximal axis viewport ratio (see Qt3DRender::QViewport::normalizedRect()).
@ Crs
Respect CRS directions.
Qt::AnchorPoint verticalPosition() const
Returns the vertical position for the 3d axis.
void setHorizontalPosition(Qt::AnchorPoint position)
Sets the horizontal position for the 3d axis.
int defaultViewportSize() const
Returns the default axis viewport size in millimeters.
Qgs3DAxisSettings::Mode mode() const
Returns the type of the 3daxis.
Qt::AnchorPoint horizontalPosition() const
Returns the horizontal position for the 3d axis.
void setVerticalPosition(Qt::AnchorPoint position)
Sets the vertical position for the 3d axis.
Display 3D ortho axis in the main 3D view.
Convenience wrapper to simplify the creation of a 3D window ready to be used with QGIS.
Qt3DRender::QSubtreeEnabler * mRendererEnabler
QgsAbstractRenderView(const QString &viewName)
Constructor for QgsAbstractRenderView with the specified parent object.
Object that controls camera movement based on user input.
Qt3DRender::QCamera * camera() const
Returns camera that is being controlled.
static int debugLevel()
Reads the environment variable QGIS_DEBUG and converts it to int.
Contains configuration for rendering maps.
double dpiTarget() const
Returns the target DPI (dots per inch) to be taken into consideration when rendering.
float devicePixelRatio() const
Returns the device pixel ratio.
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
#define QgsDebugMsgLevel(str, level)