DYTSrouce/src/scene/MeshManager.cpp

84 lines
2.8 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "scene/MeshManager.h"
#include <osgDB/ReadFile>
#ifndef USE_OCEAN
#include <osgEarth/Registry>
#endif
#include "common/SpdLogger.h"
#include "common/RecourceHelper.h"
template<> MeshManager* Singleton<MeshManager>::instance_ = nullptr;
MeshManager::MeshManager(QObject* parent)
: QObject(parent) {
std::string basePath = RecourceHelper::Get().GetBasePath().toStdString();
std::string resoucePath = basePath + "/resources/";
osgDB::Registry::instance()->getDataFilePathList().push_back(resoucePath);
LOG_INFO("osgDB::Registry::instance add resources path: {}", resoucePath);
std::string effectPath = resoucePath + "effects/";
osgDB::Registry::instance()->getDataFilePathList().push_back(effectPath);
LOG_INFO("osgDB::Registry::instance add effectPath path: {}", effectPath);
std::string ocShaderPath = resoucePath + "shaders/";
osgDB::Registry::instance()->getDataFilePathList().push_back(ocShaderPath);
LOG_INFO("osgDB::Registry::instance add ocShaderPath path: {}", ocShaderPath);
std::string texturesPath = resoucePath + "textures/";
osgDB::Registry::instance()->getDataFilePathList().push_back(texturesPath);
LOG_INFO("osgDB::Registry::instance add texturesPath path: {}", texturesPath);
std::string modelPath = resoucePath + "model/";
osgDB::Registry::instance()->getDataFilePathList().push_back(modelPath);
LOG_INFO("osgDB::Registry::instance add model path: {}", modelPath);
}
MeshManager::~MeshManager(void) {
}
void MeshManager::OnDestory() {
2025-11-13 14:13:40 +00:00
// Defer destruction of cached nodes to process exit to avoid
// shutdown-order crashes inside OSG ref_ptr/Drawable destructors.
// We intentionally leak the cache at exit; acceptable for application shutdown.
LOG_INFO("MeshManager::OnDestory - deferring cache release to process exit");
static NodeMap* leakedCache = nullptr;
if (!leakedCache) leakedCache = new NodeMap();
leakedCache->swap(nodes_);
2025-01-04 04:12:51 +00:00
}
osg::MatrixTransform* MeshManager::ReadNode(const std::string& file) {
LOG_INFO("load node:{}", file);
const auto iter = nodes_.find(file);
if (nodes_.end() != iter) {
2025-11-13 14:13:40 +00:00
// Guard against previously cleared cache entries
osg::ref_ptr<osg::Node> cached = iter->second;
if (cached.valid()) {
osg::MatrixTransform* mt = new osg::MatrixTransform;
mt->addChild(cached.get());
return mt;
} else {
// Remove invalid entry and fall through to reload
nodes_.erase(iter);
}
2025-01-04 04:12:51 +00:00
}
osg::Node* node = osgDB::readNodeFile(file);
if (nullptr == node) {
LOG_WARN("load file failed, {}", file);
return nullptr;
}
#ifndef USE_OCEAN
osgEarth::Registry::shaderGenerator().run(node);
#endif
nodes_[file] = node;
osg::MatrixTransform* mt = new osg::MatrixTransform;
mt->addChild(node);
return mt;
}