QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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;
51class QgsCameraController4Test;
52
58#ifndef SIP_RUN
59class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
60{
61#else
62class _3D_EXPORT QgsCameraController : public QObject
63{
64#endif
65
66 Q_OBJECT
67 public:
68
72
73#ifndef SIP_RUN
74
79 Qt3DRender::QCamera *camera() const { return mCamera; }
80#endif
81
86 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
87
92 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
93
98 void setCameraMovementSpeed( double movementSpeed );
99
104 Qgis::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
105
110 void setVerticalAxisInversion( Qgis::VerticalAxisInversion inversion );
111
113 void frameTriggered( float dt );
114
116 void resetView( float distance );
117
119 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
120
122 QgsVector3D lookingAtPoint() const;
123
130 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
131
136 void setCameraPose( const QgsCameraPose &camPose );
137
142 QgsCameraPose cameraPose() const { return mCameraPose; }
143
149 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
150
156 float pitch() const { return mCameraPose.pitchAngle(); }
157
163 float yaw() const { return mCameraPose.headingAngle(); }
164
166 QDomElement writeXml( QDomDocument &doc ) const;
168 void readXml( const QDomElement &elem );
169
171 void zoom( float factor );
173 void tiltUpAroundViewCenter( float deltaPitch );
175 void rotateAroundViewCenter( float deltaYaw );
177 void setCameraHeadingAngle( float angle );
179 void moveView( float tx, float ty );
180
185 void walkView( double tx, double ty, double tz );
186
193 void rotateCamera( float diffPitch, float diffYaw );
194
200 bool willHandleKeyEvent( QKeyEvent *event );
201
202 public slots:
203
208 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
209
214 void depthBufferCaptured( const QImage &depthImage );
215
216 private:
217#ifdef SIP_RUN
220#endif
221
222 void updateCameraFromPose();
223 void moveCameraPositionBy( const QVector3D &posDiff );
225 QWindow *window() const;
226
228 enum class MouseOperation
229 {
230 None = 0, // no operation
231 Translation, // left button pressed, no modifier
232 RotationCamera, // left button pressed + ctrl modifier
233 RotationCenter, // left button pressed + shift modifier
234 Zoom, // right button pressed
235 ZoomWheel // mouse wheel scroll
236 };
237
238 // This list gathers all the rotation and translation operations.
239 // It is used to update the appropriate parameters when successive
240 // translation and rotation happen.
241 const QList<MouseOperation> mTranslateOrRotate =
242 {
243 MouseOperation::Translation,
244 MouseOperation::RotationCamera,
245 MouseOperation::RotationCenter
246 };
247
248 // check that current sequence (current operation and new operation) is a rotation or translation
249 bool isATranslationRotationSequence( MouseOperation newOperation ) const;
250
251 void setMouseParameters( const MouseOperation &newOperation, const QPoint &clickPoint = QPoint() );
252
253 signals:
256
259
263 void cameraMovementSpeedChanged( double speed );
264
269 void setCursorPosition( QPoint point );
270
276
281 void cameraRotationCenterChanged( QVector3D position );
282
283 private slots:
284 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
285 void onWheel( Qt3DInput::QWheelEvent *wheel );
286 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
287 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
288 void onKeyPressed( Qt3DInput::QKeyEvent *event );
289 void onKeyReleased( Qt3DInput::QKeyEvent *event );
290 void applyFlyModeKeyMovements();
291
292 private:
293 void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
294 void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
295 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
296 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
297
298 void handleTerrainNavigationWheelZoom();
299
304 double sampleDepthBuffer( const QImage &buffer, int px, int py );
305
306#ifndef SIP_RUN
308 bool screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *cameraBefore, double &depth, QVector3D &worldPosition );
309#endif
310
312 Qgs3DMapScene *mScene = nullptr;
313
315 Qt3DRender::QCamera *mCamera = nullptr;
316
318 QgsCameraPose mCameraPose;
319
321 QPoint mMousePos;
322
324 QPoint mClickPoint;
325
326 bool mDepthBufferIsReady = false;
327 QImage mDepthBufferImage;
328
329 std::unique_ptr< Qt3DRender::QCamera > mCameraBefore;
330
331 bool mRotationCenterCalculated = false;
332 QVector3D mRotationCenter;
333 double mRotationDistanceFromCenter;
334 double mRotationPitch = 0;
335 double mRotationYaw = 0;
336
337 bool mDragPointCalculated = false;
338 QVector3D mDragPoint;
339 double mDragDepth;
340
341 bool mZoomPointCalculated = false;
342 QVector3D mZoomPoint;
343
344 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
345 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
348 double mCameraMovementSpeed = 5.0;
349
350 QSet< int > mDepressedKeys;
351 bool mCaptureFpsMouseMovements = false;
352 bool mIgnoreNextMouseMove = false;
353 QTimer *mFpsNavTimer = nullptr;
354
355 double mCumulatedWheelY = 0;
356
357 MouseOperation mCurrentOperation = MouseOperation::None;
358
359 friend QgsCameraController4Test;
360};
361
362#endif // QGSCAMERACONTROLLER_H
VerticalAxisInversion
Vertical axis inversion options for 3D views.
Definition qgis.h:3360
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
NavigationMode
The navigation mode used by 3D cameras.
Definition qgis.h:3348
@ 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