2012-03-22 01:36:20 +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
|
|
|
*
|
2012-03-22 01:36:20 +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
|
2008-11-22 20:14:19 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2008-11-22 20:14:19 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-11-22 20:14:19 +08:00
|
|
|
* 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() {}
|
2011-06-24 03:58:52 +08:00
|
|
|
|
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) {}
|
2011-06-24 03:58:52 +08:00
|
|
|
|
|
|
|
Skeleton::UpdateSkeleton::UpdateSkeleton(const UpdateSkeleton& us, const osg::CopyOp& copyop) :
|
|
|
|
osg::Object(us, copyop),
|
2016-06-08 16:46:09 +08:00
|
|
|
osg::Callback(us, copyop),
|
2011-06-24 03:58:52 +08:00
|
|
|
osg::NodeCallback(us, copyop)
|
2009-11-13 21:39:21 +08:00
|
|
|
{
|
|
|
|
_needValidate = true;
|
|
|
|
}
|
2011-06-24 03:58:52 +08:00
|
|
|
|
2009-11-13 21:39:21 +08:00
|
|
|
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) {}
|
2013-06-28 20:00:43 +08:00
|
|
|
void apply(osg::Node& /*node*/) { return; }
|
2012-03-22 01:36:20 +08:00
|
|
|
void apply(osg::Transform& node)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2010-05-29 00:07:09 +08:00
|
|
|
OSG_WARN <<
|
2009-08-28 00:21:01 +08:00
|
|
|
"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)
|
2012-03-22 01:36:20 +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
|
|
|
}
|