mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
98 lines
2.8 KiB
C++
98 lines
2.8 KiB
C++
#include "Cubed/render/model_manager.hpp"
|
|
|
|
#include "Cubed/tools/cubed_assert.hpp"
|
|
#include "Cubed/tools/log.hpp"
|
|
#include "Cubed/tools/name_space.hpp"
|
|
namespace Cubed {
|
|
|
|
ModelManager& ModelManager::instance() {
|
|
static ModelManager inst;
|
|
return inst;
|
|
}
|
|
|
|
[[nodiscard]]
|
|
ModelManager::Handle ModelManager::model(const std::string& model_name) {
|
|
return instance().get_model(model_name);
|
|
}
|
|
[[nodiscard]]
|
|
ModelManager::Handle ModelManager::model(ModelID id) {
|
|
return instance().get_model(id);
|
|
}
|
|
|
|
ModelManager::ModelManager() { init(); }
|
|
|
|
ModelManager::~ModelManager() {}
|
|
|
|
void ModelManager::init() {
|
|
|
|
};
|
|
|
|
ModelManager::Handle ModelManager::get_model(const std::string& model_name) {
|
|
return get_model(get_model_id(model_name));
|
|
}
|
|
|
|
ModelManager::Handle ModelManager::get_model(ModelID id) {
|
|
|
|
ModelMap::const_accessor cacc;
|
|
if (m_models.find(cacc, id)) {
|
|
return {cacc->second, cacc->first};
|
|
}
|
|
return load_model(get_model_name(id));
|
|
}
|
|
|
|
const ModelNode& get_model(const std::string& model_name);
|
|
const ModelNode& get_model(ModelID id);
|
|
ModelID ModelManager::get_model_id(const std::string& name) {
|
|
IDMap::const_accessor cacc;
|
|
if (m_id_map.find(cacc, name)) {
|
|
return cacc->second;
|
|
}
|
|
return load_model(name).id;
|
|
}
|
|
|
|
const std::string& ModelManager::get_model_name(ModelID id) {
|
|
NameMap::const_accessor cacc;
|
|
if (m_name_map.find(cacc, id)) {
|
|
return cacc->second;
|
|
}
|
|
ASSERT_MSG(false, std::format("ModelManager: Can't find {}", id));
|
|
static std::string n = "";
|
|
return n;
|
|
}
|
|
|
|
ModelManager::Handle ModelManager::load_model(std::string_view model_name) {
|
|
auto p = model_name.find(':');
|
|
if (p == std::string::npos) {
|
|
Logger::error("Can't Parse Model name {}", model_name);
|
|
ASSERT(false);
|
|
}
|
|
auto space = parse_namespace(model_name);
|
|
if (space.size() < 2) {
|
|
Logger::error("Can't Parse Model name {}", model_name);
|
|
ASSERT(false);
|
|
}
|
|
std::string path;
|
|
if (space[0] == "cubed") {
|
|
path = std::format("{}model/creature/{}/{}.glb", ASSETS_PATH, space[1],
|
|
space[1]);
|
|
} else {
|
|
path = std::format("./{}/model/creature/{}/{}.glb", space[0], space[1],
|
|
space[1]);
|
|
}
|
|
auto model = m_loader.load(path);
|
|
ModelMap::accessor acc;
|
|
if (m_models.insert(acc, m_next++)) {
|
|
acc->second = std::move(model);
|
|
} else {
|
|
Logger::error("Can't Insert Model {}", model_name);
|
|
ModelMap::const_accessor cacc;
|
|
if (m_models.find(cacc, get_model_id(std::string(model_name)))) {
|
|
return {cacc->second, cacc->first};
|
|
}
|
|
}
|
|
m_id_map.emplace(model_name, acc->first);
|
|
m_name_map.emplace(acc->first, model_name);
|
|
return {acc->second, acc->first};
|
|
}
|
|
|
|
} // namespace Cubed
|