| 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 | |
|---|
| 15 | #ifndef OSGANIMATION_UPDATE_CALLBACK_H |
|---|
| 16 | #define OSGANIMATION_UPDATE_CALLBACK_H |
|---|
| 17 | |
|---|
| 18 | #include <osg/Vec3> |
|---|
| 19 | #include <osg/NodeCallback> |
|---|
| 20 | #include <osg/StateAttribute> |
|---|
| 21 | #include <osg/Material> |
|---|
| 22 | #include <osg/observer_ptr> |
|---|
| 23 | #include <osgAnimation/AnimationManagerBase> |
|---|
| 24 | #include <osgAnimation/Export> |
|---|
| 25 | |
|---|
| 26 | namespace osgAnimation |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | class AnimationUpdateCallbackBase |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 32 | virtual osg::Object* clone(const osg::CopyOp& copyop) const = 0; |
|---|
| 33 | virtual AnimationManagerBase* getAnimationManager() = 0; |
|---|
| 34 | virtual bool needLink() const = 0; |
|---|
| 35 | virtual bool link(osgAnimation::Channel* channel) = 0; |
|---|
| 36 | virtual int link(osgAnimation::Animation* animation) = 0; |
|---|
| 37 | virtual void updateLink() = 0; |
|---|
| 38 | virtual const std::string& getName() const = 0; |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | template <class T> |
|---|
| 42 | class AnimationUpdateCallback : public AnimationUpdateCallbackBase, public T |
|---|
| 43 | { |
|---|
| 44 | protected: |
|---|
| 45 | |
|---|
| 46 | osg::observer_ptr<osgAnimation::AnimationManagerBase> _manager; |
|---|
| 47 | |
|---|
| 48 | public: |
|---|
| 49 | AnimationUpdateCallback(const std::string& name) { T::setName(name);} |
|---|
| 50 | AnimationUpdateCallback(const AnimationUpdateCallback& apc,const osg::CopyOp& copyop): |
|---|
| 51 | T(apc, copyop), |
|---|
| 52 | _manager(apc._manager) {} |
|---|
| 53 | |
|---|
| 54 | osgAnimation::AnimationManagerBase* getAnimationManager() { return _manager.get(); } |
|---|
| 55 | |
|---|
| 56 | const std::string& getName() const { return T::getName(); } |
|---|
| 57 | int link(osgAnimation::Animation* animation) |
|---|
| 58 | { |
|---|
| 59 | if (T::getName().empty()) |
|---|
| 60 | osg::notify(osg::WARN) << "An update callback has no name, it means it can link only with \"\" named Target, often an error" << std::endl; |
|---|
| 61 | int nbLinks = 0; |
|---|
| 62 | for (osgAnimation::ChannelList::iterator it = animation->getChannels().begin(); |
|---|
| 63 | it != animation->getChannels().end(); |
|---|
| 64 | it++) |
|---|
| 65 | { |
|---|
| 66 | std::string targetName = (*it)->getTargetName(); |
|---|
| 67 | if (targetName == T::getName()) |
|---|
| 68 | { |
|---|
| 69 | AnimationUpdateCallbackBase* a = this; |
|---|
| 70 | a->link((*it).get()); |
|---|
| 71 | nbLinks++; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | return nbLinks; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void updateLink() |
|---|
| 78 | { |
|---|
| 79 | if (_manager.valid()) |
|---|
| 80 | { |
|---|
| 81 | if (needLink()) |
|---|
| 82 | { |
|---|
| 83 | /** this item is not linked yet then we do it for all animation |
|---|
| 84 | registered in the manager. |
|---|
| 85 | Maybe this function should be on the manager side like |
|---|
| 86 | _manager->linkItem(Bone); |
|---|
| 87 | */ |
|---|
| 88 | const AnimationList& animationList = _manager->getAnimationList(); |
|---|
| 89 | for (AnimationList::const_iterator it = animationList.begin(); it != animationList.end(); it++) |
|---|
| 90 | { |
|---|
| 91 | AnimationUpdateCallbackBase* a = this; |
|---|
| 92 | a->link(it->get()); |
|---|
| 93 | } |
|---|
| 94 | _manager->buildTargetReference(); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | class OSGANIMATION_EXPORT UpdateTransform : public AnimationUpdateCallback<osg::NodeCallback> |
|---|
| 104 | { |
|---|
| 105 | protected: |
|---|
| 106 | osg::ref_ptr<osgAnimation::Vec3Target> _euler; |
|---|
| 107 | osg::ref_ptr<osgAnimation::Vec3Target> _position; |
|---|
| 108 | osg::ref_ptr<osgAnimation::Vec3Target> _scale; |
|---|
| 109 | |
|---|
| 110 | public: |
|---|
| 111 | |
|---|
| 112 | META_Object(osgAnimation, UpdateTransform); |
|---|
| 113 | |
|---|
| 114 | UpdateTransform(const std::string& name = ""); |
|---|
| 115 | UpdateTransform(const UpdateTransform& apc,const osg::CopyOp& copyop); |
|---|
| 116 | |
|---|
| 117 | /** Callback method called by the NodeVisitor when visiting a node.*/ |
|---|
| 118 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv); |
|---|
| 119 | void update(osg::MatrixTransform& mat); |
|---|
| 120 | void update(osg::PositionAttitudeTransform& pat); |
|---|
| 121 | bool needLink() const; |
|---|
| 122 | bool link(osgAnimation::Channel* channel); |
|---|
| 123 | |
|---|
| 124 | osgAnimation::Vec3Target* getEuler() {return _euler.get();} |
|---|
| 125 | osgAnimation::Vec3Target* getPosition() {return _position.get();} |
|---|
| 126 | osgAnimation::Vec3Target* getScale() {return _scale.get();} |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | class OSGANIMATION_EXPORT UpdateMaterial : public AnimationUpdateCallback<osg::StateAttributeCallback> |
|---|
| 132 | { |
|---|
| 133 | protected: |
|---|
| 134 | osg::ref_ptr<osgAnimation::Vec4Target> _diffuse; |
|---|
| 135 | |
|---|
| 136 | public: |
|---|
| 137 | |
|---|
| 138 | META_Object(osgAnimation, UpdateMaterial); |
|---|
| 139 | |
|---|
| 140 | UpdateMaterial(const std::string& name = ""); |
|---|
| 141 | UpdateMaterial(const UpdateMaterial& apc,const osg::CopyOp& copyop); |
|---|
| 142 | |
|---|
| 143 | /** Callback method called by the NodeVisitor when visiting a node.*/ |
|---|
| 144 | virtual void operator () (osg::StateAttribute*, osg::NodeVisitor*); |
|---|
| 145 | void update(osg::Material& material); |
|---|
| 146 | bool needLink() const; |
|---|
| 147 | bool link(osgAnimation::Channel* channel); |
|---|
| 148 | osgAnimation::Vec4Target* getDiffuse(); |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | #endif |
|---|