root/OpenSceneGraph/trunk/include/osgAnimation/Channel @ 13041

Revision 13041, 6.6 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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_CHANNEL
20#define OSGANIMATION_CHANNEL 1
21
22#include <osgAnimation/Export>
23#include <osgAnimation/Sampler>
24#include <osgAnimation/Target>
25#include <osg/Referenced>
26#include <string>
27
28namespace osgAnimation
29{
30
31    class OSGANIMATION_EXPORT Channel : public osg::Referenced
32    {
33    public:
34
35        Channel();
36        Channel(const Channel& channel);
37        virtual ~Channel();
38        virtual Channel* clone() const = 0;
39
40        virtual void update(double time, float weight, int priority) = 0;
41        virtual void reset() = 0;
42        virtual Target* getTarget() = 0;
43        virtual bool setTarget(Target*) = 0;
44
45        const std::string& getName() const;
46        void setName(const std::string& name);
47
48        virtual double getStartTime() const = 0;
49        virtual double getEndTime() const = 0;
50
51        const std::string& getTargetName() const;
52        void setTargetName(const std::string& name);
53
54        virtual Sampler* getSampler() = 0;
55        virtual const Sampler* getSampler() const = 0;
56
57        // create a keyframe container from current target value
58        // with one key only, can be used for debug or to create
59        // easily a default channel from an existing one
60        virtual bool createKeyframeContainerFromTargetValue() = 0;
61
62    protected:
63
64        std::string _targetName;
65        std::string _name;
66    };
67
68
69    template <typename SamplerType>
70    class TemplateChannel : public Channel
71    {
72    public:
73
74        typedef typename SamplerType::UsingType UsingType;
75        typedef TemplateTarget<UsingType> TargetType;
76        typedef TemplateKeyframeContainer<typename SamplerType::KeyframeType> KeyframeContainerType;
77        Channel* clone() const { return new TemplateChannel<SamplerType>(*this); }
78
79        TemplateChannel (const TemplateChannel& channel) :
80            Channel(channel)
81        {
82            if (channel.getTargetTyped())
83                _target = new TargetType(*channel.getTargetTyped());
84
85            if (channel.getSamplerTyped())
86                _sampler = new SamplerType(*channel.getSamplerTyped());
87        }
88
89        TemplateChannel (SamplerType* s = 0,TargetType* target = 0)
90        {
91            if (target)
92                _target = target;
93            else
94                _target = new TargetType;
95            _sampler = s;
96        }
97
98        virtual bool createKeyframeContainerFromTargetValue()
99        {
100            if (!_target.valid()) // no target it does not make sense to do it
101            {
102                return false;
103            }
104
105            // create a key from current target value
106            typename KeyframeContainerType::KeyType key(0, _target->getValue());
107            // recreate the keyframe container
108            getOrCreateSampler()->setKeyframeContainer(0);
109            getOrCreateSampler()->getOrCreateKeyframeContainer();
110            // add the key
111            _sampler->getKeyframeContainerTyped()->push_back(key);
112            return true;
113        }
114
115        virtual ~TemplateChannel() {}
116        virtual void update(double time, float weight, int priority)
117        {
118            // skip if weight == 0
119            if (weight < 1e-4)
120                return;
121            typename SamplerType::UsingType value;
122            _sampler->getValueAt(time, value);
123            _target->update(weight, value, priority);
124        }
125        virtual void reset() { _target->reset(); }
126        virtual Target* getTarget() { return _target.get();}
127        virtual bool setTarget(Target* target)
128        {
129            _target = dynamic_cast<TargetType*>(target);
130            return _target.get() == target;
131        }
132
133        SamplerType* getOrCreateSampler()
134        {
135            if (!_sampler.valid())
136                _sampler = new SamplerType;
137            return _sampler.get();
138        }
139
140        Sampler* getSampler() { return _sampler.get(); }
141        const Sampler* getSampler() const { return _sampler.get(); }
142
143        SamplerType* getSamplerTyped() { return _sampler.get();}
144        const SamplerType* getSamplerTyped() const { return _sampler.get();}
145        void setSampler(SamplerType* sampler) { _sampler = sampler; }
146
147        TargetType* getTargetTyped() { return _target.get(); }
148        const TargetType* getTargetTyped() const { return _target.get(); }
149        void setTarget(TargetType* target) { _target = target; }
150
151        virtual double getStartTime() const { return _sampler->getStartTime(); }
152        virtual double getEndTime() const { return _sampler->getEndTime(); }
153
154    protected:
155        osg::ref_ptr<TargetType> _target;
156        osg::ref_ptr<SamplerType> _sampler;
157    };
158
159
160    typedef std::vector<osg::ref_ptr<osgAnimation::Channel> > ChannelList;
161
162    typedef TemplateChannel<DoubleStepSampler> DoubleStepChannel;
163    typedef TemplateChannel<FloatStepSampler> FloatStepChannel;
164    typedef TemplateChannel<Vec2StepSampler> Vec2StepChannel;
165    typedef TemplateChannel<Vec3StepSampler> Vec3StepChannel;
166    typedef TemplateChannel<Vec4StepSampler> Vec4StepChannel;
167    typedef TemplateChannel<QuatStepSampler> QuatStepChannel;
168
169    typedef TemplateChannel<DoubleLinearSampler> DoubleLinearChannel;
170    typedef TemplateChannel<FloatLinearSampler> FloatLinearChannel;
171    typedef TemplateChannel<Vec2LinearSampler> Vec2LinearChannel;
172    typedef TemplateChannel<Vec3LinearSampler> Vec3LinearChannel;
173    typedef TemplateChannel<Vec4LinearSampler> Vec4LinearChannel;
174    typedef TemplateChannel<QuatSphericalLinearSampler> QuatSphericalLinearChannel;
175    typedef TemplateChannel<MatrixLinearSampler> MatrixLinearChannel;
176
177    typedef TemplateChannel<FloatCubicBezierSampler> FloatCubicBezierChannel;
178    typedef TemplateChannel<DoubleCubicBezierSampler> DoubleCubicBezierChannel;
179    typedef TemplateChannel<Vec2CubicBezierSampler> Vec2CubicBezierChannel;
180    typedef TemplateChannel<Vec3CubicBezierSampler> Vec3CubicBezierChannel;
181    typedef TemplateChannel<Vec4CubicBezierSampler> Vec4CubicBezierChannel;
182
183}
184
185#endif
Note: See TracBrowser for help on using the browser.