summaryrefslogtreecommitdiffstats
path: root/src/plugins/sceneparsers/gltfexport/gltfexporter.h
blob: 254bc2e2a9685b4534e3890d5eb40cd503725723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef GLTFEXPORTER_H
#define GLTFEXPORTER_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of other Qt classes.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qhash.h>
#include <QtCore/qset.h>
#include <QtGui/qvector3d.h>

#include <Qt3DRender/qabstractlight.h>
#include <Qt3DRender/qshaderprogram.h>

#include <private/qsceneexporter_p.h>

QT_BEGIN_NAMESPACE

class QByteArray;

namespace Qt3DCore {
class QEntity;
class QTransform;
}

namespace Qt3DRender {

class QCamera;
class QCameraLens;
class QMaterial;
class QGeometryRenderer;
class QTechnique;
class QRenderPass;
class QEffect;

Q_DECLARE_LOGGING_CATEGORY(GLTFExporterLog)

class GLTFExporter : public QSceneExporter
{
    Q_OBJECT

public:
    GLTFExporter();
    ~GLTFExporter();

    bool exportScene(Qt3DCore::QEntity *sceneRoot, const QString &outDir,
                     const QString &exportName, const QVariantHash &options) final;

    struct GltfOptions {
        bool compactJson;
    };

private:
    enum PropertyCacheType {
        TypeNone = 0,
        TypeConeMesh,
        TypeCuboidMesh,
        TypeCylinderMesh,
        TypePlaneMesh,
        TypeSphereMesh,
        TypeTorusMesh
    };

    struct MeshInfo {
        struct BufferView {
            BufferView() : bufIndex(0), offset(0), length(0), componentType(0), target(0) { }
            QString name;
            uint bufIndex;
            uint offset;
            uint length;
            uint componentType;
            uint target;
        };
        QList<BufferView> views;
        struct Accessor {
            Accessor() : offset(0), stride(0), count(0), componentType(0) { }
            QString name;
            QString usage;
            QString bufferView;
            uint offset;
            uint stride;
            uint count;
            uint componentType;
            QString type;
        };
        QList<Accessor> accessors;
        QString name; // generated
        QString originalName; // may be empty
        QString materialName;
        Qt3DRender::QGeometryRenderer *meshComponent;
        PropertyCacheType meshType;
        QString meshTypeStr;
    };

    struct MaterialInfo {
        enum MaterialType {
            TypeCustom,
            TypePhong,
            TypePhongAlpha,
            TypeDiffuseMap,
            TypeDiffuseSpecularMap,
            TypeNormalDiffuseMap,
            TypeNormalDiffuseMapAlpha,
            TypeNormalDiffuseSpecularMap,
            TypeGooch,
            TypePerVertex
        };

        QString name;
        QString originalName;
        MaterialType type;

        // These are only used for default materials
        QHash<QString, QColor> colors;
        QHash<QString, QString> textures;
        QHash<QString, QVariant> values;
        std::vector<int> blendArguments;
        std::vector<int> blendEquations;
    };

    struct ProgramInfo {
        QString name;
        QString vertexShader;
        QString tessellationControlShader;
        QString tessellationEvaluationShader;
        QString geometryShader;
        QString fragmentShader;
        QString computeShader;
    };

    struct ShaderInfo {
        QString name;
        QString uri;
        QShaderProgram::ShaderType type;
        QByteArray code;
    };

    struct CameraInfo {
        QString name;
        QString originalName;
        bool perspective; // Orthographic if false
        float zfar;
        float znear;

        // Perspective properties
        float aspectRatio;
        float yfov;

        // Orthographic properties
        float xmag;
        float ymag;

        QCamera *cameraEntity;
    };

    struct LightInfo {
        QString name;
        QString originalName;
        QAbstractLight::Type type;
        QColor color;
        float intensity;
        QVector3D direction; // Spot and diractional lights
        QVector3D attenuation; // Spot and point lights
        float cutOffAngle; // Spot light only
    };

