QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
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:
66
70
71#ifndef SIP_RUN
72
77 Qt3DRender::QCamera *camera() const { return mCamera; }
78#endif
79
84 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
85
90 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
91
96 void setCameraMovementSpeed( double movementSpeed );
97
102 Qgis::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
103
108 void setVerticalAxisInversion( Qgis::VerticalAxisInversion inversion );
109
111 void frameTriggered( float dt );
112
114 void resetView( float distance );
115
117 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
118
120 QgsVector3D lookingAtPoint() const;
121
128 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
129
134 void setCameraPose( const QgsCameraPose &camPose );
135
140 QgsCameraPose cameraPose() const { return mCameraPose; }
141
147 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
148
154 float pitch() const { return mCameraPose.pitchAngle(); }
155
161 float yaw() const { return mCameraPose.headingAngle(); }
162
164 QDomElement writeXml( QDomDocument &doc ) const;
166 void readXml( const QDomElement &elem );
167
169 void zoom( float factor );
171 void tiltUpAroundViewCenter( float deltaPitch );
173 void rotateAroundViewCenter( float deltaYaw );
175 void setCameraHeadingAngle( float angle );
177 void moveView( float tx, float ty );
178
183 void walkView( double tx, double ty, double tz );
184
191 void rotateCamera( float diffPitch, float diffYaw );
192
198 bool willHandleKeyEvent( QKeyEvent *event );
199
200 public slots:
201
206 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
207
212 void depthBufferCaptured( const QImage &depthImage );
213
214 private:
215#ifdef SIP_RUN
218#endif
219
220 void updateCameraFromPose();
221 void moveCameraPositionBy( const QVector3D &posDiff );
223 QWindow *window() const;
224
226 enum class MouseOperation
227 {
228 None = 0, // no operation
229 Translation, // left button pressed, no modifier
230 RotationCamera, // left button pressed + ctrl modifier
231 RotationCenter, // left button pressed + shift modifier
232 Zoom, // right button pressed
233 ZoomWheel // mouse wheel scroll
234 };
235
236 // This list gathers all the rotation and translation operations.
237 // It is used to update the appropriate parameters when successive
238 // translation and rotation happen.
239 const QList<MouseOperation> mTranslateOrRotate =
240 {
241 MouseOperation::Translation,
242 MouseOperation::RotationCamera,
243 MouseOperation::RotationCenter
244 };
245
246 // check that current sequence (current operation and new operation) is a rotation or translation
247 bool isATranslationRotationSequence( MouseOperation newOperation ) const;
248
249 void setMouseParameters( const MouseOperation &newOperation, const QPoint &clickPoint = QPoint() );
250
251 signals:
254
257
261 void cameraMovementSpeedChanged( double speed );
262
267 void setCursorPosition( QPoint point );
268
274
279 void cameraRotationCenterChanged( QVector3D position );
280
281 private slots:
282 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
283 void onWheel( Qt3DInput::QWheelEvent *wheel );
284 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
285 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
286 void onKeyPressed( Qt3DInput::QKeyEvent *event );
287 void onKeyReleased( Qt3DInput::QKeyEvent *event );
288 void applyFlyModeKeyMovements();
289
290 private:
291 void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
292 void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
293 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
294 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
295
296 void handleTerrainNavigationWheelZoom();
297
302 double sampleDepthBuffer( const QImage &buffer, int px, int py );
303
304#ifndef SIP_RUN
306 bool screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *cameraBefore, double &depth, QVector3D &worldPosition );
307#endif
308
310 Qgs3DMapScene *mScene = nullptr;
311
313 Qt3DRender::QCamera *mCamera = nullptr;
314
316 QgsCameraPose mCameraPose;
317
319 QPoint mMousePos;
320
322 QPoint mClickPoint;
323
324 bool mDepthBufferIsReady = false;
325 QImage mDepthBufferImage;
326
327 std::unique_ptr< Qt3DRender::QCamera > mCameraBefore;
328
329 bool mRotationCenterCalculated = false;
330 QVector3D mRotationCenter;
331 double mRotationDistanceFromCenter;
332 double mRotationPitch = 0;
333 double mRotationYaw = 0;
334
335 bool mDragPointCalculated = false;
336 QVector3D mDragPoint;
337 double mDragDepth;
338
339 bool mZoomPointCalculated = false;
340 QVector3D mZoomPoint;
341
342 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
343 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
346 double mCameraMovementSpeed = 5.0;
347
348 QSet< int > mDepressedKeys;
349 bool mCaptureFpsMouseMovements = false;
350 bool mIgnoreNextMouseMove = false;
351 QTimer *mFpsNavTimer = nullptr;
352
353 double mCumulatedWheelY = 0;
354
355 MouseOperation mCurrentOperation = MouseOperation::None;
356
357 // To test the cameracontroller
358 friend class TestQgs3DRendering;
359 friend class TestQgs3DCameraController;
360};
361
362#endif // QGSCAMERACONTROLLER_H
VerticalAxisInversion
Vertical axis inversion options for 3D views.
Definition qgis.h:3884
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
NavigationMode
The navigation mode used by 3D cameras.
Definition qgis.h:3872
@ 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