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 GLTFGEOMETRYLOADER_H |
5 | #define GLTFGEOMETRYLOADER_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 <QtCore/QHash> |
19 | #include <QtCore/QJsonDocument> |
20 | |
21 | #include <Qt3DRender/private/qgeometryloaderinterface_p.h> |
22 | #include <Qt3DCore/qattribute.h> |
23 | #include <Qt3DCore/qbuffer.h> |
24 | |
25 | #include <private/qlocale_tools_p.h> |
26 | |
27 | QT_BEGIN_NAMESPACE |
28 | |
29 | namespace Qt3DCore { |
30 | class QGeometry; |
31 | } |
32 | |
33 | namespace Qt3DRender { |
34 | |
35 | #define GLTFGEOMETRYLOADER_EXT QLatin1String("gltf") |
36 | #define JSONGEOMETRYLOADER_EXT QLatin1String("json") |
37 | #define QGLTFGEOMETRYLOADER_EXT QLatin1String("qgltf") |
38 | |
39 | class QCamera; |
40 | class QCameraLens; |
41 | class QMaterial; |
42 | class QShaderProgram; |
43 | class QEffect; |
44 | class QAbstractTexture; |
45 | class QRenderState; |
46 | class QTechnique; |
47 | class QParameter; |
48 | class QGeometryRenderer; |
49 | |
50 | class GLTFGeometryLoader : public QGeometryLoaderInterface |
51 | { |
52 | class BufferData |
53 | { |
54 | public: |
55 | BufferData(); |
56 | explicit BufferData(const QJsonObject &json); |
57 | |
58 | quint64 length; |
59 | QString path; |
60 | QByteArray *data; |
61 | // type if ever useful |
62 | }; |
63 | |
64 | class ParameterData |
65 | { |
66 | public: |
67 | ParameterData(); |
68 | explicit ParameterData(const QJsonObject &json); |
69 | |
70 | QString semantic; |
71 | int type; |
72 | }; |
73 | |
74 | class AccessorData |
75 | { |
76 | public: |
77 | AccessorData(); |
78 | explicit AccessorData(const QJsonObject &json); |
79 | |
80 | QString bufferViewName; |
81 | int bufferViewIndex; |
82 | Qt3DCore::QAttribute::VertexBaseType type; |
83 | uint dataSize; |
84 | int count; |
85 | int offset; |
86 | int stride; |
87 | }; |
88 | |
89 | struct Gltf1 |
90 | { |
91 | QHash<QString, AccessorData> m_accessorDict; |
92 | QHash<QString, BufferData> m_bufferDatas; |
93 | QHash<QString, Qt3DCore::QBuffer*> m_buffers; |
94 | }; |
95 | |
96 | struct Gltf2 |
97 | { |
98 | QVector<BufferData> m_bufferDatas; |
99 | QVector<Qt3DCore::QBuffer*> m_buffers; |
100 | QVector<AccessorData> m_accessors; |
101 | }; |
102 | |
103 | Q_OBJECT |
104 | public: |
105 | GLTFGeometryLoader(); |
106 | ~GLTFGeometryLoader(); |
107 | |
108 | Qt3DCore::QGeometry *geometry() const final; |
109 | |
110 | bool load(QIODevice *ioDev, const QString &subMesh = QString()) final; |
111 | |
112 | protected: |
113 | void setBasePath(const QString &path); |
114 | bool setJSON(const QJsonDocument &json); |
115 | |
116 | static QString standardAttributeNameFromSemantic(const QString &semantic); |
117 | |
118 | void parse(); |
119 | void parseGLTF1(); |
120 | void parseGLTF2(); |
121 | void cleanup(); |
122 | |
123 | void processJSONBuffer(const QString &id, const QJsonObject &json); |
124 | void processJSONBufferView(const QString &id, const QJsonObject &json); |
125 | void processJSONAccessor(const QString &id, const QJsonObject &json); |
126 | void processJSONMesh(const QString &id, const QJsonObject &json); |
127 | |
128 | void loadBufferData(); |
129 | void unloadBufferData(); |
130 | |
131 | void processJSONBufferV2(const QJsonObject &json); |
132 | void processJSONBufferViewV2(const QJsonObject &json); |
133 | void processJSONAccessorV2(const QJsonObject &json); |
134 | void processJSONMeshV2(const QJsonObject &json); |
135 | |
136 | void loadBufferDataV2(); |
137 | void unloadBufferDataV2(); |
138 | |
139 | QByteArray resolveLocalData(const QString &path) const; |
140 | |
141 | static Qt3DCore::QAttribute::VertexBaseType accessorTypeFromJSON(int componentType); |
142 | static uint accessorDataSizeFromJson(const QString &type); |
143 | |
144 | private: |
145 | QJsonDocument m_json; |
146 | QString m_basePath; |
147 | QString m_mesh; |
148 | |
149 | Gltf1 m_gltf1; |
150 | Gltf2 m_gltf2; |
151 | |
152 | Qt3DCore::QGeometry *m_geometry; |
153 | }; |
154 | |
155 | } // namespace Qt3DRender |
156 | |
157 | QT_END_NAMESPACE |
158 | |
159 | #endif // GLTFGEOMETRYLOADER_H |
160 | |