root/OpenSceneGraph/trunk/src/osgAnimation/Bone.cpp @ 9370

Revision 9370, 4.5 kB (checked in by robert, 9 years ago)

From Cedric Pinson and Jeremey Moles, Changes to OpenSceneGraph-osgWidget-dev branch.

Notes from Robert Osfield, Merged changes to OpenSceneGraph-osgWidget-dev r9367 (prior to my botched attempt at merged svn/trunk into the branch).

Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <mornifle@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/Skinning>
16#include <osgAnimation/Bone>
17#include <osgAnimation/Skeleton>
18
19osgAnimation::Bone::UpdateBone::UpdateBone(const osgAnimation::Bone::UpdateBone& apc,const osg::CopyOp& copyop):
20    osgAnimation::AnimationUpdateCallback(apc, copyop),
21    _position(apc._position),
22    _quaternion(apc._quaternion),
23    _scale(apc._scale)
24{
25}
26
27
28osgAnimation::Bone::Bone(const Bone& b, const osg::CopyOp& copyop)
29    : osg::Transform(b,copyop),
30      _position(b._position),
31    _rotation(b._rotation),
32    _scale(b._scale)
33{
34}
35
36osgAnimation::Bone::Bone(const std::string& name)
37{
38    if (!name.empty())
39        setName(name);
40    _needToRecomputeBindMatrix = false;
41}
42
43
44void osgAnimation::Bone::setDefaultUpdateCallback(const std::string& name)
45{
46    std::string cbName = name;
47    if (cbName.empty())
48        cbName = getName();
49    setUpdateCallback(new UpdateBone(cbName));
50}
51
52void osgAnimation::Bone::computeBindMatrix()
53{
54    _invBindInSkeletonSpace = osg::Matrix::inverse(_bindInBoneSpace);
55    const Bone* parent = getBoneParent();
56    _needToRecomputeBindMatrix = false;
57    if (!parent)
58    {
59#if 0
60        // no more parent means, we get the skeleton
61        if (getParents().empty()) {
62            osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you should not have this message, it means you miss to attach this bone(" << getName() <<") to a Skeleton node" << std::endl;
63            return;
64        } else if (getParents().size() > 1) {
65            osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you have more than one parent in a skeleton structure (" << getName() <<") unknown behaviour" << std::endl;
66            return;
67        }
68        osgAnimation::Skeleton* skel = dynamic_cast<osgAnimation::Skeleton*>(getParents()[0]);
69        if (!skel) {
70            osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you should not have this message, it means you miss to attach this bone(" << getName() <<") to a Skeleton node" << std::endl;
71            return;
72        }
73        _invBindInSkeletonSpace = osg::Matrix::inverse(skel->getMatrix()) * _invBindInSkeletonSpace;
74#else
75        osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you should not have this message, it means you miss to attach this bone(" << getName() <<") to a Skeleton node" << std::endl;
76#endif
77        return;
78    }
79    _invBindInSkeletonSpace = parent->getInvBindMatrixInSkeletonSpace() * _invBindInSkeletonSpace;
80}
81
82osgAnimation::Bone* osgAnimation::Bone::getBoneParent()
83{
84    if (getParents().empty())
85        return 0;
86    osg::Node::ParentList parents = getParents();
87    for (osg::Node::ParentList::iterator it = parents.begin(); it != parents.end(); it++)
88    {
89        Bone* pb = dynamic_cast<Bone*>(*it);
90        if (pb)
91            return pb;
92    }
93    return 0;
94}
95const osgAnimation::Bone* osgAnimation::Bone::getBoneParent() const
96{
97    if (getParents().empty())
98        return 0;
99    const osg::Node::ParentList& parents = getParents();
100    for (osg::Node::ParentList::const_iterator it = parents.begin(); it != parents.end(); it++)
101    {
102        const Bone* pb = dynamic_cast<const Bone*>(*it);
103        if (pb)
104            return pb;
105    }
106    return 0;
107}
108
109
110/** Add Node to Group.
111 * If node is not NULL and is not contained in Group then increment its
112 * reference count, add it to the child list and dirty the bounding
113 * sphere to force it to recompute on next getBound() and return true for success.
114 * Otherwise return false. Scene nodes can't be added as child nodes.
115 */
116bool osgAnimation::Bone::addChild( Node *child )
117{
118    Bone* bone = dynamic_cast<Bone*>(child);
119    if (bone)
120        bone->setNeedToComputeBindMatrix(true);
121    return osg::Group::addChild(child);
122}
123
124osgAnimation::Bone::BoneMap osgAnimation::Bone::getBoneMap()
125{
126    BoneMapVisitor mapVisitor;
127    this->accept(mapVisitor);
128    return mapVisitor._map;
129}
Note: See TracBrowser for help on using the browser.