Fix Drawable removal issues

openmw-37
scrawl 9 years ago
parent 1edcb219a7
commit 0a52ee17c3

@ -175,58 +175,71 @@ namespace
return 0.0f; return 0.0f;
} }
/// @brief Base class for visitors that remove nodes from a scene graph.
// Removes all drawables from a graph. /// Subclasses need to fill the mToRemove vector.
class RemoveDrawableVisitor : public osg::NodeVisitor /// To use, node->accept(removeVisitor); removeVisitor.remove();
class RemoveVisitor : public osg::NodeVisitor
{ {
public: public:
RemoveDrawableVisitor() RemoveVisitor()
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{ {
} }
virtual void apply(osg::Geode &node) void remove()
{ {
// Not safe to remove in apply(), since the visitor is still iterating the child list for (RemoveVec::iterator it = mToRemove.begin(); it != mToRemove.end(); ++it)
osg::Group* parent = node.getParent(0); it->second->removeChild(it->first);
// prune nodes that would be empty after the removal }
if (parent->getNumChildren() == 1 && parent->getDataVariance() == osg::Object::STATIC)
mToRemove.push_back(parent); protected:
else // <node to remove, parent node to remove it from>
mToRemove.push_back(&node); typedef std::vector<std::pair<osg::Node*, osg::Group*> > RemoveVec;
traverse(node); std::vector<std::pair<osg::Node*, osg::Group*> > mToRemove;
};
// Removes all drawables from a graph.
class RemoveDrawableVisitor : public RemoveVisitor
{
public:
virtual void apply(osg::Geode &geode)
{
applyImpl(geode);
} }
#if OSG_VERSION_GREATER_OR_EQUAL(3,3,3) #if OSG_VERSION_GREATER_OR_EQUAL(3,3,3)
virtual void apply(osg::Drawable& drw) virtual void apply(osg::Drawable& drw)
{ {
mToRemove.push_back(&drw); applyImpl(drw);
} }
#endif #endif
void remove() void applyImpl(osg::Node& node)
{ {
for (std::vector<osg::Node*>::iterator it = mToRemove.begin(); it != mToRemove.end(); ++it) osg::NodePath::iterator parent = getNodePath().end()-2;
// We know that the parent is a Group because only Groups can have children.
osg::Group* parentGroup = static_cast<osg::Group*>(*parent);
// Try to prune nodes that would be empty after the removal
if (parent != getNodePath().begin())
{
// This could be extended to remove the parent's parent, and so on if they are empty as well.
// But for NIF files, there won't be a benefit since only TriShapes can be set to STATIC dataVariance.
osg::Group* parentParent = static_cast<osg::Group*>(*(parent - 1));
if (parentGroup->getNumChildren() == 1 && parentGroup->getDataVariance() == osg::Object::STATIC)
{ {
// FIXME: a Drawable might have more than one parent mToRemove.push_back(std::make_pair(parentGroup, parentParent));
osg::Node* node = *it; return;
if (node->getNumParents())
node->getParent(0)->removeChild(node);
} }
} }
private: mToRemove.push_back(std::make_pair(&node, parentGroup));
std::vector<osg::Node*> mToRemove; }
}; };
class RemoveTriBipVisitor : public osg::NodeVisitor class RemoveTriBipVisitor : public RemoveVisitor
{ {
public: public:
RemoveTriBipVisitor()
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
}
virtual void apply(osg::Geode &node) virtual void apply(osg::Geode &node)
{ {
applyImpl(node); applyImpl(node);
@ -244,24 +257,11 @@ namespace
const std::string toFind = "tri bip"; const std::string toFind = "tri bip";
if (Misc::StringUtils::ciCompareLen(node.getName(), toFind, toFind.size()) == 0) if (Misc::StringUtils::ciCompareLen(node.getName(), toFind, toFind.size()) == 0)
{ {
osg::Group* parent = static_cast<osg::Group*>(*(getNodePath().end()-2));
// Not safe to remove in apply(), since the visitor is still iterating the child list // Not safe to remove in apply(), since the visitor is still iterating the child list
mToRemove.push_back(&node); mToRemove.push_back(std::make_pair(&node, parent));
} }
} }
void remove()
{
for (std::vector<osg::Node*>::iterator it = mToRemove.begin(); it != mToRemove.end(); ++it)
{
// FIXME: a Drawable might have more than one parent
osg::Node* node = *it;
if (node->getNumParents())
node->getParent(0)->removeChild(node);
}
}
private:
std::vector<osg::Node*> mToRemove;
}; };
} }

Loading…
Cancel
Save