| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qfirstpersoncameracontroller.h" |
| 5 | |
| 6 | #include <Qt3DRender/QCamera> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | namespace Qt3DExtras { |
| 11 | |
| 12 | /*! |
| 13 | \class Qt3DExtras::QFirstPersonCameraController |
| 14 | \ingroup qt3d-extras-cameracontrollers |
| 15 | \brief The QFirstPersonCameraController class allows controlling the scene camera |
| 16 | from the first person perspective. |
| 17 | \inmodule Qt3DExtras |
| 18 | \since 5.7 |
| 19 | \inherits Qt3DCore::QEntity |
| 20 | |
| 21 | The controls are: |
| 22 | \table |
| 23 | \header |
| 24 | \li Input |
| 25 | \li Action |
| 26 | \row |
| 27 | \li Left mouse button |
| 28 | \li While the left mouse button is pressed, mouse movement along x-axis pans the camera and |
| 29 | movement along y-axis tilts it. |
| 30 | \row |
| 31 | \li Mouse scroll wheel |
| 32 | \li Zooms the camera in and out without changing the view center. |
| 33 | \row |
| 34 | \li Shift key |
| 35 | \li Turns the fine motion control active while pressed. Makes mouse pan and tilt less |
| 36 | sensitive. |
| 37 | \row |
| 38 | \li Arrow keys |
| 39 | \li Move the camera horizontally relative to camera viewport. |
| 40 | \row |
| 41 | \li Page up and page down keys |
| 42 | \li Move the camera vertically relative to camera viewport. |
| 43 | \row |
| 44 | \li Escape |
| 45 | \li Moves the camera so that entire scene is visible in the camera viewport. |
| 46 | \endtable |
| 47 | */ |
| 48 | |
| 49 | QFirstPersonCameraController::QFirstPersonCameraController(Qt3DCore::QNode *parent) |
| 50 | : QAbstractCameraController(parent) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | QFirstPersonCameraController::~QFirstPersonCameraController() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | |
| 59 | void QFirstPersonCameraController::moveCamera(const QAbstractCameraController::InputState &state, float dt) |
| 60 | { |
| 61 | Qt3DRender::QCamera *theCamera = camera(); |
| 62 | |
| 63 | if (theCamera == nullptr) |
| 64 | return; |
| 65 | |
| 66 | theCamera->translate(vLocal: QVector3D(state.txAxisValue * linearSpeed(), |
| 67 | state.tyAxisValue * linearSpeed(), |
| 68 | state.tzAxisValue * linearSpeed()) * dt); |
| 69 | if (state.leftMouseButtonActive) { |
| 70 | float theLookSpeed = lookSpeed(); |
| 71 | if (state.shiftKeyActive) { |
| 72 | theLookSpeed *= 0.2f; |
| 73 | } |
| 74 | |
| 75 | const QVector3D upVector(0.0f, 1.0f, 0.0f); |
| 76 | |
| 77 | theCamera->pan(angle: state.rxAxisValue * theLookSpeed * dt, axis: upVector); |
| 78 | theCamera->tilt(angle: state.ryAxisValue * theLookSpeed * dt); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | } // Qt3DExtras |
| 83 | |
| 84 | QT_END_NAMESPACE |
| 85 | |
| 86 | #include "moc_qfirstpersoncameracontroller.cpp" |
| 87 |
