| 1 | /* -*-c++-*- |
|---|
| 2 | * Copyright (C) 2008 Cedric Pinson <cedric.pinson@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 | * Authors: |
|---|
| 15 | * Cedric Pinson <cedric.pinson@plopbyte.net> |
|---|
| 16 | * Michael Platings <mplatings@pixelpower.com> |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef OSGANIMATION_BONE_H |
|---|
| 20 | #define OSGANIMATION_BONE_H |
|---|
| 21 | |
|---|
| 22 | #include <osg/Transform> |
|---|
| 23 | #include <osg/Quat> |
|---|
| 24 | #include <osg/Vec3> |
|---|
| 25 | #include <osg/Node> |
|---|
| 26 | #include <osg/Geode> |
|---|
| 27 | #include <osg/Geometry> |
|---|
| 28 | #include <osg/Notify> |
|---|
| 29 | #include <osg/io_utils> |
|---|
| 30 | #include <osg/NodeVisitor> |
|---|
| 31 | #include <osgAnimation/Export> |
|---|
| 32 | #include <osgAnimation/Target> |
|---|
| 33 | #include <osgAnimation/Sampler> |
|---|
| 34 | #include <osgAnimation/Channel> |
|---|
| 35 | #include <osgAnimation/Keyframe> |
|---|
| 36 | #include <osgAnimation/UpdateCallback> |
|---|
| 37 | #include <osgAnimation/Animation> |
|---|
| 38 | #include <osgAnimation/AnimationManagerBase> |
|---|
| 39 | #include <osgAnimation/VertexInfluence> |
|---|
| 40 | |
|---|
| 41 | namespace osgAnimation |
|---|
| 42 | { |
|---|
| 43 | |
|---|
| 44 | // A bone can't have more than one parent Bone, so sharing a part of Bone's hierarchy |
|---|
| 45 | // makes no sense. You can share the entire hierarchy but not only a part of it. |
|---|
| 46 | class OSGANIMATION_EXPORT Bone : public osg::Transform |
|---|
| 47 | { |
|---|
| 48 | public: |
|---|
| 49 | typedef osg::ref_ptr<Bone> PointerType; |
|---|
| 50 | typedef std::map<std::string, PointerType > BoneMap; |
|---|
| 51 | typedef osg::Matrix MatrixType; |
|---|
| 52 | |
|---|
| 53 | META_Node(osgAnimation, Bone); |
|---|
| 54 | Bone(const Bone& b, const osg::CopyOp& copyop= osg::CopyOp::SHALLOW_COPY); |
|---|
| 55 | Bone(const std::string& name = ""); |
|---|
| 56 | |
|---|
| 57 | void setDefaultUpdateCallback(const std::string& name = ""); |
|---|
| 58 | |
|---|
| 59 | class OSGANIMATION_EXPORT UpdateBone : public AnimationUpdateCallback <osg::NodeCallback> |
|---|
| 60 | { |
|---|
| 61 | protected: |
|---|
| 62 | osg::ref_ptr<osgAnimation::Vec3Target> _position; |
|---|
| 63 | osg::ref_ptr<osgAnimation::QuatTarget> _quaternion; |
|---|
| 64 | osg::ref_ptr<osgAnimation::Vec3Target> _scale; |
|---|
| 65 | |
|---|
| 66 | public: |
|---|
| 67 | META_Object(osgAnimation, UpdateBone); |
|---|
| 68 | UpdateBone(const UpdateBone& apc,const osg::CopyOp& copyop); |
|---|
| 69 | |
|---|
| 70 | UpdateBone(const std::string& name = "") : AnimationUpdateCallback <osg::NodeCallback>(name) |
|---|
| 71 | { |
|---|
| 72 | setName(name); |
|---|
| 73 | _quaternion = new osgAnimation::QuatTarget; |
|---|
| 74 | _position = new osgAnimation::Vec3Target; |
|---|
| 75 | _scale = new osgAnimation::Vec3Target; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void update(osgAnimation::Bone& bone) |
|---|
| 79 | { |
|---|
| 80 | bone.setTranslation(_position->getValue()); |
|---|
| 81 | bone.setRotation(_quaternion->getValue()); |
|---|
| 82 | bone.setScale(_scale->getValue()); |
|---|
| 83 | bone.dirtyBound(); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | osgAnimation::QuatTarget* getQuaternion() {return _quaternion.get();} |
|---|
| 87 | osgAnimation::Vec3Target* getPosition() {return _position.get();} |
|---|
| 88 | osgAnimation::Vec3Target* getScale() {return _scale.get();} |
|---|
| 89 | |
|---|
| 90 | bool needLink() const |
|---|
| 91 | { |
|---|
| 92 | // the idea is to return true if nothing is linked |
|---|
| 93 | return !((_position->getCount() + _quaternion->getCount() + _scale->getCount()) > 3); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** Link channel*/ |
|---|
| 97 | bool link(osgAnimation::Channel* channel); |
|---|
| 98 | |
|---|
| 99 | /** Callback method called by the NodeVisitor when visiting a node.*/ |
|---|
| 100 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv); |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const; |
|---|
| 104 | virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const; |
|---|
| 105 | |
|---|
| 106 | Bone* getBoneParent(); |
|---|
| 107 | const Bone* getBoneParent() const; |
|---|
| 108 | |
|---|
| 109 | void setTranslation(const osg::Vec3& trans) { _position = trans;} |
|---|
| 110 | void setRotation(const osg::Quat& quat) { _rotation = quat;} |
|---|
| 111 | void setScale(const osg::Vec3& scale) { _scale = scale;} |
|---|
| 112 | |
|---|
| 113 | const osg::Vec3& getTranslation() const { return _position;} |
|---|
| 114 | const osg::Quat& getRotation() const { return _rotation;} |
|---|
| 115 | const osg::Vec3& getScale() const { return _scale;} |
|---|
| 116 | |
|---|
| 117 | osg::Matrix getMatrixInBoneSpace() const { return (osg::Matrix(getRotation())) * osg::Matrix::translate(getTranslation()) * _bindInBoneSpace;} |
|---|
| 118 | const osg::Matrix& getBindMatrixInBoneSpace() const { return _bindInBoneSpace; } |
|---|
| 119 | const osg::Matrix& getMatrixInSkeletonSpace() const { return _boneInSkeletonSpace; } |
|---|
| 120 | const osg::Matrix& getInvBindMatrixInSkeletonSpace() const { return _invBindInSkeletonSpace;} |
|---|
| 121 | void setMatrixInSkeletonSpace(const osg::Matrix& matrix) { _boneInSkeletonSpace = matrix; } |
|---|
| 122 | void setBindMatrixInBoneSpace(const osg::Matrix& matrix) |
|---|
| 123 | { |
|---|
| 124 | _bindInBoneSpace = matrix; |
|---|
| 125 | _needToRecomputeBindMatrix = true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | inline bool needToComputeBindMatrix() { return _needToRecomputeBindMatrix;} |
|---|
| 129 | virtual void computeBindMatrix(); |
|---|
| 130 | |
|---|
| 131 | void setNeedToComputeBindMatrix(bool state) { _needToRecomputeBindMatrix = state; } |
|---|
| 132 | |
|---|
| 133 | /** Add Node to Group. |
|---|
| 134 | * If node is not NULL and is not contained in Group then increment its |
|---|
| 135 | * reference count, add it to the child list and dirty the bounding |
|---|
| 136 | * sphere to force it to recompute on next getBound() and return true for success. |
|---|
| 137 | * Otherwise return false. Scene nodes can't be added as child nodes. |
|---|
| 138 | */ |
|---|
| 139 | virtual bool addChild( Node *child ); |
|---|
| 140 | BoneMap getBoneMap(); |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | protected: |
|---|
| 144 | |
|---|
| 145 | osg::Vec3 _position; |
|---|
| 146 | osg::Quat _rotation; |
|---|
| 147 | osg::Vec3 _scale; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | // flag to recompute bind pose |
|---|
| 151 | bool _needToRecomputeBindMatrix; |
|---|
| 152 | |
|---|
| 153 | // bind data |
|---|
| 154 | osg::Matrix _bindInBoneSpace; |
|---|
| 155 | osg::Matrix _invBindInSkeletonSpace; |
|---|
| 156 | |
|---|
| 157 | // bone updated |
|---|
| 158 | osg::Matrix _boneInSkeletonSpace; |
|---|
| 159 | |
|---|
| 160 | }; |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | inline bool Bone::computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor*) const |
|---|
| 164 | { |
|---|
| 165 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 166 | matrix.preMult(getMatrixInBoneSpace()); |
|---|
| 167 | else |
|---|
| 168 | matrix = getMatrixInBoneSpace(); |
|---|
| 169 | return true; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | inline bool Bone::computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor*) const |
|---|
| 174 | { |
|---|
| 175 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 176 | matrix.postMult(osg::Matrix::inverse(getMatrixInBoneSpace())); |
|---|
| 177 | else |
|---|
| 178 | matrix = osg::Matrix::inverse(getMatrixInBoneSpace()); |
|---|
| 179 | return true; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | } |
|---|
| 183 | #endif |
|---|