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