#include "attach.hpp" #include #include #include #include #include #include #include #include #include #include "visitor.hpp" namespace SceneUtil { class CopyRigVisitor : public osg::NodeVisitor { public: CopyRigVisitor(osg::ref_ptr parent, const std::string& filter) : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) , mParent(parent) , mFilter(Misc::StringUtils::lowerCase(filter)) { mFilter2 = "tri " + mFilter; } virtual void apply(osg::Node& node) { std::string lowerName = Misc::StringUtils::lowerCase(node.getName()); if ((lowerName.size() >= mFilter.size() && lowerName.compare(0, mFilter.size(), mFilter) == 0) || (lowerName.size() >= mFilter2.size() && lowerName.compare(0, mFilter2.size(), mFilter2) == 0)) mToCopy.push_back(&node); else traverse(node); } void doCopy() { for (std::vector >::iterator it = mToCopy.begin(); it != mToCopy.end(); ++it) { osg::ref_ptr node = *it; if (node->getNumParents() > 1) std::cerr << "CopyRigVisitor warning: node has multiple parents" << std::endl; while (node->getNumParents()) node->getParent(0)->removeChild(node); mParent->addChild(node); } mToCopy.clear(); } private: typedef std::vector > NodeVector; NodeVector mToCopy; osg::ref_ptr mParent; std::string mFilter; std::string mFilter2; }; osg::ref_ptr attach(osg::ref_ptr toAttach, osg::Node *master, const std::string &filter, const std::string &attachNode) { if (dynamic_cast(toAttach.get())) { osg::ref_ptr handle = new osg::Group; CopyRigVisitor copyVisitor(handle, filter); toAttach->accept(copyVisitor); copyVisitor.doCopy(); if (handle->getNumChildren() == 1) { osg::ref_ptr newHandle = handle->getChild(0); handle->removeChild(newHandle); master->asGroup()->addChild(newHandle); return newHandle; } else { master->asGroup()->addChild(handle); return handle; } } else { FindByNameVisitor find(attachNode); master->accept(find); if (!find.mFoundNode) throw std::runtime_error(std::string("Can't find attachment node ") + attachNode); FindByNameVisitor findBoneOffset("BoneOffset"); toAttach->accept(findBoneOffset); osg::ref_ptr trans; if (findBoneOffset.mFoundNode) { osg::MatrixTransform* boneOffset = dynamic_cast(findBoneOffset.mFoundNode); if (!boneOffset) throw std::runtime_error("BoneOffset must be a MatrixTransform"); trans = new osg::PositionAttitudeTransform; trans->setPosition(boneOffset->getMatrix().getTrans()); // The BoneOffset rotation seems to be incorrect trans->setAttitude(osg::Quat(osg::DegreesToRadians(-90.f), osg::Vec3f(1,0,0))); } if (attachNode.find("Left") != std::string::npos) { if (!trans) trans = new osg::PositionAttitudeTransform; trans->setScale(osg::Vec3f(-1.f, 1.f, 1.f)); // Need to invert culling because of the negative scale // Note: for absolute correctness we would need to check the current front face for every mesh then invert it // However MW isn't doing this either, so don't. Assuming all meshes are using backface culling is more efficient. static osg::ref_ptr frontFaceStateSet; if (!frontFaceStateSet) { frontFaceStateSet = new osg::StateSet; osg::FrontFace* frontFace = new osg::FrontFace; frontFace->setMode(osg::FrontFace::CLOCKWISE); frontFaceStateSet->setAttributeAndModes(frontFace, osg::StateAttribute::ON); } trans->setStateSet(frontFaceStateSet); } if (trans) { find.mFoundNode->addChild(trans); trans->addChild(toAttach); return trans; } else { find.mFoundNode->addChild(toAttach); return toAttach; } } } }