diff options
Diffstat (limited to 'src/extras/defaults')
48 files changed, 3248 insertions, 1341 deletions
diff --git a/src/extras/defaults/defaults.pri b/src/extras/defaults/defaults.pri index 8a18fb6e9..db4db24db 100644 --- a/src/extras/defaults/defaults.pri +++ b/src/extras/defaults/defaults.pri @@ -1,6 +1,8 @@ INCLUDEPATH += $$PWD HEADERS += \ + $$PWD/qdiffusespecularmaterial.h \ + $$PWD/qdiffusespecularmaterial_p.h \ $$PWD/qphongmaterial.h \ $$PWD/qphongmaterial_p.h \ $$PWD/qdiffusemapmaterial_p.h \ @@ -25,20 +27,29 @@ HEADERS += \ $$PWD/qphongalphamaterial_p.h \ $$PWD/qt3dwindow.h \ $$PWD/qt3dwindow_p.h \ + $$PWD/qabstractcameracontroller.h \ + $$PWD/qabstractcameracontroller_p.h \ $$PWD/qfirstpersoncameracontroller.h \ - $$PWD/qfirstpersoncameracontroller_p.h \ $$PWD/qorbitcameracontroller.h \ $$PWD/qorbitcameracontroller_p.h \ + $$PWD/qabstractspritesheet.h \ + $$PWD/qabstractspritesheet_p.h \ + $$PWD/qspritegrid.h \ + $$PWD/qspritegrid_p.h \ + $$PWD/qspritesheet.h \ + $$PWD/qspritesheet_p.h \ + $$PWD/qspritesheetitem.h \ + $$PWD/qspritesheetitem_p.h \ $$PWD/qtexturematerial.h \ $$PWD/qtexturematerial_p.h \ $$PWD/qmetalroughmaterial.h \ $$PWD/qmetalroughmaterial_p.h \ $$PWD/qtexturedmetalroughmaterial.h \ - $$PWD/qtexturedmetalroughmaterial_p.h \ $$PWD/qmorphphongmaterial.h \ $$PWD/qmorphphongmaterial_p.h SOURCES += \ + $$PWD/qdiffusespecularmaterial.cpp \ $$PWD/qphongmaterial.cpp \ $$PWD/qdiffusemapmaterial.cpp \ $$PWD/qnormaldiffusespecularmapmaterial.cpp \ @@ -51,8 +62,13 @@ SOURCES += \ $$PWD/qgoochmaterial.cpp \ $$PWD/qphongalphamaterial.cpp \ $$PWD/qt3dwindow.cpp \ + $$PWD/qabstractcameracontroller.cpp \ $$PWD/qfirstpersoncameracontroller.cpp \ $$PWD/qorbitcameracontroller.cpp \ + $$PWD/qabstractspritesheet.cpp \ + $$PWD/qspritegrid.cpp \ + $$PWD/qspritesheet.cpp \ + $$PWD/qspritesheetitem.cpp \ $$PWD/qtexturematerial.cpp \ $$PWD/qmetalroughmaterial.cpp \ $$PWD/qtexturedmetalroughmaterial.cpp \ diff --git a/src/extras/defaults/qabstractcameracontroller.cpp b/src/extras/defaults/qabstractcameracontroller.cpp new file mode 100644 index 000000000..2ebf187aa --- /dev/null +++ b/src/extras/defaults/qabstractcameracontroller.cpp @@ -0,0 +1,436 @@ +/**************************************************************************** +** +** 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 "qabstractcameracontroller.h" +#include "qabstractcameracontroller_p.h" + +#include <Qt3DRender/QCamera> +#include <Qt3DInput/QAxis> +#include <Qt3DInput/QAnalogAxisInput> +#include <Qt3DInput/QButtonAxisInput> +#include <Qt3DInput/QAction> +#include <Qt3DInput/QActionInput> +#include <Qt3DInput/QLogicalDevice> +#include <Qt3DInput/QKeyboardDevice> +#include <Qt3DInput/QMouseDevice> +#include <Qt3DInput/QMouseEvent> +#include <Qt3DLogic/QFrameAction> +#include <QtCore/QtGlobal> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +QAbstractCameraControllerPrivate::QAbstractCameraControllerPrivate() + : Qt3DCore::QEntityPrivate() + , m_camera(nullptr) + , m_leftMouseButtonAction(new Qt3DInput::QAction()) + , m_middleMouseButtonAction(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_middleMouseButtonInput(new Qt3DInput::QActionInput()) + , m_rightMouseButtonInput(new Qt3DInput::QActionInput()) + , m_altButtonInput(new Qt3DInput::QActionInput()) + , m_shiftButtonInput(new Qt3DInput::QActionInput()) + , m_mouseRxInput(new Qt3DInput::QAnalogAxisInput()) + , m_mouseRyInput(new Qt3DInput::QAnalogAxisInput()) + , m_mouseTzXInput(new Qt3DInput::QAnalogAxisInput()) + , m_mouseTzYInput(new Qt3DInput::QAnalogAxisInput()) + , m_keyboardTxPosInput(new Qt3DInput::QButtonAxisInput()) + , m_keyboardTyPosInput(new Qt3DInput::QButtonAxisInput()) + , m_keyboardTzPosInput(new Qt3DInput::QButtonAxisInput()) + , m_keyboardTxNegInput(new Qt3DInput::QButtonAxisInput()) + , m_keyboardTyNegInput(new Qt3DInput::QButtonAxisInput()) + , m_keyboardTzNegInput(new Qt3DInput::QButtonAxisInput()) + , 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_acceleration(-1.0f) + , m_deceleration(-1.0f) + , m_sceneUp(0.0f, 1.0f, 0.0f) +{} + +void QAbstractCameraControllerPrivate::init() +{ + //// Actions + + // Left Mouse Button Action + m_leftMouseButtonInput->setButtons(QVector<int>() << Qt::LeftButton); + m_leftMouseButtonInput->setSourceDevice(m_mouseDevice); + m_leftMouseButtonAction->addInput(m_leftMouseButtonInput); + + // Middle Mouse Button Action + m_middleMouseButtonInput->setButtons(QVector<int>() << Qt::MiddleButton); + m_middleMouseButtonInput->setSourceDevice(m_mouseDevice); + m_middleMouseButtonAction->addInput(m_middleMouseButtonInput); + + // Right Mouse Button Action + m_rightMouseButtonInput->setButtons(QVector<int>() << Qt::RightButton); + m_rightMouseButtonInput->setSourceDevice(m_mouseDevice); + m_rightMouseButtonAction->addInput(m_rightMouseButtonInput); + + // Alt Button Action + m_altButtonInput->setButtons(QVector<int>() << Qt::Key_Alt); + m_altButtonInput->setSourceDevice(m_keyboardDevice); + m_altButtonAction->addInput(m_altButtonInput); + + // Shift Button Action + m_shiftButtonInput->setButtons(QVector<int>() << 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); + + // Mouse Wheel X + m_mouseTzXInput->setAxis(Qt3DInput::QMouseDevice::WheelX); + m_mouseTzXInput->setSourceDevice(m_mouseDevice); + m_tzAxis->addInput(m_mouseTzXInput); + + // Mouse Wheel Y + m_mouseTzYInput->setAxis(Qt3DInput::QMouseDevice::WheelY); + m_mouseTzYInput->setSourceDevice(m_mouseDevice); + m_tzAxis->addInput(m_mouseTzYInput); + + // Keyboard Pos Tx + m_keyboardTxPosInput->setButtons(QVector<int>() << Qt::Key_Right); + m_keyboardTxPosInput->setScale(1.0f); + m_keyboardTxPosInput->setSourceDevice(m_keyboardDevice); + m_txAxis->addInput(m_keyboardTxPosInput); + + // Keyboard Pos Tz + m_keyboardTzPosInput->setButtons(QVector<int>() << Qt::Key_PageUp); + m_keyboardTzPosInput->setScale(1.0f); + m_keyboardTzPosInput->setSourceDevice(m_keyboardDevice); + m_tzAxis->addInput(m_keyboardTzPosInput); + + // Keyboard Pos Ty + m_keyboardTyPosInput->setButtons(QVector<int>() << Qt::Key_Up); + m_keyboardTyPosInput->setScale(1.0f); + m_keyboardTyPosInput->setSourceDevice(m_keyboardDevice); + m_tyAxis->addInput(m_keyboardTyPosInput); + + // Keyboard Neg Tx + m_keyboardTxNegInput->setButtons(QVector<int>() << Qt::Key_Left); + m_keyboardTxNegInput->setScale(-1.0f); + m_keyboardTxNegInput->setSourceDevice(m_keyboardDevice); + m_txAxis->addInput(m_keyboardTxNegInput); + + // Keyboard Neg Tz + m_keyboardTzNegInput->setButtons(QVector<int>() << Qt::Key_PageDown); + m_keyboardTzNegInput->setScale(-1.0f); + m_keyboardTzNegInput->setSourceDevice(m_keyboardDevice); + m_tzAxis->addInput(m_keyboardTzNegInput); + + // Keyboard Neg Ty + m_keyboardTyNegInput->setButtons(QVector<int>() << 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_middleMouseButtonAction); + 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); + + applyInputAccelerations(); + + Q_Q(QAbstractCameraController); + //// FrameAction + + // Disable the logical device when the entity is disabled + QObject::connect(q, &Qt3DCore::QEntity::enabledChanged, + m_logicalDevice, &Qt3DInput::QLogicalDevice::setEnabled); + + q->addComponent(m_frameAction); + q->addComponent(m_logicalDevice); +} + +void QAbstractCameraControllerPrivate::applyInputAccelerations() +{ + const auto inputs = { + m_keyboardTxPosInput, + m_keyboardTyPosInput, + m_keyboardTzPosInput, + m_keyboardTxNegInput, + m_keyboardTyNegInput, + m_keyboardTzNegInput + }; + + for (auto input : inputs) { + input->setAcceleration(m_acceleration); + input->setDeceleration(m_deceleration); + } +} + +/*! + \class Qt3DExtras::QAbstractCameraController + + \brief The QAbstractCameraController class provides basic + functionality for camera controllers. + + \inmodule Qt3DExtras + \since 5.10 + + QAbstractCameraController sets up and handles input from keyboard, + mouse, and other devices. QAbstractCameraController is an abstract + class and cannot itself be instantiated. It provides a standard + interface for camera controllers. + + Derived classes need only implement the frameActionTriggered() + method to move the camera. +*/ + +/*! + \fn void QAbstractCameraController::moveCamera(const InputState &state, float dt) = 0 + + This method is called whenever a frame action is triggered. Derived + classes must override this method to implement the camera movement + specific to the controller. + + In the base class this is a pure virtual function. +*/ + +QAbstractCameraController::QAbstractCameraController(Qt3DCore::QNode *parent) + : QAbstractCameraController(*new QAbstractCameraControllerPrivate, parent) +{ +} + +/*! \internal + */ +QAbstractCameraController::QAbstractCameraController(QAbstractCameraControllerPrivate &dd, Qt3DCore::QNode *parent) + : Qt3DCore::QEntity(dd, parent) +{ + Q_D(QAbstractCameraController); + d->init(); + + QObject::connect(d->m_frameAction, &Qt3DLogic::QFrameAction::triggered, + this, [=] (float dt) { + InputState state; + + state.rxAxisValue = d->m_rxAxis->value(); + state.ryAxisValue = d->m_ryAxis->value(); + state.txAxisValue = d->m_txAxis->value(); + state.tyAxisValue = d->m_tyAxis->value(); + state.tzAxisValue = d->m_tzAxis->value(); + + state.leftMouseButtonActive = d->m_leftMouseButtonAction->isActive(); + state.middleMouseButtonActive = d->m_middleMouseButtonAction->isActive(); + state.rightMouseButtonActive = d->m_rightMouseButtonAction->isActive(); + + state.altKeyActive = d->m_altButtonAction->isActive(); + state.shiftKeyActive = d->m_shiftButtonAction->isActive(); + + moveCamera(state, dt); + }); +} + +QAbstractCameraController::~QAbstractCameraController() +{ +} + +/*! + \property QAbstractCameraController::camera + + Holds the currently controlled camera. +*/ +Qt3DRender::QCamera *QAbstractCameraController::camera() const +{ + Q_D(const QAbstractCameraController); + return d->m_camera; +} + +/*! + \property QAbstractCameraController::linearSpeed + + Holds the current linear speed of the camera controller. Linear speed determines the + movement speed of the camera. + + The default is 10.0. +*/ +float QAbstractCameraController::linearSpeed() const +{ + Q_D(const QAbstractCameraController); + return d->m_linearSpeed; +} + +/*! + \property QAbstractCameraController::lookSpeed + + Holds the current look speed of the camera controller. The look speed determines the turn rate + of the camera pan and tilt. + + The default is 180.0. +*/ +float QAbstractCameraController::lookSpeed() const +{ + Q_D(const QAbstractCameraController); + return d->m_lookSpeed; +} + +/*! + \property QAbstractCameraController::acceleration + + Holds the current acceleration of the camera controller. +*/ +float QAbstractCameraController::acceleration() const +{ + Q_D(const QAbstractCameraController); + return d->m_acceleration; +} + +/*! + \property QAbstractCameraController::deceleration + + Holds the current deceleration of the camera controller. +*/ +float QAbstractCameraController::deceleration() const +{ + Q_D(const QAbstractCameraController); + return d->m_deceleration; +} + +void QAbstractCameraController::setCamera(Qt3DRender::QCamera *camera) +{ + Q_D(QAbstractCameraController); + if (d->m_camera != camera) { + + if (d->m_camera) + d->unregisterDestructionHelper(d->m_camera); + + if (camera && !camera->parent()) + camera->setParent(this); + + d->m_camera = camera; + + // Ensures proper bookkeeping + if (d->m_camera) + d->registerDestructionHelper(d->m_camera, &QAbstractCameraController::setCamera, d->m_camera); + + emit cameraChanged(); + } +} + +void QAbstractCameraController::setLinearSpeed(float linearSpeed) +{ + Q_D(QAbstractCameraController); + if (d->m_linearSpeed != linearSpeed) { + d->m_linearSpeed = linearSpeed; + emit linearSpeedChanged(); + } +} + +void QAbstractCameraController::setLookSpeed(float lookSpeed) +{ + Q_D(QAbstractCameraController); + if (d->m_lookSpeed != lookSpeed) { + d->m_lookSpeed = lookSpeed; + emit lookSpeedChanged(); + } +} + +void QAbstractCameraController::setAcceleration(float acceleration) +{ + Q_D(QAbstractCameraController); + if (d->m_acceleration != acceleration) { + d->m_acceleration = acceleration; + d->applyInputAccelerations(); + emit accelerationChanged(acceleration); + } +} + +void QAbstractCameraController::setDeceleration(float deceleration) +{ + Q_D(QAbstractCameraController); + if (d->m_deceleration != deceleration) { + d->m_deceleration = deceleration; + d->applyInputAccelerations(); + emit decelerationChanged(deceleration); + } +} + +/*! + Provides access to the keyboard device. +*/ + +Qt3DInput::QKeyboardDevice *QAbstractCameraController::keyboardDevice() const +{ + Q_D(const QAbstractCameraController); + return d->m_keyboardDevice; +} + +/*! + Provides access to the mouse device. +*/ + +Qt3DInput::QMouseDevice *QAbstractCameraController::mouseDevice() const +{ + Q_D(const QAbstractCameraController); + return d->m_mouseDevice; +} + +} // Qt3DExtras + +QT_END_NAMESPACE + +#include "moc_qabstractcameracontroller.cpp" diff --git a/src/extras/defaults/qabstractcameracontroller.h b/src/extras/defaults/qabstractcameracontroller.h new file mode 100644 index 000000000..f13079f5f --- /dev/null +++ b/src/extras/defaults/qabstractcameracontroller.h @@ -0,0 +1,126 @@ +/**************************************************************************** +** +** 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_QABSTRACTCAMERACONTROLLER_H +#define QT3DEXTRAS_QABSTRACTCAMERACONTROLLER_H + +#include <Qt3DCore/QEntity> +#include <Qt3DExtras/qt3dextras_global.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DInput { +class QKeyboardDevice; +class QMouseDevice; +} + +namespace Qt3DRender { +class QCamera; +} + +namespace Qt3DExtras { + +class QAbstractCameraControllerPrivate; + +class QT3DEXTRASSHARED_EXPORT QAbstractCameraController : 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 acceleration READ acceleration WRITE setAcceleration NOTIFY accelerationChanged) + Q_PROPERTY(float deceleration READ deceleration WRITE setDeceleration NOTIFY decelerationChanged) + +public: + ~QAbstractCameraController(); + + Qt3DRender::QCamera *camera() const; + float linearSpeed() const; + float lookSpeed() const; + + float acceleration() const; + float deceleration() const; + + void setCamera(Qt3DRender::QCamera *camera); + void setLinearSpeed(float linearSpeed); + void setLookSpeed(float lookSpeed); + + void setAcceleration(float acceleration); + void setDeceleration(float deceleration); + +Q_SIGNALS: + void cameraChanged(); + void linearSpeedChanged(); + void lookSpeedChanged(); + + void accelerationChanged(float acceleration); + void decelerationChanged(float deceleration); + +protected: + explicit QAbstractCameraController(Qt3DCore::QNode *parent = nullptr); + QAbstractCameraController(QAbstractCameraControllerPrivate &dd, Qt3DCore::QNode *parent = nullptr); + + Qt3DInput::QKeyboardDevice *keyboardDevice() const; + Qt3DInput::QMouseDevice *mouseDevice() const; + + struct InputState + { + float rxAxisValue; + float ryAxisValue; + float txAxisValue; + float tyAxisValue; + float tzAxisValue; + + bool leftMouseButtonActive; + bool middleMouseButtonActive; + bool rightMouseButtonActive; + + bool altKeyActive; + bool shiftKeyActive; + }; + +private: + virtual void moveCamera(const InputState &state, float dt) = 0; + +private: + Q_DECLARE_PRIVATE(QAbstractCameraController) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QABSTRACTCAMERACONTROLLER_H diff --git a/src/extras/defaults/qabstractcameracontroller_p.h b/src/extras/defaults/qabstractcameracontroller_p.h new file mode 100644 index 000000000..00424a55b --- /dev/null +++ b/src/extras/defaults/qabstractcameracontroller_p.h @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** 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 QT3DEXTRAS_QABSTRACTCAMERACONTROLLER_P_H +#define QT3DEXTRAS_QABSTRACTCAMERACONTROLLER_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 <Qt3DExtras/qabstractcameracontroller.h> +#include <QtGui/QVector3D> + +#include <Qt3DCore/private/qentity_p.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 QAnalogAxisInput; +class QButtonAxisInput; +class QAxisActionHandler; +} + +namespace Qt3DExtras { + +class QAbstractCameraControllerPrivate : public Qt3DCore::QEntityPrivate +{ +public: + QAbstractCameraControllerPrivate(); + + void init(); + void applyInputAccelerations(); + + Qt3DRender::QCamera *m_camera; + + Qt3DInput::QAction *m_leftMouseButtonAction; + Qt3DInput::QAction *m_middleMouseButtonAction; + 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_middleMouseButtonInput; + Qt3DInput::QActionInput *m_rightMouseButtonInput; + Qt3DInput::QActionInput *m_altButtonInput; + Qt3DInput::QActionInput *m_shiftButtonInput; + + Qt3DInput::QAnalogAxisInput *m_mouseRxInput; + Qt3DInput::QAnalogAxisInput *m_mouseRyInput; + Qt3DInput::QAnalogAxisInput *m_mouseTzXInput; + Qt3DInput::QAnalogAxisInput *m_mouseTzYInput; + Qt3DInput::QButtonAxisInput *m_keyboardTxPosInput; + Qt3DInput::QButtonAxisInput *m_keyboardTyPosInput; + Qt3DInput::QButtonAxisInput *m_keyboardTzPosInput; + Qt3DInput::QButtonAxisInput *m_keyboardTxNegInput; + Qt3DInput::QButtonAxisInput *m_keyboardTyNegInput; + Qt3DInput::QButtonAxisInput *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_acceleration; + float m_deceleration; + + QVector3D m_sceneUp; + + Q_DECLARE_PUBLIC(QAbstractCameraController) +}; + +} // namespace Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QABSTRACTCAMERACONTROLLER_P_H diff --git a/src/extras/defaults/qabstractspritesheet.cpp b/src/extras/defaults/qabstractspritesheet.cpp new file mode 100644 index 000000000..640e600c7 --- /dev/null +++ b/src/extras/defaults/qabstractspritesheet.cpp @@ -0,0 +1,143 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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$ +** +****************************************************************************/ + +#include "qabstractspritesheet.h" +#include "qabstractspritesheet_p.h" + +#include <Qt3DRender/qabstracttexture.h> + +QT_BEGIN_NAMESPACE + +using namespace Qt3DCore; + +namespace Qt3DExtras { + +QAbstractSpriteSheetPrivate::QAbstractSpriteSheetPrivate() + : QNodePrivate() + , m_texture(nullptr) + , m_currentIndex(-1) +{ +} + +void QAbstractSpriteSheetPrivate::init() +{ + m_textureTransform.setToIdentity(); +} + +void QAbstractSpriteSheetPrivate::updateTexture(Qt3DRender::QAbstractTexture *texture) +{ + if (m_texture) { + disconnect(m_texture, &Qt3DRender::QAbstractTexture::widthChanged, + this, &QAbstractSpriteSheetPrivate::updateSizes); + disconnect(m_texture, &Qt3DRender::QAbstractTexture::heightChanged, + this, &QAbstractSpriteSheetPrivate::updateSizes); + } + m_texture = texture; + if (m_texture) { + connect(m_texture, &Qt3DRender::QAbstractTexture::widthChanged, + this, &QAbstractSpriteSheetPrivate::updateSizes); + connect(m_texture, &Qt3DRender::QAbstractTexture::heightChanged, + this, &QAbstractSpriteSheetPrivate::updateSizes); + } +} + +void QAbstractSpriteSheetPrivate::updateIndex(int newIndex) +{ + Q_Q(QAbstractSpriteSheet); + if (newIndex > maxIndex()) + newIndex = 0; + + m_currentIndex = newIndex; + emit q->currentIndexChanged(newIndex); + updateTransform(); +} + +QAbstractSpriteSheet::QAbstractSpriteSheet(QAbstractSpriteSheetPrivate &dd, QNode *parent) + : Qt3DCore::QNode(dd, parent) +{ + Q_D(QAbstractSpriteSheet); + d->init(); +} + +QAbstractSpriteSheet::~QAbstractSpriteSheet() +{ +} + +/*! + \property QAbstractSpriteSheet::texture + + Holds the current texture used by the material. +*/ +Qt3DRender::QAbstractTexture *QAbstractSpriteSheet::texture() const +{ + Q_D(const QAbstractSpriteSheet); + return d->m_texture; +} + +void QAbstractSpriteSheet::setTexture(Qt3DRender::QAbstractTexture *texture) +{ + Q_D(QAbstractSpriteSheet); + if (d->m_texture != texture) { + d->updateTexture(texture); + d->updateSizes(); + emit textureChanged(d->m_texture); + } +} + +QMatrix3x3 QAbstractSpriteSheet::textureTransform() const +{ + Q_D(const QAbstractSpriteSheet); + return d->m_textureTransform; +} + +int QAbstractSpriteSheet::currentIndex() const +{ + Q_D(const QAbstractSpriteSheet); + return d->m_currentIndex; +} + +void QAbstractSpriteSheet::setCurrentIndex(int currentIndex) +{ + Q_D(QAbstractSpriteSheet); + d->updateIndex(currentIndex); +} + +} // namespace Qt3DExtras + +QT_END_NAMESPACE diff --git a/src/extras/defaults/qabstractspritesheet.h b/src/extras/defaults/qabstractspritesheet.h new file mode 100644 index 000000000..cb8b70fd9 --- /dev/null +++ b/src/extras/defaults/qabstractspritesheet.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QABSTRACTSPRITESHEET_H +#define QT3DEXTRAS_QABSTRACTSPRITESHEET_H + +#include <Qt3DExtras/qt3dextras_global.h> +#include <Qt3DCore/QNode> +#include <QVector2D> +#include <QMatrix3x3> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +class QAbstractTexture; + +} // namespace Qt3DRender + +namespace Qt3DExtras { + +class QAbstractSpriteSheetPrivate; + +class QT3DEXTRASSHARED_EXPORT QAbstractSpriteSheet : public Qt3DCore::QNode +{ + Q_OBJECT + Q_PROPERTY(Qt3DRender::QAbstractTexture *texture READ texture WRITE setTexture NOTIFY textureChanged) + Q_PROPERTY(QMatrix3x3 textureTransform READ textureTransform NOTIFY textureTransformChanged) + Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) +public: + ~QAbstractSpriteSheet(); + + Qt3DRender::QAbstractTexture *texture() const; + QMatrix3x3 textureTransform() const; + int currentIndex() const; + +public Q_SLOTS: + void setTexture(Qt3DRender::QAbstractTexture *texture); + void setCurrentIndex(int currentIndex); + +Q_SIGNALS: + void textureChanged(Qt3DRender::QAbstractTexture *texture); + void textureTransformChanged(const QMatrix3x3 &textureTransform); + void currentIndexChanged(int currentIndex); + +protected: + explicit QAbstractSpriteSheet(QAbstractSpriteSheetPrivate &d, Qt3DCore::QNode *parent = nullptr); + +private: + Q_DECLARE_PRIVATE(QAbstractSpriteSheet) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QABSTRACTSPRITESHEET_H diff --git a/src/extras/defaults/qabstractspritesheet_p.h b/src/extras/defaults/qabstractspritesheet_p.h new file mode 100644 index 000000000..604fc3c62 --- /dev/null +++ b/src/extras/defaults/qabstractspritesheet_p.h @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QABSTRACTSPRITESHEET_P_H +#define QT3DEXTRAS_QABSTRACTSPRITESHEET_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/qnode_p.h> +#include <QMatrix3x3> +#include <QSize> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +class QFilterKey; +class QEffect; +class QAbstractTexture; +class QTechnique; +class QParameter; +class QShaderProgram; +class QRenderPass; + +} // namespace Qt3DRender + +namespace Qt3DExtras { + +class QAbstractSpriteSheet; + +class QAbstractSpriteSheetPrivate : public Qt3DCore::QNodePrivate +{ +public: + QAbstractSpriteSheetPrivate(); + + virtual void init(); + void updateTexture(Qt3DRender::QAbstractTexture *texture); + void updateIndex(int newIndex); + virtual int maxIndex() const = 0; + virtual void updateSizes() = 0; + virtual void updateTransform() = 0; + + Qt3DRender::QAbstractTexture *m_texture; + QMatrix3x3 m_textureTransform; + QSize m_textureSize; + int m_currentIndex; + + Q_DECLARE_PUBLIC(QAbstractSpriteSheet) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QABSTRACTSPRITESHEET_P_H + diff --git a/src/extras/defaults/qdiffusemapmaterial.cpp b/src/extras/defaults/qdiffusemapmaterial.cpp index a6da98a2e..26fb99c2c 100644 --- a/src/extras/defaults/qdiffusemapmaterial.cpp +++ b/src/extras/defaults/qdiffusemapmaterial.cpp @@ -46,6 +46,7 @@ #include <Qt3DRender/qtexture.h> #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> @@ -75,7 +76,9 @@ QDiffuseMapMaterialPrivate::QDiffuseMapMaterialPrivate() , m_diffuseMapGL2RenderPass(new QRenderPass()) , m_diffuseMapES2RenderPass(new QRenderPass()) , m_diffuseMapGL3Shader(new QShaderProgram()) + , m_diffuseMapGL3ShaderBuilder(new QShaderProgramBuilder()) , m_diffuseMapGL2ES2Shader(new QShaderProgram()) + , m_diffuseMapGL2ES2ShaderBuilder(new QShaderProgramBuilder()) , m_filterKey(new QFilterKey) { m_diffuseTexture->setMagnificationFilter(QAbstractTexture::Linear); @@ -87,6 +90,8 @@ QDiffuseMapMaterialPrivate::QDiffuseMapMaterialPrivate() void QDiffuseMapMaterialPrivate::init() { + Q_Q(QDiffuseMapMaterial); + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, this, &QDiffuseMapMaterialPrivate::handleAmbientChanged); connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, @@ -98,10 +103,21 @@ void QDiffuseMapMaterialPrivate::init() connect(m_textureScaleParameter, &Qt3DRender::QParameter::valueChanged, this, &QDiffuseMapMaterialPrivate::handleTextureScaleChanged); - m_diffuseMapGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusemap.vert")))); - m_diffuseMapGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusemap.frag")))); - m_diffuseMapGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusemap.vert")))); - m_diffuseMapGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusemap.frag")))); + m_diffuseMapGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_diffuseMapGL3ShaderBuilder->setParent(q); + m_diffuseMapGL3ShaderBuilder->setShaderProgram(m_diffuseMapGL3Shader); + m_diffuseMapGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_diffuseMapGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specular"), + QStringLiteral("normal")}); + + m_diffuseMapGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_diffuseMapGL2ES2ShaderBuilder->setParent(q); + m_diffuseMapGL2ES2ShaderBuilder->setShaderProgram(m_diffuseMapGL2ES2Shader); + m_diffuseMapGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_diffuseMapGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specular"), + QStringLiteral("normal")}); m_diffuseMapGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_diffuseMapGL3Technique->graphicsApiFilter()->setMajorVersion(3); @@ -118,7 +134,6 @@ void QDiffuseMapMaterialPrivate::init() m_diffuseMapES2Technique->graphicsApiFilter()->setMinorVersion(0); m_diffuseMapES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); - Q_Q(QDiffuseMapMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -186,6 +201,9 @@ void QDiffuseMapMaterialPrivate::handleTextureScaleChanged(const QVariant &var) \since 5.7 \inherits Qt3DRender::QMaterial + \deprecated + \see Qt3DExtras::QDiffuseSpecularMaterial + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. The relative strengths of these components are controlled by means of their reflectivity coefficients which are modelled as RGB triplets: diff --git a/src/extras/defaults/qdiffusemapmaterial_p.h b/src/extras/defaults/qdiffusemapmaterial_p.h index c0bcc01eb..cef75a3f0 100644 --- a/src/extras/defaults/qdiffusemapmaterial_p.h +++ b/src/extras/defaults/qdiffusemapmaterial_p.h @@ -63,6 +63,7 @@ class QAbstractTexture; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; } // namespace Qt3DRender @@ -97,7 +98,9 @@ class QDiffuseMapMaterialPrivate : public Qt3DRender::QMaterialPrivate Qt3DRender::QRenderPass *m_diffuseMapGL2RenderPass; Qt3DRender::QRenderPass *m_diffuseMapES2RenderPass; Qt3DRender::QShaderProgram *m_diffuseMapGL3Shader; + Qt3DRender::QShaderProgramBuilder *m_diffuseMapGL3ShaderBuilder; Qt3DRender::QShaderProgram *m_diffuseMapGL2ES2Shader; + Qt3DRender::QShaderProgramBuilder *m_diffuseMapGL2ES2ShaderBuilder; Qt3DRender::QFilterKey *m_filterKey; Q_DECLARE_PUBLIC(QDiffuseMapMaterial) diff --git a/src/extras/defaults/qdiffusespecularmapmaterial.cpp b/src/extras/defaults/qdiffusespecularmapmaterial.cpp index 615ee6305..727eaed27 100644 --- a/src/extras/defaults/qdiffusespecularmapmaterial.cpp +++ b/src/extras/defaults/qdiffusespecularmapmaterial.cpp @@ -46,6 +46,7 @@ #include <Qt3DRender/qtexture.h> #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> @@ -76,7 +77,9 @@ QDiffuseSpecularMapMaterialPrivate::QDiffuseSpecularMapMaterialPrivate() , m_diffuseSpecularMapGL2RenderPass(new QRenderPass()) , m_diffuseSpecularMapES2RenderPass(new QRenderPass()) , m_diffuseSpecularMapGL3Shader(new QShaderProgram()) + , m_diffuseSpecularMapGL3ShaderBuilder(new QShaderProgramBuilder()) , m_diffuseSpecularMapGL2ES2Shader(new QShaderProgram()) + , m_diffuseSpecularMapGL2ES2ShaderBuilder(new QShaderProgramBuilder()) , m_filterKey(new QFilterKey) { m_diffuseTexture->setMagnificationFilter(QAbstractTexture::Linear); @@ -94,6 +97,8 @@ QDiffuseSpecularMapMaterialPrivate::QDiffuseSpecularMapMaterialPrivate() void QDiffuseSpecularMapMaterialPrivate::init() { + Q_Q(QDiffuseSpecularMapMaterial); + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, this, &QDiffuseSpecularMapMaterialPrivate::handleAmbientChanged); connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, @@ -105,10 +110,21 @@ void QDiffuseSpecularMapMaterialPrivate::init() connect(m_textureScaleParameter, &Qt3DRender::QParameter::valueChanged, this, &QDiffuseSpecularMapMaterialPrivate::handleTextureScaleChanged); - m_diffuseSpecularMapGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusemap.vert")))); - m_diffuseSpecularMapGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/diffusespecularmap.frag")))); - m_diffuseSpecularMapGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusemap.vert")))); - m_diffuseSpecularMapGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/diffusespecularmap.frag")))); + m_diffuseSpecularMapGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_diffuseSpecularMapGL3ShaderBuilder->setParent(q); + m_diffuseSpecularMapGL3ShaderBuilder->setShaderProgram(m_diffuseSpecularMapGL3Shader); + m_diffuseSpecularMapGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_diffuseSpecularMapGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specularTexture"), + QStringLiteral("normal")}); + + m_diffuseSpecularMapGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_diffuseSpecularMapGL2ES2ShaderBuilder->setParent(q); + m_diffuseSpecularMapGL2ES2ShaderBuilder->setShaderProgram(m_diffuseSpecularMapGL2ES2Shader); + m_diffuseSpecularMapGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_diffuseSpecularMapGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specularTexture"), + QStringLiteral("normal")}); m_diffuseSpecularMapGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_diffuseSpecularMapGL3Technique->graphicsApiFilter()->setMajorVersion(3); @@ -125,7 +141,6 @@ void QDiffuseSpecularMapMaterialPrivate::init() m_diffuseSpecularMapES2Technique->graphicsApiFilter()->setMinorVersion(0); m_diffuseSpecularMapES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); - Q_Q(QDiffuseSpecularMapMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -193,6 +208,9 @@ void QDiffuseSpecularMapMaterialPrivate::handleTextureScaleChanged(const QVarian \since 5.7 \inherits Qt3DRender::QMaterial + \deprecated + \see Qt3DExtras::QDiffuseSpecularMaterial + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. The relative strengths of these components are controlled by means of their reflectivity coefficients which are modelled as RGB triplets: diff --git a/src/extras/defaults/qdiffusespecularmapmaterial_p.h b/src/extras/defaults/qdiffusespecularmapmaterial_p.h index b358e088a..f80922dff 100644 --- a/src/extras/defaults/qdiffusespecularmapmaterial_p.h +++ b/src/extras/defaults/qdiffusespecularmapmaterial_p.h @@ -63,6 +63,7 @@ class QAbstractTexture; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; } // namespace Qt3DRender @@ -99,7 +100,9 @@ public: Qt3DRender::QRenderPass *m_diffuseSpecularMapGL2RenderPass; Qt3DRender::QRenderPass *m_diffuseSpecularMapES2RenderPass; Qt3DRender::QShaderProgram *m_diffuseSpecularMapGL3Shader; + Qt3DRender::QShaderProgramBuilder *m_diffuseSpecularMapGL3ShaderBuilder; Qt3DRender::QShaderProgram *m_diffuseSpecularMapGL2ES2Shader; + Qt3DRender::QShaderProgramBuilder *m_diffuseSpecularMapGL2ES2ShaderBuilder; Qt3DRender::QFilterKey *m_filterKey; Q_DECLARE_PUBLIC(QDiffuseSpecularMapMaterial) diff --git a/src/extras/defaults/qdiffusespecularmaterial.cpp b/src/extras/defaults/qdiffusespecularmaterial.cpp new file mode 100644 index 000000000..83bba9e41 --- /dev/null +++ b/src/extras/defaults/qdiffusespecularmaterial.cpp @@ -0,0 +1,496 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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$ +** +****************************************************************************/ + +#include "qdiffusespecularmaterial.h" +#include "qdiffusespecularmaterial_p.h" + +#include <Qt3DRender/qfilterkey.h> +#include <Qt3DRender/qmaterial.h> +#include <Qt3DRender/qeffect.h> +#include <Qt3DRender/qtechnique.h> +#include <Qt3DRender/qtexture.h> +#include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> +#include <Qt3DRender/qparameter.h> +#include <Qt3DRender/qrenderpass.h> +#include <Qt3DRender/qgraphicsapifilter.h> +#include <Qt3DRender/qblendequation.h> +#include <Qt3DRender/qblendequationarguments.h> +#include <Qt3DRender/qnodepthmask.h> +#include <QtCore/QUrl> +#include <QtGui/QVector3D> +#include <QtGui/QVector4D> + + +QT_BEGIN_NAMESPACE + +using namespace Qt3DRender; + +namespace Qt3DExtras { + +QDiffuseSpecularMaterialPrivate::QDiffuseSpecularMaterialPrivate() + : QMaterialPrivate() + , m_effect(new QEffect()) + , m_ambientParameter(new QParameter(QStringLiteral("ka"), QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f))) + , m_diffuseParameter(new QParameter(QStringLiteral("kd"), QColor::fromRgbF(0.7f, 0.7f, 0.7f, 1.0f))) + , m_specularParameter(new QParameter(QStringLiteral("ks"), QColor::fromRgbF(0.01f, 0.01f, 0.01f, 1.0f))) + , m_diffuseTextureParameter(new QParameter(QStringLiteral("diffuseTexture"), QVariant())) + , m_specularTextureParameter(new QParameter(QStringLiteral("specularTexture"), QVariant())) + , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f)) + , m_normalTextureParameter(new QParameter(QStringLiteral("normalTexture"), QVariant())) + , m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale"), 1.0f)) + , m_gl3Technique(new QTechnique()) + , m_gl2Technique(new QTechnique()) + , m_es2Technique(new QTechnique()) + , m_gl3RenderPass(new QRenderPass()) + , m_gl2RenderPass(new QRenderPass()) + , m_es2RenderPass(new QRenderPass()) + , m_gl3Shader(new QShaderProgram()) + , m_gl3ShaderBuilder(new QShaderProgramBuilder()) + , m_gl2es2Shader(new QShaderProgram()) + , m_gl2es2ShaderBuilder(new QShaderProgramBuilder()) + , m_noDepthMask(new QNoDepthMask()) + , m_blendState(new QBlendEquationArguments()) + , m_blendEquation(new QBlendEquation()) + , m_filterKey(new QFilterKey) +{ +} + +void QDiffuseSpecularMaterialPrivate::init() +{ + Q_Q(QDiffuseSpecularMaterial); + + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, + this, &QDiffuseSpecularMaterialPrivate::handleAmbientChanged); + QObject::connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, + q, &QDiffuseSpecularMaterial::diffuseChanged); + QObject::connect(m_specularParameter, &Qt3DRender::QParameter::valueChanged, + q, &QDiffuseSpecularMaterial::specularChanged); + connect(m_shininessParameter, &Qt3DRender::QParameter::valueChanged, + this, &QDiffuseSpecularMaterialPrivate::handleShininessChanged); + QObject::connect(m_normalTextureParameter, &Qt3DRender::QParameter::valueChanged, + q, &QDiffuseSpecularMaterial::normalChanged); + connect(m_textureScaleParameter, &Qt3DRender::QParameter::valueChanged, + this, &QDiffuseSpecularMaterialPrivate::handleTextureScaleChanged); + QObject::connect(m_noDepthMask, &QNoDepthMask::enabledChanged, + q, &QDiffuseSpecularMaterial::alphaBlendingEnabledChanged); + + m_gl3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_gl3ShaderBuilder->setParent(q); + m_gl3ShaderBuilder->setShaderProgram(m_gl3Shader); + m_gl3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_gl3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse"), + QStringLiteral("specular"), + QStringLiteral("normal")}); + + m_gl2es2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_gl2es2ShaderBuilder->setParent(q); + m_gl2es2ShaderBuilder->setShaderProgram(m_gl2es2Shader); + m_gl2es2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_gl2es2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse"), + QStringLiteral("specular"), + QStringLiteral("normal")}); + + + m_gl3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); + m_gl3Technique->graphicsApiFilter()->setMajorVersion(3); + m_gl3Technique->graphicsApiFilter()->setMinorVersion(1); + m_gl3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile); + + m_gl2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); + m_gl2Technique->graphicsApiFilter()->setMajorVersion(2); + m_gl2Technique->graphicsApiFilter()->setMinorVersion(0); + m_gl2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); + + m_es2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGLES); + m_es2Technique->graphicsApiFilter()->setMajorVersion(2); + m_es2Technique->graphicsApiFilter()->setMinorVersion(0); + m_es2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); + + m_noDepthMask->setEnabled(false); + m_blendState->setEnabled(false); + m_blendState->setSourceRgb(QBlendEquationArguments::SourceAlpha); + m_blendState->setDestinationRgb(QBlendEquationArguments::OneMinusSourceAlpha); + m_blendEquation->setEnabled(false); + m_blendEquation->setBlendFunction(QBlendEquation::Add); + + m_gl3RenderPass->setShaderProgram(m_gl3Shader); + m_gl2RenderPass->setShaderProgram(m_gl2es2Shader); + m_es2RenderPass->setShaderProgram(m_gl2es2Shader); + + m_gl3RenderPass->addRenderState(m_noDepthMask); + m_gl3RenderPass->addRenderState(m_blendState); + m_gl3RenderPass->addRenderState(m_blendEquation); + + m_gl2RenderPass->addRenderState(m_noDepthMask); + m_gl2RenderPass->addRenderState(m_blendState); + m_gl2RenderPass->addRenderState(m_blendEquation); + + m_es2RenderPass->addRenderState(m_noDepthMask); + m_es2RenderPass->addRenderState(m_blendState); + m_es2RenderPass->addRenderState(m_blendEquation); + + m_gl3Technique->addRenderPass(m_gl3RenderPass); + m_gl2Technique->addRenderPass(m_gl2RenderPass); + m_es2Technique->addRenderPass(m_es2RenderPass); + + m_filterKey->setParent(q); + m_filterKey->setName(QStringLiteral("renderingStyle")); + m_filterKey->setValue(QStringLiteral("forward")); + + m_gl3Technique->addFilterKey(m_filterKey); + m_gl2Technique->addFilterKey(m_filterKey); + m_es2Technique->addFilterKey(m_filterKey); + + m_effect->addTechnique(m_gl3Technique); + m_effect->addTechnique(m_gl2Technique); + m_effect->addTechnique(m_es2Technique); + + m_effect->addParameter(m_ambientParameter); + m_effect->addParameter(m_diffuseParameter); + m_effect->addParameter(m_specularParameter); + m_effect->addParameter(m_shininessParameter); + m_effect->addParameter(m_textureScaleParameter); + + q->setEffect(m_effect); +} + +void QDiffuseSpecularMaterialPrivate::handleAmbientChanged(const QVariant &var) +{ + Q_Q(QDiffuseSpecularMaterial); + emit q->ambientChanged(var.value<QColor>()); +} + +void QDiffuseSpecularMaterialPrivate::handleShininessChanged(const QVariant &var) +{ + Q_Q(QDiffuseSpecularMaterial); + emit q->shininessChanged(var.toFloat()); +} + +void QDiffuseSpecularMaterialPrivate::handleTextureScaleChanged(const QVariant &var) +{ + Q_Q(QDiffuseSpecularMaterial); + emit q->textureScaleChanged(var.toFloat()); +} + +/*! + \class Qt3DExtras::QDiffuseSpecularMaterial + \brief The QDiffuseSpecularMaterial class provides a default implementation + of the phong lighting effect. + \inmodule Qt3DExtras + \since 5.10 + \inherits Qt3DRender::QMaterial + + The phong lighting effect is based on the combination of 3 lighting + components ambient, diffuse and specular. The relative strengths of these + components are controlled by means of their reflectivity coefficients which + are modelled as RGB triplets: + + \list + \li Ambient is the color that is emitted by an object without any other + light source. + \li Diffuse is the color that is emitted for rought surface reflections + with the lights. + \li Specular is the color emitted for shiny surface reflections with the + lights. + \li The shininess of a surface is controlled by a float property. + \endlist + + This material uses an effect with a single render pass approach and + performs per fragment lighting. Techniques are provided for OpenGL 2, + OpenGL 3 or above as well as OpenGL ES 2. +*/ +/*! + \qmltype DiffuseSpecularMaterial + \brief The DiffuseSpecularMaterial class provides a default implementation + of the phong lighting effect. + \since 5.10 + \inqmlmodule Qt3D.Extras + \instantiates Qt3DExtras::QDiffuseSpecularMaterial + + The phong lighting effect is based on the combination of 3 lighting + components ambient, diffuse and specular. The relative strengths of these + components are controlled by means of their reflectivity coefficients which + are modelled as RGB triplets: + + \list + \li Ambient is the color that is emitted by an object without any other + light source. + \li Diffuse is the color that is emitted for rought surface reflections + with the lights. + \li Specular is the color emitted for shiny surface reflections with the + lights. + \li The shininess of a surface is controlled by a float property. + \endlist + + This material uses an effect with a single render pass approach and + performs per fragment lighting. Techniques are provided for OpenGL 2, + OpenGL 3 or above as well as OpenGL ES 2. + */ + +/*! + Constructs a new QDiffuseSpecularMaterial instance with parent object \a parent. +*/ +QDiffuseSpecularMaterial::QDiffuseSpecularMaterial(QNode *parent) + : QMaterial(*new QDiffuseSpecularMaterialPrivate, parent) +{ + Q_D(QDiffuseSpecularMaterial); + d->init(); +} + +/*! + Destroys the QDiffuseSpecularMaterial. +*/ +QDiffuseSpecularMaterial::~QDiffuseSpecularMaterial() +{ +} + +/*! + \property QDiffuseSpecularMaterial::ambient + + Holds the ambient color. +*/ +/*! + \qmlproperty color DiffuseSpecularMaterial::ambient + + Holds the ambient color. +*/ +QColor QDiffuseSpecularMaterial::ambient() const +{ + Q_D(const QDiffuseSpecularMaterial); + return d->m_ambientParameter->value().value<QColor>(); +} + +/*! + \property QDiffuseSpecularMaterial::diffuse + + Holds the diffuse color of the material. This can be either a plain color + value or a texture. +*/ +/*! + \qmlproperty var DiffuseSpecularMaterial::diffuse + + Holds the diffuse color of the material. This can be either a plain color + value or a texture. +*/ +QVariant QDiffuseSpecularMaterial::diffuse() const +{ + Q_D(const QDiffuseSpecularMaterial); + return d->m_diffuseParameter->value(); +} + +/*! + \property QDiffuseSpecularMaterial::specular + + Holds the specular color of the material. This can be either a plain color + value or a texture. +*/ +/*! + \qmlproperty var DiffuseSpecularMaterial::specular + + Holds the specular color of the material. This can be either a plain color + value or a texture. +*/ +QVariant QDiffuseSpecularMaterial::specular() const +{ + Q_D(const QDiffuseSpecularMaterial); + return d->m_specularParameter->value(); +} + +/*! + \property QDiffuseSpecularMaterial::shininess + + Holds the shininess exponent. +*/ +/*! + \qmlproperty real DiffuseSpecularMaterial::shininess + + Holds the shininess exponent. +*/ +float QDiffuseSpecularMaterial::shininess() const +{ + Q_D(const QDiffuseSpecularMaterial); + return d->m_shininessParameter->value().toFloat(); +} + +/*! + \property QDiffuseSpecularMaterial::normal + + Holds the current normal map texture of the material. This can only be a + texture, otherwise it is ignored. By default this map is not set. +*/ +/*! + \qmlproperty var DiffuseSpecularMaterial::normal + + Holds the current normal map texture of the material. This can only be a + texture, otherwise it is ignored. By default this map is not set. +*/ +QVariant QDiffuseSpecularMaterial::normal() const +{ + Q_D(const QDiffuseSpecularMaterial); + return d->m_normalTextureParameter->value(); +} + +/*! + \property QDiffuseSpecularMaterial::textureScale + + Holds the current texture scale. It is applied as a multiplier to texture + coordinates at render time. Defaults to 1.0. +*/ +/*! + \qmlproperty real DiffuseSpecularMaterial::textureScale + + Holds the current texture scale. It is applied as a multiplier to texture + coordinates at render time. Defaults to 1.0. +*/ +float QDiffuseSpecularMaterial::textureScale() const +{ + Q_D(const QDiffuseSpecularMaterial); + return d->m_textureScaleParameter->value().toFloat(); +} + +/*! + \property QDiffuseSpecularMaterial::alphaBlending + + Indicates if the alpha information coming from the diffuse property will + be taken into account during rendering. Defaults to false. +*/ +/*! + \qmlproperty bool DiffuseSpecularMaterial::alphaBlending + + Indicates if the alpha information coming from the diffuse property will + be taken into account during rendering. Defaults to false. +*/ +bool QDiffuseSpecularMaterial::isAlphaBlendingEnabled() const +{ + Q_D(const QDiffuseSpecularMaterial); + return d->m_noDepthMask->isEnabled(); +} + +void QDiffuseSpecularMaterial::setAmbient(const QColor &ambient) +{ + Q_D(QDiffuseSpecularMaterial); + d->m_ambientParameter->setValue(ambient); +} + +void QDiffuseSpecularMaterial::setDiffuse(const QVariant &diffuse) +{ + Q_D(QDiffuseSpecularMaterial); + d->m_diffuseParameter->setValue(diffuse); + d->m_diffuseTextureParameter->setValue(diffuse); + + auto layers = d->m_gl3ShaderBuilder->enabledLayers(); + if (diffuse.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("diffuse")); + layers.append(QStringLiteral("diffuseTexture")); + d->m_effect->addParameter(d->m_diffuseTextureParameter); + d->m_effect->removeParameter(d->m_diffuseParameter); + } else { + layers.removeAll(QStringLiteral("diffuseTexture")); + layers.append(QStringLiteral("diffuse")); + d->m_effect->removeParameter(d->m_diffuseTextureParameter); + d->m_effect->addParameter(d->m_diffuseParameter); + } + d->m_gl3ShaderBuilder->setEnabledLayers(layers); + d->m_gl2es2ShaderBuilder->setEnabledLayers(layers); +} + +void QDiffuseSpecularMaterial::setSpecular(const QVariant &specular) +{ + Q_D(QDiffuseSpecularMaterial); + d->m_specularParameter->setValue(specular); + d->m_specularTextureParameter->setValue(specular); + + auto layers = d->m_gl3ShaderBuilder->enabledLayers(); + if (specular.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("specular")); + layers.append(QStringLiteral("specularTexture")); + d->m_effect->addParameter(d->m_specularTextureParameter); + d->m_effect->removeParameter(d->m_specularParameter); + } else { + layers.removeAll(QStringLiteral("specularTexture")); + layers.append(QStringLiteral("specular")); + d->m_effect->removeParameter(d->m_specularTextureParameter); + d->m_effect->addParameter(d->m_specularParameter); + } + d->m_gl3ShaderBuilder->setEnabledLayers(layers); + d->m_gl2es2ShaderBuilder->setEnabledLayers(layers); +} + +void QDiffuseSpecularMaterial::setShininess(float shininess) +{ + Q_D(QDiffuseSpecularMaterial); + d->m_shininessParameter->setValue(shininess); +} + +void QDiffuseSpecularMaterial::setNormal(const QVariant &normal) +{ + Q_D(QDiffuseSpecularMaterial); + d->m_normalTextureParameter->setValue(normal); + + auto layers = d->m_gl3ShaderBuilder->enabledLayers(); + if (normal.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("normal")); + layers.append(QStringLiteral("normalTexture")); + d->m_effect->addParameter(d->m_normalTextureParameter); + } else { + layers.removeAll(QStringLiteral("normalTexture")); + layers.append(QStringLiteral("normal")); + d->m_effect->removeParameter(d->m_normalTextureParameter); + } + d->m_gl3ShaderBuilder->setEnabledLayers(layers); +} + +void QDiffuseSpecularMaterial::setTextureScale(float textureScale) +{ + Q_D(QDiffuseSpecularMaterial); + d->m_textureScaleParameter->setValue(textureScale); +} + +void QDiffuseSpecularMaterial::setAlphaBlendingEnabled(bool enabled) +{ + Q_D(QDiffuseSpecularMaterial); + d->m_noDepthMask->setEnabled(enabled); + d->m_blendState->setEnabled(enabled); + d->m_blendEquation->setEnabled(enabled); +} + +} // namespace Qt3DExtras + +QT_END_NAMESPACE diff --git a/src/extras/defaults/qdiffusespecularmaterial.h b/src/extras/defaults/qdiffusespecularmaterial.h new file mode 100644 index 000000000..5f21da093 --- /dev/null +++ b/src/extras/defaults/qdiffusespecularmaterial.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QDIFFUSESPECULARMATERIAL_H +#define QT3DEXTRAS_QDIFFUSESPECULARMATERIAL_H + +#include <Qt3DExtras/qt3dextras_global.h> +#include <Qt3DRender/qmaterial.h> +#include <QtGui/QColor> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +class QDiffuseSpecularMaterialPrivate; + +class QT3DEXTRASSHARED_EXPORT QDiffuseSpecularMaterial : public Qt3DRender::QMaterial +{ + Q_OBJECT + Q_PROPERTY(QColor ambient READ ambient WRITE setAmbient NOTIFY ambientChanged) + Q_PROPERTY(QVariant diffuse READ diffuse WRITE setDiffuse NOTIFY diffuseChanged) + Q_PROPERTY(QVariant specular READ specular WRITE setSpecular NOTIFY specularChanged) + Q_PROPERTY(float shininess READ shininess WRITE setShininess NOTIFY shininessChanged) + Q_PROPERTY(QVariant normal READ normal WRITE setNormal NOTIFY normalChanged) + Q_PROPERTY(float textureScale READ textureScale WRITE setTextureScale NOTIFY textureScaleChanged) + Q_PROPERTY(bool alphaBlending READ isAlphaBlendingEnabled WRITE setAlphaBlendingEnabled NOTIFY alphaBlendingEnabledChanged) + +public: + explicit QDiffuseSpecularMaterial(Qt3DCore::QNode *parent = nullptr); + ~QDiffuseSpecularMaterial(); + + QColor ambient() const; + QVariant diffuse() const; + QVariant specular() const; + float shininess() const; + QVariant normal() const; + float textureScale() const; + bool isAlphaBlendingEnabled() const; + +public Q_SLOTS: + void setAmbient(const QColor &ambient); + void setDiffuse(const QVariant &diffuse); + void setSpecular(const QVariant &specular); + void setShininess(float shininess); + void setNormal(const QVariant &normal); + void setTextureScale(float textureScale); + void setAlphaBlendingEnabled(bool enabled); + +Q_SIGNALS: + void ambientChanged(const QColor &ambient); + void diffuseChanged(const QVariant &diffuse); + void specularChanged(const QVariant &specular); + void shininessChanged(float shininess); + void normalChanged(const QVariant &normal); + void textureScaleChanged(float textureScale); + void alphaBlendingEnabledChanged(bool enabled); + +private: + Q_DECLARE_PRIVATE(QDiffuseSpecularMaterial) +}; + +} // namespace Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QDIFFUSESPECULARMATERIAL_H diff --git a/src/extras/defaults/qtexturedmetalroughmaterial_p.h b/src/extras/defaults/qdiffusespecularmaterial_p.h index 8972726e5..2b0b2184f 100644 --- a/src/extras/defaults/qtexturedmetalroughmaterial_p.h +++ b/src/extras/defaults/qdiffusespecularmaterial_p.h @@ -37,8 +37,8 @@ ** ****************************************************************************/ -#ifndef QT3DEXTRAS_QTEXTUREDMETALROUGHMATERIAL_P_H -#define QT3DEXTRAS_QTEXTUREDMETALROUGHMATERIAL_P_H +#ifndef QT3DEXTRAS_QDIFFUSESPECULARMATERIAL_P_H +#define QT3DEXTRAS_QDIFFUSESPECULARMATERIAL_P_H // // W A R N I N G @@ -59,57 +59,62 @@ namespace Qt3DRender { class QFilterKey; class QEffect; -class QAbstractTexture; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; +class QNoDepthMask; +class QBlendEquationArguments; +class QBlendEquation; } // namespace Qt3DRender namespace Qt3DExtras { -class QTexturedMetalRoughMaterial; +class QDiffuseSpecularMaterial; -class QTexturedMetalRoughMaterialPrivate : public Qt3DRender::QMaterialPrivate +class QDiffuseSpecularMaterialPrivate : public Qt3DRender::QMaterialPrivate { public: - QTexturedMetalRoughMaterialPrivate(); + QDiffuseSpecularMaterialPrivate(); void init(); - void handleBaseColorChanged(const QVariant &var); - void handleMetallicChanged(const QVariant &var); - void handleRoughnessChanged(const QVariant &var); - void handleAmbientOcclusionChanged(const QVariant &var); - void handleNormalChanged(const QVariant &var); - - Qt3DRender::QAbstractTexture *m_baseColorTexture; - Qt3DRender::QAbstractTexture *m_metalnessTexture; - Qt3DRender::QAbstractTexture *m_roughnessTexture; - Qt3DRender::QAbstractTexture *m_ambientOcclusionTexture; - Qt3DRender::QAbstractTexture *m_normalTexture; - Qt3DRender::QAbstractTexture *m_environmentIrradianceTexture; - Qt3DRender::QAbstractTexture *m_environmentSpecularTexture; - Qt3DRender::QParameter *m_baseColorParameter; - Qt3DRender::QParameter *m_metalnessParameter; - Qt3DRender::QParameter *m_roughnessParameter; - Qt3DRender::QParameter *m_ambientOcclusionParameter; - Qt3DRender::QParameter *m_normalParameter; - Qt3DRender::QParameter *m_environmentIrradianceParameter; - Qt3DRender::QParameter *m_environmentSpecularParameter; - Qt3DRender::QEffect *m_metalRoughEffect; - Qt3DRender::QTechnique *m_metalRoughGL3Technique; - Qt3DRender::QRenderPass *m_metalRoughGL3RenderPass; - Qt3DRender::QShaderProgram *m_metalRoughGL3Shader; + void handleAmbientChanged(const QVariant &var); + void handleShininessChanged(const QVariant &var); + void handleTextureScaleChanged(const QVariant &var); + + Qt3DRender::QEffect *m_effect; + Qt3DRender::QParameter *m_ambientParameter; + Qt3DRender::QParameter *m_diffuseParameter; + Qt3DRender::QParameter *m_specularParameter; + Qt3DRender::QParameter *m_diffuseTextureParameter; + Qt3DRender::QParameter *m_specularTextureParameter; + Qt3DRender::QParameter *m_shininessParameter; + Qt3DRender::QParameter *m_normalTextureParameter; + Qt3DRender::QParameter *m_textureScaleParameter; + Qt3DRender::QTechnique *m_gl3Technique; + Qt3DRender::QTechnique *m_gl2Technique; + Qt3DRender::QTechnique *m_es2Technique; + Qt3DRender::QRenderPass *m_gl3RenderPass; + Qt3DRender::QRenderPass *m_gl2RenderPass; + Qt3DRender::QRenderPass *m_es2RenderPass; + Qt3DRender::QShaderProgram *m_gl3Shader; + Qt3DRender::QShaderProgramBuilder *m_gl3ShaderBuilder; + Qt3DRender::QShaderProgram *m_gl2es2Shader; + Qt3DRender::QShaderProgramBuilder *m_gl2es2ShaderBuilder; + Qt3DRender::QNoDepthMask *m_noDepthMask; + Qt3DRender::QBlendEquationArguments *m_blendState; + Qt3DRender::QBlendEquation *m_blendEquation; Qt3DRender::QFilterKey *m_filterKey; - Q_DECLARE_PUBLIC(QTexturedMetalRoughMaterial) + Q_DECLARE_PUBLIC(QDiffuseSpecularMaterial) }; } // Qt3DExtras QT_END_NAMESPACE -#endif // QT3DEXTRAS_QTEXTUREDMETALROUGHMATERIAL_P_H +#endif // QT3DEXTRAS_QDIFFUSESPECULARMATERIAL_P_H diff --git a/src/extras/defaults/qfirstpersoncameracontroller.cpp b/src/extras/defaults/qfirstpersoncameracontroller.cpp index 5321bfcf2..2ad997d05 100644 --- a/src/extras/defaults/qfirstpersoncameracontroller.cpp +++ b/src/extras/defaults/qfirstpersoncameracontroller.cpp @@ -35,188 +35,13 @@ ****************************************************************************/ #include "qfirstpersoncameracontroller.h" -#include "qfirstpersoncameracontroller_p.h" -#include <Qt3DInput/QAction> -#include <Qt3DInput/QActionInput> -#include <Qt3DInput/QAnalogAxisInput> -#include <Qt3DInput/QAxis> -#include <Qt3DInput/QButtonAxisInput> -#include <Qt3DLogic/QFrameAction> -#include <Qt3DInput/QKeyboardDevice> -#include <Qt3DInput/QLogicalDevice> -#include <Qt3DInput/QMouseDevice> -#include <Qt3DInput/QMouseEvent> #include <Qt3DRender/QCamera> QT_BEGIN_NAMESPACE namespace Qt3DExtras { -QFirstPersonCameraControllerPrivate::QFirstPersonCameraControllerPrivate() - : Qt3DCore::QEntityPrivate() - , m_camera(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::QAnalogAxisInput()) - , m_mouseRyInput(new Qt3DInput::QAnalogAxisInput()) - , m_mouseTzXInput(new Qt3DInput::QAnalogAxisInput()) - , m_mouseTzYInput(new Qt3DInput::QAnalogAxisInput()) - , m_keyboardTxPosInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTyPosInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTzPosInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTxNegInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTyNegInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTzNegInput(new Qt3DInput::QButtonAxisInput()) - , 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_acceleration(-1.0f) - , m_deceleration(-1.0f) - , m_firstPersonUp(QVector3D(0.0f, 1.0f, 0.0f)) -{} - -void QFirstPersonCameraControllerPrivate::init() -{ - //// Actions - - // Left Mouse Button Action - m_leftMouseButtonInput->setButtons(QVector<int>() << Qt::LeftButton); - m_leftMouseButtonInput->setSourceDevice(m_mouseDevice); - m_leftMouseButtonAction->addInput(m_leftMouseButtonInput); - - // Fine Motion Action - m_fineMotionKeyInput->setButtons(QVector<int>() << 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); - - // Mouse Wheel X - m_mouseTzXInput->setAxis(Qt3DInput::QMouseDevice::WheelX); - m_mouseTzXInput->setSourceDevice(m_mouseDevice); - m_tzAxis->addInput(m_mouseTzXInput); - - // Mouse Wheel Y - m_mouseTzYInput->setAxis(Qt3DInput::QMouseDevice::WheelY); - m_mouseTzYInput->setSourceDevice(m_mouseDevice); - m_tzAxis->addInput(m_mouseTzYInput); - - // Keyboard Pos Tx - m_keyboardTxPosInput->setButtons(QVector<int>() << Qt::Key_Right); - m_keyboardTxPosInput->setScale(1.0f); - m_keyboardTxPosInput->setSourceDevice(m_keyboardDevice); - m_txAxis->addInput(m_keyboardTxPosInput); - - // Keyboard Pos Ty - m_keyboardTyPosInput->setButtons(QVector<int>() << Qt::Key_PageUp); - m_keyboardTyPosInput->setScale(1.0f); - m_keyboardTyPosInput->setSourceDevice(m_keyboardDevice); - m_tyAxis->addInput(m_keyboardTyPosInput); - - // Keyboard Pos Tz - m_keyboardTzPosInput->setButtons(QVector<int>() << Qt::Key_Up); - m_keyboardTzPosInput->setScale(1.0f); - m_keyboardTzPosInput->setSourceDevice(m_keyboardDevice); - m_tzAxis->addInput(m_keyboardTzPosInput); - - // Keyboard Neg Tx - m_keyboardTxNegInput->setButtons(QVector<int>() << Qt::Key_Left); - m_keyboardTxNegInput->setScale(-1.0f); - m_keyboardTxNegInput->setSourceDevice(m_keyboardDevice); - m_txAxis->addInput(m_keyboardTxNegInput); - - // Keyboard Neg Ty - m_keyboardTyNegInput->setButtons(QVector<int>() << Qt::Key_PageDown); - m_keyboardTyNegInput->setScale(-1.0f); - m_keyboardTyNegInput->setSourceDevice(m_keyboardDevice); - m_tyAxis->addInput(m_keyboardTyNegInput); - - // Keyboard Neg Tz - m_keyboardTzNegInput->setButtons(QVector<int>() << 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); - - applyAccelerations(); - - Q_Q(QFirstPersonCameraController); - //// FrameAction - - QObject::connect(m_frameAction, SIGNAL(triggered(float)), - q, SLOT(_q_onTriggered(float))); - - // Disable the logical device when the entity is disabled - QObject::connect(q, &Qt3DCore::QEntity::enabledChanged, - m_logicalDevice, &Qt3DInput::QLogicalDevice::setEnabled); - - q->addComponent(m_frameAction); - q->addComponent(m_logicalDevice); -} - -void QFirstPersonCameraControllerPrivate::applyAccelerations() -{ - const auto inputs = { - m_keyboardTxPosInput, - m_keyboardTyPosInput, - m_keyboardTzPosInput, - m_keyboardTxNegInput, - m_keyboardTyNegInput, - m_keyboardTzNegInput - }; - - for (auto input : inputs) { - input->setAcceleration(m_acceleration); - input->setDeceleration(m_deceleration); - } -} - -void QFirstPersonCameraControllerPrivate::_q_onTriggered(float dt) -{ - if (m_camera != 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()) { - float lookSpeed = m_lookSpeed; - if (m_fineMotionAction->isActive()) - lookSpeed *= 0.2f; - m_camera->pan(m_rxAxis->value() * lookSpeed * dt, m_firstPersonUp); - m_camera->tilt(m_ryAxis->value() * lookSpeed * dt); - } - } -} - /*! \class Qt3DExtras::QFirstPersonCameraController \brief The QFirstPersonCameraController class allows controlling the scene camera @@ -251,129 +76,35 @@ void QFirstPersonCameraControllerPrivate::_q_onTriggered(float dt) */ QFirstPersonCameraController::QFirstPersonCameraController(Qt3DCore::QNode *parent) - : Qt3DCore::QEntity(*new QFirstPersonCameraControllerPrivate, parent) + : QAbstractCameraController(parent) { - Q_D(QFirstPersonCameraController); - d->init(); } QFirstPersonCameraController::~QFirstPersonCameraController() { } -/*! - \property QFirstPersonCameraController::camera - - Holds the currently controlled camera. -*/ -Qt3DRender::QCamera *QFirstPersonCameraController::camera() const -{ - Q_D(const QFirstPersonCameraController); - return d->m_camera; -} - -/*! - \property QFirstPersonCameraController::linearSpeed - Holds the current linear speed of the camera controller. Linear speed determines the - movement speed of the camera. -*/ -float QFirstPersonCameraController::linearSpeed() const +void QFirstPersonCameraController::moveCamera(const QAbstractCameraController::InputState &state, float dt) { - Q_D(const QFirstPersonCameraController); - return d->m_linearSpeed; -} - -/*! - \property QFirstPersonCameraController::lookSpeed - - Holds the current look speed of the camera controller. The look speed determines the turn rate - of the camera pan and tilt. -*/ -float QFirstPersonCameraController::lookSpeed() const -{ - Q_D(const QFirstPersonCameraController); - return d->m_lookSpeed; -} - -/*! - \property QFirstPersonCameraController::acceleration - - Holds the current acceleration of the camera controller. -*/ -float QFirstPersonCameraController::acceleration() const -{ - Q_D(const QFirstPersonCameraController); - return d->m_acceleration; -} - -/*! - \property QFirstPersonCameraController::deceleration - - Holds the current deceleration of the camera controller. -*/ -float QFirstPersonCameraController::deceleration() const -{ - Q_D(const QFirstPersonCameraController); - return d->m_deceleration; -} - -void QFirstPersonCameraController::setCamera(Qt3DRender::QCamera *camera) -{ - Q_D(QFirstPersonCameraController); - if (d->m_camera != camera) { - - if (d->m_camera) - d->unregisterDestructionHelper(d->m_camera); - - if (camera && !camera->parent()) - camera->setParent(this); - - d->m_camera = camera; - - // Ensures proper bookkeeping - if (d->m_camera) - d->registerDestructionHelper(d->m_camera, &QFirstPersonCameraController::setCamera, d->m_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(); - } -} + Qt3DRender::QCamera *theCamera = camera(); + + if (theCamera == nullptr) + return; + + theCamera->translate(QVector3D(state.txAxisValue * linearSpeed(), + state.tyAxisValue * linearSpeed(), + state.tzAxisValue * linearSpeed()) * dt); + if (state.leftMouseButtonActive) { + float theLookSpeed = lookSpeed(); + if (state.shiftKeyActive) { + theLookSpeed *= 0.2f; + } -void QFirstPersonCameraController::setAcceleration(float acceleration) -{ - Q_D(QFirstPersonCameraController); - if (d->m_acceleration != acceleration) { - d->m_acceleration = acceleration; - d->applyAccelerations(); - emit accelerationChanged(acceleration); - } -} + const QVector3D upVector(0.0f, 1.0f, 0.0f); -void QFirstPersonCameraController::setDeceleration(float deceleration) -{ - Q_D(QFirstPersonCameraController); - if (d->m_deceleration != deceleration) { - d->m_deceleration = deceleration; - d->applyAccelerations(); - emit decelerationChanged(deceleration); + theCamera->pan(state.rxAxisValue * theLookSpeed * dt, upVector); + theCamera->tilt(state.ryAxisValue * theLookSpeed * dt); } } diff --git a/src/extras/defaults/qfirstpersoncameracontroller.h b/src/extras/defaults/qfirstpersoncameracontroller.h index 3f7a6acc4..60edf7cb8 100644 --- a/src/extras/defaults/qfirstpersoncameracontroller.h +++ b/src/extras/defaults/qfirstpersoncameracontroller.h @@ -37,53 +37,22 @@ #ifndef QT3DEXTRAS_QFIRSTPERSONCAMERACONTROLLER_H #define QT3DEXTRAS_QFIRSTPERSONCAMERACONTROLLER_H -#include <Qt3DExtras/qt3dextras_global.h> -#include <Qt3DCore/QEntity> +#include <Qt3DExtras/qabstractcameracontroller.h> QT_BEGIN_NAMESPACE -namespace Qt3DRender { -class QCamera; -} - namespace Qt3DExtras { -class QFirstPersonCameraControllerPrivate; - -class QT3DEXTRASSHARED_EXPORT QFirstPersonCameraController : public Qt3DCore::QEntity +class QT3DEXTRASSHARED_EXPORT QFirstPersonCameraController : public QAbstractCameraController { 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 acceleration READ acceleration WRITE setAcceleration NOTIFY accelerationChanged) - Q_PROPERTY(float deceleration READ deceleration WRITE setDeceleration NOTIFY decelerationChanged) + public: explicit QFirstPersonCameraController(Qt3DCore::QNode *parent = nullptr); ~QFirstPersonCameraController(); - Qt3DRender::QCamera *camera() const; - float linearSpeed() const; - float lookSpeed() const; - float acceleration() const; - float deceleration() const; - - void setCamera(Qt3DRender::QCamera *camera); - void setLinearSpeed(float linearSpeed); - void setLookSpeed(float lookSpeed); - void setAcceleration(float acceleration); - void setDeceleration(float deceleration); - -Q_SIGNALS: - void cameraChanged(); - void linearSpeedChanged(); - void lookSpeedChanged(); - void accelerationChanged(float acceleration); - void decelerationChanged(float deceleration); - private: - Q_DECLARE_PRIVATE(QFirstPersonCameraController) - Q_PRIVATE_SLOT(d_func(), void _q_onTriggered(float)) + void moveCamera(const QAbstractCameraController::InputState &state, float dt) override; }; } // Qt3DExtras diff --git a/src/extras/defaults/qfirstpersoncameracontroller_p.h b/src/extras/defaults/qfirstpersoncameracontroller_p.h deleted file mode 100644 index 48a7c7998..000000000 --- a/src/extras/defaults/qfirstpersoncameracontroller_p.h +++ /dev/null @@ -1,152 +0,0 @@ -/**************************************************************************** -** -** 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 <Qt3DExtras/qfirstpersoncameracontroller.h> -#include <QtGui/QVector3D> - -#include <Qt3DCore/private/qentity_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. -// - -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 QAnalogAxisInput; -class QButtonAxisInput; -class QAxisActionHandler; - -} - -namespace Qt3DExtras { - -class QFirstPersonCameraControllerPrivate : public Qt3DCore::QEntityPrivate -{ -public: - QFirstPersonCameraControllerPrivate(); - - void init(); - void applyAccelerations(); - - 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::QAnalogAxisInput *m_mouseRxInput; - Qt3DInput::QAnalogAxisInput *m_mouseRyInput; - Qt3DInput::QAnalogAxisInput *m_mouseTzXInput; - Qt3DInput::QAnalogAxisInput *m_mouseTzYInput; - Qt3DInput::QButtonAxisInput *m_keyboardTxPosInput; - Qt3DInput::QButtonAxisInput *m_keyboardTyPosInput; - Qt3DInput::QButtonAxisInput *m_keyboardTzPosInput; - Qt3DInput::QButtonAxisInput *m_keyboardTxNegInput; - Qt3DInput::QButtonAxisInput *m_keyboardTyNegInput; - Qt3DInput::QButtonAxisInput *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_acceleration; - float m_deceleration; - 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/qforwardrenderer.cpp b/src/extras/defaults/qforwardrenderer.cpp index f3137ea2d..b02077298 100644 --- a/src/extras/defaults/qforwardrenderer.cpp +++ b/src/extras/defaults/qforwardrenderer.cpp @@ -48,6 +48,13 @@ #include <Qt3DRender/qfrustumculling.h> #include <Qt3DRender/qrendersurfaceselector.h> +static void initResources() +{ +#ifdef QT_STATIC + Q_INIT_RESOURCE(extras); +#endif +} + QT_BEGIN_NAMESPACE using namespace Qt3DRender; @@ -68,6 +75,8 @@ void QForwardRendererPrivate::init() { Q_Q(QForwardRenderer); + initResources(); + m_frustumCulling->setParent(m_clearBuffer); m_clearBuffer->setParent(m_cameraSelector); m_cameraSelector->setParent(m_viewport); diff --git a/src/extras/defaults/qmetalroughmaterial.cpp b/src/extras/defaults/qmetalroughmaterial.cpp index ea213ab82..4d537f20f 100644 --- a/src/extras/defaults/qmetalroughmaterial.cpp +++ b/src/extras/defaults/qmetalroughmaterial.cpp @@ -45,6 +45,7 @@ #include <Qt3DRender/qtexture.h> #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> @@ -65,12 +66,19 @@ QMetalRoughMaterialPrivate::QMetalRoughMaterialPrivate() , m_baseColorParameter(new QParameter(QStringLiteral("baseColor"), QColor("grey"))) , m_metalnessParameter(new QParameter(QStringLiteral("metalness"), 0.0f)) , m_roughnessParameter(new QParameter(QStringLiteral("roughness"), 0.0f)) + , m_baseColorMapParameter(new QParameter(QStringLiteral("baseColorMap"), QVariant())) + , m_metalnessMapParameter(new QParameter(QStringLiteral("metalnessMap"), QVariant())) + , m_roughnessMapParameter(new QParameter(QStringLiteral("roughnessMap"), QVariant())) + , m_ambientOcclusionMapParameter(new QParameter(QStringLiteral("ambientOcclusionMap"), QVariant())) + , m_normalMapParameter(new QParameter(QStringLiteral("normalMap"), QVariant())) + , m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale"), 1.0f)) , m_environmentIrradianceParameter(new QParameter(QStringLiteral("envLight.irradiance"), m_environmentIrradianceTexture)) , m_environmentSpecularParameter(new QParameter(QStringLiteral("envLight.specular"), m_environmentSpecularTexture)) , m_metalRoughEffect(new QEffect()) , m_metalRoughGL3Technique(new QTechnique()) , m_metalRoughGL3RenderPass(new QRenderPass()) , m_metalRoughGL3Shader(new QShaderProgram()) + , m_metalRoughGL3ShaderBuilder(new QShaderProgramBuilder()) , m_filterKey(new QFilterKey) { m_environmentIrradianceTexture->setMagnificationFilter(QAbstractTexture::Linear); @@ -88,22 +96,37 @@ QMetalRoughMaterialPrivate::QMetalRoughMaterialPrivate() void QMetalRoughMaterialPrivate::init() { - connect(m_baseColorParameter, &Qt3DRender::QParameter::valueChanged, - this, &QMetalRoughMaterialPrivate::handleBaseColorChanged); - connect(m_metalnessParameter, &Qt3DRender::QParameter::valueChanged, - this, &QMetalRoughMaterialPrivate::handleMetallicChanged); - connect(m_roughnessParameter, &Qt3DRender::QParameter::valueChanged, - this, &QMetalRoughMaterialPrivate::handleRoughnessChanged); + Q_Q(QMetalRoughMaterial); - m_metalRoughGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/metalrough.vert")))); - m_metalRoughGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/metalroughuniform.frag")))); + QObject::connect(m_baseColorParameter, &Qt3DRender::QParameter::valueChanged, + q, &QMetalRoughMaterial::baseColorChanged); + QObject::connect(m_metalnessParameter, &Qt3DRender::QParameter::valueChanged, + q, &QMetalRoughMaterial::metalnessChanged); + QObject::connect(m_roughnessParameter, &Qt3DRender::QParameter::valueChanged, + q, &QMetalRoughMaterial::roughnessChanged); + QObject::connect(m_ambientOcclusionMapParameter, &Qt3DRender::QParameter::valueChanged, + q, &QMetalRoughMaterial::roughnessChanged); + QObject::connect(m_normalMapParameter, &Qt3DRender::QParameter::valueChanged, + q, &QMetalRoughMaterial::normalChanged); + connect(m_textureScaleParameter, &Qt3DRender::QParameter::valueChanged, + this, &QMetalRoughMaterialPrivate::handleTextureScaleChanged); + + m_metalRoughGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + + m_metalRoughGL3ShaderBuilder->setParent(q); + m_metalRoughGL3ShaderBuilder->setShaderProgram(m_metalRoughGL3Shader); + m_metalRoughGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/metalrough.frag.json"))); + m_metalRoughGL3ShaderBuilder->setEnabledLayers({QStringLiteral("baseColor"), + QStringLiteral("metalness"), + QStringLiteral("roughness"), + QStringLiteral("ambientOcclusion"), + QStringLiteral("normal")}); m_metalRoughGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_metalRoughGL3Technique->graphicsApiFilter()->setMajorVersion(3); m_metalRoughGL3Technique->graphicsApiFilter()->setMinorVersion(1); m_metalRoughGL3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile); - Q_Q(QMetalRoughMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -116,6 +139,7 @@ void QMetalRoughMaterialPrivate::init() m_metalRoughEffect->addParameter(m_baseColorParameter); m_metalRoughEffect->addParameter(m_metalnessParameter); m_metalRoughEffect->addParameter(m_roughnessParameter); + m_metalRoughEffect->addParameter(m_textureScaleParameter); // Note that even though those parameters are not exposed in the API, // they need to be kept around for now due to a bug in some drivers/GPUs @@ -129,21 +153,10 @@ void QMetalRoughMaterialPrivate::init() q->setEffect(m_metalRoughEffect); } -void QMetalRoughMaterialPrivate::handleBaseColorChanged(const QVariant &var) +void QMetalRoughMaterialPrivate::handleTextureScaleChanged(const QVariant &var) { Q_Q(QMetalRoughMaterial); - emit q->baseColorChanged(var.value<QColor>()); -} - -void QMetalRoughMaterialPrivate::handleMetallicChanged(const QVariant &var) -{ - Q_Q(QMetalRoughMaterial); - emit q->metalnessChanged(var.toFloat()); -} -void QMetalRoughMaterialPrivate::handleRoughnessChanged(const QVariant &var) -{ - Q_Q(QMetalRoughMaterial); - emit q->roughnessChanged(var.toFloat()); + emit q->textureScaleChanged(var.toFloat()); } /*! @@ -186,53 +199,181 @@ QMetalRoughMaterial::~QMetalRoughMaterial() /*! \property QMetalRoughMaterial::baseColor - Holds the current base color of the material. + Holds the current base color of the material. This can be either a plain + color value or a texture. By default the value of this property is "grey". */ -QColor QMetalRoughMaterial::baseColor() const +QVariant QMetalRoughMaterial::baseColor() const { Q_D(const QMetalRoughMaterial); - return d->m_baseColorParameter->value().value<QColor>(); + return d->m_baseColorParameter->value(); } /*! \property QMetalRoughMaterial::metalness - Holds the current metalness level of the material, since is a value between 0 (purely dielectric, the default) - and 1 (purely metallic). + Holds the current metalness level of the material, since is a value between + 0 (purely dielectric, the default) and 1 (purely metallic). This can be + either a plain uniform value or a texture. By default the value of this + property is 0. */ -float QMetalRoughMaterial::metalness() const +QVariant QMetalRoughMaterial::metalness() const { Q_D(const QMetalRoughMaterial); - return d->m_metalnessParameter->value().toFloat(); + return d->m_metalnessParameter->value(); } /*! \property QMetalRoughMaterial::roughness - Holds the current roughness level of the material. + Holds the current roughness level of the material. This can be either a + plain uniform value or a texture. By default the value of this property is + 0. +*/ +QVariant QMetalRoughMaterial::roughness() const +{ + Q_D(const QMetalRoughMaterial); + return d->m_roughnessParameter->value(); +} + +/*! + \property QMetalRoughMaterial::ambientOcclusion + + Holds the current ambient occlusion map texture of the material. This can + only be a texture, otherwise it is ignored. By default this map is not set. +*/ +QVariant QMetalRoughMaterial::ambientOcclusion() const +{ + Q_D(const QMetalRoughMaterial); + return d->m_ambientOcclusionMapParameter->value(); +} + +/*! + \property QMetalRoughMaterial::normal + + Holds the current normal map texture of the material. This can only be a + texture, otherwise it is ignored. By default this map is not set. */ -float QMetalRoughMaterial::roughness() const +QVariant QMetalRoughMaterial::normal() const { Q_D(const QMetalRoughMaterial); - return d->m_roughnessParameter->value().toFloat(); + return d->m_normalMapParameter->value(); +} + +/*! + \property QMetalRoughMaterial::textureScale + + Holds the current texture scale. It is applied as a multiplier to texture + coordinates at render time. Defaults to 1.0. +*/ +float QMetalRoughMaterial::textureScale() const +{ + Q_D(const QMetalRoughMaterial); + return d->m_textureScaleParameter->value().toFloat(); +} + +void QMetalRoughMaterial::setBaseColor(const QVariant &baseColor) +{ + Q_D(QMetalRoughMaterial); + d->m_baseColorParameter->setValue(baseColor); + d->m_baseColorMapParameter->setValue(baseColor); + + auto layers = d->m_metalRoughGL3ShaderBuilder->enabledLayers(); + if (baseColor.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("baseColor")); + layers.append(QStringLiteral("baseColorMap")); + d->m_metalRoughEffect->addParameter(d->m_baseColorMapParameter); + d->m_metalRoughEffect->removeParameter(d->m_baseColorParameter); + } else { + layers.removeAll(QStringLiteral("baseColorMap")); + layers.append(QStringLiteral("baseColor")); + d->m_metalRoughEffect->removeParameter(d->m_baseColorMapParameter); + d->m_metalRoughEffect->addParameter(d->m_baseColorParameter); + } + d->m_metalRoughGL3ShaderBuilder->setEnabledLayers(layers); +} + +void QMetalRoughMaterial::setMetalness(const QVariant &metalness) +{ + Q_D(QMetalRoughMaterial); + d->m_metalnessParameter->setValue(metalness); + d->m_metalnessMapParameter->setValue(metalness); + + auto layers = d->m_metalRoughGL3ShaderBuilder->enabledLayers(); + if (metalness.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("metalness")); + layers.append(QStringLiteral("metalnessMap")); + d->m_metalRoughEffect->addParameter(d->m_metalnessMapParameter); + d->m_metalRoughEffect->removeParameter(d->m_metalnessParameter); + } else { + layers.removeAll(QStringLiteral("metalnessMap")); + layers.append(QStringLiteral("metalness")); + d->m_metalRoughEffect->removeParameter(d->m_metalnessMapParameter); + d->m_metalRoughEffect->addParameter(d->m_metalnessParameter); + } + d->m_metalRoughGL3ShaderBuilder->setEnabledLayers(layers); +} + +void QMetalRoughMaterial::setRoughness(const QVariant &roughness) +{ + Q_D(QMetalRoughMaterial); + d->m_roughnessParameter->setValue(roughness); + d->m_roughnessMapParameter->setValue(roughness); + + auto layers = d->m_metalRoughGL3ShaderBuilder->enabledLayers(); + if (roughness.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("roughness")); + layers.append(QStringLiteral("roughnessMap")); + d->m_metalRoughEffect->addParameter(d->m_roughnessMapParameter); + d->m_metalRoughEffect->removeParameter(d->m_roughnessParameter); + } else { + layers.removeAll(QStringLiteral("roughnessMap")); + layers.append(QStringLiteral("roughness")); + d->m_metalRoughEffect->removeParameter(d->m_roughnessMapParameter); + d->m_metalRoughEffect->addParameter(d->m_roughnessParameter); + } + d->m_metalRoughGL3ShaderBuilder->setEnabledLayers(layers); } -void QMetalRoughMaterial::setBaseColor(const QColor &baseColor) +void QMetalRoughMaterial::setAmbientOcclusion(const QVariant &ambientOcclusion) { Q_D(QMetalRoughMaterial); - d->m_baseColorParameter->setValue(QVariant::fromValue(baseColor)); + d->m_ambientOcclusionMapParameter->setValue(ambientOcclusion); + + auto layers = d->m_metalRoughGL3ShaderBuilder->enabledLayers(); + if (ambientOcclusion.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("ambientOcclusion")); + layers.append(QStringLiteral("ambientOcclusionMap")); + d->m_metalRoughEffect->addParameter(d->m_ambientOcclusionMapParameter); + } else { + layers.removeAll(QStringLiteral("ambientOcclusionMap")); + layers.append(QStringLiteral("ambientOcclusion")); + d->m_metalRoughEffect->removeParameter(d->m_ambientOcclusionMapParameter); + } + d->m_metalRoughGL3ShaderBuilder->setEnabledLayers(layers); } -void QMetalRoughMaterial::setMetalness(float metalness) +void QMetalRoughMaterial::setNormal(const QVariant &normal) { Q_D(QMetalRoughMaterial); - d->m_metalnessParameter->setValue(QVariant::fromValue(metalness)); + d->m_normalMapParameter->setValue(normal); + + auto layers = d->m_metalRoughGL3ShaderBuilder->enabledLayers(); + if (normal.value<QAbstractTexture *>()) { + layers.removeAll(QStringLiteral("normal")); + layers.append(QStringLiteral("normalMap")); + d->m_metalRoughEffect->addParameter(d->m_normalMapParameter); + } else { + layers.removeAll(QStringLiteral("normalMap")); + layers.append(QStringLiteral("normal")); + d->m_metalRoughEffect->removeParameter(d->m_normalMapParameter); + } + d->m_metalRoughGL3ShaderBuilder->setEnabledLayers(layers); } -void QMetalRoughMaterial::setRoughness(float roughness) +void QMetalRoughMaterial::setTextureScale(float textureScale) { Q_D(QMetalRoughMaterial); - d->m_roughnessParameter->setValue(QVariant::fromValue(roughness)); + d->m_textureScaleParameter->setValue(textureScale); } } // namespace Qt3DExtras diff --git a/src/extras/defaults/qmetalroughmaterial.h b/src/extras/defaults/qmetalroughmaterial.h index 28c9438a7..400437338 100644 --- a/src/extras/defaults/qmetalroughmaterial.h +++ b/src/extras/defaults/qmetalroughmaterial.h @@ -57,27 +57,39 @@ class QMetalRoughMaterialPrivate; class QT3DEXTRASSHARED_EXPORT QMetalRoughMaterial : public Qt3DRender::QMaterial { Q_OBJECT - Q_PROPERTY(QColor baseColor READ baseColor WRITE setBaseColor NOTIFY baseColorChanged) - Q_PROPERTY(float metalness READ metalness WRITE setMetalness NOTIFY metalnessChanged) - Q_PROPERTY(float roughness READ roughness WRITE setRoughness NOTIFY roughnessChanged) + Q_PROPERTY(QVariant baseColor READ baseColor WRITE setBaseColor NOTIFY baseColorChanged) + Q_PROPERTY(QVariant metalness READ metalness WRITE setMetalness NOTIFY metalnessChanged) + Q_PROPERTY(QVariant roughness READ roughness WRITE setRoughness NOTIFY roughnessChanged) + Q_PROPERTY(QVariant ambientOcclusion READ ambientOcclusion WRITE setAmbientOcclusion NOTIFY ambientOcclusionChanged REVISION 10) + Q_PROPERTY(QVariant normal READ normal WRITE setNormal NOTIFY normalChanged REVISION 10) + Q_PROPERTY(float textureScale READ textureScale WRITE setTextureScale NOTIFY textureScaleChanged REVISION 10) public: explicit QMetalRoughMaterial(Qt3DCore::QNode *parent = nullptr); ~QMetalRoughMaterial(); - QColor baseColor() const; - float metalness() const; - float roughness() const; + QVariant baseColor() const; + QVariant metalness() const; + QVariant roughness() const; + QVariant ambientOcclusion() const; + QVariant normal() const; + float textureScale() const; public Q_SLOTS: - void setBaseColor(const QColor &baseColor); - void setMetalness(float metalness); - void setRoughness(float roughness); + void setBaseColor(const QVariant &baseColor); + void setMetalness(const QVariant &metalness); + void setRoughness(const QVariant &roughness); + void setAmbientOcclusion(const QVariant &ambientOcclusion); + void setNormal(const QVariant &normal); + void setTextureScale(float textureScale); Q_SIGNALS: - void baseColorChanged(const QColor &baseColor); - void metalnessChanged(float metalness); - void roughnessChanged(float roughness); + void baseColorChanged(const QVariant &baseColor); + void metalnessChanged(const QVariant &metalness); + void roughnessChanged(const QVariant &roughness); + void ambientOcclusionChanged(const QVariant &ambientOcclusion); + void normalChanged(const QVariant &normal); + void textureScaleChanged(float textureScale); protected: explicit QMetalRoughMaterial(QMetalRoughMaterialPrivate &dd, Qt3DCore::QNode *parent = nullptr); diff --git a/src/extras/defaults/qmetalroughmaterial_p.h b/src/extras/defaults/qmetalroughmaterial_p.h index 3090b9757..838474490 100644 --- a/src/extras/defaults/qmetalroughmaterial_p.h +++ b/src/extras/defaults/qmetalroughmaterial_p.h @@ -63,6 +63,7 @@ class QAbstractTexture; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; } // namespace Qt3DRender @@ -78,21 +79,26 @@ public: void init(); - void handleBaseColorChanged(const QVariant &var); - void handleMetallicChanged(const QVariant &var); - void handleRoughnessChanged(const QVariant &var); + void handleTextureScaleChanged(const QVariant &var); Qt3DRender::QAbstractTexture *m_environmentIrradianceTexture; Qt3DRender::QAbstractTexture *m_environmentSpecularTexture; Qt3DRender::QParameter *m_baseColorParameter; Qt3DRender::QParameter *m_metalnessParameter; Qt3DRender::QParameter *m_roughnessParameter; + Qt3DRender::QParameter *m_baseColorMapParameter; + Qt3DRender::QParameter *m_metalnessMapParameter; + Qt3DRender::QParameter *m_roughnessMapParameter; + Qt3DRender::QParameter *m_ambientOcclusionMapParameter; + Qt3DRender::QParameter *m_normalMapParameter; + Qt3DRender::QParameter *m_textureScaleParameter; Qt3DRender::QParameter *m_environmentIrradianceParameter; Qt3DRender::QParameter *m_environmentSpecularParameter; Qt3DRender::QEffect *m_metalRoughEffect; Qt3DRender::QTechnique *m_metalRoughGL3Technique; Qt3DRender::QRenderPass *m_metalRoughGL3RenderPass; Qt3DRender::QShaderProgram *m_metalRoughGL3Shader; + Qt3DRender::QShaderProgramBuilder *m_metalRoughGL3ShaderBuilder; Qt3DRender::QFilterKey *m_filterKey; Q_DECLARE_PUBLIC(QMetalRoughMaterial) diff --git a/src/extras/defaults/qnormaldiffusemapalphamaterial.cpp b/src/extras/defaults/qnormaldiffusemapalphamaterial.cpp index eb6398341..9b2a64520 100644 --- a/src/extras/defaults/qnormaldiffusemapalphamaterial.cpp +++ b/src/extras/defaults/qnormaldiffusemapalphamaterial.cpp @@ -45,6 +45,7 @@ #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> #include <Qt3DRender/qalphacoverage.h> @@ -69,6 +70,8 @@ QNormalDiffuseMapAlphaMaterialPrivate::QNormalDiffuseMapAlphaMaterialPrivate() void QNormalDiffuseMapAlphaMaterialPrivate::init() { + Q_Q(QNormalDiffuseMapMaterial); + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, this, &QNormalDiffuseMapMaterialPrivate::handleAmbientChanged); connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, @@ -82,10 +85,21 @@ void QNormalDiffuseMapAlphaMaterialPrivate::init() connect(m_textureScaleParameter, &Qt3DRender::QParameter::valueChanged, this, &QNormalDiffuseMapMaterialPrivate::handleTextureScaleChanged); - m_normalDiffuseGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemap.vert")))); - m_normalDiffuseGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemapalpha.frag")))); - m_normalDiffuseGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemap.vert")))); - m_normalDiffuseGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemapalpha.frag")))); + m_normalDiffuseGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_normalDiffuseGL3ShaderBuilder->setParent(q); + m_normalDiffuseGL3ShaderBuilder->setShaderProgram(m_normalDiffuseGL3Shader); + m_normalDiffuseGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_normalDiffuseGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specular"), + QStringLiteral("normalTexture")}); + + m_normalDiffuseGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_normalDiffuseGL2ES2ShaderBuilder->setParent(q); + m_normalDiffuseGL2ES2ShaderBuilder->setShaderProgram(m_normalDiffuseGL2ES2Shader); + m_normalDiffuseGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_normalDiffuseGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specular"), + QStringLiteral("normalTexture")}); m_normalDiffuseGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_normalDiffuseGL3Technique->graphicsApiFilter()->setMajorVersion(3); @@ -102,7 +116,6 @@ void QNormalDiffuseMapAlphaMaterialPrivate::init() m_normalDiffuseES2Technique->graphicsApiFilter()->setMinorVersion(0); m_normalDiffuseES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); - Q_Q(QNormalDiffuseMapMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -152,6 +165,9 @@ void QNormalDiffuseMapAlphaMaterialPrivate::init() \since 5.7 \inherits Qt3DExtras::QNormalDiffuseMapMaterial + \deprecated + \see Qt3DExtras::QDiffuseSpecularMaterial + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. The relative strengths of these components are controlled by means of their reflectivity coefficients which are modelled as RGB triplets: diff --git a/src/extras/defaults/qnormaldiffusemapmaterial.cpp b/src/extras/defaults/qnormaldiffusemapmaterial.cpp index 35cea095a..beed4085e 100644 --- a/src/extras/defaults/qnormaldiffusemapmaterial.cpp +++ b/src/extras/defaults/qnormaldiffusemapmaterial.cpp @@ -46,6 +46,7 @@ #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> #include <QtCore/QUrl> @@ -76,7 +77,9 @@ QNormalDiffuseMapMaterialPrivate::QNormalDiffuseMapMaterialPrivate() , m_normalDiffuseGL2RenderPass(new QRenderPass()) , m_normalDiffuseES2RenderPass(new QRenderPass()) , m_normalDiffuseGL3Shader(new QShaderProgram()) + , m_normalDiffuseGL3ShaderBuilder(new QShaderProgramBuilder()) , m_normalDiffuseGL2ES2Shader(new QShaderProgram()) + , m_normalDiffuseGL2ES2ShaderBuilder(new QShaderProgramBuilder()) , m_filterKey(new QFilterKey) { m_diffuseTexture->setMagnificationFilter(QAbstractTexture::Linear); @@ -94,6 +97,8 @@ QNormalDiffuseMapMaterialPrivate::QNormalDiffuseMapMaterialPrivate() void QNormalDiffuseMapMaterialPrivate::init() { + Q_Q(QNormalDiffuseMapMaterial); + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, this, &QNormalDiffuseMapMaterialPrivate::handleAmbientChanged); connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, @@ -107,10 +112,21 @@ void QNormalDiffuseMapMaterialPrivate::init() connect(m_textureScaleParameter, &Qt3DRender::QParameter::valueChanged, this, &QNormalDiffuseMapMaterialPrivate::handleTextureScaleChanged); - m_normalDiffuseGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemap.vert")))); - m_normalDiffuseGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemap.frag")))); - m_normalDiffuseGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemap.vert")))); - m_normalDiffuseGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemap.frag")))); + m_normalDiffuseGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_normalDiffuseGL3ShaderBuilder->setParent(q); + m_normalDiffuseGL3ShaderBuilder->setShaderProgram(m_normalDiffuseGL3Shader); + m_normalDiffuseGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_normalDiffuseGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specular"), + QStringLiteral("normalTexture")}); + + m_normalDiffuseGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_normalDiffuseGL2ES2ShaderBuilder->setParent(q); + m_normalDiffuseGL2ES2ShaderBuilder->setShaderProgram(m_normalDiffuseGL2ES2Shader); + m_normalDiffuseGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_normalDiffuseGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specular"), + QStringLiteral("normalTexture")}); m_normalDiffuseGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_normalDiffuseGL3Technique->graphicsApiFilter()->setMajorVersion(3); @@ -127,7 +143,6 @@ void QNormalDiffuseMapMaterialPrivate::init() m_normalDiffuseES2Technique->graphicsApiFilter()->setMinorVersion(0); m_normalDiffuseES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); - Q_Q(QNormalDiffuseMapMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -203,6 +218,9 @@ void QNormalDiffuseMapMaterialPrivate::handleTextureScaleChanged(const QVariant \since 5.7 \inherits Qt3DRender::QMaterial + \deprecated + \see Qt3DExtras::QDiffuseSpecularMaterial + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. The relative strengths of these components are controlled by means of their reflectivity coefficients which are modelled as RGB triplets: diff --git a/src/extras/defaults/qnormaldiffusemapmaterial_p.h b/src/extras/defaults/qnormaldiffusemapmaterial_p.h index d88b01f1f..1ac937b1e 100644 --- a/src/extras/defaults/qnormaldiffusemapmaterial_p.h +++ b/src/extras/defaults/qnormaldiffusemapmaterial_p.h @@ -63,6 +63,7 @@ class QAbstractTexture; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; } // namespace Qt3DRender @@ -101,7 +102,9 @@ public: Qt3DRender::QRenderPass *m_normalDiffuseGL2RenderPass; Qt3DRender::QRenderPass *m_normalDiffuseES2RenderPass; Qt3DRender::QShaderProgram *m_normalDiffuseGL3Shader; + Qt3DRender::QShaderProgramBuilder *m_normalDiffuseGL3ShaderBuilder; Qt3DRender::QShaderProgram *m_normalDiffuseGL2ES2Shader; + Qt3DRender::QShaderProgramBuilder *m_normalDiffuseGL2ES2ShaderBuilder; Qt3DRender::QFilterKey *m_filterKey; Q_DECLARE_PUBLIC(QNormalDiffuseMapMaterial) diff --git a/src/extras/defaults/qnormaldiffusespecularmapmaterial.cpp b/src/extras/defaults/qnormaldiffusespecularmapmaterial.cpp index c6f8ced9c..028fd14e7 100644 --- a/src/extras/defaults/qnormaldiffusespecularmapmaterial.cpp +++ b/src/extras/defaults/qnormaldiffusespecularmapmaterial.cpp @@ -46,6 +46,7 @@ #include <Qt3DRender/qtexture.h> #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> @@ -78,7 +79,9 @@ QNormalDiffuseSpecularMapMaterialPrivate::QNormalDiffuseSpecularMapMaterialPriva , m_normalDiffuseSpecularGL2RenderPass(new QRenderPass()) , m_normalDiffuseSpecularES2RenderPass(new QRenderPass()) , m_normalDiffuseSpecularGL3Shader(new QShaderProgram()) + , m_normalDiffuseSpecularGL3ShaderBuilder(new QShaderProgramBuilder()) , m_normalDiffuseSpecularGL2ES2Shader(new QShaderProgram()) + , m_normalDiffuseSpecularGL2ES2ShaderBuilder(new QShaderProgramBuilder()) , m_filterKey(new QFilterKey) { m_diffuseTexture->setMagnificationFilter(QAbstractTexture::Linear); @@ -102,6 +105,8 @@ QNormalDiffuseSpecularMapMaterialPrivate::QNormalDiffuseSpecularMapMaterialPriva void QNormalDiffuseSpecularMapMaterialPrivate::init() { + Q_Q(QNormalDiffuseSpecularMapMaterial); + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, this, &QNormalDiffuseSpecularMapMaterialPrivate::handleAmbientChanged); connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, @@ -115,10 +120,21 @@ void QNormalDiffuseSpecularMapMaterialPrivate::init() connect(m_textureScaleParameter, &Qt3DRender::QParameter::valueChanged, this, &QNormalDiffuseSpecularMapMaterialPrivate::handleTextureScaleChanged); - m_normalDiffuseSpecularGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusemap.vert")))); - m_normalDiffuseSpecularGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/normaldiffusespecularmap.frag")))); - m_normalDiffuseSpecularGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusemap.vert")))); - m_normalDiffuseSpecularGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/normaldiffusespecularmap.frag")))); + m_normalDiffuseSpecularGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_normalDiffuseSpecularGL3ShaderBuilder->setParent(q); + m_normalDiffuseSpecularGL3ShaderBuilder->setShaderProgram(m_normalDiffuseSpecularGL3Shader); + m_normalDiffuseSpecularGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_normalDiffuseSpecularGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specularTexture"), + QStringLiteral("normalTexture")}); + + m_normalDiffuseSpecularGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_normalDiffuseSpecularGL2ES2ShaderBuilder->setParent(q); + m_normalDiffuseSpecularGL2ES2ShaderBuilder->setShaderProgram(m_normalDiffuseSpecularGL2ES2Shader); + m_normalDiffuseSpecularGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_normalDiffuseSpecularGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture"), + QStringLiteral("specularTexture"), + QStringLiteral("normalTexture")}); m_normalDiffuseSpecularGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_normalDiffuseSpecularGL3Technique->graphicsApiFilter()->setMajorVersion(3); @@ -135,7 +151,6 @@ void QNormalDiffuseSpecularMapMaterialPrivate::init() m_normalDiffuseSpecularES2Technique->graphicsApiFilter()->setMinorVersion(0); m_normalDiffuseSpecularES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); - Q_Q(QNormalDiffuseSpecularMapMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -211,6 +226,9 @@ void QNormalDiffuseSpecularMapMaterialPrivate::handleTextureScaleChanged(const Q \since 5.7 \inherits Qt3DRender::QMaterial + \deprecated + \see Qt3DExtras::QDiffuseSpecularMaterial + The specular lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. The relative strengths of these components are controlled by means of their reflectivity coefficients which are modelled as RGB triplets: diff --git a/src/extras/defaults/qnormaldiffusespecularmapmaterial_p.h b/src/extras/defaults/qnormaldiffusespecularmapmaterial_p.h index 3316044ff..e02451cc0 100644 --- a/src/extras/defaults/qnormaldiffusespecularmapmaterial_p.h +++ b/src/extras/defaults/qnormaldiffusespecularmapmaterial_p.h @@ -63,6 +63,7 @@ class QAbstractTexture; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; } // namespace Qt3DRender @@ -102,7 +103,9 @@ public: Qt3DRender::QRenderPass *m_normalDiffuseSpecularGL2RenderPass; Qt3DRender::QRenderPass *m_normalDiffuseSpecularES2RenderPass; Qt3DRender::QShaderProgram *m_normalDiffuseSpecularGL3Shader; + Qt3DRender::QShaderProgramBuilder *m_normalDiffuseSpecularGL3ShaderBuilder; Qt3DRender::QShaderProgram *m_normalDiffuseSpecularGL2ES2Shader; + Qt3DRender::QShaderProgramBuilder *m_normalDiffuseSpecularGL2ES2ShaderBuilder; Qt3DRender::QFilterKey *m_filterKey; Q_DECLARE_PUBLIC(QNormalDiffuseSpecularMapMaterial) diff --git a/src/extras/defaults/qorbitcameracontroller.cpp b/src/extras/defaults/qorbitcameracontroller.cpp index 565a75ea1..c4f976f89 100644 --- a/src/extras/defaults/qorbitcameracontroller.cpp +++ b/src/extras/defaults/qorbitcameracontroller.cpp @@ -38,22 +38,15 @@ #include "qorbitcameracontroller_p.h" #include <Qt3DRender/QCamera> -#include <Qt3DInput/QAxis> -#include <Qt3DInput/QAnalogAxisInput> -#include <Qt3DInput/QButtonAxisInput> -#include <Qt3DInput/QAction> -#include <Qt3DInput/QActionInput> -#include <Qt3DInput/QLogicalDevice> -#include <Qt3DInput/QKeyboardDevice> -#include <Qt3DInput/QMouseDevice> -#include <Qt3DInput/QMouseEvent> -#include <Qt3DLogic/QFrameAction> -#include <QtCore/QtGlobal> QT_BEGIN_NAMESPACE namespace Qt3DExtras { +QOrbitCameraControllerPrivate::QOrbitCameraControllerPrivate() + : m_zoomInLimit(2.0f) +{} + /*! \class Qt3DExtras::QOrbitCameraController \brief The QOrbitCameraController class allows controlling the scene camera along orbital path. @@ -99,251 +92,20 @@ namespace Qt3DExtras { \endtable */ -QOrbitCameraControllerPrivate::QOrbitCameraControllerPrivate() - : Qt3DCore::QEntityPrivate() - , m_camera(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::QAnalogAxisInput()) - , m_mouseRyInput(new Qt3DInput::QAnalogAxisInput()) - , m_mouseTzXInput(new Qt3DInput::QAnalogAxisInput()) - , m_mouseTzYInput(new Qt3DInput::QAnalogAxisInput()) - , m_keyboardTxPosInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTyPosInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTzPosInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTxNegInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTyNegInput(new Qt3DInput::QButtonAxisInput()) - , m_keyboardTzNegInput(new Qt3DInput::QButtonAxisInput()) - , 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 QOrbitCameraControllerPrivate::init() -{ - //// Actions - - // Left Mouse Button Action - m_leftMouseButtonInput->setButtons(QVector<int>() << Qt::LeftButton); - m_leftMouseButtonInput->setSourceDevice(m_mouseDevice); - m_leftMouseButtonAction->addInput(m_leftMouseButtonInput); - - // Right Mouse Button Action - m_rightMouseButtonInput->setButtons(QVector<int>() << Qt::RightButton); - m_rightMouseButtonInput->setSourceDevice(m_mouseDevice); - m_rightMouseButtonAction->addInput(m_rightMouseButtonInput); - - // Alt Button Action - m_altButtonInput->setButtons(QVector<int>() << Qt::Key_Alt); - m_altButtonInput->setSourceDevice(m_keyboardDevice); - m_altButtonAction->addInput(m_altButtonInput); - - // Shift Button Action - m_shiftButtonInput->setButtons(QVector<int>() << 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); - - // Mouse Wheel X - m_mouseTzXInput->setAxis(Qt3DInput::QMouseDevice::WheelX); - m_mouseTzXInput->setSourceDevice(m_mouseDevice); - m_tzAxis->addInput(m_mouseTzXInput); - - // Mouse Wheel Y - m_mouseTzYInput->setAxis(Qt3DInput::QMouseDevice::WheelY); - m_mouseTzYInput->setSourceDevice(m_mouseDevice); - m_tzAxis->addInput(m_mouseTzYInput); - - // Keyboard Pos Tx - m_keyboardTxPosInput->setButtons(QVector<int>() << Qt::Key_Right); - m_keyboardTxPosInput->setScale(1.0f); - m_keyboardTxPosInput->setSourceDevice(m_keyboardDevice); - m_txAxis->addInput(m_keyboardTxPosInput); - - // Keyboard Pos Tz - m_keyboardTzPosInput->setButtons(QVector<int>() << Qt::Key_PageUp); - m_keyboardTzPosInput->setScale(1.0f); - m_keyboardTzPosInput->setSourceDevice(m_keyboardDevice); - m_tzAxis->addInput(m_keyboardTzPosInput); - - // Keyboard Pos Ty - m_keyboardTyPosInput->setButtons(QVector<int>() << Qt::Key_Up); - m_keyboardTyPosInput->setScale(1.0f); - m_keyboardTyPosInput->setSourceDevice(m_keyboardDevice); - m_tyAxis->addInput(m_keyboardTyPosInput); - - // Keyboard Neg Tx - m_keyboardTxNegInput->setButtons(QVector<int>() << Qt::Key_Left); - m_keyboardTxNegInput->setScale(-1.0f); - m_keyboardTxNegInput->setSourceDevice(m_keyboardDevice); - m_txAxis->addInput(m_keyboardTxNegInput); - - // Keyboard Neg Tz - m_keyboardTzNegInput->setButtons(QVector<int>() << Qt::Key_PageDown); - m_keyboardTzNegInput->setScale(-1.0f); - m_keyboardTzNegInput->setSourceDevice(m_keyboardDevice); - m_tzAxis->addInput(m_keyboardTzNegInput); - - // Keyboard Neg Ty - m_keyboardTyNegInput->setButtons(QVector<int>() << 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(QOrbitCameraController); - //// FrameAction - - QObject::connect(m_frameAction, SIGNAL(triggered(float)), - q, SLOT(_q_onTriggered(float))); - - // Disable the logical device when the entity is disabled - QObject::connect(q, &Qt3DCore::QEntity::enabledChanged, - m_logicalDevice, &Qt3DInput::QLogicalDevice::setEnabled); - - 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 QOrbitCameraControllerPrivate::_q_onTriggered(float dt) -{ - if (m_camera != 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); - } - } -} - QOrbitCameraController::QOrbitCameraController(Qt3DCore::QNode *parent) - : Qt3DCore::QEntity(*new QOrbitCameraControllerPrivate, parent) + : QOrbitCameraController(*new QOrbitCameraControllerPrivate, parent) { - Q_D(QOrbitCameraController); - d->init(); } -QOrbitCameraController::~QOrbitCameraController() +/*! \internal + */ +QOrbitCameraController::QOrbitCameraController(QOrbitCameraControllerPrivate &dd, Qt3DCore::QNode *parent) + : QAbstractCameraController(dd, parent) { } -/*! - \property QOrbitCameraController::camera - - Holds the currently controlled camera. -*/ -Qt3DRender::QCamera *QOrbitCameraController::camera() const -{ - Q_D(const QOrbitCameraController); - return d->m_camera; -} - -/*! - \property QOrbitCameraController::linearSpeed - - Holds the current linear speed of the camera controller. Linear speed determines the - movement speed of the camera. -*/ -float QOrbitCameraController::linearSpeed() const -{ - Q_D(const QOrbitCameraController); - return d->m_linearSpeed; -} - -/*! - \property QOrbitCameraController::lookSpeed - - Holds the current look speed of the camera controller. The look speed determines the turn rate - of the camera pan and tilt. -*/ -float QOrbitCameraController::lookSpeed() const +QOrbitCameraController::~QOrbitCameraController() { - Q_D(const QOrbitCameraController); - return d->m_lookSpeed; } /*! @@ -358,51 +120,77 @@ float QOrbitCameraController::zoomInLimit() const return d->m_zoomInLimit; } -void QOrbitCameraController::setCamera(Qt3DRender::QCamera *camera) +void QOrbitCameraController::setZoomInLimit(float zoomInLimit) { Q_D(QOrbitCameraController); - if (d->m_camera != camera) { - - if (d->m_camera) - d->unregisterDestructionHelper(d->m_camera); - - if (camera && !camera->parent()) - camera->setParent(this); - - d->m_camera = camera; - - // Ensures proper bookkeeping - if (d->m_camera) - d->registerDestructionHelper(d->m_camera, &QOrbitCameraController::setCamera, d->m_camera); - - emit cameraChanged(); + if (d->m_zoomInLimit != zoomInLimit) { + d->m_zoomInLimit = zoomInLimit; + emit zoomInLimitChanged(); } } -void QOrbitCameraController::setLinearSpeed(float linearSpeed) +inline float clampInputs(float input1, float input2) { - Q_D(QOrbitCameraController); - if (d->m_linearSpeed != linearSpeed) { - d->m_linearSpeed = linearSpeed; - emit linearSpeedChanged(); - } + float axisValue = input1 + input2; + return (axisValue < -1) ? -1 : (axisValue > 1) ? 1 : axisValue; } -void QOrbitCameraController::setLookSpeed(float lookSpeed) +inline float zoomDistance(QVector3D firstPoint, QVector3D secondPoint) { - Q_D(QOrbitCameraController); - if (d->m_lookSpeed != lookSpeed) { - d->m_lookSpeed = lookSpeed; - emit lookSpeedChanged(); - } + return (secondPoint - firstPoint).lengthSquared(); } -void QOrbitCameraController::setZoomInLimit(float zoomInLimit) +void QOrbitCameraController::moveCamera(const QAbstractCameraController::InputState &state, float dt) { Q_D(QOrbitCameraController); - if (d->m_zoomInLimit != zoomInLimit) { - d->m_zoomInLimit = zoomInLimit; - emit zoomInLimitChanged(); + + Qt3DRender::QCamera *theCamera = camera(); + + if (theCamera == nullptr) + return; + + const QVector3D upVector(0.0f, 1.0f, 0.0f); + + // Mouse input + if (state.leftMouseButtonActive) { + if (state.rightMouseButtonActive) { + if ( zoomDistance(camera()->position(), theCamera->viewCenter()) > d->m_zoomInLimit * d->m_zoomInLimit) { + // Dolly up to limit + theCamera->translate(QVector3D(0, 0, state.ryAxisValue), theCamera->DontTranslateViewCenter); + } else { + theCamera->translate(QVector3D(0, 0, -0.5), theCamera->DontTranslateViewCenter); + } + } else { + // Translate + theCamera->translate(QVector3D(clampInputs(state.rxAxisValue, state.txAxisValue) * linearSpeed(), + clampInputs(state.ryAxisValue, state.tyAxisValue) * linearSpeed(), + 0) * dt); + } + return; + } + else if (state.rightMouseButtonActive) { + // Orbit + theCamera->panAboutViewCenter((state.rxAxisValue * lookSpeed()) * dt, upVector); + theCamera->tiltAboutViewCenter((state.ryAxisValue * lookSpeed()) * dt); + } + + // Keyboard Input + if (state.altKeyActive) { + // Orbit + theCamera->panAboutViewCenter((state.txAxisValue * lookSpeed()) * dt, upVector); + theCamera->tiltAboutViewCenter((state.tyAxisValue * lookSpeed()) * dt); + } else if (state.shiftKeyActive) { + if (zoomDistance(camera()->position(), theCamera->viewCenter()) > d->m_zoomInLimit * d->m_zoomInLimit) { + // Dolly + theCamera->translate(QVector3D(0, 0, state.tyAxisValue * linearSpeed() * dt), theCamera->DontTranslateViewCenter); + } else { + theCamera->translate(QVector3D(0, 0, -0.5), theCamera->DontTranslateViewCenter); + } + } else { + // Translate + theCamera->translate(QVector3D(clampInputs(state.leftMouseButtonActive ? state.rxAxisValue : 0, state.txAxisValue) * linearSpeed(), + clampInputs(state.leftMouseButtonActive ? state.ryAxisValue : 0, state.tyAxisValue) * linearSpeed(), + state.tzAxisValue * linearSpeed()) * dt); } } diff --git a/src/extras/defaults/qorbitcameracontroller.h b/src/extras/defaults/qorbitcameracontroller.h index 7cb8b3eb7..684bfddf1 100644 --- a/src/extras/defaults/qorbitcameracontroller.h +++ b/src/extras/defaults/qorbitcameracontroller.h @@ -37,50 +37,38 @@ #ifndef QT3DEXTRAS_QORBITCAMERACONTROLLER_H #define QT3DEXTRAS_QORBITCAMERACONTROLLER_H -#include <Qt3DCore/QEntity> -#include <Qt3DExtras/qt3dextras_global.h> +#include <Qt3DExtras/qabstractcameracontroller.h> QT_BEGIN_NAMESPACE -namespace Qt3DRender { -class QCamera; -} - namespace Qt3DExtras { class QOrbitCameraControllerPrivate; -class QT3DEXTRASSHARED_EXPORT QOrbitCameraController : public Qt3DCore::QEntity +class QT3DEXTRASSHARED_EXPORT QOrbitCameraController : public QAbstractCameraController { 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 QOrbitCameraController(Qt3DCore::QNode *parent = nullptr); ~QOrbitCameraController(); - 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(); +protected: + QOrbitCameraController(QOrbitCameraControllerPrivate &dd, Qt3DCore::QNode *parent = nullptr); + +private: + void moveCamera(const QAbstractCameraController::InputState &state, float dt) override; + private: Q_DECLARE_PRIVATE(QOrbitCameraController) - Q_PRIVATE_SLOT(d_func(), void _q_onTriggered(float)) }; } // Qt3DExtras diff --git a/src/extras/defaults/qorbitcameracontroller_p.h b/src/extras/defaults/qorbitcameracontroller_p.h index 8105d4375..1e4e3d4d2 100644 --- a/src/extras/defaults/qorbitcameracontroller_p.h +++ b/src/extras/defaults/qorbitcameracontroller_p.h @@ -48,88 +48,25 @@ // We mean it. // -#include <Qt3DExtras/qorbitcameracontroller.h> -#include <QtGui/QVector3D> - -#include <Qt3DCore/private/qentity_p.h> +#include <Qt3DExtras/private/qabstractcameracontroller_p.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 QAnalogAxisInput; -class QButtonAxisInput; -class QAxisActionHandler; - -} - namespace Qt3DExtras { -class QOrbitCameraControllerPrivate : public Qt3DCore::QEntityPrivate +class QOrbitCameraControllerPrivate : public QAbstractCameraControllerPrivate { + Q_DECLARE_PUBLIC(QOrbitCameraController) + public: QOrbitCameraControllerPrivate(); 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::QAnalogAxisInput *m_mouseRxInput; - Qt3DInput::QAnalogAxisInput *m_mouseRyInput; - Qt3DInput::QAnalogAxisInput *m_mouseTzXInput; - Qt3DInput::QAnalogAxisInput *m_mouseTzYInput; - Qt3DInput::QButtonAxisInput *m_keyboardTxPosInput; - Qt3DInput::QButtonAxisInput *m_keyboardTyPosInput; - Qt3DInput::QButtonAxisInput *m_keyboardTzPosInput; - Qt3DInput::QButtonAxisInput *m_keyboardTxNegInput; - Qt3DInput::QButtonAxisInput *m_keyboardTyNegInput; - Qt3DInput::QButtonAxisInput *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(QOrbitCameraController) +private: + QOrbitCameraController *q_ptr; }; } // namespace Qt3DExtras diff --git a/src/extras/defaults/qphongalphamaterial.cpp b/src/extras/defaults/qphongalphamaterial.cpp index d7f054d71..24cff94e9 100644 --- a/src/extras/defaults/qphongalphamaterial.cpp +++ b/src/extras/defaults/qphongalphamaterial.cpp @@ -45,6 +45,7 @@ #include <Qt3DRender/qeffect.h> #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> @@ -65,10 +66,9 @@ QPhongAlphaMaterialPrivate::QPhongAlphaMaterialPrivate() : QMaterialPrivate() , m_phongEffect(new QEffect()) , m_ambientParameter(new QParameter(QStringLiteral("ka"), QColor::fromRgbF(0.05f, 0.05f, 0.05f, 1.0f))) - , m_diffuseParameter(new QParameter(QStringLiteral("kd"), QColor::fromRgbF(0.7f, 0.7f, 0.7f, 1.0f))) + , m_diffuseParameter(new QParameter(QStringLiteral("kd"), QColor::fromRgbF(0.7f, 0.7f, 0.7f, 0.5f))) , m_specularParameter(new QParameter(QStringLiteral("ks"), QColor::fromRgbF(0.01f, 0.01f, 0.01f, 1.0f))) , m_shininessParameter(new QParameter(QStringLiteral("shininess"), 150.0f)) - , m_alphaParameter(new QParameter(QStringLiteral("alpha"), 0.5f)) , m_phongAlphaGL3Technique(new QTechnique()) , m_phongAlphaGL2Technique(new QTechnique()) , m_phongAlphaES2Technique(new QTechnique()) @@ -76,7 +76,9 @@ QPhongAlphaMaterialPrivate::QPhongAlphaMaterialPrivate() , m_phongAlphaGL2RenderPass(new QRenderPass()) , m_phongAlphaES2RenderPass(new QRenderPass()) , m_phongAlphaGL3Shader(new QShaderProgram()) + , m_phongAlphaGL3ShaderBuilder(new QShaderProgramBuilder()) , m_phongAlphaGL2ES2Shader(new QShaderProgram()) + , m_phongAlphaGL2ES2ShaderBuilder(new QShaderProgramBuilder()) , m_noDepthMask(new QNoDepthMask()) , m_blendState(new QBlendEquationArguments()) , m_blendEquation(new QBlendEquation()) @@ -87,6 +89,8 @@ QPhongAlphaMaterialPrivate::QPhongAlphaMaterialPrivate() // TODO: Define how lights are properties are set in the shaders. Ideally using a QShaderData void QPhongAlphaMaterialPrivate::init() { + Q_Q(QPhongAlphaMaterial); + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, this, &QPhongAlphaMaterialPrivate::handleAmbientChanged); connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, @@ -95,13 +99,22 @@ void QPhongAlphaMaterialPrivate::init() this, &QPhongAlphaMaterialPrivate::handleSpecularChanged); connect(m_shininessParameter, &Qt3DRender::QParameter::valueChanged, this, &QPhongAlphaMaterialPrivate::handleShininessChanged); - connect(m_alphaParameter, &Qt3DRender::QParameter::valueChanged, - this, &QPhongAlphaMaterialPrivate::handleAlphaChanged); - m_phongAlphaGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/phong.vert")))); - m_phongAlphaGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/phongalpha.frag")))); - m_phongAlphaGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/phong.vert")))); - m_phongAlphaGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/phongalpha.frag")))); + m_phongAlphaGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_phongAlphaGL3ShaderBuilder->setParent(q); + m_phongAlphaGL3ShaderBuilder->setShaderProgram(m_phongAlphaGL3Shader); + m_phongAlphaGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_phongAlphaGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse"), + QStringLiteral("specular"), + QStringLiteral("normal")}); + + m_phongAlphaGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_phongAlphaGL2ES2ShaderBuilder->setParent(q); + m_phongAlphaGL2ES2ShaderBuilder->setShaderProgram(m_phongAlphaGL2ES2Shader); + m_phongAlphaGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_phongAlphaGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse"), + QStringLiteral("specular"), + QStringLiteral("normal")}); m_phongAlphaGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_phongAlphaGL3Technique->graphicsApiFilter()->setMajorVersion(3); @@ -118,7 +131,6 @@ void QPhongAlphaMaterialPrivate::init() m_phongAlphaES2Technique->graphicsApiFilter()->setMinorVersion(0); m_phongAlphaES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); - Q_Q(QPhongAlphaMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -159,7 +171,6 @@ void QPhongAlphaMaterialPrivate::init() m_phongEffect->addParameter(m_diffuseParameter); m_phongEffect->addParameter(m_specularParameter); m_phongEffect->addParameter(m_shininessParameter); - m_phongEffect->addParameter(m_alphaParameter); q->setEffect(m_phongEffect); } @@ -174,6 +185,7 @@ void QPhongAlphaMaterialPrivate::handleDiffuseChanged(const QVariant &var) { Q_Q(QPhongAlphaMaterial); emit q->diffuseChanged(var.value<QColor>()); + emit q->alphaChanged(var.value<QColor>().alphaF()); } void QPhongAlphaMaterialPrivate::handleSpecularChanged(const QVariant &var) @@ -188,12 +200,6 @@ void QPhongAlphaMaterialPrivate::handleShininessChanged(const QVariant &var) emit q->shininessChanged(var.toFloat()); } -void QPhongAlphaMaterialPrivate::handleAlphaChanged(const QVariant &var) -{ - Q_Q(QPhongAlphaMaterial); - emit q->alphaChanged(var.toFloat()); -} - /*! \class Qt3DExtras::QPhongAlphaMaterial @@ -203,6 +209,9 @@ void QPhongAlphaMaterialPrivate::handleAlphaChanged(const QVariant &var) \since 5.7 \inherits Qt3DRender::QMaterial + \deprecated + \see Qt3DExtras::QDiffuseSpecularMaterial + The phong lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. The relative strengths of these components are controlled by means of their reflectivity coefficients which are modelled as RGB triplets: @@ -301,7 +310,7 @@ float QPhongAlphaMaterial::shininess() const float QPhongAlphaMaterial::alpha() const { Q_D(const QPhongAlphaMaterial); - return d->m_alphaParameter->value().toFloat(); + return d->m_diffuseParameter->value().value<QColor>().alphaF(); } /*! @@ -378,7 +387,10 @@ void QPhongAlphaMaterial::setAmbient(const QColor &ambient) void QPhongAlphaMaterial::setDiffuse(const QColor &diffuse) { Q_D(QPhongAlphaMaterial); - d->m_diffuseParameter->setValue(diffuse); + QColor currentDiffuse = d->m_diffuseParameter->value().value<QColor>(); + QColor newDiffuse = diffuse; + newDiffuse.setAlphaF(currentDiffuse.alphaF()); + d->m_diffuseParameter->setValue(newDiffuse); } void QPhongAlphaMaterial::setSpecular(const QColor &specular) @@ -396,7 +408,9 @@ void QPhongAlphaMaterial::setShininess(float shininess) void QPhongAlphaMaterial::setAlpha(float alpha) { Q_D(QPhongAlphaMaterial); - d->m_alphaParameter->setValue(alpha); + QColor diffuse = d->m_diffuseParameter->value().value<QColor>(); + diffuse.setAlphaF(alpha); + d->m_diffuseParameter->setValue(diffuse); } void QPhongAlphaMaterial::setSourceRgbArg(QBlendEquationArguments::Blending sourceRgbArg) diff --git a/src/extras/defaults/qphongalphamaterial_p.h b/src/extras/defaults/qphongalphamaterial_p.h index 623eca0ea..97eaf7bc4 100644 --- a/src/extras/defaults/qphongalphamaterial_p.h +++ b/src/extras/defaults/qphongalphamaterial_p.h @@ -62,6 +62,7 @@ class QEffect; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; class QNoDepthMask; class QBlendEquationArguments; @@ -84,14 +85,12 @@ public: void handleDiffuseChanged(const QVariant &var); void handleSpecularChanged(const QVariant &var); void handleShininessChanged(const QVariant &var); - void handleAlphaChanged(const QVariant &var); Qt3DRender::QEffect *m_phongEffect; Qt3DRender::QParameter *m_ambientParameter; Qt3DRender::QParameter *m_diffuseParameter; Qt3DRender::QParameter *m_specularParameter; Qt3DRender::QParameter *m_shininessParameter; - Qt3DRender::QParameter *m_alphaParameter; Qt3DRender::QTechnique *m_phongAlphaGL3Technique; Qt3DRender::QTechnique *m_phongAlphaGL2Technique; Qt3DRender::QTechnique *m_phongAlphaES2Technique; @@ -99,7 +98,9 @@ public: Qt3DRender::QRenderPass *m_phongAlphaGL2RenderPass; Qt3DRender::QRenderPass *m_phongAlphaES2RenderPass; Qt3DRender::QShaderProgram *m_phongAlphaGL3Shader; + Qt3DRender::QShaderProgramBuilder *m_phongAlphaGL3ShaderBuilder; Qt3DRender::QShaderProgram *m_phongAlphaGL2ES2Shader; + Qt3DRender::QShaderProgramBuilder *m_phongAlphaGL2ES2ShaderBuilder; Qt3DRender::QNoDepthMask *m_noDepthMask; Qt3DRender::QBlendEquationArguments *m_blendState; Qt3DRender::QBlendEquation *m_blendEquation; diff --git a/src/extras/defaults/qphongmaterial.cpp b/src/extras/defaults/qphongmaterial.cpp index 449eb7351..17d837568 100644 --- a/src/extras/defaults/qphongmaterial.cpp +++ b/src/extras/defaults/qphongmaterial.cpp @@ -45,6 +45,7 @@ #include <Qt3DRender/qeffect.h> #include <Qt3DRender/qtechnique.h> #include <Qt3DRender/qshaderprogram.h> +#include <Qt3DRender/qshaderprogrambuilder.h> #include <Qt3DRender/qparameter.h> #include <Qt3DRender/qrenderpass.h> #include <Qt3DRender/qgraphicsapifilter.h> @@ -73,13 +74,17 @@ QPhongMaterialPrivate::QPhongMaterialPrivate() , m_phongGL2RenderPass(new QRenderPass()) , m_phongES2RenderPass(new QRenderPass()) , m_phongGL3Shader(new QShaderProgram()) + , m_phongGL3ShaderBuilder(new QShaderProgramBuilder()) , m_phongGL2ES2Shader(new QShaderProgram()) + , m_phongGL2ES2ShaderBuilder(new QShaderProgramBuilder()) , m_filterKey(new QFilterKey) { } void QPhongMaterialPrivate::init() { + Q_Q(QPhongMaterial); + connect(m_ambientParameter, &Qt3DRender::QParameter::valueChanged, this, &QPhongMaterialPrivate::handleAmbientChanged); connect(m_diffuseParameter, &Qt3DRender::QParameter::valueChanged, @@ -89,11 +94,21 @@ void QPhongMaterialPrivate::init() connect(m_shininessParameter, &Qt3DRender::QParameter::valueChanged, this, &QPhongMaterialPrivate::handleShininessChanged); - - m_phongGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/phong.vert")))); - m_phongGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/phong.frag")))); - m_phongGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/phong.vert")))); - m_phongGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/phong.frag")))); + m_phongGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert")))); + m_phongGL3ShaderBuilder->setParent(q); + m_phongGL3ShaderBuilder->setShaderProgram(m_phongGL3Shader); + m_phongGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_phongGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse"), + QStringLiteral("specular"), + QStringLiteral("normal")}); + + m_phongGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/es2/default.vert")))); + m_phongGL2ES2ShaderBuilder->setParent(q); + m_phongGL2ES2ShaderBuilder->setShaderProgram(m_phongGL2ES2Shader); + m_phongGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json"))); + m_phongGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse"), + QStringLiteral("specular"), + QStringLiteral("normal")}); m_phongGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); m_phongGL3Technique->graphicsApiFilter()->setMajorVersion(3); @@ -118,7 +133,6 @@ void QPhongMaterialPrivate::init() m_phongGL2Technique->addRenderPass(m_phongGL2RenderPass); m_phongES2Technique->addRenderPass(m_phongES2RenderPass); - Q_Q(QPhongMaterial); m_filterKey->setParent(q); m_filterKey->setName(QStringLiteral("renderingStyle")); m_filterKey->setValue(QStringLiteral("forward")); @@ -170,6 +184,9 @@ void QPhongMaterialPrivate::handleShininessChanged(const QVariant &var) \since 5.7 \inherits Qt3DRender::QMaterial + \deprecated + \see Qt3DExtras::QDiffuseSpecularMaterial + The phong lighting effect is based on the combination of 3 lighting components ambient, diffuse and specular. The relative strengths of these components are controlled by means of their reflectivity coefficients which are modelled as RGB triplets: diff --git a/src/extras/defaults/qphongmaterial_p.h b/src/extras/defaults/qphongmaterial_p.h index bc6d0ce44..f1e55242b 100644 --- a/src/extras/defaults/qphongmaterial_p.h +++ b/src/extras/defaults/qphongmaterial_p.h @@ -62,6 +62,7 @@ class QEffect; class QTechnique; class QParameter; class QShaderProgram; +class QShaderProgramBuilder; class QRenderPass; } // namespace Qt3DRender @@ -94,7 +95,9 @@ public: Qt3DRender::QRenderPass *m_phongGL2RenderPass; Qt3DRender::QRenderPass *m_phongES2RenderPass; Qt3DRender::QShaderProgram *m_phongGL3Shader; + Qt3DRender::QShaderProgramBuilder *m_phongGL3ShaderBuilder; Qt3DRender::QShaderProgram *m_phongGL2ES2Shader; + Qt3DRender::QShaderProgramBuilder *m_phongGL2ES2ShaderBuilder; Qt3DRender::QFilterKey *m_filterKey; Q_DECLARE_PUBLIC(QPhongMaterial) diff --git a/src/extras/defaults/qspritegrid.cpp b/src/extras/defaults/qspritegrid.cpp new file mode 100644 index 000000000..31d4dd5c0 --- /dev/null +++ b/src/extras/defaults/qspritegrid.cpp @@ -0,0 +1,156 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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$ +** +****************************************************************************/ + +#include "qspritegrid.h" +#include "qspritegrid_p.h" + +#include <Qt3DRender/qabstracttexture.h> + +QT_BEGIN_NAMESPACE + +using namespace Qt3DCore; + +namespace Qt3DExtras { + +QSpriteGridPrivate::QSpriteGridPrivate() + : QAbstractSpriteSheetPrivate() + , m_numColumns(1) + , m_numRows(1) +{ +} + +int QSpriteGridPrivate::maxIndex() const +{ + return m_numColumns * m_numRows; +} + +void QSpriteGridPrivate::updateSizes() +{ + Q_Q(QSpriteGrid); + if (m_texture && m_numColumns && m_numRows) { + m_textureSize = QSize(m_texture->width(), m_texture->height()); + m_cellSize = QSize(m_texture->width() / m_numColumns, m_texture->height() / m_numRows); + } else { + m_textureSize = QSize(); + m_cellSize = QSize(); + } + + if (m_cellSize.isEmpty() || m_numColumns == 0 || m_numRows == 0) { + if (m_currentIndex != -1) { + m_currentIndex = -1; + emit q->currentIndexChanged(m_currentIndex); + } + m_textureTransform.setToIdentity(); + emit q->textureTransformChanged(m_textureTransform); + return; + } + + if (m_currentIndex == -1) { + m_currentIndex = 0; + emit q->currentIndexChanged(m_currentIndex); + } + updateTransform(); +} + +void QSpriteGridPrivate::updateTransform() +{ + Q_Q(QSpriteGrid); + const float xScale = (float) m_cellSize.width() / (float) m_textureSize.width(); + const float yScale = (float) m_cellSize.height() / (float) m_textureSize.height(); + + const int currentRow = m_currentIndex / m_numColumns; + const int currentColumn = m_currentIndex % m_numColumns; + const float xTranslate = currentColumn * xScale; + const float yTranslate = currentRow * yScale; + + m_textureTransform.setToIdentity(); + m_textureTransform(0, 0) = xScale; + m_textureTransform(1, 1) = yScale; + m_textureTransform(0, 2) = xTranslate; + m_textureTransform(1, 2) = yTranslate; + emit q->textureTransformChanged(m_textureTransform); +} + +/*! + Constructs a new QSpriteGrid instance with parent object \a parent. + */ +QSpriteGrid::QSpriteGrid(QNode *parent) + : QAbstractSpriteSheet(*new QSpriteGridPrivate, parent) +{ +} + +QSpriteGrid::~QSpriteGrid() +{ +} + +int QSpriteGrid::rows() const +{ + Q_D(const QSpriteGrid); + return d->m_numRows; +} + +void QSpriteGrid::setRows(int rows) +{ + Q_D(QSpriteGrid); + if (d->m_numRows != rows) { + d->m_numRows = rows; + d->updateSizes(); + emit rowsChanged(rows); + } +} + +int QSpriteGrid::columns() const +{ + Q_D(const QSpriteGrid); + return d->m_numColumns; +} + +void QSpriteGrid::setColumns(int columns) +{ + Q_D(QSpriteGrid); + if (d->m_numColumns != columns) { + d->m_numColumns = columns; + d->updateSizes(); + emit columnsChanged(columns); + } +} + +} // namespace Qt3DExtras + +QT_END_NAMESPACE diff --git a/src/extras/defaults/qspritegrid.h b/src/extras/defaults/qspritegrid.h new file mode 100644 index 000000000..3b6a7005a --- /dev/null +++ b/src/extras/defaults/qspritegrid.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QSPRITEGRID_H +#define QT3DEXTRAS_QSPRITEGRID_H + +#include <Qt3DExtras/qabstractspritesheet.h> +#include <Qt3DCore/qcomponent.h> +#include <QVector2D> +#include <QMatrix3x3> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +class QSpriteGridPrivate; + +class QT3DEXTRASSHARED_EXPORT QSpriteGrid : public QAbstractSpriteSheet +{ + Q_OBJECT + Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowsChanged) + Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged) +public: + explicit QSpriteGrid(Qt3DCore::QNode *parent = nullptr); + ~QSpriteGrid(); + + int rows() const; + int columns() const; + +public Q_SLOTS: + void setRows(int rows); + void setColumns(int columns); + +Q_SIGNALS: + void rowsChanged(int rows); + void columnsChanged(int columns); + +private: + Q_DECLARE_PRIVATE(QSpriteGrid) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QSPRITEGRID_H diff --git a/src/extras/defaults/qspritegrid_p.h b/src/extras/defaults/qspritegrid_p.h new file mode 100644 index 000000000..6135ebf0d --- /dev/null +++ b/src/extras/defaults/qspritegrid_p.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QSPRITEGRID_P_H +#define QT3DEXTRAS_QSPRITEGRID_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 <Qt3DExtras/private/qabstractspritesheet_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +class QSpriteGrid; + +class QSpriteGridPrivate : public QAbstractSpriteSheetPrivate +{ + QSpriteGridPrivate(); + + int maxIndex() const override; + void updateSizes() override; + void updateTransform() override; + + int m_numColumns; + int m_numRows; + QSize m_cellSize; + + Q_DECLARE_PUBLIC(QSpriteGrid) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QSPRITEGRID_P_H + diff --git a/src/extras/defaults/qspritesheet.cpp b/src/extras/defaults/qspritesheet.cpp new file mode 100644 index 000000000..87f664ce4 --- /dev/null +++ b/src/extras/defaults/qspritesheet.cpp @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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$ +** +****************************************************************************/ + +#include "qspritesheet.h" +#include "qspritesheetitem.h" +#include "qspritesheet_p.h" + +#include <Qt3DRender/qabstracttexture.h> + +QT_BEGIN_NAMESPACE + +using namespace Qt3DCore; + +namespace Qt3DExtras { + +QSpriteSheetPrivate::QSpriteSheetPrivate() + : QAbstractSpriteSheetPrivate() +{ +} + +int QSpriteSheetPrivate::maxIndex() const +{ + return m_sprites.count(); +} + +void QSpriteSheetPrivate::updateSizes() +{ + Q_Q(QSpriteSheet); + if (m_texture) + m_textureSize = QSize(m_texture->width(), m_texture->height()); + else + m_textureSize = QSize(); + + if (m_textureSize.isEmpty() || m_sprites.isEmpty()) { + if (m_currentIndex != -1) { + m_currentIndex = -1; + emit q->currentIndexChanged(m_currentIndex); + } + m_textureTransform.setToIdentity(); + emit q->textureTransformChanged(m_textureTransform); + return; + } + + if (m_currentIndex < 0 || m_currentIndex > m_sprites.size()) { + m_currentIndex = 0; + emit q->currentIndexChanged(m_currentIndex); + } + updateTransform(); +} + +void QSpriteSheetPrivate::updateTransform() +{ + Q_Q(QSpriteSheet); + if (m_currentIndex < 0 || m_currentIndex >= m_sprites.size()) + return; + + const QSpriteSheetItem *r = m_sprites.at(m_currentIndex); + const float xScale = static_cast<float>(r->width()) / static_cast<float>(m_textureSize.width()); + const float yScale = static_cast<float>(r->height()) / static_cast<float>(m_textureSize.height()); + + const float xTranslate = static_cast<float>(r->x()) / static_cast<float>(m_textureSize.width()); + const float yTranslate = static_cast<float>(r->y()) / static_cast<float>(m_textureSize.height()); + + m_textureTransform.setToIdentity(); + m_textureTransform(0, 0) = xScale; + m_textureTransform(1, 1) = yScale; + m_textureTransform(0, 2) = xTranslate; + m_textureTransform(1, 2) = yTranslate; + emit q->textureTransformChanged(m_textureTransform); +} + +/*! + Constructs a new QSpriteSheet instance with parent object \a parent. + */ +QSpriteSheet::QSpriteSheet(QNode *parent) + : QAbstractSpriteSheet(*new QSpriteSheetPrivate, parent) +{ +} + +QSpriteSheet::~QSpriteSheet() +{ +} + +QVector<QSpriteSheetItem *> QSpriteSheet::sprites() const +{ + Q_D(const QSpriteSheet); + return d->m_sprites; +} + +QSpriteSheetItem *QSpriteSheet::addSprite(int x, int y, int width, int height) +{ + QSpriteSheetItem *item = new QSpriteSheetItem(this); + item->setX(x); + item->setX(y); + item->setWidth(width); + item->setHeight(height); + addSprite(item); + return item; +} + +void QSpriteSheet::addSprite(QSpriteSheetItem *sprite) +{ + Q_ASSERT(sprite); + Q_D(QSpriteSheet); + if (!d->m_sprites.contains(sprite)) { + d->m_sprites << sprite; + + // Ensures proper bookkeeping + d->registerDestructionHelper(sprite, &QSpriteSheet::removeSprite, d->m_sprites); + if (!sprite->parent()) + sprite->setParent(this); + + emit spritesChanged(d->m_sprites); + d->updateSizes(); + } +} + +void QSpriteSheet::removeSprite(QSpriteSheetItem *sprite) +{ + Q_ASSERT(sprite); + Q_D(QSpriteSheet); + d->m_sprites.removeOne(sprite); + // Remove bookkeeping connection + d->unregisterDestructionHelper(sprite); +} + +void QSpriteSheet::setSprites(QVector<QSpriteSheetItem *> sprites) +{ + Q_D(QSpriteSheet); + d->m_sprites = sprites; + emit spritesChanged(sprites); + d->updateSizes(); +} + +} // namespace Qt3DExtras + +QT_END_NAMESPACE diff --git a/src/extras/defaults/qspritesheet.h b/src/extras/defaults/qspritesheet.h new file mode 100644 index 000000000..00fd293c3 --- /dev/null +++ b/src/extras/defaults/qspritesheet.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QSPRITESHEET_H +#define QT3DEXTRAS_QSPRITESHEET_H + +#include <Qt3DExtras/qabstractspritesheet.h> +#include <QRect> +#include <QVector> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +class QSpriteSheetItem; +class QSpriteSheetPrivate; + +class QT3DEXTRASSHARED_EXPORT QSpriteSheet : public QAbstractSpriteSheet +{ + Q_OBJECT + Q_PROPERTY(QVector<QSpriteSheetItem *> sprites READ sprites WRITE setSprites NOTIFY spritesChanged) +public: + explicit QSpriteSheet(Qt3DCore::QNode *parent = nullptr); + ~QSpriteSheet(); + + QVector<QSpriteSheetItem *> sprites() const; + QSpriteSheetItem *addSprite(int x, int y, int width, int height); + void addSprite(QSpriteSheetItem *sprite); + void removeSprite(QSpriteSheetItem *sprite); + +public Q_SLOTS: + void setSprites(QVector<QSpriteSheetItem *> sprites); + +Q_SIGNALS: + void spritesChanged(QVector<QSpriteSheetItem *> sprites); + +private: + Q_DECLARE_PRIVATE(QSpriteSheet) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QSPRITESHEET_H diff --git a/src/extras/defaults/qspritesheet_p.h b/src/extras/defaults/qspritesheet_p.h new file mode 100644 index 000000000..5091800fe --- /dev/null +++ b/src/extras/defaults/qspritesheet_p.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QSPRITESHEET_P_H +#define QT3DEXTRAS_QSPRITESHEET_P_H +#include <QRect> +#include <QVector> + +// +// 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 <Qt3DExtras/private/qabstractspritesheet_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +class QSpriteSheetItem; +class QSpriteSheet; + +class QSpriteSheetPrivate : public QAbstractSpriteSheetPrivate +{ + QSpriteSheetPrivate(); + + int maxIndex() const override; + void updateSizes() override; + void updateTransform() override; + + QVector<QSpriteSheetItem *> m_sprites; + + Q_DECLARE_PUBLIC(QSpriteSheet) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QSPRITESHEET_P_H + diff --git a/src/extras/defaults/qspritesheetitem.cpp b/src/extras/defaults/qspritesheetitem.cpp new file mode 100644 index 000000000..b4e096ae9 --- /dev/null +++ b/src/extras/defaults/qspritesheetitem.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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$ +** +****************************************************************************/ + +#include "qspritesheetitem.h" +#include "qspritesheetitem_p.h" + +QT_BEGIN_NAMESPACE + +using namespace Qt3DCore; + +namespace Qt3DExtras { + +QSpriteSheetItemPrivate::QSpriteSheetItemPrivate() + : QNodePrivate() + , m_x(0) + , m_y(0) + , m_width(0) + , m_height(0) +{ + +} + +QSpriteSheetItem::QSpriteSheetItem(QNode *parent) + : Qt3DCore::QNode(* new QSpriteSheetItemPrivate(), parent) +{ + +} + +int QSpriteSheetItem::x() const +{ + Q_D(const QSpriteSheetItem); + return d->m_x; +} + +void QSpriteSheetItem::setX(int x) +{ + Q_D(QSpriteSheetItem); + if (x != d->m_x) { + d->m_x = x; + emit xChanged(x); + } +} + +int QSpriteSheetItem::y() const +{ + Q_D(const QSpriteSheetItem); + return d->m_y; +} + +void QSpriteSheetItem::setY(int y) +{ + Q_D(QSpriteSheetItem); + if (y != d->m_y) { + d->m_y = y; + emit yChanged(y); + } +} + +int QSpriteSheetItem::width() const +{ + Q_D(const QSpriteSheetItem); + return d->m_width; +} + +void QSpriteSheetItem::setWidth(int width) +{ + Q_D(QSpriteSheetItem); + if (width != d->m_width) { + d->m_width = width; + emit widthChanged(width); + } +} + +int QSpriteSheetItem::height() const +{ + Q_D(const QSpriteSheetItem); + return d->m_height; +} + +void QSpriteSheetItem::setHeight(int height) +{ + Q_D(QSpriteSheetItem); + if (height != d->m_height) { + d->m_height = height; + emit heightChanged(height); + } +} + +} // namespace Qt3DExtras + +QT_END_NAMESPACE diff --git a/src/extras/defaults/qspritesheetitem.h b/src/extras/defaults/qspritesheetitem.h new file mode 100644 index 000000000..f47846071 --- /dev/null +++ b/src/extras/defaults/qspritesheetitem.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QSPRITESHEETITEM_H +#define QT3DEXTRAS_QSPRITESHEETITEM_H + +#include <Qt3DExtras/qabstractspritesheet.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +class QSpriteSheetItemPrivate; + +class QT3DEXTRASSHARED_EXPORT QSpriteSheetItem : public Qt3DCore::QNode +{ + Q_OBJECT + Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged) + Q_PROPERTY(int y READ y WRITE setY NOTIFY yChanged) + Q_PROPERTY(int width READ width WRITE setWidth NOTIFY widthChanged) + Q_PROPERTY(int height READ height WRITE setHeight NOTIFY heightChanged) +public: + explicit QSpriteSheetItem(Qt3DCore::QNode *parent = nullptr); + + int x() const; + int y() const; + int width() const; + int height() const; + +public Q_SLOTS: + void setX(int x); + void setY(int y); + void setWidth(int width); + void setHeight(int height); + +Q_SIGNALS: + void xChanged(int x); + void yChanged(int y); + void widthChanged(int width); + void heightChanged(int height); + +private: + Q_DECLARE_PRIVATE(QSpriteSheetItem) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QSPRITESHEETITEM_H diff --git a/src/extras/defaults/qspritesheetitem_p.h b/src/extras/defaults/qspritesheetitem_p.h new file mode 100644 index 000000000..f23d71cd7 --- /dev/null +++ b/src/extras/defaults/qspritesheetitem_p.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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 QT3DEXTRAS_QSPRITESHEET_P_H +#define QT3DEXTRAS_QSPRITESHEET_P_H +#include <QRect> +#include <QVector> + +// +// 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/qnode_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DExtras { + +class QSpriteSheetItem; + +class QSpriteSheetItemPrivate: public Qt3DCore::QNodePrivate +{ + QSpriteSheetItemPrivate(); + + int m_x; + int m_y; + int m_width; + int m_height; + + Q_DECLARE_PUBLIC(QSpriteSheetItem) +}; + +} // Qt3DExtras + +QT_END_NAMESPACE + +#endif // QT3DEXTRAS_QSPRITESHEET_P_H + diff --git a/src/extras/defaults/qt3dwindow.cpp b/src/extras/defaults/qt3dwindow.cpp index 635d81956..12e210f0d 100644 --- a/src/extras/defaults/qt3dwindow.cpp +++ b/src/extras/defaults/qt3dwindow.cpp @@ -62,6 +62,13 @@ #include <Qt3DRender/qcamera.h> #include <QtGui/qopenglcontext.h> +static void initResources() +{ +#ifdef QT_STATIC + Q_INIT_RESOURCE(extras); +#endif +} + QT_BEGIN_NAMESPACE namespace Qt3DExtras { @@ -86,6 +93,8 @@ Qt3DWindow::Qt3DWindow(QScreen *screen) { Q_D(Qt3DWindow); + initResources(); + if (!d->parentWindow) d->connectToScreen(screen ? screen : d->topLevelScreen.data()); @@ -125,6 +134,9 @@ Qt3DWindow::~Qt3DWindow() delete d->m_aspectEngine; } +/*! + Registers the specified \a aspect. +*/ void Qt3DWindow::registerAspect(Qt3DCore::QAbstractAspect *aspect) { Q_ASSERT(!isVisible()); @@ -132,6 +144,9 @@ void Qt3DWindow::registerAspect(Qt3DCore::QAbstractAspect *aspect) d->m_aspectEngine->registerAspect(aspect); } +/*! + Registers the specified aspect \a name. +*/ void Qt3DWindow::registerAspect(const QString &name) { Q_ASSERT(!isVisible()); @@ -139,6 +154,9 @@ void Qt3DWindow::registerAspect(const QString &name) d->m_aspectEngine->registerAspect(name); } +/*! + Sets the specified \a root entity of the scene. +*/ void Qt3DWindow::setRootEntity(Qt3DCore::QEntity *root) { Q_D(Qt3DWindow); @@ -151,18 +169,27 @@ void Qt3DWindow::setRootEntity(Qt3DCore::QEntity *root) } } +/*! + Activates the specified \a activeFrameGraph. +*/ void Qt3DWindow::setActiveFrameGraph(Qt3DRender::QFrameGraphNode *activeFrameGraph) { Q_D(Qt3DWindow); d->m_renderSettings->setActiveFrameGraph(activeFrameGraph); } +/*! + Returns the node of the active frame graph. +*/ Qt3DRender::QFrameGraphNode *Qt3DWindow::activeFrameGraph() const { Q_D(const Qt3DWindow); return d->m_renderSettings->activeFrameGraph(); } +/*! + Returns the node of the default framegraph +*/ Qt3DExtras::QForwardRenderer *Qt3DWindow::defaultFrameGraph() const { Q_D(const Qt3DWindow); @@ -175,12 +202,18 @@ Qt3DRender::QCamera *Qt3DWindow::camera() const return d->m_defaultCamera; } +/*! + Returns the render settings of the 3D Window. +*/ Qt3DRender::QRenderSettings *Qt3DWindow::renderSettings() const { Q_D(const Qt3DWindow); return d->m_renderSettings; } +/*! + Manages the display events specified in \a e. +*/ void Qt3DWindow::showEvent(QShowEvent *e) { Q_D(Qt3DWindow); @@ -194,7 +227,9 @@ void Qt3DWindow::showEvent(QShowEvent *e) QWindow::showEvent(e); } - +/*! + Resets the aspect ratio of the 3D window. +*/ void Qt3DWindow::resizeEvent(QResizeEvent *) { Q_D(Qt3DWindow); diff --git a/src/extras/defaults/qtexturedmetalroughmaterial.cpp b/src/extras/defaults/qtexturedmetalroughmaterial.cpp index e09517866..8e26ddccc 100644 --- a/src/extras/defaults/qtexturedmetalroughmaterial.cpp +++ b/src/extras/defaults/qtexturedmetalroughmaterial.cpp @@ -38,334 +38,28 @@ ****************************************************************************/ #include "qtexturedmetalroughmaterial.h" -#include "qtexturedmetalroughmaterial_p.h" -#include <Qt3DRender/qfilterkey.h> -#include <Qt3DRender/qmaterial.h> -#include <Qt3DRender/qeffect.h> -#include <Qt3DRender/qtexture.h> -#include <Qt3DRender/qtechnique.h> -#include <Qt3DRender/qshaderprogram.h> -#include <Qt3DRender/qparameter.h> -#include <Qt3DRender/qrenderpass.h> -#include <Qt3DRender/qgraphicsapifilter.h> -#include <QUrl> -#include <QVector3D> -#include <QVector4D> QT_BEGIN_NAMESPACE -using namespace Qt3DRender; - namespace Qt3DExtras { -QTexturedMetalRoughMaterialPrivate::QTexturedMetalRoughMaterialPrivate() - : QMaterialPrivate() - , m_baseColorTexture(new QTexture2D()) - , m_metalnessTexture(new QTexture2D()) - , m_roughnessTexture(new QTexture2D()) - , m_ambientOcclusionTexture(new QTexture2D()) - , m_normalTexture(new QTexture2D()) - , m_environmentIrradianceTexture(new QTexture2D()) - , m_environmentSpecularTexture(new QTexture2D()) - , m_baseColorParameter(new QParameter(QStringLiteral("baseColorMap"), m_baseColorTexture)) - , m_metalnessParameter(new QParameter(QStringLiteral("metalnessMap"), m_metalnessTexture)) - , m_roughnessParameter(new QParameter(QStringLiteral("roughnessMap"), m_roughnessTexture)) - , m_ambientOcclusionParameter(new QParameter(QStringLiteral("ambientOcclusionMap"), m_ambientOcclusionTexture)) - , m_normalParameter(new QParameter(QStringLiteral("normalMap"), m_normalTexture)) - , m_environmentIrradianceParameter(new QParameter(QStringLiteral("envLight.irradiance"), m_environmentIrradianceTexture)) - , m_environmentSpecularParameter(new QParameter(QStringLiteral("envLight.specular"), m_environmentSpecularTexture)) - , m_metalRoughEffect(new QEffect()) - , m_metalRoughGL3Technique(new QTechnique()) - , m_metalRoughGL3RenderPass(new QRenderPass()) - , m_metalRoughGL3Shader(new QShaderProgram()) - , m_filterKey(new QFilterKey) -{ - m_baseColorTexture->setMagnificationFilter(QAbstractTexture::Linear); - m_baseColorTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); - m_baseColorTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); - m_baseColorTexture->setGenerateMipMaps(true); - m_baseColorTexture->setMaximumAnisotropy(16.0f); - - m_metalnessTexture->setMagnificationFilter(QAbstractTexture::Linear); - m_metalnessTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); - m_metalnessTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); - m_metalnessTexture->setGenerateMipMaps(true); - m_metalnessTexture->setMaximumAnisotropy(16.0f); - - m_roughnessTexture->setMagnificationFilter(QAbstractTexture::Linear); - m_roughnessTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); - m_roughnessTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); - m_roughnessTexture->setGenerateMipMaps(true); - m_roughnessTexture->setMaximumAnisotropy(16.0f); - - m_ambientOcclusionTexture->setMagnificationFilter(QAbstractTexture::Linear); - m_ambientOcclusionTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); - m_ambientOcclusionTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); - m_ambientOcclusionTexture->setGenerateMipMaps(true); - m_ambientOcclusionTexture->setMaximumAnisotropy(16.0f); - - m_normalTexture->setMagnificationFilter(QAbstractTexture::Linear); - m_normalTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); - m_normalTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); - m_normalTexture->setGenerateMipMaps(true); - m_normalTexture->setMaximumAnisotropy(16.0f); - - m_environmentIrradianceTexture->setMagnificationFilter(QAbstractTexture::Linear); - m_environmentIrradianceTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); - m_environmentIrradianceTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); - m_environmentIrradianceTexture->setGenerateMipMaps(true); - m_environmentIrradianceTexture->setMaximumAnisotropy(16.0f); - - m_environmentSpecularTexture->setMagnificationFilter(QAbstractTexture::Linear); - m_environmentSpecularTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); - m_environmentSpecularTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); - m_environmentSpecularTexture->setGenerateMipMaps(true); - m_environmentSpecularTexture->setMaximumAnisotropy(16.0f); -} - -void QTexturedMetalRoughMaterialPrivate::init() -{ - connect(m_baseColorParameter, &Qt3DRender::QParameter::valueChanged, - this, &QTexturedMetalRoughMaterialPrivate::handleBaseColorChanged); - connect(m_metalnessParameter, &Qt3DRender::QParameter::valueChanged, - this, &QTexturedMetalRoughMaterialPrivate::handleMetallicChanged); - connect(m_roughnessParameter, &Qt3DRender::QParameter::valueChanged, - this, &QTexturedMetalRoughMaterialPrivate::handleRoughnessChanged); - connect(m_ambientOcclusionParameter, &Qt3DRender::QParameter::valueChanged, - this, &QTexturedMetalRoughMaterialPrivate::handleAmbientOcclusionChanged); - connect(m_normalParameter, &Qt3DRender::QParameter::valueChanged, - this, &QTexturedMetalRoughMaterialPrivate::handleNormalChanged); - - m_metalRoughGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/metalrough.vert")))); - m_metalRoughGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/metalrough.frag")))); - - m_metalRoughGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); - m_metalRoughGL3Technique->graphicsApiFilter()->setMajorVersion(3); - m_metalRoughGL3Technique->graphicsApiFilter()->setMinorVersion(1); - m_metalRoughGL3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile); - - Q_Q(QTexturedMetalRoughMaterial); - m_filterKey->setParent(q); - m_filterKey->setName(QStringLiteral("renderingStyle")); - m_filterKey->setValue(QStringLiteral("forward")); - - m_metalRoughGL3Technique->addFilterKey(m_filterKey); - m_metalRoughGL3RenderPass->setShaderProgram(m_metalRoughGL3Shader); - m_metalRoughGL3Technique->addRenderPass(m_metalRoughGL3RenderPass); - m_metalRoughEffect->addTechnique(m_metalRoughGL3Technique); - - m_metalRoughEffect->addParameter(m_baseColorParameter); - m_metalRoughEffect->addParameter(m_metalnessParameter); - m_metalRoughEffect->addParameter(m_roughnessParameter); - m_metalRoughEffect->addParameter(m_ambientOcclusionParameter); - m_metalRoughEffect->addParameter(m_normalParameter); - - // Note that even though those parameters are not exposed in the API, - // they need to be kept around for now due to a bug in some drivers/GPUs - // (at least Intel) which cause issues with unbound textures even if you - // don't try to sample from them. - // Can probably go away once we generate the shaders and deal in this - // case in a better way. - m_metalRoughEffect->addParameter(m_environmentIrradianceParameter); - m_metalRoughEffect->addParameter(m_environmentSpecularParameter); - - q->setEffect(m_metalRoughEffect); -} - -void QTexturedMetalRoughMaterialPrivate::handleBaseColorChanged(const QVariant &var) -{ - Q_Q(QTexturedMetalRoughMaterial); - emit q->baseColorChanged(var.value<QAbstractTexture *>()); -} - -void QTexturedMetalRoughMaterialPrivate::handleMetallicChanged(const QVariant &var) -{ - Q_Q(QTexturedMetalRoughMaterial); - emit q->metalnessChanged(var.value<QAbstractTexture *>()); -} -void QTexturedMetalRoughMaterialPrivate::handleRoughnessChanged(const QVariant &var) -{ - Q_Q(QTexturedMetalRoughMaterial); - emit q->roughnessChanged(var.value<QAbstractTexture *>()); -} -void QTexturedMetalRoughMaterialPrivate::handleAmbientOcclusionChanged(const QVariant &var) -{ - Q_Q(QTexturedMetalRoughMaterial); - emit q->ambientOcclusionChanged(var.value<QAbstractTexture *>()); -} - -void QTexturedMetalRoughMaterialPrivate::handleNormalChanged(const QVariant &var) -{ - Q_Q(QTexturedMetalRoughMaterial); - emit q->normalChanged(var.value<QAbstractTexture *>()); -} - /*! \class Qt3DExtras::QTexturedMetalRoughMaterial - \brief The QTexturedMetalRoughMaterial provides a default implementation of PBR - lighting, environment maps and bump effect where the components are read from texture - maps (including normal maps). + \brief This is deprecated, please use QMetalRoughMaterial instead. \inmodule Qt3DExtras \since 5.9 - \inherits Qt3DRender::QMaterial + \inherits Qt3DExtras::QMetalRoughMaterial - This material uses an effect with a single render pass approach and performs per fragment - lighting. Techniques are provided for OpenGL 3 only. + \deprecated + \see Qt3DExtras::QMetalRoughMaterial */ /*! Constructs a new QTexturedMetalRoughMaterial instance with parent object \a parent. */ QTexturedMetalRoughMaterial::QTexturedMetalRoughMaterial(QNode *parent) - : QMaterial(*new QTexturedMetalRoughMaterialPrivate, parent) -{ - Q_D(QTexturedMetalRoughMaterial); - d->init(); -} - -/*! \internal */ -QTexturedMetalRoughMaterial::QTexturedMetalRoughMaterial(QTexturedMetalRoughMaterialPrivate &dd, QNode *parent) - : QMaterial(dd, parent) -{ - Q_D(QTexturedMetalRoughMaterial); - d->init(); -} - -/*! - Destroys the QTexturedMetalRoughMaterial instance. -*/ -QTexturedMetalRoughMaterial::~QTexturedMetalRoughMaterial() -{ -} - -/*! - \property QTexturedMetalRoughMaterial::baseColor - - Holds the current base color map texture. - - By default, the base color texture has the following properties: - - \list - \li Linear minification and magnification filters - \li Linear mipmap with mipmapping enabled - \li Repeat wrap mode - \li Maximum anisotropy of 16.0 - \endlist -*/ -QAbstractTexture *QTexturedMetalRoughMaterial::baseColor() const -{ - Q_D(const QTexturedMetalRoughMaterial); - return d->m_baseColorParameter->value().value<QAbstractTexture *>(); -} - -/*! - \property QTexturedMetalRoughMaterial::metalness - - Holds the current metalness map texture. - - By default, the metalness texture has the following properties: - - \list - \li Linear minification and magnification filters - \li Linear mipmap with mipmapping enabled - \li Repeat wrap mode - \li Maximum anisotropy of 16.0 - \endlist -*/ -QAbstractTexture *QTexturedMetalRoughMaterial::metalness() const -{ - Q_D(const QTexturedMetalRoughMaterial); - return d->m_metalnessParameter->value().value<QAbstractTexture *>(); -} - -/*! - \property QTexturedMetalRoughMaterial::roughness - - Holds the current roughness map texture. - - By default, the roughness texture has the following properties: - - \list - \li Linear minification and magnification filters - \li Linear mipmap with mipmapping enabled - \li Repeat wrap mode - \li Maximum anisotropy of 16.0 - \endlist -*/ -QAbstractTexture *QTexturedMetalRoughMaterial::roughness() const -{ - Q_D(const QTexturedMetalRoughMaterial); - return d->m_roughnessParameter->value().value<QAbstractTexture *>(); -} - -/*! - \property QTexturedMetalRoughMaterial::ambientOcclusion - - Holds the current ambient occlusion map texture. - - By default, the ambient occlusion texture has the following properties: - - \list - \li Linear minification and magnification filters - \li Linear mipmap with mipmapping enabled - \li Repeat wrap mode - \li Maximum anisotropy of 16.0 - \endlist -*/ -QAbstractTexture *QTexturedMetalRoughMaterial::ambientOcclusion() const -{ - Q_D(const QTexturedMetalRoughMaterial); - return d->m_ambientOcclusionParameter->value().value<QAbstractTexture *>(); -} - -/*! - \property QTexturedMetalRoughMaterial::normal - - Holds the current normal map texture. - - By default, the normal texture has the following properties: - - \list - \li Linear minification and magnification filters - \li Repeat wrap mode - \li Maximum anisotropy of 16.0 - \endlist -*/ -QAbstractTexture *QTexturedMetalRoughMaterial::normal() const -{ - Q_D(const QTexturedMetalRoughMaterial); - return d->m_normalParameter->value().value<QAbstractTexture *>(); -} - - -void QTexturedMetalRoughMaterial::setBaseColor(QAbstractTexture *baseColor) -{ - Q_D(QTexturedMetalRoughMaterial); - d->m_baseColorParameter->setValue(QVariant::fromValue(baseColor)); -} - -void QTexturedMetalRoughMaterial::setMetalness(QAbstractTexture *metalness) -{ - Q_D(QTexturedMetalRoughMaterial); - d->m_metalnessParameter->setValue(QVariant::fromValue(metalness)); -} - -void QTexturedMetalRoughMaterial::setRoughness(QAbstractTexture *roughness) -{ - Q_D(QTexturedMetalRoughMaterial); - d->m_roughnessParameter->setValue(QVariant::fromValue(roughness)); -} - -void QTexturedMetalRoughMaterial::setAmbientOcclusion(QAbstractTexture *ambientOcclusion) -{ - Q_D(QTexturedMetalRoughMaterial); - d->m_ambientOcclusionParameter->setValue(QVariant::fromValue(ambientOcclusion)); -} - -void QTexturedMetalRoughMaterial::setNormal(QAbstractTexture *normal) + : QMetalRoughMaterial(parent) { - Q_D(QTexturedMetalRoughMaterial); - d->m_normalParameter->setValue(QVariant::fromValue(normal)); } } // namespace Qt3DExtras diff --git a/src/extras/defaults/qtexturedmetalroughmaterial.h b/src/extras/defaults/qtexturedmetalroughmaterial.h index 62755a781..60f41b27f 100644 --- a/src/extras/defaults/qtexturedmetalroughmaterial.h +++ b/src/extras/defaults/qtexturedmetalroughmaterial.h @@ -40,53 +40,24 @@ #ifndef QT3DEXTRAS_QTEXTUREDMETALROUGHMATERIAL_H #define QT3DEXTRAS_QTEXTUREDMETALROUGHMATERIAL_H -#include <Qt3DExtras/qt3dextras_global.h> -#include <Qt3DRender/qmaterial.h> +#include <Qt3DExtras/qmetalroughmaterial.h> QT_BEGIN_NAMESPACE namespace Qt3DExtras { -class QTexturedMetalRoughMaterialPrivate; - -class QT3DEXTRASSHARED_EXPORT QTexturedMetalRoughMaterial : public Qt3DRender::QMaterial +class QT_DEPRECATED_X("Use Qt3DExtras::QMetalRoughMaterial") QT3DEXTRASSHARED_EXPORT QTexturedMetalRoughMaterial : public QMetalRoughMaterial { Q_OBJECT - Q_PROPERTY(Qt3DRender::QAbstractTexture *baseColor READ baseColor WRITE setBaseColor NOTIFY baseColorChanged) - Q_PROPERTY(Qt3DRender::QAbstractTexture *metalness READ metalness WRITE setMetalness NOTIFY metalnessChanged) - Q_PROPERTY(Qt3DRender::QAbstractTexture *roughness READ roughness WRITE setRoughness NOTIFY roughnessChanged) - Q_PROPERTY(Qt3DRender::QAbstractTexture *ambientOcclusion READ ambientOcclusion WRITE setAmbientOcclusion NOTIFY ambientOcclusionChanged) - Q_PROPERTY(Qt3DRender::QAbstractTexture *normal READ normal WRITE setNormal NOTIFY normalChanged) + Q_PROPERTY(QVariant ambientOcclusion READ ambientOcclusion WRITE setAmbientOcclusion NOTIFY ambientOcclusionChanged) + Q_PROPERTY(QVariant normal READ normal WRITE setNormal NOTIFY normalChanged) public: explicit QTexturedMetalRoughMaterial(Qt3DCore::QNode *parent = nullptr); - ~QTexturedMetalRoughMaterial(); - - Qt3DRender::QAbstractTexture *baseColor() const; - Qt3DRender::QAbstractTexture *metalness() const; - Qt3DRender::QAbstractTexture *roughness() const; - Qt3DRender::QAbstractTexture *ambientOcclusion() const; - Qt3DRender::QAbstractTexture *normal() const; - -public Q_SLOTS: - void setBaseColor(Qt3DRender::QAbstractTexture *baseColor); - void setMetalness(Qt3DRender::QAbstractTexture *metalness); - void setRoughness(Qt3DRender::QAbstractTexture *roughness); - void setAmbientOcclusion(Qt3DRender::QAbstractTexture *ambientOcclusion); - void setNormal(Qt3DRender::QAbstractTexture *normal); Q_SIGNALS: - void baseColorChanged(Qt3DRender::QAbstractTexture *baseColor); - void metalnessChanged(Qt3DRender::QAbstractTexture *metalness); - void roughnessChanged(Qt3DRender::QAbstractTexture *roughness); - void ambientOcclusionChanged(Qt3DRender::QAbstractTexture *ambientOcclusion); - void normalChanged(Qt3DRender::QAbstractTexture *normal); - -protected: - explicit QTexturedMetalRoughMaterial(QTexturedMetalRoughMaterialPrivate &dd, Qt3DCore::QNode *parent = nullptr); - -private: - Q_DECLARE_PRIVATE(QTexturedMetalRoughMaterial) + void ambientOcclusionChanged(const QVariant &ambientOcclusion); + void normalChanged(const QVariant &normal); }; } // namespace Qt3DExtras diff --git a/src/extras/defaults/qtexturematerial.cpp b/src/extras/defaults/qtexturematerial.cpp index 0038dd44a..32e60360e 100644 --- a/src/extras/defaults/qtexturematerial.cpp +++ b/src/extras/defaults/qtexturematerial.cpp @@ -60,7 +60,7 @@ QTextureMaterialPrivate::QTextureMaterialPrivate() : QMaterialPrivate() , m_textureEffect(new QEffect) , m_textureParameter(new QParameter(QStringLiteral("diffuseTexture"), new QTexture2D)) - , m_textureOffsetParameter(new QParameter(QStringLiteral("texCoordOffset"), QVector2D(0.0f, 0.0f))) + , m_textureTransformParameter(new QParameter(QStringLiteral("texCoordTransform"), qVariantFromValue(QMatrix3x3()))) , m_textureGL3Technique(new QTechnique) , m_textureGL2Technique(new QTechnique) , m_textureES2Technique(new QTechnique) @@ -77,8 +77,8 @@ void QTextureMaterialPrivate::init() { connect(m_textureParameter, &Qt3DRender::QParameter::valueChanged, this, &QTextureMaterialPrivate::handleTextureChanged); - connect(m_textureOffsetParameter, &Qt3DRender::QParameter::valueChanged, - this, &QTextureMaterialPrivate::handleTextureOffsetChanged); + connect(m_textureTransformParameter, &Qt3DRender::QParameter::valueChanged, + this, &QTextureMaterialPrivate::handleTextureTransformChanged); m_textureGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/unlittexture.vert")))); m_textureGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/gl3/unlittexture.frag")))); @@ -122,7 +122,7 @@ void QTextureMaterialPrivate::init() m_textureEffect->addTechnique(m_textureES2Technique); m_textureEffect->addParameter(m_textureParameter); - m_textureEffect->addParameter(m_textureOffsetParameter); + m_textureEffect->addParameter(m_textureTransformParameter); q->setEffect(m_textureEffect); } @@ -133,10 +133,12 @@ void QTextureMaterialPrivate::handleTextureChanged(const QVariant &var) emit q->textureChanged(var.value<QAbstractTexture *>()); } -void QTextureMaterialPrivate::handleTextureOffsetChanged(const QVariant &var) +void QTextureMaterialPrivate::handleTextureTransformChanged(const QVariant &var) { Q_Q(QTextureMaterial); - emit q->textureOffsetChanged(var.value<QVector2D>()); + const QMatrix3x3 matrix = var.value<QMatrix3x3>(); + emit q->textureTransformChanged(matrix); + emit q->textureOffsetChanged(QVector2D(matrix(0, 2), matrix(1, 2))); } /*! @@ -182,14 +184,29 @@ QAbstractTexture *QTextureMaterial::texture() const /*! \property QTextureMaterial::textureOffset - Holds the current texture offset. It is applied to texture - coordinates at render time. Defaults to (0.0, 0.0). + This is a utility property. It sets the translation component of the general + texture transform matrix */ QVector2D QTextureMaterial::textureOffset() const { Q_D(const QTextureMaterial); - return d->m_textureOffsetParameter->value().value<QVector2D>(); + const QMatrix3x3 matrix = d->m_textureTransformParameter->value().value<QMatrix3x3>(); + return QVector2D(matrix(0, 2), matrix(1, 2)); +} + + +/*! + \property QTextureMaterial::textureTransform + + Holds the current texture transform. It is applied to texture + coordinates at render time. Defaults to identity matrix. + +*/ +QMatrix3x3 QTextureMaterial::textureTransform() const +{ + Q_D(const QTextureMaterial); + return d->m_textureTransformParameter->value().value<QMatrix3x3>(); } void QTextureMaterial::setTexture(QAbstractTexture *texture) @@ -201,7 +218,16 @@ void QTextureMaterial::setTexture(QAbstractTexture *texture) void QTextureMaterial::setTextureOffset(QVector2D textureOffset) { Q_D(QTextureMaterial); - d->m_textureOffsetParameter->setValue(QVariant::fromValue(textureOffset)); + QMatrix3x3 matrix = d->m_textureTransformParameter->value().value<QMatrix3x3>(); + matrix(0, 2) = textureOffset.x(); + matrix(1, 2) = textureOffset.y(); + d->m_textureTransformParameter->setValue(qVariantFromValue(matrix)); +} + +void QTextureMaterial::setTextureTransform(const QMatrix3x3 &matrix) +{ + Q_D(QTextureMaterial); + d->m_textureTransformParameter->setValue(qVariantFromValue(matrix)); } } // namespace Qt3DExtras diff --git a/src/extras/defaults/qtexturematerial.h b/src/extras/defaults/qtexturematerial.h index 3101b9a1b..b6347aff4 100644 --- a/src/extras/defaults/qtexturematerial.h +++ b/src/extras/defaults/qtexturematerial.h @@ -43,6 +43,7 @@ #include <Qt3DExtras/qt3dextras_global.h> #include <Qt3DRender/qmaterial.h> #include <QVector2D> +#include <QMatrix3x3> QT_BEGIN_NAMESPACE @@ -61,20 +62,24 @@ class QT3DEXTRASSHARED_EXPORT QTextureMaterial : public Qt3DRender::QMaterial Q_OBJECT Q_PROPERTY(Qt3DRender::QAbstractTexture *texture READ texture WRITE setTexture NOTIFY textureChanged) Q_PROPERTY(QVector2D textureOffset READ textureOffset WRITE setTextureOffset NOTIFY textureOffsetChanged) + Q_PROPERTY(QMatrix3x3 textureTransform READ textureTransform WRITE setTextureTransform NOTIFY textureTransformChanged REVISION 10) public: explicit QTextureMaterial(Qt3DCore::QNode *parent = nullptr); ~QTextureMaterial(); Qt3DRender::QAbstractTexture *texture() const; QVector2D textureOffset() const; + QMatrix3x3 textureTransform() const; public Q_SLOTS: void setTexture(Qt3DRender::QAbstractTexture *texture); void setTextureOffset(QVector2D textureOffset); + void setTextureTransform(const QMatrix3x3 &matrix); Q_SIGNALS: void textureChanged(Qt3DRender::QAbstractTexture *texture); void textureOffsetChanged(QVector2D textureOffset); + void textureTransformChanged(const QMatrix3x3 &textureTransform); private: Q_DECLARE_PRIVATE(QTextureMaterial) diff --git a/src/extras/defaults/qtexturematerial_p.h b/src/extras/defaults/qtexturematerial_p.h index 5a8e78154..d09458aa3 100644 --- a/src/extras/defaults/qtexturematerial_p.h +++ b/src/extras/defaults/qtexturematerial_p.h @@ -40,6 +40,8 @@ #ifndef QT3DEXTRAS_QTEXTUREMATERIAL_P_H #define QT3DEXTRAS_QTEXTUREMATERIAL_P_H +#include <QMatrix3x3> + // // W A R N I N G // ------------- @@ -78,11 +80,11 @@ class QTextureMaterialPrivate : public Qt3DRender::QMaterialPrivate void init(); void handleTextureChanged(const QVariant &var); - void handleTextureOffsetChanged(const QVariant &var); + void handleTextureTransformChanged(const QVariant &var); Qt3DRender::QEffect *m_textureEffect; Qt3DRender::QParameter *m_textureParameter; - Qt3DRender::QParameter *m_textureOffsetParameter; + Qt3DRender::QParameter *m_textureTransformParameter; Qt3DRender::QTechnique *m_textureGL3Technique; Qt3DRender::QTechnique *m_textureGL2Technique; Qt3DRender::QTechnique *m_textureES2Technique; |