| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #ifndef QT3DRENDER_RENDER_SKELETON_H |
| 38 | #define QT3DRENDER_RENDER_SKELETON_H |
| 39 | |
| 40 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists for the convenience |
| 45 | // of other Qt classes. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | // |
| 50 | |
| 51 | #include <Qt3DRender/private/backendnode_p.h> |
| 52 | #include <Qt3DRender/private/skeletondata_p.h> |
| 53 | #include <Qt3DRender/private/handle_types_p.h> |
| 54 | #include <Qt3DCore/qskeletonloader.h> |
| 55 | |
| 56 | #include <QtGui/qmatrix4x4.h> |
| 57 | #include <QDebug> |
| 58 | |
| 59 | #if defined(QT_BUILD_INTERNAL) |
| 60 | class tst_Skeleton; |
| 61 | #endif |
| 62 | |
| 63 | QT_BEGIN_NAMESPACE |
| 64 | |
| 65 | namespace Qt3DCore { |
| 66 | class QJoint; |
| 67 | } |
| 68 | |
| 69 | namespace Qt3DRender { |
| 70 | namespace Render { |
| 71 | |
| 72 | class JointManager; |
| 73 | class SkeletonManager; |
| 74 | |
| 75 | class Q_3DRENDERSHARED_PRIVATE_EXPORT Skeleton : public BackendNode |
| 76 | { |
| 77 | public: |
| 78 | enum SkeletonDataType { |
| 79 | Unknown, |
| 80 | File, |
| 81 | Data |
| 82 | }; |
| 83 | |
| 84 | Skeleton(); |
| 85 | |
| 86 | void setSkeletonManager(SkeletonManager *skeletonManager) { m_skeletonManager = skeletonManager; } |
| 87 | SkeletonManager *skeletonManager() const { return m_skeletonManager; } |
| 88 | |
| 89 | void setJointManager(JointManager *jointManager) { m_jointManager = jointManager; } |
| 90 | JointManager *jointManager() const { return m_jointManager; } |
| 91 | |
| 92 | void cleanup(); |
| 93 | void syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) override; |
| 94 | void setStatus(Qt3DCore::QSkeletonLoader::Status status); |
| 95 | Qt3DCore::QSkeletonLoader::Status status() const { return m_status; } |
| 96 | |
| 97 | QUrl source() const { return m_source; } |
| 98 | SkeletonDataType dataType() const { return m_dataType; } |
| 99 | bool createJoints() const { return m_createJoints; } |
| 100 | |
| 101 | void setName(const QString &name) { m_name = name; } |
| 102 | QString name() const { return m_name; } |
| 103 | |
| 104 | int jointCount() const { return m_skeletonData.joints.size(); } |
| 105 | QVector<JointInfo> joints() const { return m_skeletonData.joints; } |
| 106 | QVector<QString> jointNames() const { return m_skeletonData.jointNames; } |
| 107 | QVector<Qt3DCore::Sqt> localPoses() const { return m_skeletonData.localPoses; } |
| 108 | |
| 109 | Qt3DCore::QNodeId rootJointId() const { return m_rootJointId; } |
| 110 | |
| 111 | // Called from jobs |
| 112 | void setLocalPose(HJoint jointHandle, const Qt3DCore::Sqt &localPose); |
| 113 | QVector<QMatrix4x4> calculateSkinningMatrixPalette(); |
| 114 | |
| 115 | void clearData(); |
| 116 | void setSkeletonData(const SkeletonData &data); |
| 117 | const SkeletonData &skeletonData() const { return m_skeletonData; } |
| 118 | SkeletonData skeletonData() { return m_skeletonData; } |
| 119 | |
| 120 | // Allow unit tests to set the data type |
| 121 | #if defined(QT_BUILD_INTERNAL) |
| 122 | public: |
| 123 | void setDataType(SkeletonDataType dataType) { m_dataType = dataType; } |
| 124 | #endif |
| 125 | |
| 126 | private: |
| 127 | QVector<QMatrix4x4> m_skinningPalette; |
| 128 | |
| 129 | // QSkeletonLoader Properties |
| 130 | QUrl m_source; |
| 131 | Qt3DCore::QSkeletonLoader::Status m_status; |
| 132 | bool m_createJoints; |
| 133 | |
| 134 | // QSkeleton properties |
| 135 | Qt3DCore::QNodeId m_rootJointId; |
| 136 | |
| 137 | SkeletonDataType m_dataType; |
| 138 | |
| 139 | QString m_name; |
| 140 | SkeletonData m_skeletonData; |
| 141 | SkeletonManager *m_skeletonManager; |
| 142 | JointManager *m_jointManager; |
| 143 | HSkeleton m_skeletonHandle; // Our own handle to set on joints |
| 144 | |
| 145 | #if defined(QT_BUILD_INTERNAL) |
| 146 | friend class ::tst_Skeleton; |
| 147 | #endif |
| 148 | }; |
| 149 | |
| 150 | #ifndef QT_NO_DEBUG_STREAM |
| 151 | inline QDebug operator<<(QDebug dbg, const Skeleton &skeleton) |
| 152 | { |
| 153 | QDebugStateSaver saver(dbg); |
| 154 | dbg << "QNodeId =" << skeleton.peerId() << Qt::endl |
| 155 | << "Name =" << skeleton.name() << Qt::endl; |
| 156 | return dbg; |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | class SkeletonFunctor : public Qt3DCore::QBackendNodeMapper |
| 161 | { |
| 162 | public: |
| 163 | explicit SkeletonFunctor(AbstractRenderer *renderer, |
| 164 | SkeletonManager *skeletonManager, |
| 165 | JointManager *jointManager); |
| 166 | Qt3DCore::QBackendNode *create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const final; |
| 167 | Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const final; |
| 168 | void destroy(Qt3DCore::QNodeId id) const final; |
| 169 | |
| 170 | private: |
| 171 | AbstractRenderer *m_renderer; |
| 172 | SkeletonManager *m_skeletonManager; |
| 173 | JointManager *m_jointManager; |
| 174 | }; |
| 175 | |
| 176 | } // namespace Render |
| 177 | } // namespace Qt3DRender |
| 178 | |
| 179 | |
| 180 | QT_END_NAMESPACE |
| 181 | |
| 182 | #endif // QT3DRENDER_RENDER_SKELETON_H |
| 183 | |