QGIS API Documentation 3.39.0-Master (47f7b3a4989)
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}
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
57#ifndef SIP_RUN
58class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
59{
60#else
61class _3D_EXPORT QgsCameraController : public QObject
62{
63#endif
64
65 Q_OBJECT
66 public:
67
71
72#ifndef SIP_RUN
73
78 Qt3DRender::QCamera *camera() const { return mCamera; }
79#endif
80
85 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
86
91 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
92
97 void setCameraMovementSpeed( double movementSpeed );
98
103 Qgis::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
104
109 void setVerticalAxisInversion( Qgis::VerticalAxisInversion inversion );
110
112 void frameTriggered( float dt );
113
115 void resetView( float distance );
116
118 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
119
121 QgsVector3D lookingAtPoint() const;
122
129 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
130
135 void setCameraPose( const QgsCameraPose &camPose );
136
141 QgsCameraPose cameraPose() const { return mCameraPose; }
142
148 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
149
155 float pitch() const { return mCameraPose.pitchAngle(); }
156
162 float yaw() const { return mCameraPose.headingAngle(); }
163
165 QDomElement writeXml( QDomDocument &doc ) const;
167 void readXml( const QDomElement &elem );
168
170 void zoom( float factor );
172 void tiltUpAroundViewCenter( float deltaPitch );
174 void rotateAroundViewCenter( float deltaYaw );
176 void setCameraHeadingAngle( float angle );
178 void moveView( float tx, float ty );
179
184 void walkView( double tx, double ty, double tz );
185
192 void rotateCamera( float diffPitch, float diffYaw );
193
199 bool willHandleKeyEvent( QKeyEvent *event );
200
201 public slots:
202
207 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
208
213 void depthBufferCaptured( const QImage &depthImage );
214
215 private:
216#ifdef SIP_RUN
219#endif
220
221 void updateCameraFromPose();
222 void moveCameraPositionBy( const QVector3D &posDiff );
224 QWindow *window() const;
225
227 enum class MouseOperation
228 {
229 None = 0, // no operation
230 Translation, // left button pressed, no modifier
231 RotationCamera, // left button pressed + ctrl modifier
232 RotationCenter, // left button pressed + shift modifier
233 Zoom, // right button pressed
234 ZoomWheel // mouse wheel scroll
235 };
236
237 // This list gathers all the rotation and translation operations.
238 // It is used to update the appropriate parameters when successive
239 // translation and rotation happen.
240 const QList<MouseOperation> mTranslateOrRotate =
241 {
242 MouseOperation::Translation,
243 MouseOperation::RotationCamera,
244 MouseOperation::RotationCenter
245 };
246
247 // check that current sequence (current operation and new operation) is a rotation or translation
248 bool isATranslationRotationSequence( MouseOperation newOperation ) const;
249
250 void setMouseParameters( const MouseOperation &newOperation, const QPoint &clickPoint = QPoint() );
251
252 signals:
255
258
262 void cameraMovementSpeedChanged( double speed );
263
268 void setCursorPosition( QPoint point );
269
275
280 void cameraRotationCenterChanged( QVector3D position );
281
282 private slots:
283 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
284 void onWheel( Qt3DInput::QWheelEvent *wheel );
285 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
286 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
287 void onKeyPressed( Qt3DInput::QKeyEvent *event );
288 void onKeyReleased( Qt3DInput::QKeyEvent *event );
289 void applyFlyModeKeyMovements();
290
291 private:
292 void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
293 void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
294 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
295 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
296
297 void handleTerrainNavigationWheelZoom();
298
303 double sampleDepthBuffer( const QImage &buffer, int px, int py );
304
305#ifndef SIP_RUN
307 bool screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *cameraBefore, double &depth, QVector3D &worldPosition );
308#endif
309
311 Qgs3DMapScene *mScene = nullptr;
312
314 Qt3DRender::QCamera *mCamera = nullptr;
315
317 QgsCameraPose mCameraPose;
318
320 QPoint mMousePos;
321
323 QPoint mClickPoint;
324
325 bool mDepthBufferIsReady = false;
326 QImage mDepthBufferImage;
327
328 std::unique_ptr< Qt3DRender::QCamera > mCameraBefore;
329
330 bool mRotationCenterCalculated = false;
331 QVector3D mRotationCenter;
332 double mRotationDistanceFromCenter;
333 double mRotationPitch = 0;
334 double mRotationYaw = 0;
335
336 bool mDragPointCalculated = false;
337 QVector3D mDragPoint;
338 double mDragDepth;
339
340 bool mZoomPointCalculated = false;
341 QVector3D mZoomPoint;
342
343 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
344 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
347 double mCameraMovementSpeed = 5.0;
348
349 QSet< int > mDepressedKeys;
350 bool mCaptureFpsMouseMovements = false;
351 bool mIgnoreNextMouseMove = false;
352 QTimer *mFpsNavTimer = nullptr;
353
354 double mCumulatedWheelY = 0;
355
356 MouseOperation mCurrentOperation = MouseOperation::None;
357
358 // To test the cameracontroller
359 friend class TestQgs3DRendering;
360 friend class TestQgs3DCameraController;
361};
362
363#endif // QGSCAMERACONTROLLER_H
VerticalAxisInversion
Vertical axis inversion options for 3D views.
Definition qgis.h:3593
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
NavigationMode
The navigation mode used by 3D cameras.
Definition qgis.h:3581
@ 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 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