    struct Node {
        QString name;
        QString uniqueName; // generated
        QList<Node *> children;
    };

    void cacheDefaultProperties(PropertyCacheType type);
    void copyTextures();
    void createShaders();
    void parseEntities(const Qt3DCore::QEntity *entity, Node *parentNode);
    void parseScene();
    void parseMaterials();
    void parseMeshes();
    void parseCameras();
    void parseLights();
    void parseTechniques(QMaterial *material);
    void parseRenderPasses(QTechnique *technique);
    QString addShaderInfo(QShaderProgram::ShaderType type, QByteArray code);

    bool saveScene();
    void delNode(Node *n);
    QString exportNodes(Node *n, QJsonObject &nodes);
    void exportMaterials(QJsonObject &materials);
    void exportGenericProperties(QJsonObject &jsonObj, PropertyCacheType type, QObject *obj);
    void clearOldExport(const QString &dir);
    void exportParameter(QJsonObject &jsonObj, const QString &name, const QVariant &variant);
    void exportRenderStates(QJsonObject &jsonObj, const QRenderPass *pass);

    QString newBufferViewName();
    QString newAccessorName();
    QString newMeshName();
    QString newMaterialName();
    QString newTechniqueName();
    QString newTextureName();
    QString newImageName();
    QString newShaderName();
    QString newProgramName();
    QString newNodeName();
    QString newCameraName();
    QString newLightName();
    QString newRenderPassName();
    QString newEffectName();

    QString textureVariantToUrl(const QVariant &var);
    void setVarToJSonObject(QJsonObject &jsObj, const QString &key, const QVariant &var);

    int m_bufferViewCount;
    int m_accessorCount;
    int m_meshCount;
    int m_materialCount;
    int m_techniqueCount;
    int m_textureCount;
    int m_imageCount;
    int m_shaderCount;
    int m_programCount;
    int m_nodeCount;
    int m_cameraCount;
    int m_lightCount;
    int m_renderPassCount;
    int m_effectCount;

    Qt3DCore::QEntity *m_sceneRoot;
    QString m_exportName;
    QString m_exportDir;

    GltfOptions m_gltfOpts;

    QJsonObject m_obj;
    QJsonDocument m_doc;

    QByteArray m_buffer;
    QHash<Node *, Qt3DRender::QGeometryRenderer *> m_meshMap;
    QHash<Node *, Qt3DRender::QMaterial *> m_materialMap;
    QHash<Node *, Qt3DRender::QCameraLens *> m_cameraMap;
    QHash<Node *, Qt3DRender::QAbstractLight *> m_lightMap;
    QHash<Node *, Qt3DCore::QTransform *> m_transformMap;
    QHash<QString, QString> m_imageMap; // Original texture URL -> generated filename
    QHash<QString, QString> m_textureIdMap;
    QHash<Qt3DRender::QRenderPass *, QString> m_renderPassIdMap;
    QHash<Qt3DRender::QEffect *, QString> m_effectIdMap;
    QHash<Qt3DRender::QTechnique *, QString> m_techniqueIdMap;
    QHash<PropertyCacheType, QObject *> m_defaultObjectCache;
    QHash<PropertyCacheType, QList<QMetaProperty>> m_propertyCache;

    QHash<Qt3DRender::QGeometryRenderer *, MeshInfo> m_meshInfo;
    QHash<Qt3DRender::QMaterial *, MaterialInfo> m_materialInfo;
    QHash<Qt3DRender::QCameraLens *, CameraInfo> m_cameraInfo;
    QHash<Qt3DRender::QAbstractLight *, LightInfo> m_lightInfo;
    QHash<Qt3DRender::QShaderProgram *, ProgramInfo> m_programInfo;
    QList<ShaderInfo> m_shaderInfo;

    Node *m_rootNode;
    bool m_rootNodeEmpty;
    QSet<QString> m_exportedFiles;
};

} // namespace Qt3DRender

QT_END_NAMESPACE

#endif // GLTFEXPORTER_H