diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2016-01-14 08:43:34 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2016-01-16 01:59:44 +0000 |
commit | 8dcd8e42458748ca3ae3293b3d2b7e5b56b87252 (patch) | |
tree | b4a63f6bd0665e9cdeb64ebdb9caaded12be16ff /examples/qt3d/examples-common | |
parent | 78ad6123fb4f8597eb936e2c4b1636d307df4ecc (diff) |
Link examples against static lib
Contains:
Qt3DWindow
Qt3DQuickWindow
QFirstPersonCameraController
FirstPersonCameraController.qml
This will allow to get rid of the Camera configuration hack
Change-Id: I55c5634aa4c162f9dd2852d3258073a6352b426c
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'examples/qt3d/examples-common')
-rw-r--r-- | examples/qt3d/examples-common/FirstPersonCameraController.qml | 255 | ||||
-rw-r--r-- | examples/qt3d/examples-common/examples-common.pri | 20 | ||||
-rw-r--r-- | examples/qt3d/examples-common/examples-common.pro | 44 | ||||
-rw-r--r-- | examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp | 278 | ||||
-rw-r--r-- | examples/qt3d/examples-common/qfirstpersoncameracontroller.h | 88 | ||||
-rw-r--r-- | examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h | 115 | ||||
-rw-r--r-- | examples/qt3d/examples-common/qt3dquickwindow.cpp | 121 | ||||
-rw-r--r-- | examples/qt3d/examples-common/qt3dquickwindow.h | 94 | ||||
-rw-r--r-- | examples/qt3d/examples-common/qt3dwindow.cpp | 179 | ||||
-rw-r--r-- | examples/qt3d/examples-common/qt3dwindow.h | 111 |
10 files changed, 1305 insertions, 0 deletions
diff --git a/examples/qt3d/examples-common/FirstPersonCameraController.qml b/examples/qt3d/examples-common/FirstPersonCameraController.qml new file mode 100644 index 000000000..27b794afc --- /dev/null +++ b/examples/qt3d/examples-common/FirstPersonCameraController.qml @@ -0,0 +1,255 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Paul Lemire <paul.lemire350@gmail.com> +** 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$ +** +****************************************************************************/ + +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 + + QtObject { + id: d + readonly property vector3d firstPersonUp: Qt.vector3d(0, 1, 0) + property bool leftMouseButtonPressed: false + property real vx: 0; + property real vy: 0; + property real vz: 0; + property real dx: 0 + property real dy: 0 + property bool fineMotion: false + } + + KeyboardController { + id: keyboardSourceDevice + } + + MouseController { + id: mouseSourceDevice + sensitivity: d.fineMotion ? 0.01 : 0.1 + } + + LogicalDevice { + id: cameraControlDevice + + actions: [ + Action { + name: "LMB" + inputs: [ + ActionInput { + sourceDevice: mouseSourceDevice + keys: [MouseController.Left] + } + ] + }, + Action { + name: "fineMotion" + inputs: [ + ActionInput { + sourceDevice: keyboardSourceDevice + keys: [Qt.Key_Shift] + } + ] + } + ] // actions + + axes: [ + // Rotation + Axis { + name: "RX" + inputs: [ + AxisInput { + sourceDevice: mouseSourceDevice + axis: MouseController.X + } + ] + }, + Axis { + name: "RY" + inputs: [ + AxisInput { + sourceDevice: mouseSourceDevice + axis: MouseController.Y + } + ] + }, + // Translation + Axis { + name: "TX" + inputs: [ + AxisInput { + sourceDevice: keyboardSourceDevice + keys: [Qt.Key_Left] + scale: -1.0 + }, + AxisInput { + sourceDevice: keyboardSourceDevice + keys: [Qt.Key_Right] + scale: 1.0 + } + ] + }, + Axis { + name: "TZ" + inputs: [ + AxisInput { + sourceDevice: keyboardSourceDevice + keys: [Qt.Key_Up] + scale: 1.0 + }, + AxisInput { + sourceDevice: keyboardSourceDevice + keys: [Qt.Key_Down] + scale: -1.0 + } + ] + }, + Axis { + name: "TY" + inputs: [ + AxisInput { + sourceDevice: keyboardSourceDevice + keys: [Qt.Key_PageUp] + scale: 1.0 + }, + AxisInput { + sourceDevice: keyboardSourceDevice + keys: [Qt.Key_PageDown] + scale: -1.0 + } + ] + } + ] // axes + } + + components: [ + AxisActionHandler { + id: handler + logicalDevice: cameraControlDevice + + onAxisValueChanged: { + + switch (name) { + + case "TX": { + d.vx = axisValue * linearSpeed + break; + } + + case "TY": { + d.vy = axisValue * linearSpeed + break; + } + + case "TZ": { + d.vz = axisValue * linearSpeed + break; + } + + case "RX": { + d.dx = axisValue; + break; + } + + case "RY": { + d.dy = axisValue; + break; + } + case "MouseWheel": { + d.zoom = axisValue; + break + } + + } + } + + onActionStarted: { + + switch (name) { + + case "LMB": { + d.leftMouseButtonPressed = true; + break; + } + + case "fineMotion": { + console.log("fineMotion started") + d.fineMotion = true; + break; + } + + } + + } + + onActionFinished: { + + switch (name) { + + case "LMB": { + d.leftMouseButtonPressed = false; + break; + } + + case "fineMotion": { + console.log("fineMotion finished") + d.fineMotion = false; + break; + } + + } + } + }, + + LogicComponent { + onFrameUpdate: { + // The time difference since the last frame is passed in as the + // argument dt. It is a floating point value in units of seconds. + root.camera.translate(Qt.vector3d(d.vx, d.vy, d.vz).times(dt)) + + if (d.leftMouseButtonPressed) { + root.camera.pan(root.lookSpeed * d.dx * dt, d.firstPersonUp) + root.camera.tilt(root.lookSpeed * d.dy * dt) + } + } + } + ] // components +} diff --git a/examples/qt3d/examples-common/examples-common.pri b/examples/qt3d/examples-common/examples-common.pri new file mode 100644 index 000000000..e50e39edd --- /dev/null +++ b/examples/qt3d/examples-common/examples-common.pri @@ -0,0 +1,20 @@ +QT += 3dcore 3drender 3dinput 3dlogic qml + +INCLUDEPATH += $$PWD + +win32 { + build_pass { + CONFIG(debug, debug|release) { + common_static_lib = $$OUT_PWD/../examples-common/debug/$${QMAKE_PREFIX_STATICLIB}examples-common.$${QMAKE_EXTENSION_STATICLIB} + } else { + common_static_lib = $$OUT_PWD/../examples-common/release/$${QMAKE_PREFIX_STATICLIB}examples-common.$${QMAKE_EXTENSION_STATICLIB} + } + } +} else { + common_static_lib = $$OUT_PWD/../examples-common/$${QMAKE_PREFIX_STATICLIB}examples-common.$${QMAKE_EXTENSION_STATICLIB} +} + +LIBS += $$common_static_lib +PRE_TARGETDEPS += $$common_static_lib + +RESOURCES += $$OUT_PWD/../examples-common/examples-common.qrc diff --git a/examples/qt3d/examples-common/examples-common.pro b/examples/qt3d/examples-common/examples-common.pro new file mode 100644 index 000000000..7b56b9b54 --- /dev/null +++ b/examples/qt3d/examples-common/examples-common.pro @@ -0,0 +1,44 @@ +TEMPLATE = lib +CONFIG += staticlib +TARGET = examples-common +QT += core-private 3dcore 3dcore-private 3drender 3dinput 3dlogic qml quick + +SOURCES += \ + qfirstpersoncameracontroller.cpp \ + qt3dwindow.cpp \ + qt3dquickwindow.cpp + +HEADERS += \ + qfirstpersoncameracontroller.h \ + qfirstpersoncameracontroller_p.h \ + qt3dwindow.h \ + qt3dquickwindow.h + + +QML_FILES = \ + $$PWD/FirstPersonCameraController.qml + +OTHER_FILES += \ + $$QML_FILES + +# Create a resource file for qml files that need to be registered by the plugin +GENERATED_RESOURCE_FILE = $$OUT_PWD/examples-common.qrc +INCLUDED_RESOURCE_FILES = $$QML_FILES +RESOURCE_CONTENT = \ + "<RCC>" \ + "<qresource prefix=\"/\">" + +for(resourcefile, INCLUDED_RESOURCE_FILES) { + resourcefileabsolutepath = $$absolute_path($$resourcefile) + relativepath_in = $$relative_path($$resourcefileabsolutepath, $$_PRO_FILE_PWD_) + relativepath_out = $$relative_path($$resourcefileabsolutepath, $$OUT_PWD) + RESOURCE_CONTENT += "<file alias=\"$$relativepath_in\">$$relativepath_out</file>" +} + +RESOURCE_CONTENT += \ + "</qresource>" \ + "</RCC>" + +write_file($$GENERATED_RESOURCE_FILE, RESOURCE_CONTENT)|error("Aborting.") + +RESOURCES += $$GENERATED_RESOURCE_FILE diff --git a/examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp b/examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp new file mode 100644 index 000000000..46ae0c115 --- /dev/null +++ b/examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp @@ -0,0 +1,278 @@ +/**************************************************************************** +** +** 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 "qfirstpersoncameracontroller_p.h" +#include "qfirstpersoncameracontroller.h" +#include <Qt3DCore/QCamera> +#include <Qt3DInput/QAxis> +#include <Qt3DInput/QAxisInput> +#include <Qt3DInput/QAction> +#include <Qt3DInput/QActionInput> +#include <Qt3DInput/QLogicalDevice> +#include <Qt3DInput/QAxisActionHandler> +#include <Qt3DInput/QKeyboardController> +#include <Qt3DInput/QMouseController> +#include <Qt3DLogic/QLogicComponent> + +QT_BEGIN_NAMESPACE + +namespace Qt3DInput { + + +QFirstPersonCameraControllerPrivate::QFirstPersonCameraControllerPrivate() + : Qt3DCore::QEntityPrivate() + , m_camera(Q_NULLPTR) + , m_leftMouseButtonAction(new QAction()) + , m_fineMotionAction(new QAction()) + , m_rxAxis(new QAxis()) + , m_ryAxis(new QAxis()) + , m_txAxis(new QAxis()) + , m_tyAxis(new QAxis()) + , m_tzAxis(new QAxis()) + , m_leftMouseButtonInput(new QActionInput()) + , m_fineMotionKeyInput(new QActionInput()) + , m_mouseRxInput(new QAxisInput()) + , m_mouseRyInput(new QAxisInput()) + , m_keyboardTxPosInput(new QAxisInput()) + , m_keyboardTyPosInput(new QAxisInput()) + , m_keyboardTzPosInput(new QAxisInput()) + , m_keyboardTxNegInput(new QAxisInput()) + , m_keyboardTyNegInput(new QAxisInput()) + , m_keyboardTzNegInput(new QAxisInput()) + , m_keyboardController(new QKeyboardController()) + , m_mouseController(new QMouseController()) + , m_logicalDevice(new QLogicalDevice()) + , m_axisActionHandler(new QAxisActionHandler()) + , m_logicComponent(new Qt3DLogic::QLogicComponent()) + , m_linearSpeed(10.0f) + , m_lookSpeed(180.0f) + , m_fineMotion(false) + , m_leftMouseButtonPressed(false) + , m_firstPersonUp(QVector3D(0.0f, 1.0f, 0.0f)) +{} + +void QFirstPersonCameraControllerPrivate::init() +{ + //// Actions + + // Left Mouse Button Action + m_leftMouseButtonInput->setKeys(QVariantList() << QMouseController::Left); + m_leftMouseButtonAction->addInput(m_leftMouseButtonInput); + m_leftMouseButtonAction->setName(QStringLiteral("LMB")); + + // Fine Motion Action + m_fineMotionKeyInput->setKeys(QVariantList() << Qt::Key_Shift); + m_fineMotionAction->addInput(m_fineMotionKeyInput); + m_fineMotionAction->setName(QStringLiteral("fineMotion")); + + //// Axes + + // Mouse X + m_mouseRxInput->setAxis(QMouseController::X); + m_rxAxis->addInput(m_mouseRxInput); + m_rxAxis->setName(QStringLiteral("RX")); + + // Mouse Y + m_mouseRyInput->setAxis(QMouseController::Y); + m_ryAxis->addInput(m_mouseRyInput); + m_ryAxis->setName(QStringLiteral("RY")); + + // Keyboard Pos Tx + m_keyboardTxPosInput->setKeys(QVariantList() << Qt::Key_Right); + m_keyboardTxPosInput->setScale(1.0f); + m_txAxis->addInput(m_keyboardTxPosInput); + + // Keyboard Pos Ty + m_keyboardTyPosInput->setKeys(QVariantList() << Qt::Key_PageUp); + m_keyboardTyPosInput->setScale(1.0f); + m_tyAxis->addInput(m_keyboardTyPosInput); + + // Keyboard Pos Tz + m_keyboardTzPosInput->setKeys(QVariantList() << Qt::Key_Up); + m_keyboardTzPosInput->setScale(1.0f); + m_tzAxis->addInput(m_keyboardTzPosInput); + + // Keyboard Neg Tx + m_keyboardTxNegInput->setKeys(QVariantList() << Qt::Key_Left); + m_keyboardTxNegInput->setScale(-1.0f); + m_txAxis->addInput(m_keyboardTxNegInput); + + // Keyboard Neg Ty + m_keyboardTyNegInput->setKeys(QVariantList() << Qt::Key_PageDown); + m_keyboardTyNegInput->setScale(-1.0f); + m_tyAxis->addInput(m_keyboardTyNegInput); + + // Keyboard Neg Tz + m_keyboardTzNegInput->setKeys(QVariantList() << Qt::Key_Down); + m_keyboardTzNegInput->setScale(-1.0f); + m_tzAxis->addInput(m_keyboardTzNegInput); + + m_txAxis->setName(QStringLiteral("TX")); + m_tyAxis->setName(QStringLiteral("TY")); + m_tzAxis->setName(QStringLiteral("TZ")); + + //// 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); + + //// AxisActionHandler + + QObject::connect(m_axisActionHandler, SIGNAL(actionStarted(QString)), + q, SLOT(_q_onActionStarted(QString))); + QObject::connect(m_axisActionHandler, SIGNAL(actionFinished(QString)), + q, SLOT(_q_onActionFinished(QString))); + QObject::connect(m_axisActionHandler, SIGNAL(axisValueChanged(QString,float)), + q, SLOT(_q_onAxisValueChanged(QString,float))); + + //// LogicComponent + + QObject::connect(m_logicComponent, SIGNAL(frameUpdate(float)), + q, SLOT(_q_onFrameUpdate(float))); + + q->addComponent(m_logicComponent); + q->addComponent(m_axisActionHandler); +} + +void QFirstPersonCameraControllerPrivate::_q_onFrameUpdate(float dt) +{ + if (m_camera != Q_NULLPTR) { + m_camera->translate(m_vxyz * dt); + if (m_leftMouseButtonPressed) { + m_camera->pan(m_lookSpeed * m_rxyz.x() * dt, m_firstPersonUp); + m_camera->pan(m_lookSpeed * m_rxyz.y() * dt); + } + } +} + +void QFirstPersonCameraControllerPrivate::_q_onAxisValueChanged(QString name, float value) +{ + if (name == QStringLiteral("RX")) + m_rxyz.setX(value); + else if (name == QStringLiteral("RY")) + m_rxyz.setY(value); + else if (name == QStringLiteral("TX")) + m_vxyz.setX(value); + else if (name == QStringLiteral("TY")) + m_vxyz.setY(value); + else if (name == QStringLiteral("TZ")) + m_vxyz.setZ(value); +} + +void QFirstPersonCameraControllerPrivate::_q_onActionStarted(QString name) +{ + if (name == QStringLiteral("LMB")) + m_leftMouseButtonPressed = false; + else if (name == QStringLiteral("fineMotion")) + m_fineMotion = false; +} + +void QFirstPersonCameraControllerPrivate::_q_onActionFinished(QString name) +{ + if (name == QStringLiteral("LMB")) + m_leftMouseButtonPressed = true; + else if (name == QStringLiteral("fineMotion")) + m_fineMotion = true; +} + +QFirstPersonCameraController::QFirstPersonCameraController(Qt3DCore::QNode *parent) + : Qt3DCore::QEntity(*new QFirstPersonCameraControllerPrivate, parent) +{ + Q_D(QFirstPersonCameraController); + d->init(); +} + +QFirstPersonCameraController::~QFirstPersonCameraController() +{ +} + +Qt3DCore::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(Qt3DCore::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(); + } +} + +} // Qt3DInput + +QT_END_NAMESPACE + +#include "moc_qfirstpersoncameracontroller.cpp" diff --git a/examples/qt3d/examples-common/qfirstpersoncameracontroller.h b/examples/qt3d/examples-common/qfirstpersoncameracontroller.h new file mode 100644 index 000000000..b1755d7d4 --- /dev/null +++ b/examples/qt3d/examples-common/qfirstpersoncameracontroller.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** 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 QT3DINPUT_QFIRSTPERSONCAMERACONTROLLER_H +#define QT3DINPUT_QFIRSTPERSONCAMERACONTROLLER_H + +#include <Qt3DCore/QEntity> + +QT_BEGIN_NAMESPACE + +namespace Qt3DCore { +class QCamera; +} + +namespace Qt3DInput { + +class QFirstPersonCameraControllerPrivate; + +class QFirstPersonCameraController : public Qt3DCore::QEntity +{ + Q_OBJECT + Q_PROPERTY(Qt3DCore::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(); + + Qt3DCore::QCamera *camera() const; + float linearSpeed() const; + float lookSpeed() const; + + void setCamera(Qt3DCore::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_onFrameUpdate(float)) + Q_PRIVATE_SLOT(d_func(), void _q_onAxisValueChanged(QString, float)) + Q_PRIVATE_SLOT(d_func(), void _q_onActionStarted(QString)) + Q_PRIVATE_SLOT(d_func(), void _q_onActionFinished(QString)) +}; + +} // Qt3DInput + +QT_END_NAMESPACE + +#endif // QT3DINPUT_QFIRSTPERSONCAMERACONTROLLER_H diff --git a/examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h b/examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h new file mode 100644 index 000000000..985b9f536 --- /dev/null +++ b/examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** 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 QT3DINPUT_QFIRSTPERSONCAMERACONTROLLER_P_H +#define QT3DINPUT_QFIRSTPERSONCAMERACONTROLLER_P_H + +#include <Qt3DCore/private/qentity_p.h> +#include <QVector3D> +#include "qfirstpersoncameracontroller.h" + +QT_BEGIN_NAMESPACE + +namespace Qt3DCore { +class QCamera; +} + +namespace Qt3DLogic { +class QLogicComponent; +} + +namespace Qt3DInput { + +class QKeyboardController; +class QMouseController; +class QLogicalDevice; +class QAction; +class QActionInput; +class QAxis; +class QAxisInput; +class QAxisActionHandler; + +class QFirstPersonCameraControllerPrivate : public Qt3DCore::QEntityPrivate +{ +public: + QFirstPersonCameraControllerPrivate(); + + void init(); + + Qt3DCore::QCamera *m_camera; + QAction *m_leftMouseButtonAction; + QAction *m_fineMotionAction; + QAxis *m_rxAxis; + QAxis *m_ryAxis; + QAxis *m_txAxis; + QAxis *m_tyAxis; + QAxis *m_tzAxis; + QActionInput *m_leftMouseButtonInput; + QActionInput *m_fineMotionKeyInput; + QAxisInput *m_mouseRxInput; + QAxisInput *m_mouseRyInput; + QAxisInput *m_keyboardTxPosInput; + QAxisInput *m_keyboardTyPosInput; + QAxisInput *m_keyboardTzPosInput; + QAxisInput *m_keyboardTxNegInput; + QAxisInput *m_keyboardTyNegInput; + QAxisInput *m_keyboardTzNegInput; + QKeyboardController *m_keyboardController; + QMouseController *m_mouseController; + QLogicalDevice *m_logicalDevice; + QAxisActionHandler *m_axisActionHandler; + Qt3DLogic::QLogicComponent *m_logicComponent; + float m_linearSpeed; + float m_lookSpeed; + QVector3D m_vxyz; + bool m_fineMotion; + QVector3D m_rxyz; + bool m_leftMouseButtonPressed; + QVector3D m_firstPersonUp; + + void _q_onFrameUpdate(float); + void _q_onAxisValueChanged(QString, float); + void _q_onActionStarted(QString); + void _q_onActionFinished(QString); + + Q_DECLARE_PUBLIC(QFirstPersonCameraController) +}; + +} // Qt3DInput + +QT_END_NAMESPACE + +#endif // QT3DINPUT_QFIRSTPERSONCAMERACONTROLLER_P_H diff --git a/examples/qt3d/examples-common/qt3dquickwindow.cpp b/examples/qt3d/examples-common/qt3dquickwindow.cpp new file mode 100644 index 000000000..27f2ee5ef --- /dev/null +++ b/examples/qt3d/examples-common/qt3dquickwindow.cpp @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** 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 "qt3dquickwindow.h" +#include <Qt3DQuick/QQmlAspectEngine> +#include <Qt3DRender/qrenderaspect.h> +#include <Qt3DInput/qinputaspect.h> +#include <Qt3DLogic/qlogicaspect.h> +#include <QtGui/qopenglcontext.h> + +QT_BEGIN_NAMESPACE + +Qt3DQuickWindow::Qt3DQuickWindow(QWindow *parent) + : QQuickWindow(parent) + , m_engine(new Qt3DCore::Quick::QQmlAspectEngine) + , m_renderAspect(new Qt3DRender::QRenderAspect) + , m_inputAspect(new Qt3DInput::QInputAspect) + , m_logicAspect(new Qt3DLogic::QLogicAspect) + , m_initialized(false) +{ + setSurfaceType(QSurface::OpenGLSurface); + + resize(1024, 768); + + QSurfaceFormat format; + if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { + format.setVersion(4, 3); + format.setProfile(QSurfaceFormat::CoreProfile); + } + format.setDepthBufferSize(24); + format.setSamples(4); + format.setStencilBufferSize(8); + setFormat(format); + create(); + + 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::showEvent(QShowEvent *e) +{ + if (!m_initialized) { + + // TODO: Get rid of this + QVariantMap data; + data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(this))); + data.insert(QStringLiteral("eventSource"), QVariant::fromValue(this)); + m_engine->aspectEngine()->setData(data); + + 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); +} + +QT_END_NAMESPACE diff --git a/examples/qt3d/examples-common/qt3dquickwindow.h b/examples/qt3d/examples-common/qt3dquickwindow.h new file mode 100644 index 000000000..85009de20 --- /dev/null +++ b/examples/qt3d/examples-common/qt3dquickwindow.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** 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 QT3DQUICKWINDOW_H +#define QT3DQUICKWINDOW_H + +#include <QQuickWindow> +#include <QUrl> + +QT_BEGIN_NAMESPACE + +namespace Qt3DCore { +class QAbstractAspect; +namespace Quick { +class QQmlAspectEngine; +} +} + +namespace Qt3DRender { +class QRenderAspect; +} + +namespace Qt3DInput { +class QInputAspect; +} + +namespace Qt3DLogic { +class QLogicAspect; +} + +class Qt3DQuickWindow : public QQuickWindow +{ + Q_OBJECT +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; + +protected: + void showEvent(QShowEvent *e) Q_DECL_OVERRIDE; + +private: + 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; +}; + +QT_END_NAMESPACE + +#endif // QT3DQUICKWINDOW_H diff --git a/examples/qt3d/examples-common/qt3dwindow.cpp b/examples/qt3d/examples-common/qt3dwindow.cpp new file mode 100644 index 000000000..9575424a8 --- /dev/null +++ b/examples/qt3d/examples-common/qt3dwindow.cpp @@ -0,0 +1,179 @@ +/**************************************************************************** +** +** 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 "qt3dwindow.h" + +#include <Qt3DRender/qforwardrenderer.h> +#include <Qt3DRender/qframegraph.h> +#include <Qt3DRender/qrenderaspect.h> +#include <Qt3DInput/qinputaspect.h> +#include <Qt3DLogic/qlogicaspect.h> + +#include <Qt3DCore/qaspectengine.h> +#include <Qt3DCore/qcamera.h> +#include <Qt3DCore/qentity.h> + +#include <QtGui/qopenglcontext.h> + +QT_BEGIN_NAMESPACE + +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_frameGraph(nullptr) + , m_defaultCamera(new Qt3DCore::QCamera) + , m_root(new Qt3DCore::QEntity) + , m_userRoot(nullptr) + , m_initialized(false) +{ + setSurfaceType(QSurface::OpenGLSurface); + + resize(1024, 768); + + QSurfaceFormat format; + if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { + format.setVersion(4, 3); + format.setProfile(QSurfaceFormat::CoreProfile); + } + 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); +} + +Qt3DWindow::Qt3DWindow(QWindow *parent) + : QWindow(parent) + , m_aspectEngine(new Qt3DCore::QAspectEngine) + , m_renderAspect(new Qt3DRender::QRenderAspect) + , m_inputAspect(new Qt3DInput::QInputAspect) + , m_logicAspect(new Qt3DLogic::QLogicAspect) + , m_frameGraph(nullptr) + , m_defaultCamera(new Qt3DCore::QCamera) + , m_root(new Qt3DCore::QEntity) + , m_userRoot(nullptr) + , m_initialized(false) +{ + setSurfaceType(QSurface::OpenGLSurface); + + resize(1024, 768); + + QSurfaceFormat format; + if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { + format.setVersion(4, 3); + format.setProfile(QSurfaceFormat::CoreProfile); + } + 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);} + +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; +} + +Qt3DCore::QCamera *Qt3DWindow::camera() const +{ + return m_defaultCamera; +} + +void Qt3DWindow::showEvent(QShowEvent *e) +{ + if (!m_initialized) { + if (m_userRoot != nullptr) + m_userRoot->setParent(m_root); + + if (m_frameGraph == nullptr) { + m_frameGraph = new Qt3DRender::QFrameGraph; + Qt3DRender::QForwardRenderer *forwardRenderer = new Qt3DRender::QForwardRenderer; + forwardRenderer->setCamera(m_defaultCamera); + m_frameGraph->setActiveFrameGraph(forwardRenderer); + } + + // TODO: Get rid of this + QVariantMap data; + data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(this))); + data.insert(QStringLiteral("eventSource"), QVariant::fromValue(this)); + m_aspectEngine->setData(data); + + m_root->addComponent(m_frameGraph); + m_aspectEngine->setRootEntity(m_root); + + // TODO: Get rid of this too + m_inputAspect->setCamera(m_defaultCamera); + + m_initialized = true; + } + + QWindow::showEvent(e); +} + +void Qt3DWindow::resizeEvent(QResizeEvent *) +{ + m_defaultCamera->setAspectRatio(float(width()) / float(height())); +} + +QT_END_NAMESPACE diff --git a/examples/qt3d/examples-common/qt3dwindow.h b/examples/qt3d/examples-common/qt3dwindow.h new file mode 100644 index 000000000..b42e3d81c --- /dev/null +++ b/examples/qt3d/examples-common/qt3dwindow.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** 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 QT3DWINDOW_H +#define QT3DWINDOW_H + +#include <QWindow> + +QT_BEGIN_NAMESPACE + +namespace Qt3DCore { +class QAspectEngine; +class QCamera; +class QEntity; +} + +namespace Qt3DRender { +class QFrameGraph; +class QRenderAspect; +} + +namespace Qt3DInput { +class QInputAspect; +} + +namespace Qt3DLogic { +class QLogicAspect; +} + +class Qt3DWindow : public QWindow +{ + Q_OBJECT +public: + Qt3DWindow(QScreen *screen = nullptr); + Qt3DWindow(QWindow *parent); + ~Qt3DWindow(); + + void registerAspect(Qt3DCore::QAbstractAspect *aspect); + void registerAspect(const QString &name); + + void setRootEntity(Qt3DCore::QEntity *root); + + Qt3DCore::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::QFrameGraph *m_frameGraph; + Qt3DCore::QCamera *m_defaultCamera; + + // Input configuration + + // Logic configuration + + // Scene + Qt3DCore::QEntity *m_root; + Qt3DCore::QEntity *m_userRoot; + + bool m_initialized; +}; + +QT_END_NAMESPACE + +#endif // QT3DWINDOW_H |