| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgAnimation/Bone> |
|---|
| 16 | #include <osgAnimation/Skeleton> |
|---|
| 17 | #include <osgAnimation/FindParentAnimationManagerVisitor> |
|---|
| 18 | #include <osgAnimation/BoneMapVisitor> |
|---|
| 19 | #include <osgAnimation/ComputeBindMatrixVisitor> |
|---|
| 20 | |
|---|
| 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) |
|---|
| 25 | { |
|---|
| 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 | } |
|---|
| 30 | |
|---|
| 31 | bool osgAnimation::Bone::UpdateBone::link(osgAnimation::Channel* channel) |
|---|
| 32 | { |
|---|
| 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; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 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) |
|---|
| 105 | { |
|---|
| 106 | std::string cbName = name; |
|---|
| 107 | if (cbName.empty()) |
|---|
| 108 | cbName = getName(); |
|---|
| 109 | setUpdateCallback(new UpdateBone(cbName)); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 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() |
|---|
| 126 | { |
|---|
| 127 | if (getParents().empty()) |
|---|
| 128 | return 0; |
|---|
| 129 | osg::Node::ParentList parents = getParents(); |
|---|
| 130 | for (osg::Node::ParentList::iterator it = parents.begin(); it != parents.end(); it++) |
|---|
| 131 | { |
|---|
| 132 | Bone* pb = dynamic_cast<Bone*>(*it); |
|---|
| 133 | if (pb) |
|---|
| 134 | return pb; |
|---|
| 135 | } |
|---|
| 136 | return 0; |
|---|
| 137 | } |
|---|
| 138 | const osgAnimation::Bone* osgAnimation::Bone::getBoneParent() const |
|---|
| 139 | { |
|---|
| 140 | if (getParents().empty()) |
|---|
| 141 | return 0; |
|---|
| 142 | const osg::Node::ParentList& parents = getParents(); |
|---|
| 143 | for (osg::Node::ParentList::const_iterator it = parents.begin(); it != parents.end(); it++) |
|---|
| 144 | { |
|---|
| 145 | const Bone* pb = dynamic_cast<const Bone*>(*it); |
|---|
| 146 | if (pb) |
|---|
| 147 | return pb; |
|---|
| 148 | } |
|---|
| 149 | return 0; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 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 | } |
|---|