1 | // Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). |
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 GLTFEXPORTER_H |
5 | #define GLTFEXPORTER_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/qjsondocument.h> |
19 | #include <QtCore/qjsonobject.h> |
20 | #include <QtCore/qhash.h> |
21 | #include <QtCore/qset.h> |
22 | #include <QtGui/qvector3d.h> |
23 | |
24 | #include <Qt3DRender/qabstractlight.h> |
25 | #include <Qt3DRender/qshaderprogram.h> |
26 | |
27 | #include <private/qsceneexporter_p.h> |
28 | |
29 | QT_BEGIN_NAMESPACE |
30 | |
31 | class QByteArray; |
32 | |
33 | namespace Qt3DCore { |
34 | class QEntity; |
35 | class QTransform; |
36 | } |
37 | |
38 | namespace Qt3DRender { |
39 | |
40 | class QCamera; |
41 | class QCameraLens; |
42 | class QMaterial; |
43 | class QGeometryRenderer; |
44 | class QTechnique; |
45 | class QRenderPass; |
46 | class QEffect; |
47 | |
48 | Q_DECLARE_LOGGING_CATEGORY(GLTFExporterLog) |
49 | |
50 | class GLTFExporter : public QSceneExporter |
51 | { |
52 | Q_OBJECT |
53 | |
54 | public: |
55 | GLTFExporter(); |
56 | ~GLTFExporter(); |
57 | |
58 | bool exportScene(Qt3DCore::QEntity *sceneRoot, const QString &outDir, |
59 | const QString &exportName, const QVariantHash &options) final; |
60 | |
61 | struct GltfOptions { |
62 | bool compactJson; |
63 | }; |
64 | |
65 | private: |
66 | enum PropertyCacheType { |
67 | TypeNone = 0, |
68 | TypeConeMesh, |
69 | TypeCuboidMesh, |
70 | TypeCylinderMesh, |
71 | TypePlaneMesh, |
72 | TypeSphereMesh, |
73 | TypeTorusMesh |
74 | }; |
75 | |
76 | struct MeshInfo { |
77 | struct BufferView { |
78 | BufferView() : bufIndex(0), offset(0), length(0), componentType(0), target(0) { } |
79 | QString name; |
80 | uint bufIndex; |
81 | uint offset; |
82 | uint length; |
83 | uint componentType; |
84 | uint target; |
85 | }; |
86 | QList<BufferView> views; |
87 | struct Accessor { |
88 | Accessor() : offset(0), stride(0), count(0), componentType(0) { } |
89 | QString name; |
90 | QString usage; |
91 | QString bufferView; |
92 | uint offset; |
93 | uint stride; |
94 | uint count; |
95 | uint componentType; |
96 | QString type; |
97 | }; |
98 | QList<Accessor> accessors; |
99 | QString name; // generated |
100 | QString originalName; // may be empty |
101 | QString materialName; |
102 | Qt3DRender::QGeometryRenderer *meshComponent; |
103 | PropertyCacheType meshType; |
104 | QString meshTypeStr; |
105 | }; |
106 | |
107 | struct MaterialInfo { |
108 | enum MaterialType { |
109 | TypeCustom, |
110 | TypePhong, |
111 | TypePhongAlpha, |
112 | TypeDiffuseMap, |
113 | TypeDiffuseSpecularMap, |
114 | TypeNormalDiffuseMap, |
115 | TypeNormalDiffuseMapAlpha, |
116 | TypeNormalDiffuseSpecularMap, |
117 | TypeGooch, |
118 | TypePerVertex |
119 | }; |
120 | |
121 | QString name; |
122 | QString originalName; |
123 | MaterialType type; |
124 | |
125 | // These are only used for default materials |
126 | QHash<QString, QColor> colors; |
127 | QHash<QString, QString> textures; |
128 | QHash<QString, QVariant> values; |
129 | std::vector<int> blendArguments; |
130 | std::vector<int> blendEquations; |
131 | }; |
132 | |
133 | struct ProgramInfo { |
134 | QString name; |
135 | QString vertexShader; |
136 | QString tessellationControlShader; |
137 | QString tessellationEvaluationShader; |
138 | QString geometryShader; |
139 | QString fragmentShader; |
140 | QString computeShader; |
141 | }; |
142 | |
143 | struct ShaderInfo { |
144 | QString name; |
145 | QString uri; |
146 | QShaderProgram::ShaderType type; |
147 | QByteArray code; |
148 | }; |
149 | |
150 | struct CameraInfo { |
151 | QString name; |
152 | QString originalName; |
153 | bool perspective; // Orthographic if false |
154 | float zfar; |
155 | float znear; |
156 | |
157 | // Perspective properties |
158 | float aspectRatio; |
159 | float yfov; |
160 | |
161 | // Orthographic properties |
162 | float xmag; |
163 | float ymag; |
164 | |
165 | QCamera *cameraEntity; |
166 | }; |
167 | |
168 | struct LightInfo { |
169 | QString name; |
170 | QString originalName; |
171 | QAbstractLight::Type type; |
172 | QColor color; |
173 | float intensity; |
174 | QVector3D direction; // Spot and diractional lights |
175 | QVector3D attenuation; // Spot and point lights |
176 | float cutOffAngle; // Spot light only |
177 | }; |
178 | |
179 | struct Node { |
180 | QString name; |
181 | QString uniqueName; // generated |
182 | QList<Node *> children; |
183 | }; |
184 | |
185 | void cacheDefaultProperties(PropertyCacheType type); |
186 | void copyTextures(); |
187 | void createShaders(); |
188 | void parseEntities(const Qt3DCore::QEntity *entity, Node *parentNode); |
189 | void parseScene(); |
190 | void parseMaterials(); |
191 | void parseMeshes(); |
192 | void parseCameras(); |
193 | void parseLights(); |
194 | void parseTechniques(QMaterial *material); |
195 | void parseRenderPasses(QTechnique *technique); |
196 | QString addShaderInfo(QShaderProgram::ShaderType type, QByteArray code); |
197 | |
198 | bool saveScene(); |
199 | void delNode(Node *n); |
200 | QString exportNodes(Node *n, QJsonObject &nodes); |
201 | void exportMaterials(QJsonObject &materials); |
202 | void exportGenericProperties(QJsonObject &jsonObj, PropertyCacheType type, QObject *obj); |
203 | void clearOldExport(const QString &dir); |
204 | void exportParameter(QJsonObject &jsonObj, const QString &name, const QVariant &variant); |
205 | void exportRenderStates(QJsonObject &jsonObj, const QRenderPass *pass); |
206 | |
207 | QString newBufferViewName(); |
208 | QString newAccessorName(); |
209 | QString newMeshName(); |
210 | QString newMaterialName(); |
211 | QString newTechniqueName(); |
212 | QString newTextureName(); |
213 | QString newImageName(); |
214 | QString newShaderName(); |
215 | QString newProgramName(); |
216 | QString newNodeName(); |
217 | QString newCameraName(); |
218 | QString newLightName(); |
219 | QString newRenderPassName(); |
220 | QString newEffectName(); |
221 | |
222 | QString textureVariantToUrl(const QVariant &var); |
223 | void setVarToJSonObject(QJsonObject &jsObj, const QString &key, const QVariant &var); |
224 | |
225 | int m_bufferViewCount; |
226 | int m_accessorCount; |
227 | int m_meshCount; |
228 | int m_materialCount; |
229 | int m_techniqueCount; |
230 | int m_textureCount; |
231 | int m_imageCount; |
232 | int m_shaderCount; |
233 | int m_programCount; |
234 | int m_nodeCount; |
235 | int m_cameraCount; |
236 | int m_lightCount; |
237 | int m_renderPassCount; |
238 | int m_effectCount; |
239 | |
240 | Qt3DCore::QEntity *m_sceneRoot; |
241 | QString m_exportName; |
242 | QString m_exportDir; |
243 | |
244 | GltfOptions m_gltfOpts; |
245 | |
246 | QJsonObject m_obj; |
247 | QJsonDocument m_doc; |
248 | |
249 | QByteArray m_buffer; |
250 | QHash<Node *, Qt3DRender::QGeometryRenderer *> m_meshMap; |
251 | QHash<Node *, Qt3DRender::QMaterial *> m_materialMap; |
252 | QHash<Node *, Qt3DRender::QCameraLens *> m_cameraMap; |
253 | QHash<Node *, Qt3DRender::QAbstractLight *> m_lightMap; |
254 | QHash<Node *, Qt3DCore::QTransform *> m_transformMap; |
255 | QHash<QString, QString> m_imageMap; // Original texture URL -> generated filename |
256 | QHash<QString, QString> m_textureIdMap; |
257 | QHash<Qt3DRender::QRenderPass *, QString> m_renderPassIdMap; |
258 | QHash<Qt3DRender::QEffect *, QString> m_effectIdMap; |
259 | QHash<Qt3DRender::QTechnique *, QString> m_techniqueIdMap; |
260 | QHash<PropertyCacheType, QObject *> m_defaultObjectCache; |
261 | QHash<PropertyCacheType, QList<QMetaProperty>> m_propertyCache; |
262 | |
263 | QHash<Qt3DRender::QGeometryRenderer *, MeshInfo> m_meshInfo; |
264 | QHash<Qt3DRender::QMaterial *, MaterialInfo> m_materialInfo; |
265 | QHash<Qt3DRender::QCameraLens *, CameraInfo> m_cameraInfo; |
266 | QHash<Qt3DRender::QAbstractLight *, LightInfo> m_lightInfo; |
267 | QHash<Qt3DRender::QShaderProgram *, ProgramInfo> m_programInfo; |
268 | QList<ShaderInfo> m_shaderInfo; |
269 | |
270 | Node *m_rootNode; |
271 | bool m_rootNodeEmpty; |
272 | QSet<QString> m_exportedFiles; |
273 | }; |
274 | |
275 | } // namespace Qt3DRender |
276 | |
277 | QT_END_NAMESPACE |
278 | |
279 | #endif // GLTFEXPORTER_H |
280 | |