| 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_GLTFSKELETONLOADER_P_H |
| 5 | #define QT3DRENDER_RENDER_GLTFSKELETONLOADER_P_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 <Qt3DCore/qattribute.h> |
| 20 | |
| 21 | #include <QtGui/qmatrix4x4.h> |
| 22 | #include <QtCore/qjsondocument.h> |
| 23 | |
| 24 | #include <Qt3DRender/private/skeletondata_p.h> |
| 25 | #include <Qt3DCore/private/sqt_p.h> |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class QJsonObject; |
| 30 | |
| 31 | namespace Qt3DRender { |
| 32 | namespace Render { |
| 33 | |
| 34 | class GLTFSkeletonLoader |
| 35 | { |
| 36 | class BufferData |
| 37 | { |
| 38 | public: |
| 39 | BufferData(); |
| 40 | explicit BufferData(const QJsonObject &json); |
| 41 | |
| 42 | quint64 byteLength; |
| 43 | QString path; |
| 44 | QByteArray data; |
| 45 | }; |
| 46 | |
| 47 | class BufferView |
| 48 | { |
| 49 | public: |
| 50 | BufferView(); |
| 51 | explicit BufferView(const QJsonObject &json); |
| 52 | |
| 53 | int bufferIndex; |
| 54 | quint64 byteOffset; |
| 55 | quint64 byteLength; |
| 56 | int target; // Only for per vertex attributes |
| 57 | }; |
| 58 | |
| 59 | class AccessorData |
| 60 | { |
| 61 | public: |
| 62 | AccessorData(); |
| 63 | explicit AccessorData(const QJsonObject &json); |
| 64 | |
| 65 | int bufferViewIndex; |
| 66 | Qt3DCore::QAttribute::VertexBaseType type; |
| 67 | uint dataSize; |
| 68 | int count; |
| 69 | int byteOffset; |
| 70 | int byteStride; // Only for per vertex attributes |
| 71 | |
| 72 | // TODO: Extend to support sparse accessors |
| 73 | }; |
| 74 | |
| 75 | class Skin |
| 76 | { |
| 77 | public: |
| 78 | Skin(); |
| 79 | explicit Skin(const QJsonObject &json); |
| 80 | |
| 81 | QString name; |
| 82 | int inverseBindAccessorIndex; |
| 83 | std::vector<int> jointNodeIndices; |
| 84 | }; |
| 85 | |
| 86 | class Node |
| 87 | { |
| 88 | public: |
| 89 | Node(); |
| 90 | explicit Node(const QJsonObject &json); |
| 91 | |
| 92 | Qt3DCore::Sqt localTransform; |
| 93 | std::vector<int> childNodeIndices; |
| 94 | QString name; |
| 95 | int parentNodeIndex; |
| 96 | int cameraIndex; |
| 97 | int meshIndex; |
| 98 | int skinIndex; |
| 99 | }; |
| 100 | |
| 101 | public: |
| 102 | GLTFSkeletonLoader(); |
| 103 | |
| 104 | bool load(QIODevice *ioDev); |
| 105 | |
| 106 | SkeletonData createSkeleton(const QString &skeletonName); |
| 107 | |
| 108 | private: |
| 109 | static Qt3DCore::QAttribute::VertexBaseType accessorTypeFromJSON(int componentType); |
| 110 | static uint accessorTypeSize(Qt3DCore::QAttribute::VertexBaseType componentType); |
| 111 | static uint accessorDataSizeFromJson(const QString &type); |
| 112 | |
| 113 | struct RawData |
| 114 | { |
| 115 | const char *data; |
| 116 | quint64 byteLength; |
| 117 | }; |
| 118 | |
| 119 | void setBasePath(const QString &path); |
| 120 | bool setJSON(const QJsonDocument &json); |
| 121 | |
| 122 | bool parse(); |
| 123 | bool parseGLTF2(); |
| 124 | void cleanup(); |
| 125 | |
| 126 | bool processJSONBuffer(const QJsonObject &json); |
| 127 | bool processJSONBufferView(const QJsonObject &json); |
| 128 | bool processJSONAccessor(const QJsonObject &json); |
| 129 | bool processJSONSkin(const QJsonObject &json); |
| 130 | bool processJSONNode(const QJsonObject &json); |
| 131 | void setupNodeParentLinks(); |
| 132 | QByteArray resolveLocalData(const QString &path) const; |
| 133 | |
| 134 | SkeletonData createSkeletonFromSkin(const Skin &skin) const; |
| 135 | QMatrix4x4 inverseBindMatrix(const Skin &skin, int jointIndex) const; |
| 136 | RawData accessorData(int accessorIndex, int index) const; |
| 137 | |
| 138 | QJsonDocument m_json; |
| 139 | QString m_basePath; |
| 140 | std::vector<BufferData> m_bufferDatas; |
| 141 | std::vector<BufferView> m_bufferViews; |
| 142 | std::vector<AccessorData> m_accessors; |
| 143 | std::vector<Skin> m_skins; |
| 144 | std::vector<Node> m_nodes; |
| 145 | }; |
| 146 | |
| 147 | } // namespace Render |
| 148 | } // namespace Qt3DRender |
| 149 | |
| 150 | QT_END_NAMESPACE |
| 151 | |
| 152 | #endif // QT3DRENDER_RENDER_GLTFSKELETONLOADER_P_H |
| 153 | |