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