1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014-2016 Klarälvdalens Datakonsult AB (KDAB). |
4 | ** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the Qt3D module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | #ifndef GLTFIMPORTER_H |
42 | #define GLTFIMPORTER_H |
43 | |
44 | // |
45 | // W A R N I N G |
46 | // ------------- |
47 | // |
48 | // This file is not part of the Qt API. It exists for the convenience |
49 | // of other Qt classes. This header file may change from version to |
50 | // version without notice, or even be removed. |
51 | // |
52 | // We mean it. |
53 | // |
54 | |
55 | #include <Qt3DRender/qattribute.h> |
56 | #include <Qt3DRender/qbuffer.h> |
57 | #include <QtCore/qjsondocument.h> |
58 | #include <QtCore/qjsonobject.h> |
59 | #include <QtCore/qhash.h> |
60 | #include <QtCore/qloggingcategory.h> |
61 | |
62 | #include <Qt3DRender/private/qsceneimporter_p.h> |
63 | |
64 | QT_BEGIN_NAMESPACE |
65 | |
66 | class QByteArray; |
67 | |
68 | namespace Qt3DCore { |
69 | class QEntity; |
70 | } |
71 | |
72 | namespace Qt3DRender { |
73 | |
74 | class QCamera; |
75 | class QCameraLens; |
76 | class QMaterial; |
77 | class QShaderProgram; |
78 | class QEffect; |
79 | class QAbstractTexture; |
80 | class QRenderState; |
81 | class QTechnique; |
82 | class QParameter; |
83 | class QGeometryRenderer; |
84 | class QAbstractLight; |
85 | class QRenderPass; |
86 | class QTexture2D; |
87 | |
88 | Q_DECLARE_LOGGING_CATEGORY(GLTFImporterLog) |
89 | |
90 | class GLTFImporter : public QSceneImporter |
91 | { |
92 | Q_OBJECT |
93 | |
94 | public: |
95 | GLTFImporter(); |
96 | ~GLTFImporter(); |
97 | |
98 | void setBasePath(const QString& path); |
99 | bool setJSON(const QJsonDocument &json); |
100 | |
101 | // SceneParserInterface interface |
102 | void setSource(const QUrl &source) final; |
103 | void setData(const QByteArray& data, const QString &basePath) final; |
104 | bool areFileTypesSupported(const QStringList &extensions) const final; |
105 | Qt3DCore::QEntity *node(const QString &id) final; |
106 | Qt3DCore::QEntity *scene(const QString &id = QString()) final; |
107 | |
108 | private: |
109 | class BufferData |
110 | { |
111 | public: |
112 | BufferData(); |
113 | explicit BufferData(const QJsonObject &json); |
114 | |
115 | quint64 length; |
116 | QString path; |
117 | QByteArray *data; |
118 | // type if ever useful |
119 | }; |
120 | |
121 | class ParameterData |
122 | { |
123 | public: |
124 | ParameterData(); |
125 | explicit ParameterData(const QJsonObject &json); |
126 | |
127 | QString semantic; |
128 | int type; |
129 | }; |
130 | |
131 | class AccessorData |
132 | { |
133 | public: |
134 | AccessorData(); |
135 | explicit AccessorData(const QJsonObject& json, int major, int minor); |
136 | |
137 | QString bufferViewName; |
138 | QAttribute::VertexBaseType type; |
139 | uint dataSize; |
140 | int count; |
141 | int offset; |
142 | int stride; |
143 | }; |
144 | |
145 | static bool isGLTFSupported(const QStringList &extensions); |
146 | static bool isEmbeddedResource(const QString &url); |
147 | static void renameFromJson(const QJsonObject& json, QObject * const object ); |
148 | static bool hasStandardUniformNameFromSemantic(const QString &semantic); |
149 | static QString standardAttributeNameFromSemantic(const QString &semantic); |
150 | QParameter *parameterFromTechnique(QTechnique *technique, const QString ¶meterName); |
151 | |
152 | Qt3DCore::QEntity *defaultScene(); |
153 | QMaterial *material(const QString &id); |
154 | bool fillCamera(QCameraLens &lens, QCamera *cameraEntity, const QString &id) const; |
155 | |
156 | void parse(); |
157 | void parseV1(); |
158 | void parseV2(); |
159 | void cleanup(); |
160 | |
161 | void processJSONAsset(const QJsonObject &json); |
162 | void processJSONBuffer(const QString &id, const QJsonObject &json); |
163 | void processJSONBufferView(const QString &id, const QJsonObject &json); |
164 | void processJSONShader(const QString &id, const QJsonObject &jsonObject); |
165 | void processJSONProgram(const QString &id, const QJsonObject &jsonObject); |
166 | void processJSONTechnique(const QString &id, const QJsonObject &jsonObject); |
167 | void processJSONAccessor(const QString &id, const QJsonObject &json); |
168 | void processJSONMesh(const QString &id, const QJsonObject &json); |
169 | void processJSONImage(const QString &id, const QJsonObject &jsonObject); |
170 | void processJSONTexture(const QString &id, const QJsonObject &jsonObject); |
171 | void processJSONExtensions(const QString &id, const QJsonObject &jsonObject); |
172 | void processJSONEffect(const QString &id, const QJsonObject &jsonObject); |
173 | void processJSONRenderPass(const QString &id, const QJsonObject &jsonObject); |
174 | |
175 | void loadBufferData(); |
176 | void unloadBufferData(); |
177 | |
178 | QByteArray resolveLocalData(const QString &path) const; |
179 | |
180 | QVariant parameterValueFromJSON(int type, const QJsonValue &value) const; |
181 | static QAttribute::VertexBaseType accessorTypeFromJSON(int componentType); |
182 | static uint accessorDataSizeFromJson(const QString &type); |
183 | |
184 | static QRenderState *buildStateEnable(int state); |
185 | static QRenderState *buildState(const QString& functionName, const QJsonValue &value, int &type); |
186 | QParameter *buildParameter(const QString &key, const QJsonObject ¶mObj); |
187 | void populateRenderStates(QRenderPass *pass, const QJsonObject &states); |
188 | void addProgramToPass(QRenderPass *pass, const QString &progName); |
189 | |
190 | void setTextureSamplerInfo(const QString &id, const QJsonObject &jsonObj, QTexture2D *tex); |
191 | QMaterial *materialWithCustomShader(const QString &id, const QJsonObject &jsonObj); |
192 | QMaterial *commonMaterial(const QJsonObject &jsonObj); |
193 | QMaterial *pbrMaterial(const QJsonObject &jsonObj); |
194 | |
195 | QJsonDocument m_json; |
196 | QString m_basePath; |
197 | bool m_parseDone; |
198 | int m_majorVersion; |
199 | int m_minorVersion; |
200 | QString m_defaultScene; |
201 | |
202 | // multi-hash because our QMeshData corresponds to a single primitive |
203 | // in glTf. |
204 | QMultiHash<QString, QGeometryRenderer*> m_meshDict; |
205 | |
206 | // GLTF assigns materials at the mesh level, but we do them as siblings, |
207 | // so record the association here for when we instantiate meshes |
208 | QHash<QGeometryRenderer*, QString> m_meshMaterialDict; |
209 | |
210 | QHash<QString, AccessorData> m_accessorDict; |
211 | |
212 | QHash<QString, QMaterial*> m_materialCache; |
213 | |
214 | QHash<QString, BufferData> m_bufferDatas; |
215 | QHash<QString, Qt3DRender::QBuffer*> m_buffers; |
216 | |
217 | QHash<QString, QString> m_shaderPaths; |
218 | QHash<QString, QShaderProgram*> m_programs; |
219 | |
220 | QHash<QString, QTechnique *> m_techniques; |
221 | QHash<QString, QRenderPass *> m_renderPasses; |
222 | QHash<QString, QEffect *> m_effects; |
223 | QHash<QTechnique *, QList<QParameter *> > m_techniqueParameters; |
224 | QHash<QParameter*, ParameterData> m_parameterDataDict; |
225 | |
226 | QHash<QString, QAbstractTexture*> m_textures; |
227 | QHash<QString, QString> m_imagePaths; |
228 | QHash<QString, QImage> m_imageData; |
229 | QHash<QString, QAbstractLight *> m_lights; |
230 | }; |
231 | |
232 | } // namespace Qt3DRender |
233 | |
234 | QT_END_NAMESPACE |
235 | |
236 | #endif // GLTFIMPORTER_H |
237 | |