1// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QT3DRENDER_RENDER_SKELETON_H
5#define QT3DRENDER_RENDER_SKELETON_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <Qt3DRender/private/backendnode_p.h>
19#include <Qt3DRender/private/skeletondata_p.h>
20#include <Qt3DRender/private/handle_types_p.h>
21#include <Qt3DCore/qskeletonloader.h>
22
23#include <QtGui/qmatrix4x4.h>
24#include <QDebug>
25
26#if defined(QT_BUILD_INTERNAL)
27class tst_Skeleton;
28#endif
29
30QT_BEGIN_NAMESPACE
31
32namespace Qt3DCore {
33class QJoint;
34}
35
36namespace Qt3DRender {
37namespace Render {
38
39class JointManager;
40class SkeletonManager;
41
42class Q_3DRENDERSHARED_PRIVATE_EXPORT Skeleton : public BackendNode
43{
44public:
45 enum SkeletonDataType {
46 Unknown,
47 File,
48 Data
49 };
50
51 Skeleton();
52
53 void setSkeletonManager(SkeletonManager *skeletonManager) { m_skeletonManager = skeletonManager; }
54 SkeletonManager *skeletonManager() const { return m_skeletonManager; }
55
56 void setJointManager(JointManager *jointManager) { m_jointManager = jointManager; }
57 JointManager *jointManager() const { return m_jointManager; }
58
59 void cleanup();
60 void syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) override;
61 void setStatus(Qt3DCore::QSkeletonLoader::Status status);
62 Qt3DCore::QSkeletonLoader::Status status() const { return m_status; }
63
64 QUrl source() const { return m_source; }
65 SkeletonDataType dataType() const { return m_dataType; }
66 bool createJoints() const { return m_createJoints; }
67
68 void setName(const QString &name) { m_name = name; }
69 QString name() const { return m_name; }
70
71 qsizetype jointCount() const { return m_skeletonData.joints.size(); }
72 QVector<JointInfo> joints() const { return m_skeletonData.joints; }
73 QVector<QString> jointNames() const { return m_skeletonData.jointNames; }
74 QVector<Qt3DCore::Sqt> localPoses() const { return m_skeletonData.localPoses; }
75
76 Qt3DCore::QNodeId rootJointId() const { return m_rootJointId; }
77
78 // Called from jobs
79 void setLocalPose(HJoint jointHandle, const Qt3DCore::Sqt &localPose);
80 QVector<QMatrix4x4> calculateSkinningMatrixPalette();
81
82 void clearData();
83 void setSkeletonData(const SkeletonData &data);
84 const SkeletonData &skeletonData() const { return m_skeletonData; }
85 SkeletonData skeletonData() { return m_skeletonData; }
86
87 // Allow unit tests to set the data type
88#if defined(QT_BUILD_INTERNAL)
89public:
90 void setDataType(SkeletonDataType dataType) { m_dataType = dataType; }
91#endif
92
93private:
94 QVector<QMatrix4x4> m_skinningPalette;
95
96 // QSkeletonLoader Properties
97 QUrl m_source;
98 Qt3DCore::QSkeletonLoader::Status m_status;
99 bool m_createJoints;
100
101 // QSkeleton properties
102 Qt3DCore::QNodeId m_rootJointId;
103
104 SkeletonDataType m_dataType;
105
106 QString m_name;
107 SkeletonData m_skeletonData;
108 SkeletonManager *m_skeletonManager;
109 JointManager *m_jointManager;
110 HSkeleton m_skeletonHandle; // Our own handle to set on joints
111
112#if defined(QT_BUILD_INTERNAL)
113 friend class ::tst_Skeleton;
114#endif
115};
116
117#ifndef QT_NO_DEBUG_STREAM
118inline QDebug operator<<(QDebug dbg, const Skeleton &skeleton)
119{
120 QDebugStateSaver saver(dbg);
121 dbg << "QNodeId =" << skeleton.peerId() << Qt::endl
122 << "Name =" << skeleton.name() << Qt::endl;
123 return dbg;
124}
125#endif
126
127class SkeletonFunctor : public Qt3DCore::QBackendNodeMapper
128{
129public:
130 explicit SkeletonFunctor(AbstractRenderer *renderer,
131 SkeletonManager *skeletonManager,
132 JointManager *jointManager);
133 Qt3DCore::QBackendNode *create(Qt3DCore::QNodeId id) const final;
134 Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const final;
135 void destroy(Qt3DCore::QNodeId id) const final;
136
137private:
138 AbstractRenderer *m_renderer;
139 SkeletonManager *m_skeletonManager;
140 JointManager *m_jointManager;
141};
142
143} // namespace Render
144} // namespace Qt3DRender
145
146
147QT_END_NAMESPACE
148
149#endif // QT3DRENDER_RENDER_SKELETON_H
150

source code of qt3d/src/render/geometry/skeleton_p.h