root/OpenSceneGraph/trunk/src/osgAnimation/Skeleton.cpp @ 13041

Revision 13041, 3.0 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
3 *
4 * This library is open source and may be redistributed and/or modified under
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
13*/
14
15#include <osgAnimation/Skeleton>
16#include <osgAnimation/Bone>
17#include <osg/Notify>
18
19using namespace osgAnimation;
20
21Skeleton::Skeleton() {}
22
23Skeleton::Skeleton(const Skeleton& b, const osg::CopyOp& copyop) : osg::MatrixTransform(b,copyop) {}
24
25Skeleton::UpdateSkeleton::UpdateSkeleton() : _needValidate(true) {}
26
27Skeleton::UpdateSkeleton::UpdateSkeleton(const UpdateSkeleton& us, const osg::CopyOp& copyop) :
28    osg::Object(us, copyop),
29    osg::NodeCallback(us, copyop)
30{
31    _needValidate = true;
32}
33
34bool Skeleton::UpdateSkeleton::needToValidate() const
35{
36    return _needValidate;
37}
38
39
40class ValidateSkeletonVisitor : public osg::NodeVisitor
41{
42public:
43    ValidateSkeletonVisitor(): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
44    void apply(osg::Node& node) { return; }
45    void apply(osg::Transform& node)
46    {
47        // the idea is to traverse the skeleton or bone but to stop if other node is found
48        Bone* bone = dynamic_cast<Bone*>(&node);
49        if (!bone)
50            return;
51
52        bool foundNonBone = false;
53
54        for (unsigned int i = 0; i < bone->getNumChildren(); ++i)
55        {
56            if (dynamic_cast<Bone*>(bone->getChild(i)))
57            {
58                if (foundNonBone)
59                {
60                    OSG_WARN <<
61                        "Warning: a Bone was found after a non-Bone child "
62                        "within a Skeleton. Children of a Bone must be ordered "
63                        "with all child Bones first for correct update order." << std::endl;
64                    setTraversalMode(TRAVERSE_NONE);
65                    return;
66                }
67            }
68            else
69            {
70                foundNonBone = true;
71            }
72        }
73        traverse(node);
74    }
75};
76
77void Skeleton::UpdateSkeleton::operator()(osg::Node* node, osg::NodeVisitor* nv)
78{
79    if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
80    {
81        Skeleton* skeleton = dynamic_cast<Skeleton*>(node);
82        if (_needValidate && skeleton)
83        {
84            ValidateSkeletonVisitor visitor;
85            for (unsigned int i = 0; i < skeleton->getNumChildren(); ++i)
86            {
87                osg::Node* child = skeleton->getChild(i);
88                child->accept(visitor);
89            }
90            _needValidate = false;
91        }
92    }
93    traverse(node,nv);
94}
95
96void Skeleton::setDefaultUpdateCallback()
97{
98    setUpdateCallback(new Skeleton::UpdateSkeleton );
99}
Note: See TracBrowser for help on using the browser.