QGIS API Documentation  3.0.2-Girona (307d082)
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>
23 #include <Qt3DRender>
24 
25 class QDomDocument;
26 class QDomElement;
27 
28 class QgsVector3D;
29 
35 class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
36 {
37  Q_OBJECT
38  Q_PROPERTY( Qt3DRender::QCamera *camera READ camera WRITE setCamera NOTIFY cameraChanged )
39  Q_PROPERTY( QRect viewport READ viewport WRITE setViewport NOTIFY viewportChanged )
40  public:
42  QgsCameraController( Qt3DCore::QNode *parent = nullptr );
43 
45  Qt3DRender::QCamera *camera() const { return mCamera; }
47  QRect viewport() const { return mViewport; }
48 
53  void addTerrainPicker( Qt3DRender::QObjectPicker *picker );
55  void setCamera( Qt3DRender::QCamera *camera );
57  void setViewport( const QRect &viewport );
59  void frameTriggered( float dt );
60 
62  void resetView( float distance );
63 
65  void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
66 
68  QgsVector3D lookingAtPoint() const;
70  void setLookingAtPoint( const QgsVector3D &point );
71 
73  QDomElement writeXml( QDomDocument &doc ) const;
75  void readXml( const QDomElement &elem );
76 
77  private:
78  void setCameraData( float x, float y, float dist, float pitch = 0, float yaw = 0 );
79 
80  signals:
82  void cameraChanged();
84  void viewportChanged();
85 
86  private slots:
87  void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
88  void onPickerMousePressed( Qt3DRender::QPickEvent *pick );
89 
90  private:
92  Qt3DRender::QCamera *mCamera = nullptr;
94  QRect mViewport;
96  float mLastPressedHeight = 0;
97 
98  struct CameraData
99  {
100  float x = 0, y = 0; // ground point towards which the camera is looking
101  float dist = 40; // distance of camera from the point it is looking at
102  float pitch = 0; // aircraft nose up/down (0 = looking straight down to the plane). angle in degrees
103  float yaw = 0; // aircraft nose left/right. angle in degrees
104 
105  bool operator==( const CameraData &other ) const
106  {
107  return x == other.x && y == other.y && dist == other.dist && pitch == other.pitch && yaw == other.yaw;
108  }
109  bool operator!=( const CameraData &other ) const
110  {
111  return !operator==( other );
112  }
113 
114  void setCamera( Qt3DRender::QCamera *camera )
115  {
116  // basic scene setup:
117  // - x grows to the right
118  // - z grows to the bottom
119  // - y grows towards camera
120  // so a point on the plane (x',y') is transformed to (x,-z) in our 3D world
121  camera->setUpVector( QVector3D( 0, 0, -1 ) );
122  camera->setPosition( QVector3D( x, dist, y ) );
123  camera->setViewCenter( QVector3D( x, 0, y ) );
124  camera->rotateAboutViewCenter( QQuaternion::fromEulerAngles( pitch, yaw, 0 ) );
125  }
126 
127  };
128 
129  CameraData mCameraData;
130 
132  QPoint mMousePos;
134  QPoint mLastMousePos;
135 
137  Qt3DInput::QMouseDevice *mMouseDevice = nullptr;
138 
139  Qt3DInput::QKeyboardDevice *mKeyboardDevice = nullptr;
140 
141  Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
142 
147  Qt3DInput::QLogicalDevice *mLogicalDevice = nullptr;
148 
149  Qt3DInput::QAction *mLeftMouseButtonAction = nullptr;
150  Qt3DInput::QActionInput *mLeftMouseButtonInput = nullptr;
151 
152  Qt3DInput::QAction *mMiddleMouseButtonAction = nullptr;
153  Qt3DInput::QActionInput *mMiddleMouseButtonInput = nullptr;
154 
155  Qt3DInput::QAction *mRightMouseButtonAction = nullptr;
156  Qt3DInput::QActionInput *mRightMouseButtonInput = nullptr;
157 
158  Qt3DInput::QAction *mShiftAction = nullptr;
159  Qt3DInput::QActionInput *mShiftInput = nullptr;
160 
161  Qt3DInput::QAction *mCtrlAction = nullptr;
162  Qt3DInput::QActionInput *mCtrlInput = nullptr;
163 
164  Qt3DInput::QAxis *mWheelAxis = nullptr;
165  Qt3DInput::QAnalogAxisInput *mMouseWheelInput = nullptr;
166 
167  Qt3DInput::QAxis *mTxAxis = nullptr;
168  Qt3DInput::QAxis *mTyAxis = nullptr;
169  Qt3DInput::QButtonAxisInput *mKeyboardTxPosInput = nullptr;
170  Qt3DInput::QButtonAxisInput *mKeyboardTyPosInput = nullptr;
171  Qt3DInput::QButtonAxisInput *mKeyboardTxNegInput = nullptr;
172  Qt3DInput::QButtonAxisInput *mKeyboardTyNegInput = nullptr;
173 };
174 
175 #endif // QGSCAMERACONTROLLER_H
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
Definition: qgsvector3d.h:28
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QRect viewport() const
Returns viewport rectangle.
3 Object that controls camera movement based on user input