summaryrefslogtreecommitdiffstats
path: root/AssetLoader/src/GLTFLoader.cpp
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2021-03-31 13:53:23 +0000
committers-ol <s+removethis@s-ol.nu>2021-03-31 15:49:39 +0000
commit85909b2a9fc470d0f425fed5962bb8fc5a5fde57 (patch)
tree1f99551d1cace6afe739d0b2f1644ad95cabcd5c /AssetLoader/src/GLTFLoader.cpp
parentC API for GLTFLoader (diff)
downloadDiligentTools-85909b2a9fc470d0f425fed5962bb8fc5a5fde57.tar.gz
DiligentTools-85909b2a9fc470d0f425fed5962bb8fc5a5fde57.zip
implement loading GLTF files from memory
Diffstat (limited to 'AssetLoader/src/GLTFLoader.cpp')
-rw-r--r--AssetLoader/src/GLTFLoader.cpp90
1 files changed, 81 insertions, 9 deletions
diff --git a/AssetLoader/src/GLTFLoader.cpp b/AssetLoader/src/GLTFLoader.cpp
index 1c546f7..9fabac2 100644
--- a/AssetLoader/src/GLTFLoader.cpp
+++ b/AssetLoader/src/GLTFLoader.cpp
@@ -250,7 +250,13 @@ Model::Model(IRenderDevice* pDevice,
IDeviceContext* pContext,
const IGLTFModelCreateInfo& CI)
{
- LoadFromFile(pDevice, pContext, CI);
+ if (CI.FileName) {
+ LoadFromFile(pDevice, pContext, CI);
+ } else if (CI.Data) {
+ LoadFromMemory(pDevice, pContext, CI);
+ } else {
+ LOG_ERROR_AND_THROW("Either File Name or Data must be set");
+ }
}
Model::~Model()
@@ -1753,14 +1759,85 @@ void Model::LoadFromFile(IRenderDevice* pDevice,
LOG_WARNING_MESSAGE("Loaded gltf file ", filename, " with the following warning:", warning);
}
- LoadTextureSamplers(pDevice, gltf_model);
- LoadTextures(pDevice, gltf_model, LoaderData.BaseDir, pTextureCache, pResourceMgr);
- LoadMaterials(gltf_model);
+ LoadScene(pDevice, gltf_model, LoaderData.BaseDir, pTextureCache, pResourceMgr, CI);
+
+ if (pContext != nullptr)
+ {
+ PrepareGPUResources(pDevice, pContext);
+ }
+}
+
+void Model::LoadFromMemory(IRenderDevice* pDevice,
+ IDeviceContext* pContext,
+ const IGLTFModelCreateInfo& CI)
+{
+ if (CI.Data == nullptr || CI.DataSize == 0)
+ LOG_ERROR_AND_THROW("Data must not be empty");
+ auto* const pTextureCache = CI.pTextureCache;
+ auto* const pResourceMgr = CI.pCacheInfo != nullptr ? CI.pCacheInfo->pResourceMgr : nullptr;
+ if (CI.pTextureCache != nullptr && pResourceMgr != nullptr)
+ LOG_WARNING_MESSAGE("Texture cache is ignored when resource manager is used");
+
+ Callbacks::ImageLoaderData LoaderData{pTextureCache, pResourceMgr};
+ LoaderData.BaseDir = "";
+
+ tinygltf::TinyGLTF gltf_context;
+ gltf_context.SetImageLoader(Callbacks::LoadImageData, &LoaderData);
+
+ bool binary;
+ switch (CI.FileType) {
+ case GLTF_FILE_TYPE_UNKNOWN:
+ LOG_ERROR_AND_THROW("FileType must not be UNKNOWN when loading from memory");
+ case GLTF_FILE_TYPE_ASCII:
+ binary = false;
+ break;
+ case GLTF_FILE_TYPE_BINARY:
+ binary = true;
+ break;
+ }
+
+ std::string error;
+ std::string warning;
+ tinygltf::Model gltf_model;
+
+ bool modelLoaded = false;
+ if (binary)
+ modelLoaded = gltf_context.LoadBinaryFromMemory(&gltf_model, &error, &warning, (const unsigned char*)CI.Data, CI.DataSize);
+ else
+ modelLoaded = gltf_context.LoadASCIIFromString(&gltf_model, &error, &warning, (const char*)CI.Data, CI.DataSize, "");
+ if (!modelLoaded)
+ {
+ LOG_ERROR_AND_THROW("Failed to load gltf file from memory: ", error);
+ }
+ if (!warning.empty())
+ {
+ LOG_WARNING_MESSAGE("Loaded gltf file from memory with the following warning:", warning);
+ }
+
+ LoadScene(pDevice, gltf_model, "", pTextureCache, pResourceMgr, CI);
+
+ if (pContext != nullptr)
+ {
+ PrepareGPUResources(pDevice, pContext);
+ }
+}
+
+void Model::LoadScene(IRenderDevice* pDevice,
+ const tinygltf::Model& gltf_model,
+ const std::string& BaseDir,
+ GLTF_TextureCacheType* pTextureCache,
+ ResourceManager* pResourceMgr,
+ const IGLTFModelCreateInfo& CI)
+{
std::vector<Uint32> IndexData;
std::vector<VertexBasicAttribs> VertexBasicData;
std::vector<VertexSkinAttribs> VertexSkinData;
+ LoadTextureSamplers(pDevice, gltf_model);
+ LoadTextures(pDevice, gltf_model, BaseDir, pTextureCache, pResourceMgr);
+ LoadMaterials(gltf_model);
+
// TODO: scene handling with no default scene
const tinygltf::Scene& scene = gltf_model.scenes[gltf_model.defaultScene > -1 ? gltf_model.defaultScene : 0];
for (size_t i = 0; i < scene.nodes.size(); i++)
@@ -1854,11 +1931,6 @@ void Model::LoadFromFile(IRenderDevice* pDevice,
CreateBuffer(GLTF_BUFFER_ID_INDEX, IndexData.data(), IndexData.size() * sizeof(IndexData[0]),
BIND_INDEX_BUFFER, "GLTF index buffer");
}
-
- if (pContext != nullptr)
- {
- PrepareGPUResources(pDevice, pContext);
- }
}
void Model::CalculateBoundingBox(Node* node, const Node* parent)