1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "joint_p.h"
41#include <Qt3DRender/private/managers_p.h>
42#include <Qt3DCore/QJoint>
43#include <Qt3DCore/private/qjoint_p.h>
44
45#include <algorithm>
46
47QT_BEGIN_NAMESPACE
48
49using namespace Qt3DCore;
50
51namespace Qt3DRender {
52namespace Render {
53
54Joint::Joint()
55 : BackendNode(Qt3DCore::QBackendNode::ReadOnly)
56 , m_localPose()
57 , m_jointManager(nullptr)
58 , m_skeletonManager(nullptr)
59{
60}
61
62void Joint::cleanup()
63{
64 m_inverseBindMatrix.setToIdentity();
65 m_localPose = Sqt();
66 m_childJointIds.clear();
67 m_name.clear();
68 m_owningSkeleton = HSkeleton();
69 setEnabled(false);
70}
71
72void Joint::syncFromFrontEnd(const QNode *frontEnd, bool firstTime)
73{
74 const Qt3DCore::QJoint *joint = qobject_cast<const Qt3DCore::QJoint *>(object: frontEnd);
75 if (!joint)
76 return;
77
78 bool jointDirty = firstTime;
79 if (m_localPose.scale != joint->scale()) {
80 m_localPose.scale = joint->scale();
81 jointDirty = true;
82 }
83 if (m_localPose.rotation != joint->rotation()) {
84 m_localPose.rotation = joint->rotation();
85 jointDirty = true;
86 }
87 if (m_localPose.translation != joint->translation()) {
88 m_localPose.translation = joint->translation();
89 jointDirty = true;
90 }
91 if (m_inverseBindMatrix != joint->inverseBindMatrix()) {
92 // Setting the inverse bind matrix should be a rare operation. Usually it is
93 // set once and then remains constant for the duration of the skeleton. So just
94 // trigger a rebuild of the skeleton's SkeletonData which will include obtaining
95 // the inverse bind matrix.
96 m_inverseBindMatrix = joint->inverseBindMatrix();
97 m_skeletonManager->addDirtySkeleton(dirtyFlag: SkeletonManager::SkeletonDataDirty, skeletonHandle: m_owningSkeleton);
98 }
99 if (m_name != joint->name()) {
100 // Joint name doesn't affect anything in the render aspect so no need
101 // to mark anything as dirty.
102 m_name = joint->name();
103
104 // TODO: Notify other aspects (animation) about the name change.
105 }
106
107 Qt3DCore::QNodeIdVector childIds = qIdsForNodes(nodes: joint->childJoints());
108 std::sort(first: std::begin(cont&: childIds), last: std::end(cont&: childIds));
109 if (m_childJointIds != childIds)
110 m_childJointIds = childIds;
111
112 if (jointDirty) {
113 markDirty(changes: AbstractRenderer::JointDirty);
114 m_jointManager->addDirtyJoint(jointId: peerId());
115 }
116
117 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
118}
119
120
121JointFunctor::JointFunctor(AbstractRenderer *renderer,
122 JointManager *jointManager,
123 SkeletonManager *skeletonManager)
124 : m_renderer(renderer)
125 , m_jointManager(jointManager)
126 , m_skeletonManager(skeletonManager)
127{
128}
129
130Qt3DCore::QBackendNode *JointFunctor::create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const
131{
132 Joint *backend = m_jointManager->getOrCreateResource(id: change->subjectId());
133 backend->setRenderer(m_renderer);
134 backend->setJointManager(m_jointManager);
135 backend->setSkeletonManager(m_skeletonManager);
136 return backend;
137}
138
139Qt3DCore::QBackendNode *JointFunctor::get(Qt3DCore::QNodeId id) const
140{
141 return m_jointManager->lookupResource(id);
142}
143
144void JointFunctor::destroy(Qt3DCore::QNodeId id) const
145{
146 m_jointManager->removeDirtyJoint(jointId: id);
147 m_jointManager->releaseResource(id);
148}
149
150} // namespace Render
151} // namespace Qt3DRender
152
153QT_END_NAMESPACE
154

source code of qt3d/src/render/geometry/joint.cpp