diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-04-28 15:23:24 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-04-28 15:23:24 +0000 |
| commit | ff0421c084c037504a641b176d2ca077676de207 (patch) | |
| tree | 2c87f5bba51ba026c03f50a6c7842750509af1fa /AssetLoader/src/GLTFLoader.cpp | |
| parent | Minor update to GLTF loader (diff) | |
| download | DiligentTools-ff0421c084c037504a641b176d2ca077676de207.tar.gz DiligentTools-ff0421c084c037504a641b176d2ca077676de207.zip | |
Updated GLTF loader to use Diligent file system functions
Diffstat (limited to 'AssetLoader/src/GLTFLoader.cpp')
| -rw-r--r-- | AssetLoader/src/GLTFLoader.cpp | 70 |
1 files changed, 60 insertions, 10 deletions
diff --git a/AssetLoader/src/GLTFLoader.cpp b/AssetLoader/src/GLTFLoader.cpp index b32fec1..da6062e 100644 --- a/AssetLoader/src/GLTFLoader.cpp +++ b/AssetLoader/src/GLTFLoader.cpp @@ -30,6 +30,8 @@ #include "CommonlyUsedStates.h" #include "DataBlobImpl.h" #include "Image.h" +#include "FileSystem.h" +#include "FileWrapper.h" #define TINYGLTF_IMPLEMENTATION #define TINYGLTF_NO_STB_IMAGE @@ -830,18 +832,21 @@ void Model::LoadAnimations(const tinygltf::Model& gltf_model) } } +namespace Callbacks +{ + namespace { -bool LoadImageDataFunction(tinygltf::Image* gltf_image, - const int gltf_image_idx, - std::string* error, - std::string* warning, - int req_width, - int req_height, - const unsigned char* image_data, - int size, - void* user_data) +bool LoadImageData(tinygltf::Image* gltf_image, + const int gltf_image_idx, + std::string* error, + std::string* warning, + int req_width, + int req_height, + const unsigned char* image_data, + int size, + void* user_data) { (void)user_data; (void)warning; @@ -953,13 +958,58 @@ bool LoadImageDataFunction(tinygltf::Image* gltf_image, return true; } +bool FileExists(const std::string& abs_filename, void*) +{ + return FileSystem::FileExists(abs_filename.c_str()); } +bool ReadWholeFile(std::vector<unsigned char>* out, + std::string* err, + const std::string& filepath, + void*) +{ + FileWrapper pFile(filepath.c_str(), EFileAccessMode::Read); + if (!pFile) + { + if (err) + { + (*err) += FormatString("Unable to open file ", filepath, "\n"); + } + return false; + } + + auto size = pFile->GetSize(); + if (size == 0) + { + if (err) + { + (*err) += FormatString("File is empty: ", filepath, "\n"); + } + return false; + } + + out->resize(size); + pFile->Read(out->data(), size); + + return true; +} + +} // namespace + +} // namespace Callbacks + void Model::LoadFromFile(IRenderDevice* pDevice, IDeviceContext* pContext, const std::string &filename, float scale) { tinygltf::Model gltf_model; tinygltf::TinyGLTF gltf_context; - gltf_context.SetImageLoader(LoadImageDataFunction, this); + gltf_context.SetImageLoader(Callbacks::LoadImageData, this); + tinygltf::FsCallbacks fsCallbacks = {}; + fsCallbacks.ExpandFilePath = tinygltf::ExpandFilePath; + fsCallbacks.FileExists = Callbacks::FileExists; + fsCallbacks.ReadWholeFile = Callbacks::ReadWholeFile; + fsCallbacks.WriteWholeFile = tinygltf::WriteWholeFile; + fsCallbacks.user_data = this; + gltf_context.SetFsCallbacks(fsCallbacks); bool binary = false; size_t extpos = filename.rfind('.', filename.length()); |
