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 "qjoint.h"
41#include "qjoint_p.h"
42
43#include <Qt3DCore/qnodecreatedchange.h>
44
45QT_BEGIN_NAMESPACE
46
47namespace Qt3DCore {
48
49QJointPrivate::QJointPrivate()
50 : QNodePrivate()
51 , m_inverseBindMatrix()
52 , m_rotation()
53 , m_translation()
54 , m_scale(1.0f, 1.0f, 1.0f)
55{
56}
57
58/*!
59 \qmltype Joint
60 \inqmlmodule Qt3D.Core
61 \inherits Node
62 \instantiates Qt3DCore::QJoint
63 \since 5.10
64 \brief Used to transforms parts of skinned meshes.
65
66 The Joint node is used to build skeletons as part of the skinned mesh
67 support in Qt 3D. A joint can be transformed by way of its scale, rotation
68 and translation properties. Any mesh vertices that are bound to the joint
69 will have their transformations updated accordingly.
70*/
71
72/*!
73 \qmlproperty vector3d Joint::scale
74
75 Holds the uniform scale of the joint.
76*/
77
78/*!
79 \qmlproperty quaternion Joint::rotation
80
81 Holds the rotation of the joint as quaternion.
82*/
83
84/*!
85 \qmlproperty vector3d Joint::translation
86
87 Holds the translation of the joint as vector3d.
88*/
89
90/*!
91 \qmlproperty real Joint::rotationX
92
93 Holds the x rotation of the joint as an Euler angle.
94*/
95
96/*!
97 \qmlproperty real Joint::rotationY
98
99 Holds the y rotation of the joint as an Euler angle.
100*/
101
102/*!
103 \qmlproperty real Joint::rotationZ
104
105 Holds the z rotation of the joint as an Euler angle.
106*/
107
108/*!
109 \qmlproperty matrix4x4 Joint::inverseBindMatrix
110
111 Holds the inverse bind matrix of the joint. This is used to transform
112 vertices from model space into the space of this joint so they can
113 subsequently be multiplied by the joint's global transform to perform
114 the skinning operation.
115*/
116
117/*!
118 \class Qt3DCore::QJoint
119 \inmodule Qt3DCore
120 \inherits Qt3DCore::QNode
121 \since 5.10
122 \brief Used to transforms parts of skinned meshes.
123
124 The QJoint node is used to build skeletons as part of the skinned mesh
125 support in Qt 3D. A joint can be transformed by way of its scale, rotation
126 and translation properties. Any mesh vertices that are bound to the joint
127 will have their transformations updated accordingly.
128*/
129
130/*!
131 Constructs a new QJoint with \a parent.
132*/
133QJoint::QJoint(Qt3DCore::QNode *parent)
134 : QNode(*new QJointPrivate, parent)
135{
136}
137
138/*! \internal */
139QJoint::~QJoint()
140{
141}
142
143/*!
144 \property Qt3DCore::QJoint::scale
145
146 Holds the scale of the joint.
147*/
148QVector3D QJoint::scale() const
149{
150 Q_D(const QJoint);
151 return d->m_scale;
152}
153
154/*!
155 \property Qt3DCore::QJoint::rotation
156
157 Holds the rotation of the joint as QQuaternion.
158*/
159QQuaternion QJoint::rotation() const
160{
161 Q_D(const QJoint);
162 return d->m_rotation;
163}
164
165/*!
166 \property Qt3DCore::QJoint::translation
167
168 Holds the translation of the joint as QVector3D.
169*/
170QVector3D QJoint::translation() const
171{
172 Q_D(const QJoint);
173 return d->m_translation;
174}
175
176/*!
177 \property Qt3DCore::QJoint::inverseBindMatrix
178
179 Holds the inverse bind matrix of the joint. This is used to transform
180 vertices from model space into the space of this joint so they can
181 subsequently be multiplied by the joint's global transform to perform
182 the skinning operation.
183*/
184QMatrix4x4 QJoint::inverseBindMatrix() const
185{
186 Q_D(const QJoint);
187 return d->m_inverseBindMatrix;
188}
189
190/*!
191 \property Qt3DCore::QJoint::rotationX
192
193 Holds the x rotation of the joint as an Euler angle.
194*/
195float QJoint::rotationX() const
196{
197 Q_D(const QJoint);
198 return d->m_eulerRotationAngles.x();
199}
200
201/*!
202 \property Qt3DCore::QJoint::rotationY
203
204 Holds the y rotation of the joint as an Euler angle.
205*/
206float QJoint::rotationY() const
207{
208 Q_D(const QJoint);
209 return d->m_eulerRotationAngles.y();
210}
211
212/*!
213 \property Qt3DCore::QJoint::rotationZ
214
215 Holds the z rotation of the joint as an Euler angle.
216*/
217float QJoint::rotationZ() const
218{
219 Q_D(const QJoint);
220 return d->m_eulerRotationAngles.z();
221}
222
223void QJoint::setScale(const QVector3D &scale)
224{
225 Q_D(QJoint);
226 if (scale == d->m_scale)
227 return;
228
229 d->m_scale = scale;
230 emit scaleChanged(scale);
231}
232
233void QJoint::setRotation(const QQuaternion &rotation)
234{
235 Q_D(QJoint);
236 if (rotation == d->m_rotation)
237 return;
238
239 d->m_rotation = rotation;
240 const QVector3D oldRotation = d->m_eulerRotationAngles;
241 d->m_eulerRotationAngles = d->m_rotation.toEulerAngles();
242 emit rotationChanged(rotation);
243
244 const bool wasBlocked = blockNotifications(block: true);
245 if (!qFuzzyCompare(p1: d->m_eulerRotationAngles.x(), p2: oldRotation.x()))
246 emit rotationXChanged(rotationX: d->m_eulerRotationAngles.x());
247 if (!qFuzzyCompare(p1: d->m_eulerRotationAngles.y(), p2: oldRotation.y()))
248 emit rotationYChanged(rotationY: d->m_eulerRotationAngles.y());
249 if (!qFuzzyCompare(p1: d->m_eulerRotationAngles.z(), p2: oldRotation.z()))
250 emit rotationZChanged(rotationZ: d->m_eulerRotationAngles.z());
251 blockNotifications(block: wasBlocked);
252}
253
254void QJoint::setTranslation(const QVector3D &translation)
255{
256 Q_D(QJoint);
257 if (translation == d->m_translation)
258 return;
259
260 d->m_translation = translation;
261 emit translationChanged(translation);
262}
263
264void QJoint::setInverseBindMatrix(const QMatrix4x4 &inverseBindMatrix)
265{
266 Q_D(QJoint);
267 if (d->m_inverseBindMatrix == inverseBindMatrix)
268 return;
269
270 d->m_inverseBindMatrix = inverseBindMatrix;
271 emit inverseBindMatrixChanged(inverseBindMatrix);
272}
273
274void QJoint::setRotationX(float rotationX)
275{
276 Q_D(QJoint);
277
278 if (qFuzzyCompare(p1: d->m_eulerRotationAngles.x(), p2: rotationX))
279 return;
280
281 const auto eulers = QVector3D(rotationX,
282 d->m_eulerRotationAngles.y(),
283 d->m_eulerRotationAngles.z());
284 const QQuaternion r = QQuaternion::fromEulerAngles(eulerAngles: eulers);
285 setRotation(r);
286}
287
288void QJoint::setRotationY(float rotationY)
289{
290 Q_D(QJoint);
291
292 if (qFuzzyCompare(p1: d->m_eulerRotationAngles.y(), p2: rotationY))
293 return;
294
295 const auto eulers = QVector3D(d->m_eulerRotationAngles.x(),
296 rotationY,
297 d->m_eulerRotationAngles.z());
298 const QQuaternion r = QQuaternion::fromEulerAngles(eulerAngles: eulers);
299 setRotation(r);
300}
301
302void QJoint::setRotationZ(float rotationZ)
303{
304 Q_D(QJoint);
305 if (qFuzzyCompare(p1: d->m_eulerRotationAngles.z(), p2: rotationZ))
306 return;
307
308 const auto eulers = QVector3D(d->m_eulerRotationAngles.x(),
309 d->m_eulerRotationAngles.y(),
310 rotationZ);
311 const QQuaternion r = QQuaternion::fromEulerAngles(eulerAngles: eulers);
312 setRotation(r);
313}
314
315void QJoint::setName(const QString &name)
316{
317 Q_D(QJoint);
318 if (d->m_name == name)
319 return;
320
321 d->m_name = name;
322 emit nameChanged(name);
323}
324
325/*!
326 Sets the transform matrix for this joint to the identity matrix.
327*/
328void QJoint::setToIdentity()
329{
330 setScale(QVector3D(1.0f, 1.0f, 1.0f));
331 setRotation(QQuaternion());
332 setTranslation(QVector3D());
333}
334
335/*!
336 Adds \a joint as a child of this joint. If \a joint has no parent, then
337 this joint takes ownership of it. Child joints are in the coordinate system
338 of their parent joint.
339*/
340void QJoint::addChildJoint(QJoint *joint)
341{
342 Q_D(QJoint);
343 if (!d->m_childJoints.contains(t: joint)) {
344 d->m_childJoints.push_back(t: joint);
345 // Force creation in backend by setting parent
346 if (!joint->parent())
347 joint->setParent(this);
348
349 // Ensures proper bookkeeping
350 d->registerDestructionHelper(node: joint, func: &QJoint::removeChildJoint, d->m_childJoints);
351
352 if (d->m_changeArbiter != nullptr)
353 d->updateNode(node: joint, property: "childJoint", change: PropertyValueAdded);
354 }
355}
356
357/*!
358 Removes \a joint from this joint's list of children. The child joint is not
359 destroyed.
360*/
361void QJoint::removeChildJoint(QJoint *joint)
362{
363 Q_D(QJoint);
364 if (d->m_childJoints.contains(t: joint)) {
365 if (d->m_changeArbiter != nullptr)
366 d->updateNode(node: joint, property: "childJoint", change: PropertyValueRemoved);
367
368 d->m_childJoints.removeOne(t: joint);
369
370 // Remove bookkeeping connection
371 d->unregisterDestructionHelper(node: joint);
372 }
373}
374
375/*!
376 The vector of joints this joint has as children.
377*/
378QVector<QJoint *> QJoint::childJoints() const
379{
380 Q_D(const QJoint);
381 return d->m_childJoints;
382}
383
384/*!
385 Returns the name of the joint.
386*/
387QString QJoint::name() const
388{
389 Q_D(const QJoint);
390 return d->m_name;
391}
392
393/*! \internal */
394Qt3DCore::QNodeCreatedChangeBasePtr QJoint::createNodeCreationChange() const
395{
396 auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QJointData>::create(arguments: this);
397 auto &data = creationChange->data;
398 Q_D(const QJoint);
399 data.inverseBindMatrix = d->m_inverseBindMatrix;
400 data.childJointIds = qIdsForNodes(nodes: d->m_childJoints);
401 data.rotation = d->m_rotation;
402 data.scale = d->m_scale;
403 data.translation = d->m_translation;
404 data.name = d->m_name;
405 return creationChange;
406}
407
408} // namespace Qt3DCore
409
410QT_END_NAMESPACE
411

source code of qt3d/src/core/transforms/qjoint.cpp