diff --git a/components/nifogre/ogre_nif_loader.cpp b/components/nifogre/ogre_nif_loader.cpp index 99ccb7fb1..079e6425b 100644 --- a/components/nifogre/ogre_nif_loader.cpp +++ b/components/nifogre/ogre_nif_loader.cpp @@ -182,20 +182,6 @@ static bool createSkeleton(const std::string &name, const std::string &group, Ni }; -NIFLoader::LoaderMap NIFLoader::sLoaders; - -void NIFLoader::warn(const std::string &msg) -{ - std::cerr << "NIFLoader: Warn: " << msg << std::endl; -} - -void NIFLoader::fail(const std::string &msg) -{ - std::cerr << "NIFLoader: Fail: "<< msg << std::endl; - abort(); -} - - // Conversion of blend / test mode from NIF -> OGRE. // Not in use yet, so let's comment it out. /* @@ -374,51 +360,81 @@ void NIFLoader::createMaterial(const Ogre::String &name, #endif -void NIFLoader::loadResource(Ogre::Resource *resource) +class NIFMeshLoader : Ogre::ManualResourceLoader { - warn("Found no records in NIF for "+resource->getName()); -} + std::string mName; + std::string mGroup; + std::string mShapeName; + bool mHasSkel; + void warn(const std::string &msg) + { + std::cerr << "NIFMeshLoader: Warn: " << msg << std::endl; + } + + void fail(const std::string &msg) + { + std::cerr << "NIFMeshLoader: Fail: "<< msg << std::endl; + abort(); + } -void NIFLoader::createMeshes(const std::string &name, const std::string &group, bool hasSkel, Nif::Node *node, MeshPairList &meshes, int flags) -{ - flags |= node->flags; - // TODO: Check for extra data + typedef std::map LoaderMap; + static LoaderMap sLoaders; - if(node->recType == Nif::RC_NiTriShape) +public: + NIFMeshLoader() + : mHasSkel(false) + { } + NIFMeshLoader(const std::string &name, const std::string &group, bool hasSkel) + : mName(name), mGroup(group), mHasSkel(hasSkel) + { } + + virtual void loadResource(Ogre::Resource *resource) { - Ogre::MeshManager &meshMgr = Ogre::MeshManager::getSingleton(); - std::string fullname = name+"@"+node->name; + warn("Found no records in NIF for "+resource->getName()); + } - Ogre::MeshPtr mesh = meshMgr.getByName(fullname); - if(mesh.isNull()) + void createMeshes(Nif::Node *node, MeshPairList &meshes, int flags=0) + { + flags |= node->flags; + + // TODO: Check for extra data + + if(node->recType == Nif::RC_NiTriShape) { - NIFLoader *loader = &sLoaders[fullname]; - loader->mName = name; - loader->mShapeName = node->name; - loader->mHasSkel = hasSkel; + Ogre::MeshManager &meshMgr = Ogre::MeshManager::getSingleton(); + std::string fullname = mName+"@"+node->name; - mesh = meshMgr.createManual(fullname, group, loader); - } + Ogre::MeshPtr mesh = meshMgr.getByName(fullname); + if(mesh.isNull()) + { + NIFMeshLoader *loader = &sLoaders[fullname]; + *loader = *this; + loader->mShapeName = node->name; - meshes.push_back(std::make_pair(mesh, (node->parent ? node->parent->name : std::string()))); - } - else if(node->recType != Nif::RC_NiNode && node->recType != Nif::RC_RootCollisionNode && - node->recType != Nif::RC_NiRotatingParticles) - warn("Unhandled mesh node type: "+node->recName); + mesh = meshMgr.createManual(fullname, mGroup, loader); + } - Nif::NiNode *ninode = dynamic_cast(node); - if(ninode) - { - Nif::NodeList &children = ninode->children; - for(size_t i = 0;i < children.length();i++) + meshes.push_back(std::make_pair(mesh, (node->parent ? node->parent->name : std::string()))); + } + else if(node->recType != Nif::RC_NiNode && node->recType != Nif::RC_RootCollisionNode && + node->recType != Nif::RC_NiRotatingParticles) + warn("Unhandled mesh node type: "+node->recName); + + Nif::NiNode *ninode = dynamic_cast(node); + if(ninode) { - if(!children[i].empty()) - createMeshes(name, group, hasSkel, children[i].getPtr(), meshes, flags); + Nif::NodeList &children = ninode->children; + for(size_t i = 0;i < children.length();i++) + { + if(!children[i].empty()) + createMeshes(children[i].getPtr(), meshes, flags); + } } } -} +}; +NIFMeshLoader::LoaderMap NIFMeshLoader::sLoaders; MeshPairList NIFLoader::load(const std::string &name, Ogre::SkeletonPtr *skel, const std::string &group) @@ -448,7 +464,9 @@ MeshPairList NIFLoader::load(const std::string &name, Ogre::SkeletonPtr *skel, c } bool hasSkel = NIFSkeletonLoader::createSkeleton(name, group, node, skel); - createMeshes(name, group, hasSkel, node, meshes); + + NIFMeshLoader meshldr(name, group, hasSkel); + meshldr.createMeshes(node, meshes); return meshes; } diff --git a/components/nifogre/ogre_nif_loader.hpp b/components/nifogre/ogre_nif_loader.hpp index c90d65395..431af1163 100644 --- a/components/nifogre/ogre_nif_loader.hpp +++ b/components/nifogre/ogre_nif_loader.hpp @@ -86,28 +86,12 @@ typedef std::vector< std::pair > MeshPairList; their parent nodes (as they pertain to the skeleton, which is optionally returned in the second argument if it exists). */ -class NIFLoader : Ogre::ManualResourceLoader +class NIFLoader { public: - virtual void loadResource(Ogre::Resource *resource); - static MeshPairList load(const std::string &name, Ogre::SkeletonPtr *skel=NULL, const std::string &group="General"); - -private: - std::string mName; - std::string mShapeName; - bool mHasSkel; - - static void warn(const std::string &msg); - static void fail(const std::string &msg); - - static void createMeshes(const std::string &name, const std::string &group, bool hasSkel, - Nif::Node *node, MeshPairList &meshes, int flags=0); - - typedef std::map LoaderMap; - static LoaderMap sLoaders; }; }