QGIS API Documentation 3.41.0-Master (88383c3d16f)
Loading...
Searching...
No Matches
qgscameracontroller.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscameracontroller.h
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 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#ifndef QGSCAMERACONTROLLER_H
17#define QGSCAMERACONTROLLER_H
18
19#include "qgis_3d.h"
20
21#include <Qt3DCore/QEntity>
22#include <Qt3DInput/QMouseEvent>
23#include <QImage>
24
25#ifndef SIP_RUN
26namespace Qt3DInput
27{
28 class QKeyEvent;
29 class QKeyboardHandler;
30 class QMouseEvent;
31 class QMouseHandler;
32 class QWheelEvent;
33} // namespace Qt3DInput
34
35namespace Qt3DRender
36{
37 class QCamera;
38}
39
40#endif
41
42#include "qgscamerapose.h"
43
44class QDomDocument;
45class QDomElement;
46
47class QgsCameraPose;
48class QgsVector3D;
50class Qgs3DMapScene;
51
56#ifndef SIP_RUN
57class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
58{
59#else
60class _3D_EXPORT QgsCameraController : public QObject
61{
62#endif
63
64 Q_OBJECT
65 public:
69
70#ifndef SIP_RUN
71
76 Qt3DRender::QCamera *camera() const { return mCamera; }
77#endif
78
83 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
84
89 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
90
95 void setCameraMovementSpeed( double movementSpeed );
96
101 Qgis::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
102
107 void setVerticalAxisInversion( Qgis::VerticalAxisInversion inversion );
108
110 void frameTriggered( float dt );
111
113 void resetView( float distance );
114
116 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
117
119 QgsVector3D lookingAtPoint() const;
120
127 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
128
133 void setCameraPose( const QgsCameraPose &camPose );
134
139 QgsCameraPose cameraPose() const { return mCameraPose; }
140
146 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
147
153 float pitch() const { return mCameraPose.pitchAngle(); }
154
160 float yaw() const { return mCameraPose.headingAngle(); }
161
163 QDomElement writeXml( QDomDocument &doc ) const;
165 void readXml( const QDomElement &elem );
166
168 void zoom( float factor );
170 void tiltUpAroundViewCenter( float deltaPitch );
172 void rotateAroundViewCenter( float deltaYaw );
174 void setCameraHeadingAngle( float angle );
176 void moveView( float tx, float ty );
177
182 void walkView( double tx, double ty, double tz );
183
190 void rotateCamera( float diffPitch, float diffYaw );
191
197 void rotateCameraAroundPivot( float newPitch, float newHeading, const QVector3D &pivotPoint );
198
205 void zoomCameraAroundPivot( const QVector3D &oldCameraPosition, double zoomFactor, const QVector3D &pivotPoint );
206
212 bool willHandleKeyEvent( QKeyEvent *event );
213
220 void setOrigin( const QgsVector3D &origin );
221
226 void setInputHandlersEnabled( bool enable ) { mInputHandlersEnabled = enable; }
227
228 public slots:
229
234 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
235
240 void depthBufferCaptured( const QImage &depthImage );
241
242 private:
243#ifdef SIP_RUN
246#endif
247
248 void updateCameraFromPose();
249 void moveCameraPositionBy( const QVector3D &posDiff );
251 QWindow *window() const;
252
254 enum class MouseOperation
255 {
256 None = 0, // no operation
257 Translation, // left button pressed, no modifier
258 RotationCamera, // left button pressed + ctrl modifier
259 RotationCenter, // left button pressed + shift modifier
260 Zoom, // right button pressed
261 ZoomWheel // mouse wheel scroll
262 };
263
264 // This list gathers all the rotation and translation operations.
265 // It is used to update the appropriate parameters when successive
266 // translation and rotation happen.
267 const QList<MouseOperation> mTranslateOrRotate = {
268 MouseOperation::Translation,
269 MouseOperation::RotationCamera,
270 MouseOperation::RotationCenter
271 };
272
273 // check that current sequence (current operation and new operation) is a rotation or translation
274 bool isATranslationRotationSequence( MouseOperation newOperation ) const;
275
276 void setMouseParameters( const MouseOperation &newOperation, const QPoint &clickPoint = QPoint() );
277
278 signals:
281
284
288 void cameraMovementSpeedChanged( double speed );
289
294 void setCursorPosition( QPoint point );
295
301
306 void cameraRotationCenterChanged( QVector3D position );
307
308 private slots:
309 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
310 void onWheel( Qt3DInput::QWheelEvent *wheel );
311 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
312 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
313 void onKeyPressed( Qt3DInput::QKeyEvent *event );
314 void onKeyReleased( Qt3DInput::QKeyEvent *event );
315 void applyFlyModeKeyMovements();
316
317 private:
318 void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
319 void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
320 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
321 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
322
323 void handleTerrainNavigationWheelZoom();
324
329 double sampleDepthBuffer( int px, int py );
330
331#ifndef SIP_RUN
333 bool screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *cameraBefore, double &depth, QVector3D &worldPosition );
334#endif
335
337 Qgs3DMapScene *mScene = nullptr;
338
340 Qt3DRender::QCamera *mCamera = nullptr;
341
343 QgsCameraPose mCameraPose;
344
346 QPoint mMousePos;
347
349 QPoint mClickPoint;
350
351 bool mDepthBufferIsReady = false;
352 QImage mDepthBufferImage;
353 // -1 when unset
354 // TODO: Change to std::optional<double>
355 double mDepthBufferNonVoidAverage = -1;
356
357 std::unique_ptr<Qt3DRender::QCamera> mCameraBefore;
358
359 bool mRotationCenterCalculated = false;
360 QVector3D mRotationCenter;
361 double mRotationDistanceFromCenter = 0;
362 double mRotationPitch = 0;
363 double mRotationYaw = 0;
364
365 bool mDragPointCalculated = false;
366 QVector3D mDragPoint;
367 double mDragDepth;
368
369 bool mZoomPointCalculated = false;
370 QVector3D mZoomPoint;
371
372 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
373 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
374 bool mInputHandlersEnabled = true;
377 double mCameraMovementSpeed = 5.0;
378
379 QSet<int> mDepressedKeys;
380 bool mCaptureFpsMouseMovements = false;
381 bool mIgnoreNextMouseMove = false;
382 QTimer *mFpsNavTimer = nullptr;
383
384 double mCumulatedWheelY = 0;
385
386 MouseOperation mCurrentOperation = MouseOperation::None;
387
388 // 3D world's origin in map coordinates
389 QgsVector3D mOrigin;
390
392 bool mCameraChanged = false;
393
394 // To test the cameracontroller
395 friend class TestQgs3DRendering;
396 friend class TestQgs3DCameraController;
397};
398
399#endif // QGSCAMERACONTROLLER_H
VerticalAxisInversion
Vertical axis inversion options for 3D views.
Definition qgis.h:3967
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
NavigationMode
The navigation mode used by 3D cameras.
Definition qgis.h:3955
@ TerrainBased
The default navigation based on the terrain.
void navigationModeChanged(Qgis::NavigationMode mode)
Emitted when the navigation mode is changed using the hotkey ctrl + ~.
QgsCameraPose cameraPose() const
Returns camera pose.
float pitch() const
Returns pitch angle in degrees (0 = looking from the top, 90 = looking from the side).
Qt3DRender::QCamera * camera() const
Returns camera that is being controlled.
~QgsCameraController() override
float yaw() const
Returns yaw angle in degrees.
void requestDepthBufferCapture()
Emitted to ask for the depth buffer image.
Qgis::VerticalAxisInversion verticalAxisInversion() const
Returns the vertical axis inversion behavior.
float distance() const
Returns distance of the camera from the point it is looking at.
double cameraMovementSpeed() const
Returns the camera movement speed.
void cameraChanged()
Emitted when camera has been updated.
void cameraMovementSpeedChanged(double speed)
Emitted whenever the camera movement speed is changed by the controller.
Qgis::NavigationMode cameraNavigationMode() const
Returns the navigation mode used by the camera controller.
void cameraRotationCenterChanged(QVector3D position)
Emitted when the camera rotation center changes.
void setInputHandlersEnabled(bool enable)
Sets whether the camera controller responds to mouse and keyboard events.
void setCursorPosition(QPoint point)
Emitted when the mouse cursor position should be moved to the specified point on the map viewport.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
#define SIP_SKIP
Definition qgis_sip.h:126