root/OpenSceneGraph/trunk/include/osgAnimation/UpdateCallback @ 10671

Revision 10671, 5.4 kB (checked in by robert, 5 years ago)

From Cedric Pinson, "Here a patch to be able to clone stateattribute, in order to do that i
moved the StateAttribute::Callback structure to a file
StateAttributeCallback? with the same behavior as NodeCallback?.
"

RevLine 
[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
26namespace 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;
39    };
40
41    template <class T>
42    class AnimationUpdateCallback : public AnimationUpdateCallbackBase, public T
43    {
[9093]44    protected:
[10518]45
[9150]46        osg::observer_ptr<osgAnimation::AnimationManagerBase> _manager;
[9093]47
48    public:
[10518]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        }
[9093]98    };
99
[10518]100
101
102
103    class OSGANIMATION_EXPORT UpdateTransform : public AnimationUpdateCallback<osg::NodeCallback>
[9093]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);
[10557]123
124        osgAnimation::Vec3Target* getEuler() {return _euler.get();}
125        osgAnimation::Vec3Target* getPosition() {return _position.get();}
126        osgAnimation::Vec3Target* getScale() {return _scale.get();}
[9093]127    };
128
129
[10518]130
[10671]131    class OSGANIMATION_EXPORT UpdateMaterial : public AnimationUpdateCallback<osg::StateAttributeCallback>
[10518]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);
[10671]148        osgAnimation::Vec4Target* getDiffuse();
[10518]149    };
150
[9093]151}
152
153#endif
Note: See TracBrowser for help on using the browser.