diff options
author | Mike Krus <mike.krus@kdab.com> | 2017-04-10 16:26:57 +0100 |
---|---|---|
committer | Mike Krus <mike.krus@kdab.com> | 2017-05-21 11:40:00 +0000 |
commit | b3302278a1e6094b17d4c7bd353e7717ee050bb5 (patch) | |
tree | 80f7c626805f42561e6ea17db87aafa960592031 | |
parent | 9f8fc3717508a6b8045f65e6c820692fb67dc450 (diff) |
Add support to load data from memory for scene loaders
Change-Id: I666897ae54441a226fa6f8a7f8cdfc1a576f3494
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r-- | src/plugins/sceneparsers/assimp/assimpimporter.cpp | 58 | ||||
-rw-r--r-- | src/plugins/sceneparsers/assimp/assimpimporter.h | 6 | ||||
-rw-r--r-- | src/plugins/sceneparsers/gltf/gltfimporter.cpp | 41 | ||||
-rw-r--r-- | src/plugins/sceneparsers/gltf/gltfimporter.h | 5 | ||||
-rw-r--r-- | src/render/io/qsceneimporter_p.h | 3 |
5 files changed, 84 insertions, 29 deletions
diff --git a/src/plugins/sceneparsers/assimp/assimpimporter.cpp b/src/plugins/sceneparsers/assimp/assimpimporter.cpp index d5fc113e3..4370ffe2b 100644 --- a/src/plugins/sceneparsers/assimp/assimpimporter.cpp +++ b/src/plugins/sceneparsers/assimp/assimpimporter.cpp @@ -375,17 +375,15 @@ AssimpImporter::~AssimpImporter() } /*! - * Returns \c true if the provided \a path has a suffix supported + * Returns \c true if the extensions are supported * by the Assimp Assets importer. */ -bool AssimpImporter::isAssimpPath(const QString &path) +bool AssimpImporter::areAssimpExtensions(const QStringList &extensions) { - QFileInfo fileInfo(path); - - if (!fileInfo.exists() || - !AssimpImporter::assimpSupportedFormatsList.contains(fileInfo.suffix().toLower())) - return false; - return true; + for (const auto ext : qAsConst(extensions)) + if (AssimpImporter::assimpSupportedFormatsList.contains(ext.toLower())) + return true; + return false; } /*! @@ -405,13 +403,21 @@ void AssimpImporter::setSource(const QUrl &source) } /*! + * Sets the \a source used by the parser to load the asset file. + * If the file is valid, this will trigger parsing of the file. + */ +void AssimpImporter::setData(const QByteArray &data, const QString &basePath) +{ + readSceneData(data, basePath); +} + +/*! * Returns \c true if the extension of \a source is supported by * the assimp parser. */ -bool AssimpImporter::isFileTypeSupported(const QUrl &source) const +bool AssimpImporter::areFileTypesSupported(const QStringList &extensions) const { - const QString path = QUrlHelper::urlToLocalFileOrQrc(source); - return AssimpImporter::isAssimpPath(path); + return AssimpImporter::areAssimpExtensions(extensions); } /*! @@ -603,6 +609,36 @@ void AssimpImporter::readSceneFile(const QString &path) } /*! + * Reads the scene file pointed by \a path and launches the parsing of + * the scene using Assimp, after having cleaned up previously saved values + * from eventual previous parsings. + */ +void AssimpImporter::readSceneData(const QByteArray& data, const QString &basePath) +{ + cleanup(); + + m_scene = new SceneImporter(); + + // SET THIS TO REMOVE POINTS AND LINES -> HAVE ONLY TRIANGLES + m_scene->m_importer->SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE|aiPrimitiveType_POINT); + // SET CUSTOM FILE HANDLER TO HANDLE FILE READING THROUGH QT (RESOURCES, SOCKET ...) + m_scene->m_importer->SetIOHandler(new AssimpHelper::AssimpIOSystem()); + + // type and aiProcess_Triangulate discompose polygons with more than 3 points in triangles + // aiProcess_SortByPType makes sure that meshes data are triangles + m_scene->m_aiScene = m_scene->m_importer->ReadFileFromMemory(data.data(), data.size(), + aiProcess_SortByPType| + aiProcess_Triangulate| + aiProcess_GenSmoothNormals| + aiProcess_FlipUVs); + if (m_scene->m_aiScene == nullptr) { + qCWarning(AssimpImporterLog) << "Assimp scene import failed"; + return ; + } + parse(); +} + +/*! * Cleans the various dictionaries holding the scene's information. */ void AssimpImporter::cleanup() diff --git a/src/plugins/sceneparsers/assimp/assimpimporter.h b/src/plugins/sceneparsers/assimp/assimpimporter.h index e37950981..3c0a41124 100644 --- a/src/plugins/sceneparsers/assimp/assimpimporter.h +++ b/src/plugins/sceneparsers/assimp/assimpimporter.h @@ -100,17 +100,19 @@ public: // SceneParserInterface interface void setSource(const QUrl& source) Q_DECL_OVERRIDE; - bool isFileTypeSupported(const QUrl &source) const Q_DECL_OVERRIDE; + void setData(const QByteArray& data, const QString &basePath) Q_DECL_OVERRIDE; + bool areFileTypesSupported(const QStringList &extensions) const Q_DECL_OVERRIDE; Qt3DCore::QEntity *scene(const QString &id = QString()) Q_DECL_OVERRIDE; Qt3DCore::QEntity *node(const QString &id) Q_DECL_OVERRIDE; private: - static bool isAssimpPath(const QString &path); + static bool areAssimpExtensions(const QStringList &extensions); static QStringList assimpSupportedFormats(); Qt3DCore::QEntity *node(aiNode *node); void readSceneFile(const QString &file); + void readSceneData(const QByteArray& data, const QString &basePath); void cleanup(); void parse(); diff --git a/src/plugins/sceneparsers/gltf/gltfimporter.cpp b/src/plugins/sceneparsers/gltf/gltfimporter.cpp index 78e230969..a94a4f307 100644 --- a/src/plugins/sceneparsers/gltf/gltfimporter.cpp +++ b/src/plugins/sceneparsers/gltf/gltfimporter.cpp @@ -314,13 +314,30 @@ void GLTFImporter::setSource(const QUrl &source) } /*! - * Returns true if the extension of \a path is supported by the + * Sets the \a path used by the parser to load the scene file. + * If the file is valid, parsing is automatically triggered. + */ +void GLTFImporter::setData(const QByteArray& data, const QString &basePath) +{ + QJsonDocument sceneDocument = QJsonDocument::fromBinaryData(data); + if (sceneDocument.isNull()) + sceneDocument = QJsonDocument::fromJson(data); + + if (Q_UNLIKELY(!setJSON(sceneDocument))) { + qCWarning(GLTFImporterLog, "not a JSON document"); + return; + } + + setBasePath(basePath); +} + +/*! + * Returns true if the extensions are supported by the * GLTF parser. */ -bool GLTFImporter::isFileTypeSupported(const QUrl &source) const +bool GLTFImporter::areFileTypesSupported(const QStringList &extensions) const { - const QString path = QUrlHelper::urlToLocalFileOrQrc(source); - return GLTFImporter::isGLTFPath(path); + return GLTFImporter::isGLTFSupported(extensions); } Qt3DCore::QEntity* GLTFImporter::node(const QString &id) @@ -570,16 +587,14 @@ GLTFImporter::AccessorData::AccessorData(const QJsonObject &json) stride = byteStride.toInt(); } -bool GLTFImporter::isGLTFPath(const QString& path) +bool GLTFImporter::isGLTFSupported(const QStringList &extensions) { - QFileInfo finfo(path); - if (!finfo.exists()) - return false; - - // might need to detect other things in the future, but would - // prefer to avoid doing a full parse. - QString suffix = finfo.suffix().toLower(); - return suffix == QLatin1String("json") || suffix == QLatin1String("gltf") || suffix == QLatin1String("qgltf"); + for (auto suffix: qAsConst(extensions)) { + suffix = suffix.toLower(); + if (suffix == QLatin1String("json") || suffix == QLatin1String("gltf") || suffix == QLatin1String("qgltf")) + return true; + } + return false; } void GLTFImporter::renameFromJson(const QJsonObject &json, QObject * const object) diff --git a/src/plugins/sceneparsers/gltf/gltfimporter.h b/src/plugins/sceneparsers/gltf/gltfimporter.h index 975a9ef01..8a8e1b3ee 100644 --- a/src/plugins/sceneparsers/gltf/gltfimporter.h +++ b/src/plugins/sceneparsers/gltf/gltfimporter.h @@ -98,7 +98,8 @@ public: // SceneParserInterface interface void setSource(const QUrl &source) Q_DECL_FINAL; - bool isFileTypeSupported(const QUrl &source) const Q_DECL_FINAL; + void setData(const QByteArray& data, const QString &basePath) Q_DECL_FINAL; + bool areFileTypesSupported(const QStringList &extensions) const Q_DECL_FINAL; Qt3DCore::QEntity *node(const QString &id) Q_DECL_FINAL; Qt3DCore::QEntity *scene(const QString &id = QString()) Q_DECL_FINAL; @@ -139,7 +140,7 @@ private: int stride; }; - static bool isGLTFPath(const QString &path); + static bool isGLTFSupported(const QStringList &extensions); static void renameFromJson(const QJsonObject& json, QObject * const object ); static bool hasStandardUniformNameFromSemantic(const QString &semantic); static QString standardAttributeNameFromSemantic(const QString &semantic); diff --git a/src/render/io/qsceneimporter_p.h b/src/render/io/qsceneimporter_p.h index e76eb8780..8f83231c3 100644 --- a/src/render/io/qsceneimporter_p.h +++ b/src/render/io/qsceneimporter_p.h @@ -86,7 +86,8 @@ public: virtual ~QSceneImporter(); virtual void setSource(const QUrl &source) = 0; - virtual bool isFileTypeSupported(const QUrl &source) const = 0; + virtual void setData(const QByteArray& data, const QString &basePath) = 0; + virtual bool areFileTypesSupported(const QStringList &extensions) const = 0; virtual Qt3DCore::QEntity *scene(const QString &id = QString()) = 0; virtual Qt3DCore::QEntity *node(const QString &id) = 0; |