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 QT3DANIMATION_ANIMATION_GLTFIMPORTER_H
5#define QT3DANIMATION_ANIMATION_GLTFIMPORTER_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 <QtGlobal>
19#include <Qt3DAnimation/private/fcurve_p.h>
20#include <Qt3DCore/qattribute.h>
21#include <Qt3DCore/private/sqt_p.h>
22#include <Qt3DCore/private/qmath3d_p.h>
23
24#include <QJsonDocument>
25#include <QJsonObject>
26#include <QJsonArray>
27#include <QJsonValue>
28#include <QList>
29
30QT_BEGIN_NAMESPACE
31
32class QIODevice;
33
34namespace Qt3DAnimation {
35namespace Animation {
36
37class GLTFImporter
38{
39public:
40 class BufferData
41 {
42 public:
43 BufferData();
44 explicit BufferData(const QJsonObject &json);
45
46 quint64 byteLength;
47 QString path;
48 QByteArray data;
49 };
50
51 class BufferView
52 {
53 public:
54 BufferView();
55 explicit BufferView(const QJsonObject &json);
56
57 quint64 byteOffset;
58 quint64 byteLength;
59 int bufferIndex;
60 int target; // Only for per vertex attributes
61 };
62
63 class AccessorData
64 {
65 public:
66 AccessorData();
67 explicit AccessorData(const QJsonObject &json);
68
69 int bufferViewIndex;
70 Qt3DCore::QAttribute::VertexBaseType type;
71 uint dataSize;
72 int count;
73 int byteOffset;
74 int byteStride; // Only for per vertex attributes
75
76 // TODO: Extend to support sparse accessors
77 };
78
79 class Skin
80 {
81 public:
82 Skin();
83 explicit Skin(const QJsonObject &json);
84
85 QString name;
86 int inverseBindAccessorIndex;
87 QList<qsizetype> jointNodeIndices;
88 };
89
90 class Channel
91 {
92 public:
93 Channel();
94 explicit Channel(const QJsonObject &json);
95
96 int samplerIndex;
97 int targetNodeIndex;
98 QString targetProperty;
99 };
100
101 class Sampler
102 {
103 public:
104 Sampler();
105 explicit Sampler(const QJsonObject &json);
106
107 enum InterpolationMode {
108 Linear,
109 Step,
110 CatmullRomSpline,
111 CubicSpline
112 };
113
114 QString interpolationModeString() const;
115
116 int inputAccessorIndex;
117 int outputAccessorIndex;
118 InterpolationMode interpolationMode;
119 };
120
121 class Animation
122 {
123 public:
124 Animation();
125 explicit Animation(const QJsonObject &json);
126
127 QString name;
128 QList<Channel> channels;
129 QList<Sampler> samplers;
130 };
131
132 class Node
133 {
134 public:
135 Node();
136 explicit Node(const QJsonObject &json);
137
138 Qt3DCore::Sqt localTransform;
139 QList<qsizetype> childNodeIndices;
140 QString name;
141 int parentNodeIndex;
142 int cameraIndex;
143 int meshIndex;
144 int skinIndex;
145 };
146
147 GLTFImporter();
148
149 bool load(QIODevice *ioDev);
150 const QList<Animation> animations() const { return m_animations; }
151
152 struct AnimationNameAndChannels
153 {
154 QString name;
155 QVector<Qt3DAnimation::Animation::Channel> channels;
156 };
157 AnimationNameAndChannels createAnimationData(qsizetype animationIndex, const QString &animationName = QString()) const;
158
159private:
160 static Qt3DCore::QAttribute::VertexBaseType accessorTypeFromJSON(int componentType);
161 static uint accessorTypeSize(Qt3DCore::QAttribute::VertexBaseType componentType);
162 static uint accessorDataSizeFromJson(const QString &type);
163
164 struct RawData
165 {
166 const char *data;
167 quint64 byteLength;
168 };
169
170 void setBasePath(const QString &path);
171 bool setJSON(const QJsonDocument &json);
172
173 bool parse();
174 bool parseGLTF2();
175 void cleanup();
176 QHash<qsizetype, qsizetype> createNodeIndexToJointIndexMap(const Skin &skin) const;
177
178 bool processJSONBuffer(const QJsonObject &json);
179 bool processJSONBufferView(const QJsonObject &json);
180 bool processJSONAccessor(const QJsonObject &json);
181 bool processJSONSkin(const QJsonObject &json);
182 bool processJSONAnimation(const QJsonObject &json);
183 bool processJSONNode(const QJsonObject &json);
184 void setupNodeParentLinks();
185 QByteArray resolveLocalData(const QString &path) const;
186
187 RawData accessorData(int accessorIndex, int index) const;
188
189 QJsonDocument m_json;
190 QString m_basePath;
191 QList<BufferData> m_bufferDatas;
192 QList<BufferView> m_bufferViews;
193 QList<AccessorData> m_accessors;
194 QList<Skin> m_skins;
195 QList<Animation> m_animations;
196 QList<Node> m_nodes;
197};
198
199} // namespace Animation
200} // namespace Qt3DAnimation
201
202QT_END_NAMESPACE
203
204#endif // QT3DANIMATION_ANIMATION_GLTFIMPORTER_H
205

source code of qt3d/src/animation/backend/gltfimporter_p.h