root/OpenSceneGraph/branches/OpenSceneGraph-osgWidget-dev/src/osgAnimation/Bone.cpp @ 9093

Revision 9093, 4.0 kB (checked in by cedricpinson, 9 years ago)

add osgAnimation

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
27void osgAnimation::Bone::computeBindMatrix()
28{
29    _invBindInSkeletonSpace = osg::Matrix::inverse(_bindInBoneSpace);
30    const Bone* parent = getBoneParent();
31    _needToRecomputeBindMatrix = false;
32    if (!parent)
33    {
34#if 0
35        // no more parent means, we get the skeleton
36        if (getParents().empty()) {
37            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;
38            return;
39        } else if (getParents().size() > 1) {
40            osg::notify(osg::WARN) << "Warning " << className() <<"::computeBindMatrix you have more than one parent in a skeleton structure (" << getName() <<") unknown behaviour" << std::endl;
41            return;
42        }
43        osgAnimation::Skeleton* skel = dynamic_cast<osgAnimation::Skeleton*>(getParents()[0]);
44        if (!skel) {
45            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;
46            return;
47        }
48        _invBindInSkeletonSpace = osg::Matrix::inverse(skel->getMatrix()) * _invBindInSkeletonSpace;
49#else
50        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;
51#endif
52        return;
53    }
54    _invBindInSkeletonSpace = parent->getInvBindMatrixInSkeletonSpace() * _invBindInSkeletonSpace;
55}
56
57osgAnimation::Bone* osgAnimation::Bone::getBoneParent()
58{
59    if (getParents().empty())
60        return 0;
61    osg::Node::ParentList parents = getParents();
62    for (osg::Node::ParentList::iterator it = parents.begin(); it != parents.end(); it++)
63    {
64        Bone* pb = dynamic_cast<Bone*>(*it);
65        if (pb)
66            return pb;
67    }
68    return 0;
69}
70const osgAnimation::Bone* osgAnimation::Bone::getBoneParent() const
71{
72    if (getParents().empty())
73        return 0;
74    const osg::Node::ParentList& parents = getParents();
75    for (osg::Node::ParentList::const_iterator it = parents.begin(); it != parents.end(); it++)
76    {
77        const Bone* pb = dynamic_cast<const Bone*>(*it);
78        if (pb)
79            return pb;
80    }
81    return 0;
82}
83
84
85/** Add Node to Group.
86 * If node is not NULL and is not contained in Group then increment its
87 * reference count, add it to the child list and dirty the bounding
88 * sphere to force it to recompute on next getBound() and return true for success.
89 * Otherwise return false. Scene nodes can't be added as child nodes.
90 */
91bool osgAnimation::Bone::addChild( Node *child )
92{
93    Bone* bone = dynamic_cast<Bone*>(child);
94    if (bone)
95        bone->setNeedToComputeBindMatrix(true);
96    return osg::Group::addChild(child);
97}
98
99osgAnimation::Bone::BoneMap osgAnimation::Bone::getBoneMap()
100{
101    BoneMapVisitor mapVisitor;
102    this->accept(mapVisitor);
103    return mapVisitor._map;
104}
Note: See TracBrowser for help on using the browser.