#include "scene/MeshManager.h" #include #ifndef USE_OCEAN #include #endif #include "common/SpdLogger.h" #include "common/RecourceHelper.h" template<> MeshManager* Singleton::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() { // 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_); } osg::MatrixTransform* MeshManager::ReadNode(const std::string& file) { LOG_INFO("load node:{}", file); const auto iter = nodes_.find(file); if (nodes_.end() != iter) { // Guard against previously cleared cache entries osg::ref_ptr 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); } } 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; }