diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/extras/defaults/defaults.pri | 12 | ||||
-rw-r--r-- | src/extras/defaults/qfirstpersoncameracontroller.cpp | 252 | ||||
-rw-r--r-- | src/extras/defaults/qfirstpersoncameracontroller.h | 100 | ||||
-rw-r--r-- | src/extras/defaults/qfirstpersoncameracontroller_p.h | 133 | ||||
-rw-r--r-- | src/extras/defaults/qorbitcontrol.cpp | 320 | ||||
-rw-r--r-- | src/extras/defaults/qorbitcontrol.h | 91 | ||||
-rw-r--r-- | src/extras/defaults/qorbitcontrol_p.h | 135 | ||||
-rw-r--r-- | src/extras/defaults/qt3dwindow.cpp | 179 | ||||
-rw-r--r-- | src/extras/defaults/qt3dwindow.h | 142 | ||||
-rw-r--r-- | src/extras/extras.pro | 2 | ||||
-rw-r--r-- | src/quick3d/imports/extras/defaults/defaults.pri | 6 | ||||
-rw-r--r-- | src/quick3d/imports/extras/defaults/qml/OrbitController.qml | 235 | ||||
-rw-r--r-- | src/quick3d/imports/extras/qt3dquick3dextrasplugin.cpp | 5 | ||||
-rw-r--r-- | src/quick3d/quick3dextras/qt3dquickextras_global.h | 59 | ||||
-rw-r--r-- | src/quick3d/quick3dextras/qt3dquickwindow.cpp | 254 | ||||
-rw-r--r-- | src/quick3d/quick3dextras/qt3dquickwindow.h | 140 | ||||
-rw-r--r-- | src/quick3d/quick3dextras/quick3dextras.pro | 26 | ||||
-rw-r--r-- | src/src.pro | 7 |
18 files changed, 2091 insertions, 7 deletions
diff --git a/src/extras/defaults/defaults.pri b/src/extras/defaults/defaults.pri index 1560f55ec..6ff23403b 100644 --- a/src/extras/defaults/defaults.pri +++ b/src/extras/defaults/defaults.pri @@ -22,7 +22,12 @@ HEADERS += \ $$PWD/qgoochmaterial.h \ $$PWD/qgoochmaterial_p.h \ $$PWD/qphongalphamaterial.h \ - $$PWD/qphongalphamaterial_p.h + $$PWD/qphongalphamaterial_p.h \ + $$PWD/qt3dwindow.h \ + $$PWD/qorbitcontrol.h \ + $$PWD/qorbitcontrol_p.h \ + $$PWD/qfirstpersoncameracontroller.h \ + $$PWD/qfirstpersoncameracontroller_p.h SOURCES += \ $$PWD/qphongmaterial.cpp \ @@ -35,5 +40,8 @@ SOURCES += \ $$PWD/qpervertexcolormaterial.cpp \ $$PWD/qskyboxentity.cpp \ $$PWD/qgoochmaterial.cpp \ - $$PWD/qphongalphamaterial.cpp + $$PWD/qphongalphamaterial.cpp \ + $$PWD/qt3dwindow.cpp \ + $$PWD/qorbitcontrol.cpp \ + $$PWD/qfirstpersoncameracontroller.cpp diff --git a/src/extras/defaults/qfirstpersoncameracontroller.cpp b/src/extras/defaults/qfirstpersoncameracontroller.cpp new file mode 100644 index 000000000..57cf3ba88 --- /dev/null +++ b/src/extras/defaults/qfirstpersoncameracontroller.cpp @@ -0,0 +1,252 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qfirstpersoncameracontroller_p.h" +#include "qfirstpersoncameracontroller.h" +#include <Qt3DRender/QCamera> +#include <Qt3DInput/QAxis> +#include <Qt3DInput/QAxisInput> +#include <Qt3DInput/QAction> +#include <Qt3DInput/QActionInput> +#include <Qt3DInput/QLogicalDevice> +#include <Qt3DInput/QKeyboardDevice> +#include <Qt3DInput/QMouseDevice> +#include <Qt3DInput/QMouseEvent> +#include <Qt3DLogic/QFrameAction> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +QFirstPersonCameraControllerPrivate::QFirstPersonCameraControllerPrivate() + : Qt3DCore::QEntityPrivate() + , m_camera(Q_NULLPTR) + , m_leftMouseButtonAction(new Qt3DInput::QAction()) + , m_fineMotionAction(new Qt3DInput::QAction()) + , m_rxAxis(new Qt3DInput::QAxis()) + , m_ryAxis(new Qt3DInput::QAxis()) + , m_txAxis(new Qt3DInput::QAxis()) + , m_tyAxis(new Qt3DInput::QAxis()) + , m_tzAxis(new Qt3DInput::QAxis()) + , m_leftMouseButtonInput(new Qt3DInput::QActionInput()) + , m_fineMotionKeyInput(new Qt3DInput::QActionInput()) + , m_mouseRxInput(new Qt3DInput::QAxisInput()) + , m_mouseRyInput(new Qt3DInput::QAxisInput()) + , m_keyboardTxPosInput(new Qt3DInput::QAxisInput()) + , m_keyboardTyPosInput(new Qt3DInput::QAxisInput()) + , m_keyboardTzPosInput(new Qt3DInput::QAxisInput()) + , m_keyboardTxNegInput(new Qt3DInput::QAxisInput()) + , m_keyboardTyNegInput(new Qt3DInput::QAxisInput()) + , m_keyboardTzNegInput(new Qt3DInput::QAxisInput()) + , m_keyboardDevice(new Qt3DInput::QKeyboardDevice()) + , m_mouseDevice(new Qt3DInput::QMouseDevice()) + , m_logicalDevice(new Qt3DInput::QLogicalDevice()) + , m_frameAction(new Qt3DLogic::QFrameAction()) + , m_linearSpeed(10.0f) + , m_lookSpeed(180.0f) + , m_firstPersonUp(QVector3D(0.0f, 1.0f, 0.0f)) +{} + +void QFirstPersonCameraControllerPrivate::init() +{ + //// Actions + + // Left Mouse Button Action + m_leftMouseButtonInput->setButtons(QVariantList() << Qt::LeftButton); + m_leftMouseButtonInput->setSourceDevice(m_mouseDevice); + m_leftMouseButtonAction->addInput(m_leftMouseButtonInput); + + // Fine Motion Action + m_fineMotionKeyInput->setButtons(QVariantList() << Qt::Key_Shift); + m_fineMotionKeyInput->setSourceDevice(m_keyboardDevice); + m_fineMotionAction->addInput(m_fineMotionKeyInput); + + //// Axes + + // Mouse X + m_mouseRxInput->setAxis(Qt3DInput::QMouseDevice::X); + m_mouseRxInput->setSourceDevice(m_mouseDevice); + m_rxAxis->addInput(m_mouseRxInput); + + // Mouse Y + m_mouseRyInput->setAxis(Qt3DInput::QMouseDevice::Y); + m_mouseRyInput->setSourceDevice(m_mouseDevice); + m_ryAxis->addInput(m_mouseRyInput); + + // Keyboard Pos Tx + m_keyboardTxPosInput->setButtons(QVariantList() << Qt::Key_Right); + m_keyboardTxPosInput->setScale(1.0f); + m_keyboardTxPosInput->setSourceDevice(m_keyboardDevice); + m_txAxis->addInput(m_keyboardTxPosInput); + + // Keyboard Pos Ty + m_keyboardTyPosInput->setButtons(QVariantList() << Qt::Key_PageUp); + m_keyboardTyPosInput->setScale(1.0f); + m_keyboardTyPosInput->setSourceDevice(m_keyboardDevice); + m_tyAxis->addInput(m_keyboardTyPosInput); + + // Keyboard Pos Tz + m_keyboardTzPosInput->setButtons(QVariantList() << Qt::Key_Up); + m_keyboardTzPosInput->setScale(1.0f); + m_keyboardTzPosInput->setSourceDevice(m_keyboardDevice); + m_tzAxis->addInput(m_keyboardTzPosInput); + + // Keyboard Neg Tx + m_keyboardTxNegInput->setButtons(QVariantList() << Qt::Key_Left); + m_keyboardTxNegInput->setScale(-1.0f); + m_keyboardTxNegInput->setSourceDevice(m_keyboardDevice); + m_txAxis->addInput(m_keyboardTxNegInput); + + // Keyboard Neg Ty + m_keyboardTyNegInput->setButtons(QVariantList() << Qt::Key_PageDown); + m_keyboardTyNegInput->setScale(-1.0f); + m_keyboardTyNegInput->setSourceDevice(m_keyboardDevice); + m_tyAxis->addInput(m_keyboardTyNegInput); + + // Keyboard Neg Tz + m_keyboardTzNegInput->setButtons(QVariantList() << Qt::Key_Down); + m_keyboardTzNegInput->setScale(-1.0f); + m_keyboardTzNegInput->setSourceDevice(m_keyboardDevice); + m_tzAxis->addInput(m_keyboardTzNegInput); + + //// Logical Device + + m_logicalDevice->addAction(m_fineMotionAction); + m_logicalDevice->addAction(m_leftMouseButtonAction); + m_logicalDevice->addAxis(m_rxAxis); + m_logicalDevice->addAxis(m_ryAxis); + m_logicalDevice->addAxis(m_txAxis); + m_logicalDevice->addAxis(m_tyAxis); + m_logicalDevice->addAxis(m_tzAxis); + + Q_Q(QFirstPersonCameraController); + //// FrameAction + + QObject::connect(m_frameAction, SIGNAL(triggered(float)), + q, SLOT(_q_onTriggered(float))); + + q->addComponent(m_frameAction); + q->addComponent(m_logicalDevice); +} + +void QFirstPersonCameraControllerPrivate::_q_onTriggered(float dt) +{ + if (m_camera != Q_NULLPTR) { + m_camera->translate(QVector3D(m_txAxis->value() * m_linearSpeed, + m_tyAxis->value() * m_linearSpeed, + m_tzAxis->value() * m_linearSpeed) * dt); + if (m_leftMouseButtonAction->isActive()) { + m_camera->pan(m_rxAxis->value() * m_lookSpeed * dt, m_firstPersonUp); + m_camera->tilt(m_ryAxis->value() * m_lookSpeed * dt); + } + } +} + +QFirstPersonCameraController::QFirstPersonCameraController(Qt3DCore::QNode *parent) + : Qt3DCore::QEntity(*new QFirstPersonCameraControllerPrivate, parent) +{ + Q_D(QFirstPersonCameraController); + d->init(); +} + +QFirstPersonCameraController::~QFirstPersonCameraController() +{ +} + +Qt3DRender::QCamera *QFirstPersonCameraController::camera() const +{ + Q_D(const QFirstPersonCameraController); + return d->m_camera; +} + +float QFirstPersonCameraController::linearSpeed() const +{ + Q_D(const QFirstPersonCameraController); + return d->m_linearSpeed; +} + +float QFirstPersonCameraController::lookSpeed() const +{ + Q_D(const QFirstPersonCameraController); + return d->m_lookSpeed; +} + +void QFirstPersonCameraController::setCamera(Qt3DRender::QCamera *camera) +{ + Q_D(QFirstPersonCameraController); + if (d->m_camera != camera) { + d->m_camera = camera; + emit cameraChanged(); + } +} + +void QFirstPersonCameraController::setLinearSpeed(float linearSpeed) +{ + Q_D(QFirstPersonCameraController); + if (d->m_linearSpeed != linearSpeed) { + d->m_linearSpeed = linearSpeed; + emit linearSpeedChanged(); + } +} + +void QFirstPersonCameraController::setLookSpeed(float lookSpeed) +{ + Q_D(QFirstPersonCameraController); + if (d->m_lookSpeed != lookSpeed) { + d->m_lookSpeed = lookSpeed; + emit lookSpeedChanged(); + } +} + +} // Qt3DExtras + +QT_END_NAMESPACE + +#include "moc_qfirstpersoncameracontroller.cpp" diff --git a/src/extras/defaults/qfirstpersoncameracontroller.h b/src/extras/defaults/qfirstpersoncameracontroller.h new file mode 100644 index 000000000..358f69160 --- /dev/null +++ b/src/extras/defaults/qfirstpersoncameracontroller.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DEXTRAS_QFIRSTPERSONCAMERACONTROLLER_H +#define QT3DEXTRAS_QFIRSTPERSONCAMERACONTROLLER_H + +#include <Qt3DExtras/qt3dextras_global.h> +#include <Qt3DCore/QEntity> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { +class QCamera; +} + +namespace Qt3DExtras { + +class QFirstPersonCameraControllerPrivate; + +class QT3DEXTRASSHARED_EXPORT QFirstPersonCameraController : public Qt3DCore::QEntity +{ + Q_OBJECT + Q_PROPERTY(Qt3DRender::QCamera *camera READ camera WRITE setCamera NOTIFY cameraChanged) + Q_PROPERTY(float linearSpeed READ linearSpeed WRITE setLinearSpeed NOTIFY linearSpeedChanged) + Q_PROPERTY(float lookSpeed READ lookSpeed WRITE setLookSpeed NOTIFY lookSpeedChanged) + +public: + explicit QFirstPersonCameraController(Qt3DCore::QNode *parent = Q_NULLPTR); + ~QFirstPersonCameraController(); + + Qt3DRender::QCamera *camera() const; + float linearSpeed() const; + float lookSpeed() const; + + void setCamera(Qt3DRender::QCamera *camera); + void setLinearSpeed(float linearSpeed); + void setLookSpeed(float lookSpeed); + +Q_SIGNALS: + void cameraChanged(); + void linearSpeedChanged(); + void lookSpeedChanged(); + +private: + Q_DECLARE_PRIVATE(QFirstPersonCameraController) + Q_PRIVATE_SLOT(d_func(), void _q_onTriggered(float)) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QFIRSTPERSONCAMERACONTROLLER_H diff --git a/src/extras/defaults/qfirstpersoncameracontroller_p.h b/src/extras/defaults/qfirstpersoncameracontroller_p.h new file mode 100644 index 000000000..e93b0bc33 --- /dev/null +++ b/src/extras/defaults/qfirstpersoncameracontroller_p.h @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DEXTRAS_QFIRSTPERSONCAMERACONTROLLER_P_H +#define QT3DEXTRAS_QFIRSTPERSONCAMERACONTROLLER_P_H + +#include <Qt3DCore/private/qentity_p.h> +#include <QVector3D> +#include "qfirstpersoncameracontroller.h" + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { +class QCamera; +} + +namespace Qt3DLogic { +class QFrameAction; +} + +namespace Qt3DInput { + +class QKeyboardDevice; +class QMouseDevice; +class QLogicalDevice; +class QAction; +class QActionInput; +class QAxis; +class QAxisInput; +class QAxisActionHandler; + +} + +namespace Qt3DExtras { + +class QFirstPersonCameraControllerPrivate : public Qt3DCore::QEntityPrivate +{ +public: + QFirstPersonCameraControllerPrivate(); + + void init(); + + Qt3DRender::QCamera *m_camera; + + Qt3DInput::QAction *m_leftMouseButtonAction; + Qt3DInput::QAction *m_fineMotionAction; + + Qt3DInput::QAxis *m_rxAxis; + Qt3DInput::QAxis *m_ryAxis; + Qt3DInput::QAxis *m_txAxis; + Qt3DInput::QAxis *m_tyAxis; + Qt3DInput::QAxis *m_tzAxis; + + Qt3DInput::QActionInput *m_leftMouseButtonInput; + Qt3DInput::QActionInput *m_fineMotionKeyInput; + + Qt3DInput::QAxisInput *m_mouseRxInput; + Qt3DInput::QAxisInput *m_mouseRyInput; + Qt3DInput::QAxisInput *m_keyboardTxPosInput; + Qt3DInput::QAxisInput *m_keyboardTyPosInput; + Qt3DInput::QAxisInput *m_keyboardTzPosInput; + Qt3DInput::QAxisInput *m_keyboardTxNegInput; + Qt3DInput::QAxisInput *m_keyboardTyNegInput; + Qt3DInput::QAxisInput *m_keyboardTzNegInput; + + Qt3DInput::QKeyboardDevice *m_keyboardDevice; + Qt3DInput::QMouseDevice *m_mouseDevice; + + Qt3DInput::QLogicalDevice *m_logicalDevice; + + Qt3DLogic::QFrameAction *m_frameAction; + + float m_linearSpeed; + float m_lookSpeed; + QVector3D m_firstPersonUp; + + void _q_onTriggered(float); + + Q_DECLARE_PUBLIC(QFirstPersonCameraController) +}; + +} // Qt3DInput + +QT_END_NAMESPACE + +#endif // QT3DINPUT_QFIRSTPERSONCAMERACONTROLLER_P_H diff --git a/src/extras/defaults/qorbitcontrol.cpp b/src/extras/defaults/qorbitcontrol.cpp new file mode 100644 index 000000000..65810e1a0 --- /dev/null +++ b/src/extras/defaults/qorbitcontrol.cpp @@ -0,0 +1,320 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qorbitcontrol_p.h" +#include "qorbitcontrol.h" +#include <QtGlobal> +#include <Qt3DRender/QCamera> +#include <Qt3DInput/QAxis> +#include <Qt3DInput/QAxisInput> +#include <Qt3DInput/QAction> +#include <Qt3DInput/QActionInput> +#include <Qt3DInput/QLogicalDevice> +#include <Qt3DInput/QKeyboardDevice> +#include <Qt3DInput/QMouseDevice> +#include <Qt3DInput/QMouseEvent> +#include <Qt3DLogic/QFrameAction> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +/*! + * \class QOrbitControl::QOrbitControl + * \internal + */ + +QOrbitControlPrivate::QOrbitControlPrivate() + : Qt3DCore::QEntityPrivate() + , m_camera(Q_NULLPTR) + , m_leftMouseButtonAction(new Qt3DInput::QAction()) + , m_rightMouseButtonAction(new Qt3DInput::QAction()) + , m_altButtonAction(new Qt3DInput::QAction()) + , m_shiftButtonAction(new Qt3DInput::QAction()) + , m_rxAxis(new Qt3DInput::QAxis()) + , m_ryAxis(new Qt3DInput::QAxis()) + , m_txAxis(new Qt3DInput::QAxis()) + , m_tyAxis(new Qt3DInput::QAxis()) + , m_tzAxis(new Qt3DInput::QAxis()) + , m_leftMouseButtonInput(new Qt3DInput::QActionInput()) + , m_rightMouseButtonInput(new Qt3DInput::QActionInput()) + , m_altButtonInput(new Qt3DInput::QActionInput()) + , m_shiftButtonInput(new Qt3DInput::QActionInput()) + , m_mouseRxInput(new Qt3DInput::QAxisInput()) + , m_mouseRyInput(new Qt3DInput::QAxisInput()) + , m_keyboardTxPosInput(new Qt3DInput::QAxisInput()) + , m_keyboardTyPosInput(new Qt3DInput::QAxisInput()) + , m_keyboardTzPosInput(new Qt3DInput::QAxisInput()) + , m_keyboardTxNegInput(new Qt3DInput::QAxisInput()) + , m_keyboardTyNegInput(new Qt3DInput::QAxisInput()) + , m_keyboardTzNegInput(new Qt3DInput::QAxisInput()) + , m_keyboardDevice(new Qt3DInput::QKeyboardDevice()) + , m_mouseDevice(new Qt3DInput::QMouseDevice()) + , m_logicalDevice(new Qt3DInput::QLogicalDevice()) + , m_frameAction(new Qt3DLogic::QFrameAction()) + , m_linearSpeed(10.0f) + , m_lookSpeed(180.0f) + , m_zoomInLimit(2.0f) + , m_cameraUp(QVector3D(0.0f, 1.0f, 0.0f)) +{} + +void QOrbitControlPrivate::init() +{ + //// Actions + + // Left Mouse Button Action + m_leftMouseButtonInput->setButtons(QVariantList() << Qt::LeftButton); + m_leftMouseButtonInput->setSourceDevice(m_mouseDevice); + m_leftMouseButtonAction->addInput(m_leftMouseButtonInput); + + // Right Mouse Button Action + m_rightMouseButtonInput->setButtons(QVariantList() << Qt::RightButton); + m_rightMouseButtonInput->setSourceDevice(m_mouseDevice); + m_rightMouseButtonAction->addInput(m_rightMouseButtonInput); + + // Alt Button Action + m_altButtonInput->setButtons(QVariantList() << Qt::Key_Alt); + m_altButtonInput->setSourceDevice(m_keyboardDevice); + m_altButtonAction->addInput(m_altButtonInput); + + // Shift Button Action + m_shiftButtonInput->setButtons(QVariantList() << Qt::Key_Shift); + m_shiftButtonInput->setSourceDevice(m_keyboardDevice); + m_shiftButtonAction->addInput(m_shiftButtonInput); + + //// Axes + + // Mouse X + m_mouseRxInput->setAxis(Qt3DInput::QMouseDevice::X); + m_mouseRxInput->setSourceDevice(m_mouseDevice); + m_rxAxis->addInput(m_mouseRxInput); + + // Mouse Y + m_mouseRyInput->setAxis(Qt3DInput::QMouseDevice::Y); + m_mouseRyInput->setSourceDevice(m_mouseDevice); + m_ryAxis->addInput(m_mouseRyInput); + + // Keyboard Pos Tx + m_keyboardTxPosInput->setButtons(QVariantList() << Qt::Key_Right); + m_keyboardTxPosInput->setScale(1.0f); + m_keyboardTxPosInput->setSourceDevice(m_keyboardDevice); + m_txAxis->addInput(m_keyboardTxPosInput); + + // Keyboard Pos Tz + m_keyboardTzPosInput->setButtons(QVariantList() << Qt::Key_PageUp); + m_keyboardTzPosInput->setScale(1.0f); + m_keyboardTzPosInput->setSourceDevice(m_keyboardDevice); + m_tzAxis->addInput(m_keyboardTzPosInput); + + // Keyboard Pos Ty + m_keyboardTyPosInput->setButtons(QVariantList() << Qt::Key_Up); + m_keyboardTyPosInput->setScale(1.0f); + m_keyboardTyPosInput->setSourceDevice(m_keyboardDevice); + m_tyAxis->addInput(m_keyboardTyPosInput); + + // Keyboard Neg Tx + m_keyboardTxNegInput->setButtons(QVariantList() << Qt::Key_Left); + m_keyboardTxNegInput->setScale(-1.0f); + m_keyboardTxNegInput->setSourceDevice(m_keyboardDevice); + m_txAxis->addInput(m_keyboardTxNegInput); + + // Keyboard Neg Tz + m_keyboardTzNegInput->setButtons(QVariantList() << Qt::Key_PageDown); + m_keyboardTzNegInput->setScale(-1.0f); + m_keyboardTzNegInput->setSourceDevice(m_keyboardDevice); + m_tzAxis->addInput(m_keyboardTzNegInput); + + // Keyboard Neg Ty + m_keyboardTyNegInput->setButtons(QVariantList() << Qt::Key_Down); + m_keyboardTyNegInput->setScale(-1.0f); + m_keyboardTyNegInput->setSourceDevice(m_keyboardDevice); + m_tyAxis->addInput(m_keyboardTyNegInput); + + //// Logical Device + + m_logicalDevice->addAction(m_leftMouseButtonAction); + m_logicalDevice->addAction(m_rightMouseButtonAction); + m_logicalDevice->addAction(m_altButtonAction); + m_logicalDevice->addAction(m_shiftButtonAction); + m_logicalDevice->addAxis(m_rxAxis); + m_logicalDevice->addAxis(m_ryAxis); + m_logicalDevice->addAxis(m_txAxis); + m_logicalDevice->addAxis(m_tyAxis); + m_logicalDevice->addAxis(m_tzAxis); + + Q_Q(QOrbitControl); + //// FrameAction + + QObject::connect(m_frameAction, SIGNAL(triggered(float)), + q, SLOT(_q_onTriggered(float))); + + q->addComponent(m_frameAction); + q->addComponent(m_logicalDevice); +} + +float clampInputs(float input1, float input2) +{ + float axisValue = input1 + input2; + return (axisValue < -1) ? -1 : (axisValue > 1) ? 1 : axisValue; +} + +float zoomDistance(QVector3D firstPoint, QVector3D secondPoint) +{ + return (secondPoint - firstPoint).lengthSquared(); +} + +void QOrbitControlPrivate::_q_onTriggered(float dt) +{ + if (m_camera != Q_NULLPTR) { + // Mouse input + if (m_leftMouseButtonAction->isActive()) { + if (m_rightMouseButtonAction->isActive()) { + if ( zoomDistance(m_camera->position(), m_camera->viewCenter()) > m_zoomInLimit * m_zoomInLimit) { + // Dolly up to limit + m_camera->translate(QVector3D(0, 0, m_ryAxis->value()), m_camera->DontTranslateViewCenter); + } else { + m_camera->translate(QVector3D(0, 0, -0.5), m_camera->DontTranslateViewCenter); + } + } else { + // Translate + m_camera->translate(QVector3D(clampInputs(m_rxAxis->value(), m_txAxis->value()) * m_linearSpeed, + clampInputs(m_ryAxis->value(), m_tyAxis->value()) * m_linearSpeed, + 0) * dt); + return; + } + } + else if (m_rightMouseButtonAction->isActive()) { + // Orbit + m_camera->panAboutViewCenter((m_rxAxis->value() * m_lookSpeed) * dt, m_cameraUp); + m_camera->tiltAboutViewCenter((m_ryAxis->value() * m_lookSpeed) * dt); + } + // Keyboard Input + if (m_altButtonAction->isActive()) { + // Orbit + m_camera->panAboutViewCenter((m_txAxis->value() * m_lookSpeed) * dt, m_cameraUp); + m_camera->tiltAboutViewCenter((m_tyAxis->value() * m_lookSpeed) * dt); + } else if (m_shiftButtonAction->isActive()) { + if (zoomDistance(m_camera->position(), m_camera->viewCenter()) > m_zoomInLimit * m_zoomInLimit) { + // Dolly + m_camera->translate(QVector3D(0, 0, m_tyAxis->value()), m_camera->DontTranslateViewCenter); + } else { + m_camera->translate(QVector3D(0, 0, -0.5), m_camera->DontTranslateViewCenter); + } + } else { + // Translate + m_camera->translate(QVector3D(clampInputs(m_leftMouseButtonAction->isActive() ? m_rxAxis->value() : 0, m_txAxis->value()) * m_linearSpeed, + clampInputs(m_leftMouseButtonAction->isActive() ? m_ryAxis->value() : 0, m_tyAxis->value()) * m_linearSpeed, + m_tzAxis->value() * m_linearSpeed) * dt); + } + } +} + +QOrbitControl::QOrbitControl(Qt3DCore::QNode *parent) + : Qt3DCore::QEntity(*new QOrbitControlPrivate, parent) +{ + Q_D(QOrbitControl); + d->init(); +} + +QOrbitControl::~QOrbitControl() +{ +} + +Qt3DRender::QCamera *QOrbitControl::camera() const +{ + Q_D(const QOrbitControl); + return d->m_camera; +} + +float QOrbitControl::linearSpeed() const +{ + Q_D(const QOrbitControl); + return d->m_linearSpeed; +} + +float QOrbitControl::lookSpeed() const +{ + Q_D(const QOrbitControl); + return d->m_lookSpeed; +} + +float QOrbitControl::zoomInLimit() const +{ + Q_D(const QOrbitControl); + return d->m_zoomInLimit; +} + +void QOrbitControl::setCamera(Qt3DRender::QCamera *camera) +{ + Q_D(QOrbitControl); + if (d->m_camera != camera) { + d->m_camera = camera; + emit cameraChanged(); + } +} + +void QOrbitControl::setLinearSpeed(float linearSpeed) +{ + Q_D(QOrbitControl); + if (d->m_linearSpeed != linearSpeed) { + d->m_linearSpeed = linearSpeed; + emit linearSpeedChanged(); + } +} + +void QOrbitControl::setLookSpeed(float lookSpeed) +{ + Q_D(QOrbitControl); + if (d->m_lookSpeed != lookSpeed) { + d->m_lookSpeed = lookSpeed; + emit lookSpeedChanged(); + } +} + +void QOrbitControl::setZoomInLimit(float zoomInLimit) +{ + Q_D(QOrbitControl); + if (d->m_zoomInLimit != zoomInLimit) { + d->m_zoomInLimit = zoomInLimit; + emit zoomInLimitChanged(); + } +} + +} // Qt3DExtras + +QT_END_NAMESPACE + +#include "moc_qorbitcontrol.cpp" diff --git a/src/extras/defaults/qorbitcontrol.h b/src/extras/defaults/qorbitcontrol.h new file mode 100644 index 000000000..2fb0c0bb9 --- /dev/null +++ b/src/extras/defaults/qorbitcontrol.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DEXTRAS_QORBITCONTROL_H +#define QT3DEXTRAS_QORBITCONTROL_H + +#include <Qt3DExtras/qt3dextras_global.h> +#include <Qt3DCore/QEntity> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { +class QCamera; +} + +namespace Qt3DExtras { + +class QOrbitControlPrivate; + +class QT3DEXTRASSHARED_EXPORT QOrbitControl : public Qt3DCore::QEntity +{ + Q_OBJECT + Q_PROPERTY(Qt3DRender::QCamera *camera READ camera WRITE setCamera NOTIFY cameraChanged) + Q_PROPERTY(float linearSpeed READ linearSpeed WRITE setLinearSpeed NOTIFY linearSpeedChanged) + Q_PROPERTY(float lookSpeed READ lookSpeed WRITE setLookSpeed NOTIFY lookSpeedChanged) + Q_PROPERTY(float zoomInLimit READ zoomInLimit WRITE setZoomInLimit NOTIFY zoomInLimitChanged) + +public: + explicit QOrbitControl(Qt3DCore::QNode *parent = nullptr); + ~QOrbitControl(); + + float clampInputs(float input1, float input2); + Qt3DRender::QCamera *camera() const; + float linearSpeed() const; + float lookSpeed() const; + float zoomInLimit() const; + + void setCamera(Qt3DRender::QCamera *camera); + void setLinearSpeed(float linearSpeed); + void setLookSpeed(float lookSpeed); + void setZoomInLimit(float zoomInLimit); + +Q_SIGNALS: + void cameraChanged(); + void linearSpeedChanged(); + void lookSpeedChanged(); + void zoomInLimitChanged(); + +private: + Q_DECLARE_PRIVATE(QOrbitControl) + Q_PRIVATE_SLOT(d_func(), void _q_onTriggered(float)) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QORBITCONTROL_H diff --git a/src/extras/defaults/qorbitcontrol_p.h b/src/extras/defaults/qorbitcontrol_p.h new file mode 100644 index 000000000..e37ca50f6 --- /dev/null +++ b/src/extras/defaults/qorbitcontrol_p.h @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTINPUT_QORBITCONTROL_P_H +#define QTINPUT_QORBITCONTROL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of other Qt classes. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <Qt3DCore/private/qentity_p.h> +#include <QVector3D> +#include "qorbitcontrol.h" + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { +class QCamera; +} + +namespace Qt3DLogic { +class QFrameAction; +} + +namespace Qt3DInput { + +class QKeyboardDevice; +class QMouseDevice; +class QLogicalDevice; +class QAction; +class QActionInput; +class QAxis; +class QAxisInput; +class QAxisActionHandler; + +} + +namespace Qt3DExtras { + +class QOrbitControlPrivate : public Qt3DCore::QEntityPrivate +{ +public: + QOrbitControlPrivate(); + + void init(); + + Qt3DRender::QCamera *m_camera; + + Qt3DInput::QAction *m_leftMouseButtonAction; + Qt3DInput::QAction *m_rightMouseButtonAction; + Qt3DInput::QAction *m_altButtonAction; + Qt3DInput::QAction *m_shiftButtonAction; + + Qt3DInput::QAxis *m_rxAxis; + Qt3DInput::QAxis *m_ryAxis; + Qt3DInput::QAxis *m_txAxis; + Qt3DInput::QAxis *m_tyAxis; + Qt3DInput::QAxis *m_tzAxis; + + Qt3DInput::QActionInput *m_leftMouseButtonInput; + Qt3DInput::QActionInput *m_rightMouseButtonInput; + Qt3DInput::QActionInput *m_altButtonInput; + Qt3DInput::QActionInput *m_shiftButtonInput; + + Qt3DInput::QAxisInput *m_mouseRxInput; + Qt3DInput::QAxisInput *m_mouseRyInput; + Qt3DInput::QAxisInput *m_keyboardTxPosInput; + Qt3DInput::QAxisInput *m_keyboardTyPosInput; + Qt3DInput::QAxisInput *m_keyboardTzPosInput; + Qt3DInput::QAxisInput *m_keyboardTxNegInput; + Qt3DInput::QAxisInput *m_keyboardTyNegInput; + Qt3DInput::QAxisInput *m_keyboardTzNegInput; + + Qt3DInput::QKeyboardDevice *m_keyboardDevice; + Qt3DInput::QMouseDevice *m_mouseDevice; + + Qt3DInput::QLogicalDevice *m_logicalDevice; + + Qt3DLogic::QFrameAction *m_frameAction; + + float m_linearSpeed; + float m_lookSpeed; + float m_zoomInLimit; + QVector3D m_cameraUp; + + void _q_onTriggered(float); + + Q_DECLARE_PUBLIC(QOrbitControl) +}; + +} // namespace Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QORBITCONTROL_P_H diff --git a/src/extras/defaults/qt3dwindow.cpp b/src/extras/defaults/qt3dwindow.cpp new file mode 100644 index 000000000..eed9d8862 --- /dev/null +++ b/src/extras/defaults/qt3dwindow.cpp @@ -0,0 +1,179 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qt3dwindow.h" + +#include <Qt3DExtras/qforwardrenderer.h> +#include <Qt3DRender/qrendersettings.h> +#include <Qt3DRender/qrenderaspect.h> +#include <Qt3DInput/qinputaspect.h> +#include <Qt3DInput/qinputsettings.h> +#include <Qt3DLogic/qlogicaspect.h> + +#include <Qt3DCore/qaspectengine.h> +#include <Qt3DRender/qcamera.h> +#include <Qt3DCore/qentity.h> + +#include <QtGui/qopenglcontext.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +Qt3DWindow::Qt3DWindow(QScreen *screen) + : QWindow(screen) + , m_aspectEngine(new Qt3DCore::QAspectEngine) + , m_renderAspect(new Qt3DRender::QRenderAspect) + , m_inputAspect(new Qt3DInput::QInputAspect) + , m_logicAspect(new Qt3DLogic::QLogicAspect) + , m_renderSettings(new Qt3DRender::QRenderSettings) + , m_forwardRenderer(new Qt3DExtras::QForwardRenderer) + , m_defaultCamera(new Qt3DRender::QCamera) + , m_inputSettings(new Qt3DInput::QInputSettings) + , m_root(new Qt3DCore::QEntity) + , m_userRoot(nullptr) + , m_initialized(false) +{ + setSurfaceType(QSurface::OpenGLSurface); + + resize(1024, 768); + + QSurfaceFormat format; +#ifdef QT_OPENGL_ES_2 + format.setRenderableType(QSurfaceFormat::OpenGLES); +#else + if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { + format.setVersion(4, 3); + format.setProfile(QSurfaceFormat::CoreProfile); + } +#endif + format.setDepthBufferSize(24); + format.setSamples(4); + format.setStencilBufferSize(8); + setFormat(format); + create(); + + m_aspectEngine->registerAspect(m_renderAspect); + m_aspectEngine->registerAspect(m_inputAspect); + m_aspectEngine->registerAspect(m_logicAspect); + + m_defaultCamera->setParent(m_root); + m_forwardRenderer->setCamera(m_defaultCamera); + m_forwardRenderer->setSurface(this); + m_renderSettings->setActiveFrameGraph(m_forwardRenderer); + m_inputSettings->setEventSource(this); +} + +Qt3DWindow::~Qt3DWindow() +{ +} + +void Qt3DWindow::registerAspect(Qt3DCore::QAbstractAspect *aspect) +{ + Q_ASSERT(!isVisible()); + m_aspectEngine->registerAspect(aspect); +} + +void Qt3DWindow::registerAspect(const QString &name) +{ + Q_ASSERT(!isVisible()); + m_aspectEngine->registerAspect(name); +} + +void Qt3DWindow::setRootEntity(Qt3DCore::QEntity *root) +{ + Q_ASSERT(!isVisible()); + m_userRoot = root; +} + +void Qt3DWindow::setActiveFrameGraph(Qt3DRender::QFrameGraphNode *activeFrameGraph) +{ + m_renderSettings->setActiveFrameGraph(activeFrameGraph); +} + +Qt3DRender::QFrameGraphNode *Qt3DWindow::activeFrameGraph() const +{ + return m_renderSettings->activeFrameGraph(); +} + +Qt3DExtras::QForwardRenderer *Qt3DWindow::defaultFramegraph() const +{ + return m_forwardRenderer; +} + +Qt3DRender::QCamera *Qt3DWindow::camera() const +{ + return m_defaultCamera; +} + +void Qt3DWindow::showEvent(QShowEvent *e) +{ + if (!m_initialized) { + if (m_userRoot != nullptr) + m_userRoot->setParent(m_root); + + m_root->addComponent(m_renderSettings); + m_root->addComponent(m_inputSettings); + m_aspectEngine->setRootEntity(Qt3DCore::QEntityPtr(m_root)); + + m_initialized = true; + } + + QWindow::showEvent(e); +} + +void Qt3DWindow::resizeEvent(QResizeEvent *) +{ + m_defaultCamera->setAspectRatio(float(width()) / float(height())); +} + +} // Qt3DExtras + +QT_END_NAMESPACE diff --git a/src/extras/defaults/qt3dwindow.h b/src/extras/defaults/qt3dwindow.h new file mode 100644 index 000000000..1d73966de --- /dev/null +++ b/src/extras/defaults/qt3dwindow.h @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DWINDOW_H +#define QT3DWINDOW_H + +#include <QWindow> +#include <Qt3DExtras/qt3dextras_global.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DCore { +class QAspectEngine; +class QAbstractAspect; +class QEntity; +} + +namespace Qt3DRender { +class QCamera; +class QFrameGraphNode; +class QRenderAspect; +class QRenderSettings; +} + +namespace Qt3DExtras { +class QForwardRenderer; +} + +namespace Qt3DInput { +class QInputAspect; +class QInputSettings; +} + +namespace Qt3DLogic { +class QLogicAspect; +} + +namespace Qt3DExtras { + +class QT3DEXTRASSHARED_EXPORT Qt3DWindow : public QWindow +{ + Q_OBJECT +public: + Qt3DWindow(QScreen *screen = nullptr); + ~Qt3DWindow(); + + void registerAspect(Qt3DCore::QAbstractAspect *aspect); + void registerAspect(const QString &name); + + void setRootEntity(Qt3DCore::QEntity *root); + + void setActiveFrameGraph(Qt3DRender::QFrameGraphNode *activeFrameGraph); + Qt3DRender::QFrameGraphNode *activeFrameGraph() const; + Qt3DExtras::QForwardRenderer *defaultFramegraph() const; + + Qt3DRender::QCamera *camera() const; + +public Q_SLOTS: + +Q_SIGNALS: + +protected: + void showEvent(QShowEvent *e) Q_DECL_OVERRIDE; + void resizeEvent(QResizeEvent *) Q_DECL_OVERRIDE; + +private: + QScopedPointer<Qt3DCore::QAspectEngine> m_aspectEngine; + + // Aspects + Qt3DRender::QRenderAspect *m_renderAspect; + Qt3DInput::QInputAspect *m_inputAspect; + Qt3DLogic::QLogicAspect *m_logicAspect; + + // Renderer configuration + Qt3DRender::QRenderSettings *m_renderSettings; + Qt3DExtras::QForwardRenderer *m_forwardRenderer; + Qt3DRender::QCamera *m_defaultCamera; + + // Input configuration + Qt3DInput::QInputSettings *m_inputSettings; + + // Logic configuration + + // Scene + Qt3DCore::QEntity *m_root; + Qt3DCore::QEntity *m_userRoot; + + bool m_initialized; +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DWINDOW_H diff --git a/src/extras/extras.pro b/src/extras/extras.pro index 3b7758007..57ba35652 100644 --- a/src/extras/extras.pro +++ b/src/extras/extras.pro @@ -1,6 +1,6 @@ TARGET = Qt3DExtras MODULE = 3dextras -QT += core-private 3dcore 3dcore-private 3drender 3drender-private +QT += core-private 3dcore 3dcore-private 3drender 3drender-private 3dinput 3dlogic DEFINES += QT3DEXTRAS_LIBRARY diff --git a/src/quick3d/imports/extras/defaults/defaults.pri b/src/quick3d/imports/extras/defaults/defaults.pri index eee644925..249c9139b 100644 --- a/src/quick3d/imports/extras/defaults/defaults.pri +++ b/src/quick3d/imports/extras/defaults/defaults.pri @@ -4,7 +4,7 @@ # resource file. # # To have the plugin register them as types, add an entries to the -# qmldir array in qt3dquick3drenderplugin.cpp +# qmldir array in qt3dquick3dextrasplugin.cpp QML_FILES = \ $$PWD/qml/PhongMaterial.qml \ $$PWD/qml/DiffuseMapMaterial.qml \ @@ -17,4 +17,6 @@ QML_FILES = \ $$PWD/qml/SkyboxEntity.qml \ $$PWD/qml/GoochMaterial.qml \ $$PWD/qml/PhongAlphaMaterial.qml \ - $$PWD/qml/TextureMaterial.qml + $$PWD/qml/TextureMaterial.qml \ + $$PWD/qml/OrbitController.qml \ + $$PWD/qml/FirstPersonCameraController.qml diff --git a/src/quick3d/imports/extras/defaults/qml/OrbitController.qml b/src/quick3d/imports/extras/defaults/qml/OrbitController.qml new file mode 100644 index 000000000..bba114730 --- /dev/null +++ b/src/quick3d/imports/extras/defaults/qml/OrbitController.qml @@ -0,0 +1,235 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Paul Lemire <paul.lemire350@gmail.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D.Core 2.0 +import Qt3D.Render 2.0 +import Qt3D.Input 2.0 +import Qt3D.Logic 2.0 +import QtQml 2.2 + +Entity { + id: root + property Camera camera + property real linearSpeed: 10.0 + property real lookSpeed: 180.0 + property real zoomLimit: 2.0 + + QtObject { + id: d + readonly property vector3d firstPersonUp: Qt.vector3d(0, 1, 0) + readonly property bool leftMouseButtonPressed: leftMouseButtonAction.active + readonly property bool rightMouseButtonPressed: rightMouseButtonAction.active + readonly property bool shiftPressed: shiftAction.active + readonly property bool altPressed: altAction.active + property real translationX: clampInputs(leftMouseButtonPressed ? mouseXAxis.value : 0, keyboardXAxis.value) * linearSpeed; + property real translationY: clampInputs(leftMouseButtonPressed ? mouseYAxis.value : 0, keyboardYAxis.value) * linearSpeed; + property real translationZ: keyboardZAxis.value * linearSpeed; + property real orbitX: clampInputs(rightMouseButtonPressed ? mouseXAxis.value : 0, keyboardXAxis.value) * lookSpeed; + property real orbitY: clampInputs(rightMouseButtonPressed ? mouseYAxis.value : 0, keyboardYAxis.value) * lookSpeed; + } + + function clampInputs(input1, input2) { + var axisValue = input1 + input2; + return (axisValue < -1) ? -1 : (axisValue > 1) ? 1 : axisValue; + } + + function zoomDistance(firstPoint, secondPoint) { + var u = secondPoint.minus(firstPoint); u = u.times(u); + return u.x + u.y + u.z; + } + + KeyboardDevice { + id: keyboardSourceDevice + } + + MouseDevice { + id: mouseSourceDevice + sensitivity: 0.1 + } + + components: [ + + LogicalDevice { + actions: [ + Action { + id: leftMouseButtonAction + ActionInput { + sourceDevice: mouseSourceDevice + buttons: [MouseEvent.LeftButton] + } + }, + Action { + id: rightMouseButtonAction + ActionInput { + sourceDevice: mouseSourceDevice + buttons: [MouseEvent.RightButton] + } + }, + Action { + id: shiftAction + ActionInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Shift] + } + }, + Action { + id: altAction + ActionInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Alt] + } + } + ] // actions + + axes: [ + // Mouse + Axis { + id: mouseXAxis + AxisInput { + sourceDevice: mouseSourceDevice + axis: MouseDevice.X + } + }, + Axis { + id: mouseYAxis + AxisInput { + sourceDevice: mouseSourceDevice + axis: MouseDevice.Y + } + }, + // Keyboard + Axis { + id: keyboardXAxis + AxisInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Left] + scale: -1.0 + } + AxisInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Right] + scale: 1.0 + } + }, + Axis { + id: keyboardZAxis + AxisInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Up] + scale: d.shiftPressed ? 1.0 : 0.0 + } + AxisInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Down] + scale: d.shiftPressed ? -1.0 : 0.0 + } + }, + Axis { + id: keyboardYAxis + AxisInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Up] + scale: d.shiftPressed ? 0.0 : 1.0 + } + AxisInput { + sourceDevice: keyboardSourceDevice + buttons: [Qt.Key_Down] + scale: d.shiftPressed ? 0.0 : -1.0 + } + } + ] // axes + }, + + FrameAction { + onTriggered: { + // The time difference since the last frame is passed in as the + // argument dt. It is a floating point value in units of seconds. + + // Mouse input + if (d.leftMouseButtonPressed) { + if (d.rightMouseButtonPressed) { + if (zoomDistance(root.camera.position, root.camera.viewCenter) > root.zoomLimit * root.zoomLimit) { + // Dolly up to limit + root.camera.translate(Qt.vector3d(0, 0, d.translationY).times(dt), Camera.DontTranslateViewCenter); + } else { + // Too close, Dolly backwards + root.camera.translate(Qt.vector3d(0, 0, -1).times(dt), Camera.DontTranslateViewCenter); + } + } else { + // Translate + root.camera.translate(Qt.vector3d(d.translationX, d.translationY, 0).times(dt)); + return + } + } else if (d.rightMouseButtonPressed) { + // Orbit + root.camera.panAboutViewCenter(d.orbitX * dt, d.firstPersonUp); + root.camera.tiltAboutViewCenter(d.orbitY * dt); + } + // Keyboard input + if (d.altPressed) { + // Orbit + root.camera.panAboutViewCenter(d.orbitX * dt, d.firstPersonUp); + root.camera.tiltAboutViewCenter(d.orbitY * dt); + } else if (d.shiftPressed) { + if (zoomDistance(root.camera.position, root.camera.viewCenter) > root.zoomLimit * root.zoomLimit) { + // Dolly up to limit + root.camera.translate(Qt.vector3d(0, 0, d.translationZ).times(dt), Camera.DontTranslateViewCenter); + } else { + // Too close, Dolly backwards + root.camera.translate(Qt.vector3d(0, 0, -1).times(dt), Camera.DontTranslateViewCenter); + } + } else { + // Translate + root.camera.translate(Qt.vector3d(d.translationX, d.translationY, 0).times(dt)); + } + } + } + ] // components +} diff --git a/src/quick3d/imports/extras/qt3dquick3dextrasplugin.cpp b/src/quick3d/imports/extras/qt3dquick3dextrasplugin.cpp index cd7743915..95be07095 100644 --- a/src/quick3d/imports/extras/qt3dquick3dextrasplugin.cpp +++ b/src/quick3d/imports/extras/qt3dquick3dextrasplugin.cpp @@ -72,7 +72,10 @@ static const struct { // FrameGraphs { "ForwardRenderer", 2, 0 }, // Entities - { "SkyboxEntity", 2, 0 } + { "SkyboxEntity", 2, 0 }, + // Camera Controllers + { "OrbitController", 2, 0 }, + { "FirstPersonCameraController", 2, 0 }, }; void Qt3DQuick3DExtrasPlugin::registerTypes(const char *uri) diff --git a/src/quick3d/quick3dextras/qt3dquickextras_global.h b/src/quick3d/quick3dextras/qt3dquickextras_global.h new file mode 100644 index 000000000..d9277baee --- /dev/null +++ b/src/quick3d/quick3dextras/qt3dquickextras_global.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DQUICKEXTRAS_GLOBAL_H +#define QT3DQUICKEXTRAS_GLOBAL_H + +#include <QtCore/qglobal.h> + +QT_BEGIN_NAMESPACE + +#if defined(QT_SHARED) || !defined(QT_STATIC) +# if defined(QT_BUILD_3DQUICKEXTRAS_LIB) +# define QT3DQUICKEXTRASSHARED_EXPORT Q_DECL_EXPORT +# else +# define QT3DQUICKEXTRASSHARED_EXPORT Q_DECL_IMPORT +# endif +#else +# define QT3DQUICKEXTRASSHARED_EXPORT +#endif + +QT_END_NAMESPACE + +#endif // QT3DQUICKEXTRAS_GLOBAL_H diff --git a/src/quick3d/quick3dextras/qt3dquickwindow.cpp b/src/quick3d/quick3dextras/qt3dquickwindow.cpp new file mode 100644 index 000000000..96c4bff6a --- /dev/null +++ b/src/quick3d/quick3dextras/qt3dquickwindow.cpp @@ -0,0 +1,254 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <Qt3DQuickExtras/qt3dquickwindow.h> +#include <Qt3DQuick/QQmlAspectEngine> +#include <Qt3DRender/qcamera.h> +#include <Qt3DRender/qrenderaspect.h> +#include <Qt3DRender/qrendersettings.h> +#include <Qt3DRender/qrendersurfaceselector.h> +#include <Qt3DInput/qinputaspect.h> +#include <Qt3DInput/qinputsettings.h> +#include <Qt3DLogic/qlogicaspect.h> + +#include <QQmlContext> + +#include <QtGui/qopenglcontext.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +namespace Quick { + +Qt3DQuickWindow::Qt3DQuickWindow(QWindow *parent) + : QQuickWindow(parent) + , m_engine(Q_NULLPTR) + , m_renderAspect(Q_NULLPTR) + , m_inputAspect(Q_NULLPTR) + , m_logicAspect(Q_NULLPTR) + , m_initialized(false) + , m_cameraAspectRatioMode(AutomaticAspectRatio) +{ + setSurfaceType(QSurface::OpenGLSurface); + + resize(1024, 768); + + QSurfaceFormat format; +#ifdef QT_OPENGL_ES_2 + format.setRenderableType(QSurfaceFormat::OpenGLES); +#else + if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { + format.setVersion(4, 3); + format.setProfile(QSurfaceFormat::CoreProfile); + } +#endif + format.setDepthBufferSize(24); + format.setSamples(4); + format.setStencilBufferSize(8); + setFormat(format); + QSurfaceFormat::setDefaultFormat(format); + create(); + + m_engine.reset(new Qt3DCore::Quick::QQmlAspectEngine); + m_renderAspect = new Qt3DRender::QRenderAspect; + m_inputAspect = new Qt3DInput::QInputAspect; + m_logicAspect = new Qt3DLogic::QLogicAspect; + + m_engine->aspectEngine()->registerAspect(m_renderAspect); + m_engine->aspectEngine()->registerAspect(m_inputAspect); + m_engine->aspectEngine()->registerAspect(m_logicAspect); +} + +Qt3DQuickWindow::~Qt3DQuickWindow() +{ +} + +void Qt3DQuickWindow::registerAspect(Qt3DCore::QAbstractAspect *aspect) +{ + Q_ASSERT(!isVisible()); + m_engine->aspectEngine()->registerAspect(aspect); +} + +void Qt3DQuickWindow::registerAspect(const QString &name) +{ + Q_ASSERT(!isVisible()); + m_engine->aspectEngine()->registerAspect(name); +} + +void Qt3DQuickWindow::setSource(const QUrl &source) +{ + m_source = source; +} + +Qt3DCore::Quick::QQmlAspectEngine *Qt3DQuickWindow::engine() const +{ + return m_engine.data(); +} + +void Qt3DQuickWindow::setCameraAspectRatioMode(CameraAspectRatioMode mode) +{ + if (m_cameraAspectRatioMode == mode) + return; + + m_cameraAspectRatioMode = mode; + setCameraAspectModeHelper(); + emit cameraAspectRatioModeChanged(mode); +} + +Qt3DQuickWindow::CameraAspectRatioMode Qt3DQuickWindow::cameraAspectRatioMode() const +{ + return m_cameraAspectRatioMode; +} + +void Qt3DQuickWindow::showEvent(QShowEvent *e) +{ + if (!m_initialized) { + + // Connect to the QQmlAspectEngine's statusChanged signal so that when the QML is loaded + // and th eobjects hav ebeen instantiated, but before we set them on the QAspectEngine we + // can swoop in and set the window surface and camera on the framegraph and ensure the camera + // respects the window's aspect ratio + connect(m_engine.data(), &Qt3DCore::Quick::QQmlAspectEngine::sceneCreated, + this, &Qt3DQuickWindow::onSceneCreated); + + m_engine->setSource(m_source); + + // Set the QQmlIncubationController on the window + // to benefit from asynchronous incubation + m_engine->qmlEngine()->setIncubationController(QQuickWindow::incubationController()); + + m_initialized = true; + } + QQuickWindow::showEvent(e); +} + +void Qt3DQuickWindow::onSceneCreated(QObject *rootObject) +{ + Q_ASSERT(rootObject); + + setWindowSurface(rootObject); + + if (m_cameraAspectRatioMode == AutomaticAspectRatio) { + // Set aspect ratio of first camera to match the window + QList<Qt3DRender::QCamera *> cameras + = rootObject->findChildren<Qt3DRender::QCamera *>(); + if (cameras.isEmpty()) { + qWarning() << "No camera found"; + } else { + m_camera = cameras.first(); + setCameraAspectModeHelper(); + } + } + + // Set ourselves up as a source of input events for the input aspect + Qt3DInput::QInputSettings *inputSettings = rootObject->findChild<Qt3DInput::QInputSettings *>(); + if (inputSettings) { + inputSettings->setEventSource(this); + } else { + qWarning() << "No Input Settings found, keyboard and mouse events won't be handled"; + } +} + +void Qt3DQuickWindow::setWindowSurface(QObject *rootObject) +{ + // Find surface selector in framegraph and set ourselves up as the + // render surface there + Qt3DRender::QRenderSettings *rendererSettings + = rootObject->findChild<Qt3DRender::QRenderSettings *>(); + if (!rendererSettings) { + qWarning() << "No renderer settings component found"; + return; + } + + Qt3DCore::QNode *frameGraphRoot = rendererSettings->activeFrameGraph(); + if (!frameGraphRoot) { + qWarning() << "No active frame graph found"; + return; + } + + Qt3DRender::QRenderSurfaceSelector *surfaceSelector = qobject_cast<Qt3DRender::QRenderSurfaceSelector *>(frameGraphRoot); + if (!surfaceSelector) + surfaceSelector = frameGraphRoot->findChild<Qt3DRender::QRenderSurfaceSelector *>(); + if (!surfaceSelector) { + qWarning() << "No render surface selector found in frame graph"; + return; + } + + surfaceSelector->setSurface(this); +} + +void Qt3DQuickWindow::setCameraAspectModeHelper() +{ + switch (m_cameraAspectRatioMode) { + case AutomaticAspectRatio: + connect(this, &QWindow::widthChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio); + connect(this, &QWindow::heightChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio); + break; + case UserAspectRatio: + disconnect(this, &QWindow::widthChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio); + disconnect(this, &QWindow::heightChanged, this, &Qt3DQuickWindow::updateCameraAspectRatio); + break; + } +} + +void Qt3DQuickWindow::updateCameraAspectRatio() +{ + if (m_camera) { + m_camera->setAspectRatio(static_cast<float>(width()) / + static_cast<float>(height())); + } +} + +} // Quick + +} // Qt3DExtras + +QT_END_NAMESPACE diff --git a/src/quick3d/quick3dextras/qt3dquickwindow.h b/src/quick3d/quick3dextras/qt3dquickwindow.h new file mode 100644 index 000000000..83d31dbc9 --- /dev/null +++ b/src/quick3d/quick3dextras/qt3dquickwindow.h @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DEXTRAS_QUICK_QT3DQUICKWINDOW_H +#define QT3DEXTRAS_QUICK_QT3DQUICKWINDOW_H + +#include <Qt3DQuickExtras/qt3dquickextras_global.h> +#include <QQuickWindow> +#include <QtCore/qpointer.h> +#include <QUrl> + +QT_BEGIN_NAMESPACE + +namespace Qt3DCore { +class QAbstractAspect; +namespace Quick { +class QQmlAspectEngine; +} +} + +namespace Qt3DRender { +class QRenderAspect; +class QCamera; +} + +namespace Qt3DInput { +class QInputAspect; +} + +namespace Qt3DLogic { +class QLogicAspect; +} + +namespace Qt3DExtras { + +namespace Quick { + +class QT3DQUICKEXTRASSHARED_EXPORT Qt3DQuickWindow : public QQuickWindow +{ + Q_OBJECT + Q_PROPERTY(CameraAspectRatioMode cameraAspectRatioMode READ cameraAspectRatioMode WRITE setCameraAspectRatioMode NOTIFY cameraAspectRatioModeChanged) + +public: + Qt3DQuickWindow(QWindow *parent = Q_NULLPTR); + ~Qt3DQuickWindow(); + + void registerAspect(Qt3DCore::QAbstractAspect *aspect); + void registerAspect(const QString &name); + + void setSource(const QUrl &source); + Qt3DCore::Quick::QQmlAspectEngine *engine() const; + + enum CameraAspectRatioMode { + AutomaticAspectRatio, + UserAspectRatio + }; + Q_ENUM(CameraAspectRatioMode); + + void setCameraAspectRatioMode(CameraAspectRatioMode mode); + CameraAspectRatioMode cameraAspectRatioMode() const; + +Q_SIGNALS: + void cameraAspectRatioModeChanged(CameraAspectRatioMode mode); + +protected: + void showEvent(QShowEvent *e) Q_DECL_OVERRIDE; + +private: + void onSceneCreated(QObject *rootObject); + void setWindowSurface(QObject *rootObject); + void setCameraAspectModeHelper(); + void updateCameraAspectRatio(); + + QScopedPointer<Qt3DCore::Quick::QQmlAspectEngine> m_engine; + + // Aspects + Qt3DRender::QRenderAspect *m_renderAspect; + Qt3DInput::QInputAspect *m_inputAspect; + Qt3DLogic::QLogicAspect *m_logicAspect; + + QUrl m_source; + bool m_initialized; + QPointer<Qt3DRender::QCamera> m_camera; + CameraAspectRatioMode m_cameraAspectRatioMode; +}; + +} // Quick + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QUICK_QT3DQUICKWINDOW_H diff --git a/src/quick3d/quick3dextras/quick3dextras.pro b/src/quick3d/quick3dextras/quick3dextras.pro new file mode 100644 index 000000000..aa3b485e7 --- /dev/null +++ b/src/quick3d/quick3dextras/quick3dextras.pro @@ -0,0 +1,26 @@ +TARGET = Qt3DQuickExtras +MODULE = 3dquickextras + +QT += core core-private qml qml-private 3dcore 3dinput 3dquick 3drender 3dlogic +CONFIG -= precompile_header + +gcov { + CONFIG += static + QMAKE_CXXFLAGS += -fprofile-arcs -ftest-coverage + QMAKE_LFLAGS += -fprofile-arcs -ftest-coverage +} + +SOURCES += \ + qt3dquickwindow.cpp + + +HEADERS += \ + qt3dquickextras_global.h \ + qt3dquickwindow.h + +!contains(QT_CONFIG, egl):DEFINES += QT_NO_EGL + +# otherwise mingw headers do not declare common functions like ::strcasecmp +win32-g++*:QMAKE_CXXFLAGS_CXX11 = -std=gnu++0x + +load(qt_module) diff --git a/src/src.pro b/src/src.pro index fb999f6ca..9d51e6b14 100644 --- a/src/src.pro +++ b/src/src.pro @@ -17,7 +17,7 @@ src_input.depends = src_render src_extras.subdir = $$PWD/extras src_extras.target = src_extras -src_extras.depends = src_render src_input +src_extras.depends = src_render src_input src_logic # Quick3D libs src_quick3d_core.subdir = $$PWD/quick3d/quick3d @@ -32,6 +32,10 @@ src_quick3d_input.subdir = $$PWD/quick3d/quick3dinput src_quick3d_input.target = sub-quick3d-input src_quick3d_input.depends = src_input src_quick3d_core +src_quick3d_extras.subdir = $$PWD/quick3d/quick3dextras +src_quick3d_extras.target = sub-quick3d-extras +src_quick3d_extras.depends = src_render src_logic src_input src_quick3d_core + # Quick3D imports src_quick3d_core_imports.file = $$PWD/quick3d/imports/core/importscore.pro src_quick3d_core_imports.target = sub-quick3d-imports-core @@ -72,6 +76,7 @@ SUBDIRS += \ src_quick3d_core_imports \ src_quick3d_render \ src_quick3d_input \ + src_quick3d_extras \ src_quick3d_imports_render \ src_quick3d_imports_scene3d \ src_quick3d_imports_input \ |