2 * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
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.
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.
15 * Cedric Pinson <cedric.pinson@plopbyte.net>
16 * Michael Platings <mplatings@pixelpower.com>
19#ifndef OSGANIMATION_CHANNEL
20#define OSGANIMATION_CHANNEL 1
22#include <osgAnimation/Export>
23#include <osgAnimation/Sampler>
24#include <osgAnimation/Target>
25#include <osg/Referenced>
32 class OSGANIMATION_EXPORT Channel : public osg::Object
37 Channel(const Channel& channel);
39 virtual Channel* clone() const = 0;
41 virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Channel*>(obj)!=NULL; }
42 virtual const char* libraryName() const { return "osgAnimation"; }
43 virtual const char* className() const { return "Channel"; }
45 virtual void update(double time, float weight, int priority) = 0;
46 virtual void reset() = 0;
47 virtual Target* getTarget() = 0;
48 virtual bool setTarget(Target*) = 0;
50 const std::string& getName() const;
51 void setName(const std::string& name);
53 virtual double getStartTime() const = 0;
54 virtual double getEndTime() const = 0;
56 const std::string& getTargetName() const;
57 void setTargetName(const std::string& name);
59 virtual Sampler* getSampler() = 0;
60 virtual const Sampler* getSampler() const = 0;
62 // create a keyframe container from current target value
63 // with one key only, can be used for debug or to create
64 // easily a default channel from an existing one
65 virtual bool createKeyframeContainerFromTargetValue() = 0;
69 std::string _targetName;
74 template <typename SamplerType>
75 class TemplateChannel : public Channel
79 typedef typename SamplerType::UsingType UsingType;
80 typedef TemplateTarget<UsingType> TargetType;
81 typedef TemplateKeyframeContainer<typename SamplerType::KeyframeType> KeyframeContainerType;
83 Object* cloneType() const { return new TemplateChannel(); }
84 Object* clone(const osg::CopyOp&) const { return new TemplateChannel<SamplerType>(*this); }
85 Channel* clone() const { return new TemplateChannel<SamplerType>(*this); }
87 TemplateChannel (const TemplateChannel& channel) :
90 if (channel.getTargetTyped())
91 _target = new TargetType(*channel.getTargetTyped());
93 if (channel.getSamplerTyped())
94 _sampler = new SamplerType(*channel.getSamplerTyped());
97 TemplateChannel (SamplerType* s = 0,TargetType* target = 0)
102 _target = new TargetType;
106 virtual bool createKeyframeContainerFromTargetValue()
108 if (!_target.valid()) // no target it does not make sense to do it
113 // create a key from current target value
114 typename KeyframeContainerType::KeyType key(0, _target->getValue());
115 // recreate the keyframe container
116 getOrCreateSampler()->setKeyframeContainer(0);
117 getOrCreateSampler()->getOrCreateKeyframeContainer();
119 _sampler->getKeyframeContainerTyped()->push_back(key);
123 virtual ~TemplateChannel() {}
124 virtual void update(double time, float weight, int priority)
126 // skip if weight == 0
129 typename SamplerType::UsingType value;
130 _sampler->getValueAt(time, value);
131 _target->update(weight, value, priority);
133 virtual void reset() { _target->reset(); }
134 virtual Target* getTarget() { return _target.get();}
135 virtual bool setTarget(Target* target)
137 _target = dynamic_cast<TargetType*>(target);
138 return _target.get() == target;
141 SamplerType* getOrCreateSampler()
143 if (!_sampler.valid())
144 _sampler = new SamplerType;
145 return _sampler.get();
148 Sampler* getSampler() { return _sampler.get(); }
149 const Sampler* getSampler() const { return _sampler.get(); }
151 SamplerType* getSamplerTyped() { return _sampler.get();}
152 const SamplerType* getSamplerTyped() const { return _sampler.get();}
153 void setSampler(SamplerType* sampler) { _sampler = sampler; }
155 TargetType* getTargetTyped() { return _target.get(); }
156 const TargetType* getTargetTyped() const { return _target.get(); }
157 void setTarget(TargetType* target) { _target = target; }
159 virtual double getStartTime() const { return _sampler->getStartTime(); }
160 virtual double getEndTime() const { return _sampler->getEndTime(); }
163 osg::ref_ptr<TargetType> _target;
164 osg::ref_ptr<SamplerType> _sampler;
168 typedef std::vector<osg::ref_ptr<osgAnimation::Channel> > ChannelList;
170 typedef TemplateChannel<DoubleStepSampler> DoubleStepChannel;
171 typedef TemplateChannel<FloatStepSampler> FloatStepChannel;
172 typedef TemplateChannel<Vec2StepSampler> Vec2StepChannel;
173 typedef TemplateChannel<Vec3StepSampler> Vec3StepChannel;
174 typedef TemplateChannel<Vec4StepSampler> Vec4StepChannel;
175 typedef TemplateChannel<QuatStepSampler> QuatStepChannel;
177 typedef TemplateChannel<DoubleLinearSampler> DoubleLinearChannel;
178 typedef TemplateChannel<FloatLinearSampler> FloatLinearChannel;
179 typedef TemplateChannel<Vec2LinearSampler> Vec2LinearChannel;
180 typedef TemplateChannel<Vec3LinearSampler> Vec3LinearChannel;
181 typedef TemplateChannel<Vec4LinearSampler> Vec4LinearChannel;
182 typedef TemplateChannel<QuatSphericalLinearSampler> QuatSphericalLinearChannel;
183 typedef TemplateChannel<MatrixLinearSampler> MatrixLinearChannel;
185 typedef TemplateChannel<FloatCubicBezierSampler> FloatCubicBezierChannel;
186 typedef TemplateChannel<DoubleCubicBezierSampler> DoubleCubicBezierChannel;
187 typedef TemplateChannel<Vec2CubicBezierSampler> Vec2CubicBezierChannel;
188 typedef TemplateChannel<Vec3CubicBezierSampler> Vec3CubicBezierChannel;
189 typedef TemplateChannel<Vec4CubicBezierSampler> Vec4CubicBezierChannel;