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

Revision 10751, 3.1 kB (checked in by cedricpinson, 5 years ago)

From Cedric Pinson, Fix Skeleton to compute correctly bind matrix, fix compile issue on osganimationhardware after fixing Skeleton

RevLine 
[9093]1/*  -*-c++-*-
[10558]2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
[9093]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
18using namespace osgAnimation;
19
[10751]20Skeleton::Skeleton() {}
21Skeleton::Skeleton(const Skeleton& b, const osg::CopyOp& copyop) : Bone(b,copyop) {}
22
23Skeleton::UpdateSkeleton::UpdateSkeleton() : _needValidate(true) {}
24Skeleton::UpdateSkeleton::UpdateSkeleton(const UpdateSkeleton& us, const osg::CopyOp& copyop= osg::CopyOp::SHALLOW_COPY) : osg::Object(us, copyop), osg::NodeCallback(us, copyop)
25{
26    _needValidate = true;
27}
28bool Skeleton::UpdateSkeleton::needToValidate() const
29{
30    return _needValidate;
31}
32
33
[10558]34class ValidateSkeletonVisitor : public osg::NodeVisitor
[9093]35{
[10558]36public:
37    ValidateSkeletonVisitor(): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
38    void apply(osg::Node& node) { return; }
[9093]39    void apply(osg::Transform& node)
40    {
41        // the idea is to traverse the skeleton or bone but to stop if other node is found
42        Bone* bone = dynamic_cast<Bone*>(&node);
43        if (!bone)
44            return;
45
[10558]46        bool foundNonBone = false;
47
48        for (unsigned i = 0; i < bone->getNumChildren(); ++i)
[9093]49        {
[10558]50            if (dynamic_cast<Bone*>(bone->getChild(i)))
51            {
52                if (foundNonBone)
53                {
54                    osg::notify(osg::WARN) <<
55                        "Warning: a Bone was found after a non-Bone child "
56                        "within a Skeleton. Children of a Bone must be ordered "
57                        "with all child Bones first for correct update order." << std::endl;
58                    setTraversalMode(TRAVERSE_NONE);
59                    return;
60                }
61            }
62            else
63            {
64                foundNonBone = true;
65            }
[9093]66        }
67        traverse(node);
68    }
69};
70
[9370]71void Skeleton::UpdateSkeleton::operator()(osg::Node* node, osg::NodeVisitor* nv)
[9093]72{
[10751]73    if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
[9093]74    {
[10751]75        Skeleton* skeleton = dynamic_cast<Skeleton*>(node);
76        if (_needValidate && skeleton)
[9093]77        {
[10558]78            ValidateSkeletonVisitor visitor;
79            node->accept(visitor);
[10751]80            _needValidate = false;
[9093]81        }
[10751]82        if (skeleton->needToComputeBindMatrix())
83            skeleton->computeBindMatrix();
[9093]84    }
85    traverse(node,nv);
86}
87
[10751]88void Skeleton::setDefaultUpdateCallback()
[9093]89{
[10751]90    setUpdateCallback(new Skeleton::UpdateSkeleton );
[9093]91}
[9370]92
[10751]93void Skeleton::computeBindMatrix()
[9370]94{
[10751]95    _invBindInSkeletonSpace = osg::Matrix::inverse(_bindInBoneSpace);
96    _needToRecomputeBindMatrix = false;
[9370]97}
Note: See TracBrowser for help on using the browser.