aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3d/qquick3djoint.cpp
blob: 5081bd8895d986abdcd9e6e0d6a710487dc933d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qquick3djoint_p.h"
#include "qquick3dskeleton_p.h"
#include "qquick3dobject_p.h"
#include "qquick3dscenemanager_p.h"
#include "qquick3dnode_p_p.h"

#include <QtQuick3DRuntimeRender/private/qssgrendergraphobject_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendernode_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderjoint_p.h>

QT_BEGIN_NAMESPACE

/*!
    \qmltype Joint
    \inherits Node
    \inqmlmodule QtQuick3D
    \brief Defines a node in a skeletal animation hierarchy.

    A joint is a transformable node inside a \l {Skeleton}, used for \l {Vertex Skinning}
    {skeletal animation}. It is called a "joint" because it can be seen as a joint between the bones
    of a skeleton.

    All the joints must be contained inside a Skeleton, and each joint must have a \l skeletonRoot
    pointing back to that skeleton.

    \qml
    Skeleton {
        id: qmlskeleton
        Joint {
            id: joint0
            index: 0
            skeletonRoot: qmlskeleton
            Joint {
                id: joint1
                index: 1
                skeletonRoot: qmlskeleton
            }
        }
    }
    \endqml

*/

QQuick3DJoint::QQuick3DJoint(QQuick3DNode *parent)
    : QQuick3DNode(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::Joint)), parent)
{
}

QQuick3DJoint::~QQuick3DJoint()
{
    disconnect(m_skeletonConnection);
}

/*!
    \qmlproperty int Joint::index

    Specifies the index of this joint. This index value is used in the \c JointSemantic
    \l {QQuick3DGeometry::addAttribute}{custom geometry attribute}.

    \note Index values must be unique within the same \l {Skeleton}.
    \note Negative values cannot be assigned.

    \sa {QQuick3DGeometry::addAttribute}, {Qt Quick 3D - Simple Skinning Example}
*/

qint32 QQuick3DJoint::index() const
{
    return m_index;
}

/*!
    \qmlproperty Skeleton Joint::skeletonRoot

    Specifies the \l {Skeleton} that contains this joint.

    \note All the \l {Joint}s in the \l {Skeleton} must have the same skeletonRoot.
    If not, the animation will be broken.

    \sa {Skeleton}
*/

QQuick3DSkeleton *QQuick3DJoint::skeletonRoot() const
{
    return m_skeletonRoot;
}

void QQuick3DJoint::setIndex(qint32 index)
{
    if (m_index == index)
        return;
    if (index < 0)
        return;

    m_index = index;
    m_indexDirty = true;
    emit indexChanged();
}

void QQuick3DJoint::setSkeletonRoot(QQuick3DSkeleton *skeleton)
{
    if (skeleton == m_skeletonRoot)
        return;

    QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DJoint::setSkeletonRoot, skeleton, m_skeletonRoot);
    if (m_skeletonRoot)
        QObject::disconnect(m_skeletonConnection);

    m_skeletonRoot = skeleton;

    if (m_skeletonRoot) {
        m_skeletonConnection = connect(this, &QQuick3DJoint::sceneTransformChanged,
                                       skeleton, [skeleton]() {
                                   auto skeletonNode = static_cast<QSSGRenderSkeleton *>(QQuick3DNodePrivate::get(skeleton)->spatialNode);
                                   if (skeletonNode)
                                       skeletonNode->skinningDirty = true;
                                });
    }
    m_skeletonRootDirty = true;
    emit skeletonRootChanged();
}


void QQuick3DJoint::markAllDirty()
{
    m_indexDirty = true;
    m_skeletonRootDirty = true;
    QQuick3DNode::markAllDirty();
}

QSSGRenderGraphObject *QQuick3DJoint::updateSpatialNode(QSSGRenderGraphObject *node)
{
    if (!m_skeletonRoot)
        return node;

    if (!node) {
        markAllDirty();
        node = new QSSGRenderJoint();
    }

    QQuick3DNode::updateSpatialNode(node);

    auto jointNode = static_cast<QSSGRenderJoint *>(node);

    QQuick3DObjectPrivate *skeletonPriv = QQuick3DObjectPrivate::get(m_skeletonRoot);

    if (m_skeletonRootDirty) {
        if (skeletonPriv && skeletonPriv->spatialNode)
            jointNode->skeletonRoot = static_cast<QSSGRenderSkeleton *>(skeletonPriv->spatialNode);
    }

    if (m_indexDirty) {
        jointNode->index = m_index;
        m_indexDirty = false;

        if (jointNode->skeletonRoot) {
            Q_ASSERT(m_skeletonRoot);
            m_skeletonRoot->skeletonNodeDirty();

            if (jointNode->skeletonRoot->maxIndex < m_index) {
                jointNode->skeletonRoot->maxIndex = m_index;
            }
        }
    }
    return node;
}

QT_END_NAMESPACE