Commit 632acdba authored by Lázár Bence Kis's avatar Lázár Bence Kis
Browse files

WIP

parent 38a95fa1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ namespace lfs {

Loader::Request::Request(com::Folder* const folder, std::string const& name)
    : disk_path{ osi::data()->dir() }
    , ctx_path{ folder->path(data_root()) }
    , ctx_path{ folder == data_root() ? com::ContextPath{} : folder->path(data_root()) }
{
    ctx_path.push_back(name);
    for (std::string const& path_elem : ctx_path)
@@ -57,7 +57,7 @@ ResourceHandle* Loader::load(com::ContextPath const& path_name, bool const top_p
ResourceHandle* Loader::load(std::string const& name, com::ContextPath const& path, bool const top_priority)
{
    com::Folder* const f = data_root()->push_back_folders(path);
    ASSUMPTION(f->is_under(data_root()));
    ASSUMPTION(f == data_root() || f->is_under(data_root()));
    com::ContextItem* const item = f->find(name);
    if (item == nullptr)
    {
+28 −2
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <tiny_gltf.h>
#include <lfs/index.hpp>

namespace utils {

@@ -52,19 +53,44 @@ void insert_gltf_node(const tinygltf::Model& model, std::unordered_map<int, com:
}

void import_gltf(com::Folder* root, const std::string& path, const std::string& name, gfx::Material* material_override, vec3 position, quat rotation, const com::ContextPath &skeleton_path) {
    tinygltf::FsCallbacks callbacks{};
    callbacks.ReadWholeFile = [](std::vector<unsigned char> *out,
                                std::string *err,
                                const std::string &path,
                                void *) -> bool {
        com::ContextPath ctxPath;
        boost::split(ctxPath, path, boost::is_any_of("/"));

        const auto& data = reinterpret_cast<const std::vector<char>&>(lfs::loader()->load_now(ctxPath)->data());
        if (data.empty()) {
            if (err) *err = "Resource not found: " + path;
            return false;
        }
        out->assign(data.begin(), data.end());
        return true;
    };
    callbacks.WriteWholeFile = nullptr;  // nem kell
    callbacks.user_data = nullptr;

    tinygltf::Model model;
    tinygltf::TinyGLTF loader;
    loader.SetFsCallbacks(callbacks);

    std::string err;
    std::string warn;

    com::ContextPath ctxPath;
    boost::split(ctxPath, path, boost::is_any_of("/"));
    std::string extension = path.substr(path.find_last_of(".") + 1);

    bool ret;
    if (extension == "gltf") {
        ret = loader.LoadASCIIFromFile(&model, &err, &warn, path);
        const auto& data = reinterpret_cast<const std::vector<char>&>(lfs::loader()->load_now(ctxPath)->data());
        ret = loader.LoadASCIIFromString(&model, &err, &warn, data.data(), data.size(), "data/models");
    }
    else if (extension == "glb") {
        ret = loader.LoadBinaryFromFile(&model, &err, &warn, path); // for binary glTF(.glb)
        const auto& data = reinterpret_cast<const std::vector<unsigned char>&>(lfs::loader()->load_now(ctxPath)->data());
        ret = loader.LoadBinaryFromMemory(&model, &err, &warn, data.data(), data.size()); // for binary glTF(.glb)
    }

    if (!warn.empty()) {