2008-11-22 20:14:19 +08:00
|
|
|
/* -*-c++-*-
|
2009-08-28 00:21:01 +08:00
|
|
|
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
|
2008-11-22 20:14:19 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osgAnimation/Skeleton>
|
|
|
|
#include <osgAnimation/Bone>
|
2010-01-27 20:24:55 +08:00
|
|
|
#include <osg/Notify>
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
using namespace osgAnimation;
|
|
|
|
|
2009-11-13 21:39:21 +08:00
|
|
|
Skeleton::Skeleton() {}
|
2010-01-27 20:24:55 +08:00
|
|
|
Skeleton::Skeleton(const Skeleton& b, const osg::CopyOp& copyop) : osg::MatrixTransform(b,copyop) {}
|
2009-11-13 21:39:21 +08:00
|
|
|
|
|
|
|
Skeleton::UpdateSkeleton::UpdateSkeleton() : _needValidate(true) {}
|
2010-01-27 20:24:55 +08:00
|
|
|
Skeleton::UpdateSkeleton::UpdateSkeleton(const UpdateSkeleton& us, const osg::CopyOp& copyop= osg::CopyOp::SHALLOW_COPY) : osg::Object(us, copyop), osg::NodeCallback(us, copyop)
|
2009-11-13 21:39:21 +08:00
|
|
|
{
|
|
|
|
_needValidate = true;
|
|
|
|
}
|
|
|
|
bool Skeleton::UpdateSkeleton::needToValidate() const
|
|
|
|
{
|
|
|
|
return _needValidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-28 00:21:01 +08:00
|
|
|
class ValidateSkeletonVisitor : public osg::NodeVisitor
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-08-28 00:21:01 +08:00
|
|
|
public:
|
|
|
|
ValidateSkeletonVisitor(): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
|
|
|
|
void apply(osg::Node& node) { return; }
|
2008-11-22 20:14:19 +08:00
|
|
|
void apply(osg::Transform& node)
|
|
|
|
{
|
|
|
|
// the idea is to traverse the skeleton or bone but to stop if other node is found
|
|
|
|
Bone* bone = dynamic_cast<Bone*>(&node);
|
|
|
|
if (!bone)
|
|
|
|
return;
|
|
|
|
|
2009-08-28 00:21:01 +08:00
|
|
|
bool foundNonBone = false;
|
|
|
|
|
2010-01-27 20:24:55 +08:00
|
|
|
for (unsigned int i = 0; i < bone->getNumChildren(); ++i)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-08-28 00:21:01 +08:00
|
|
|
if (dynamic_cast<Bone*>(bone->getChild(i)))
|
|
|
|
{
|
|
|
|
if (foundNonBone)
|
|
|
|
{
|
|
|
|
osg::notify(osg::WARN) <<
|
|
|
|
"Warning: a Bone was found after a non-Bone child "
|
|
|
|
"within a Skeleton. Children of a Bone must be ordered "
|
|
|
|
"with all child Bones first for correct update order." << std::endl;
|
|
|
|
setTraversalMode(TRAVERSE_NONE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foundNonBone = true;
|
|
|
|
}
|
2008-11-22 20:14:19 +08:00
|
|
|
}
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
void Skeleton::UpdateSkeleton::operator()(osg::Node* node, osg::NodeVisitor* nv)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-11-13 21:39:21 +08:00
|
|
|
if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-11-13 21:39:21 +08:00
|
|
|
Skeleton* skeleton = dynamic_cast<Skeleton*>(node);
|
|
|
|
if (_needValidate && skeleton)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-08-28 00:21:01 +08:00
|
|
|
ValidateSkeletonVisitor visitor;
|
2010-01-27 20:24:55 +08:00
|
|
|
for (unsigned int i = 0; i < skeleton->getNumChildren(); ++i)
|
|
|
|
{
|
|
|
|
osg::Node* child = skeleton->getChild(i);
|
|
|
|
child->accept(visitor);
|
|
|
|
}
|
2009-11-13 21:39:21 +08:00
|
|
|
_needValidate = false;
|
2008-11-22 20:14:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
traverse(node,nv);
|
|
|
|
}
|
|
|
|
|
2009-11-13 21:39:21 +08:00
|
|
|
void Skeleton::setDefaultUpdateCallback()
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-11-13 21:39:21 +08:00
|
|
|
setUpdateCallback(new Skeleton::UpdateSkeleton );
|
2008-12-17 04:29:00 +08:00
|
|
|
}
|