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 | #ifndef GLTFGEOMETRYLOADER_H |
41 | #define GLTFGEOMETRYLOADER_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of other Qt classes. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/QJsonDocument> |
55 | |
56 | #include <Qt3DRender/private/qgeometryloaderinterface_p.h> |
57 | #include <Qt3DRender/qattribute.h> |
58 | #include <Qt3DRender/qbuffer.h> |
59 | |
60 | #include <private/qlocale_tools_p.h> |
61 | |
62 | QT_BEGIN_NAMESPACE |
63 | |
64 | namespace Qt3DRender { |
65 | |
66 | #define GLTFGEOMETRYLOADER_EXT QLatin1String("gltf") |
67 | #define JSONGEOMETRYLOADER_EXT QLatin1String("json") |
68 | #define QGLTFGEOMETRYLOADER_EXT QLatin1String("qgltf") |
69 | |
70 | class QCamera; |
71 | class QCameraLens; |
72 | class QMaterial; |
73 | class QShaderProgram; |
74 | class QEffect; |
75 | class QAbstractTexture; |
76 | class QRenderState; |
77 | class QTechnique; |
78 | class QParameter; |
79 | class QGeometryRenderer; |
80 | class QGeometry; |
81 | |
82 | class GLTFGeometryLoader : public QGeometryLoaderInterface |
83 | { |
84 | class BufferData |
85 | { |
86 | public: |
87 | BufferData(); |
88 | explicit BufferData(const QJsonObject &json); |
89 | |
90 | quint64 length; |
91 | QString path; |
92 | QByteArray *data; |
93 | // type if ever useful |
94 | }; |
95 | |
96 | class ParameterData |
97 | { |
98 | public: |
99 | ParameterData(); |
100 | explicit ParameterData(const QJsonObject &json); |
101 | |
102 | QString semantic; |
103 | int type; |
104 | }; |
105 | |
106 | class AccessorData |
107 | { |
108 | public: |
109 | AccessorData(); |
110 | explicit AccessorData(const QJsonObject &json); |
111 | |
112 | QString bufferViewName; |
113 | int bufferViewIndex; |
114 | QAttribute::VertexBaseType type; |
115 | uint dataSize; |
116 | int count; |
117 | int offset; |
118 | int stride; |
119 | }; |
120 | |
121 | struct Gltf1 |
122 | { |
123 | QHash<QString, AccessorData> m_accessorDict; |
124 | QHash<QString, BufferData> m_bufferDatas; |
125 | QHash<QString, Qt3DRender::QBuffer*> m_buffers; |
126 | }; |
127 | |
128 | struct Gltf2 |
129 | { |
130 | QVector<BufferData> m_bufferDatas; |
131 | QVector<Qt3DRender::QBuffer*> m_buffers; |
132 | QVector<AccessorData> m_accessors; |
133 | }; |
134 | |
135 | Q_OBJECT |
136 | public: |
137 | GLTFGeometryLoader(); |
138 | ~GLTFGeometryLoader(); |
139 | |
140 | QGeometry *geometry() const final; |
141 | |
142 | bool load(QIODevice *ioDev, const QString &subMesh = QString()) final; |
143 | |
144 | protected: |
145 | void setBasePath(const QString &path); |
146 | bool setJSON(const QJsonDocument &json); |
147 | |
148 | static QString standardAttributeNameFromSemantic(const QString &semantic); |
149 | |
150 | void parse(); |
151 | void parseGLTF1(); |
152 | void parseGLTF2(); |
153 | void cleanup(); |
154 | |
155 | void processJSONBuffer(const QString &id, const QJsonObject &json); |
156 | void processJSONBufferView(const QString &id, const QJsonObject &json); |
157 | void processJSONAccessor(const QString &id, const QJsonObject &json); |
158 | void processJSONMesh(const QString &id, const QJsonObject &json); |
159 | |
160 | void loadBufferData(); |
161 | void unloadBufferData(); |
162 | |
163 | void processJSONBufferV2(const QJsonObject &json); |
164 | void processJSONBufferViewV2(const QJsonObject &json); |
165 | void processJSONAccessorV2(const QJsonObject &json); |
166 | void processJSONMeshV2(const QJsonObject &json); |
167 | |
168 | void loadBufferDataV2(); |
169 | void unloadBufferDataV2(); |
170 | |
171 | QByteArray resolveLocalData(const QString &path) const; |
172 | |
173 | static QAttribute::VertexBaseType accessorTypeFromJSON(int componentType); |
174 | static uint accessorDataSizeFromJson(const QString &type); |
175 | |
176 | private: |
177 | QJsonDocument m_json; |
178 | QString m_basePath; |
179 | QString m_mesh; |
180 | |
181 | Gltf1 m_gltf1; |
182 | Gltf2 m_gltf2; |
183 | |
184 | QGeometry *m_geometry; |
185 | }; |
186 | |
187 | } // namespace Qt3DRender |
188 | |
189 | QT_END_NAMESPACE |
190 | |
191 | #endif // GLTFGEOMETRYLOADER_H |
192 | |