Changeset 11009 for OpenSceneGraph/trunk/src/osgAnimation/Bone.cpp
- Timestamp:
- 01/27/10 13:24:55 (3 years ago)
- Files:
-
- 1 modified
-
OpenSceneGraph/trunk/src/osgAnimation/Bone.cpp (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgAnimation/Bone.cpp
r10693 r11009 15 15 #include <osgAnimation/Bone> 16 16 #include <osgAnimation/Skeleton> 17 #include <osgAnimation/ FindParentAnimationManagerVisitor>17 #include <osgAnimation/UpdateBone> 18 18 #include <osgAnimation/BoneMapVisitor> 19 #include <osgAnimation/ComputeBindMatrixVisitor>20 19 20 using namespace osgAnimation; 21 21 22 osgAnimation::Bone::UpdateBone::UpdateBone(const osgAnimation::Bone::UpdateBone& apc,const osg::CopyOp& copyop) : 23 osg::Object(apc, copyop), 24 osgAnimation::AnimationUpdateCallback<osg::NodeCallback>(apc, copyop) 22 Bone::Bone(const Bone& b, const osg::CopyOp& copyop) : osg::MatrixTransform(b,copyop), _invBindInSkeletonSpace(b._invBindInSkeletonSpace), _boneInSkeletonSpace(b._boneInSkeletonSpace) 25 23 { 26 _quaternion = new osgAnimation::QuatTarget(apc._quaternion->getValue());27 _position = new osgAnimation::Vec3Target(apc._position->getValue());28 _scale = new osgAnimation::Vec3Target(apc._scale->getValue());29 24 } 30 25 31 bool osgAnimation::Bone::UpdateBone::link(osgAnimation::Channel* channel)26 Bone::Bone(const std::string& name) 32 27 { 33 if (channel->getName().find("quaternion") != std::string::npos) 34 { 35 return channel->setTarget(_quaternion.get()); 36 } 37 else if (channel->getName().find("position") != std::string::npos) 38 { 39 return channel->setTarget(_position.get()); 40 } 41 else if (channel->getName().find("scale") != std::string::npos) 42 { 43 return channel->setTarget(_scale.get()); 44 } 45 else 46 { 47 osg::notify(osg::WARN) << "Channel " << channel->getName() << " does not contain a valid symbolic name for this class" << className() << std::endl; 48 } 49 return false; 28 if (!name.empty()) 29 setName(name); 50 30 } 51 31 52 32 53 /** Callback method called by the NodeVisitor when visiting a node.*/ 54 void osgAnimation::Bone::UpdateBone::operator()(osg::Node* node, osg::NodeVisitor* nv) 55 { 56 if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) 57 { 58 Bone* b = dynamic_cast<Bone*>(node); 59 if (!b) 60 { 61 osg::notify(osg::WARN) << "Warning: UpdateBone set on non-Bone object." << std::endl; 62 return; 63 } 64 65 if (b->needToComputeBindMatrix()) 66 { 67 ComputeBindMatrixVisitor visitor; 68 b->accept(visitor); 69 } 70 71 update(*b); 72 73 Bone* parent = b->getBoneParent(); 74 if (parent) 75 b->setMatrixInSkeletonSpace(b->getMatrixInBoneSpace() * parent->getMatrixInSkeletonSpace()); 76 else 77 b->setMatrixInSkeletonSpace(b->getMatrixInBoneSpace()); 78 79 } 80 traverse(node,nv); 81 } 82 83 84 osgAnimation::Bone::Bone(const Bone& b, const osg::CopyOp& copyop) : 85 osg::Transform(b,copyop), 86 _position(b._position), 87 _rotation(b._rotation), 88 _scale(b._scale), 89 _needToRecomputeBindMatrix(true), 90 _bindInBoneSpace(b._bindInBoneSpace), 91 _invBindInSkeletonSpace(b._invBindInSkeletonSpace), 92 _boneInSkeletonSpace(b._boneInSkeletonSpace) 93 { 94 } 95 96 osgAnimation::Bone::Bone(const std::string& name) 97 { 98 if (!name.empty()) 99 setName(name); 100 _needToRecomputeBindMatrix = false; 101 } 102 103 104 void osgAnimation::Bone::setDefaultUpdateCallback(const std::string& name) 33 void Bone::setDefaultUpdateCallback(const std::string& name) 105 34 { 106 35 std::string cbName = name; … … 110 39 } 111 40 112 void osgAnimation::Bone::computeBindMatrix() 113 { 114 _invBindInSkeletonSpace = osg::Matrix::inverse(_bindInBoneSpace); 115 const Bone* parent = getBoneParent(); 116 _needToRecomputeBindMatrix = false; 117 if (!parent) 118 { 119 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; 120 return; 121 } 122 _invBindInSkeletonSpace = parent->getInvBindMatrixInSkeletonSpace() * _invBindInSkeletonSpace; 123 } 124 125 osgAnimation::Bone* osgAnimation::Bone::getBoneParent() 41 Bone* Bone::getBoneParent() 126 42 { 127 43 if (getParents().empty()) 128 44 return 0; 129 45 osg::Node::ParentList parents = getParents(); 130 for (osg::Node::ParentList::iterator it = parents.begin(); it != parents.end(); it++)46 for (osg::Node::ParentList::iterator it = parents.begin(); it != parents.end(); ++it) 131 47 { 132 48 Bone* pb = dynamic_cast<Bone*>(*it); … … 136 52 return 0; 137 53 } 138 const osgAnimation::Bone* osgAnimation::Bone::getBoneParent() const54 const Bone* Bone::getBoneParent() const 139 55 { 140 56 if (getParents().empty()) 141 57 return 0; 142 58 const osg::Node::ParentList& parents = getParents(); 143 for (osg::Node::ParentList::const_iterator it = parents.begin(); it != parents.end(); it++)59 for (osg::Node::ParentList::const_iterator it = parents.begin(); it != parents.end(); ++it) 144 60 { 145 61 const Bone* pb = dynamic_cast<const Bone*>(*it); … … 149 65 return 0; 150 66 } 151 152 153 /** Add Node to Group.154 * If node is not NULL and is not contained in Group then increment its155 * reference count, add it to the child list and dirty the bounding156 * sphere to force it to recompute on next getBound() and return true for success.157 * Otherwise return false. Scene nodes can't be added as child nodes.158 */159 bool osgAnimation::Bone::addChild( Node *child )160 {161 Bone* bone = dynamic_cast<Bone*>(child);162 if (bone)163 bone->setNeedToComputeBindMatrix(true);164 return osg::Group::addChild(child);165 }166 167 osgAnimation::Bone::BoneMap osgAnimation::Bone::getBoneMap()168 {169 BoneMapVisitor mapVisitor;170 this->accept(mapVisitor);171 return mapVisitor.getBoneMap();172 }
