openscenegraph
Channel
Go to the documentation of this file.
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
28
29namespace osgAnimation
30{
31
32 class OSGANIMATION_EXPORT Channel : public osg::Object
33 {
34 public:
35
36 Channel();
37 Channel(const Channel& channel);
38 virtual ~Channel();
39 virtual Channel* clone() const = 0;
40
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"; }
44
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;
49
50 const std::string& getName() const;
51 void setName(const std::string& name);
52
53 virtual double getStartTime() const = 0;
54 virtual double getEndTime() const = 0;
55
56 const std::string& getTargetName() const;
57 void setTargetName(const std::string& name);
58
59 virtual Sampler* getSampler() = 0;
60 virtual const Sampler* getSampler() const = 0;
61
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;
66
67 protected:
68
69 std::string _targetName;
70 std::string _name;
71 };
72
73
74 template <typename SamplerType>
75 class TemplateChannel : public Channel
76 {
77 public:
78
79 typedef typename SamplerType::UsingType UsingType;
80 typedef TemplateTarget<UsingType> TargetType;
81 typedef TemplateKeyframeContainer<typename SamplerType::KeyframeType> KeyframeContainerType;
82
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); }
86
87 TemplateChannel (const TemplateChannel& channel) :
88 Channel(channel)
89 {
90 if (channel.getTargetTyped())
91 _target = new TargetType(*channel.getTargetTyped());
92
93 if (channel.getSamplerTyped())
94 _sampler = new SamplerType(*channel.getSamplerTyped());
95 }
96
97 TemplateChannel (SamplerType* s = 0,TargetType* target = 0)
98 {
99 if (target)
100 _target = target;
101 else
102 _target = new TargetType;
103 _sampler = s;
104 }
105
106 virtual bool createKeyframeContainerFromTargetValue()
107 {
108 if (!_target.valid()) // no target it does not make sense to do it
109 {
110 return false;
111 }
112
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();
118 // add the key
119 _sampler->getKeyframeContainerTyped()->push_back(key);
120 return true;
121 }
122
123 virtual ~TemplateChannel() {}
124 virtual void update(double time, float weight, int priority)
125 {
126 // skip if weight == 0
127 if (weight < 1e-4)
128 return;
129 typename SamplerType::UsingType value;
130 _sampler->getValueAt(time, value);
131 _target->update(weight, value, priority);
132 }
133 virtual void reset() { _target->reset(); }
134 virtual Target* getTarget() { return _target.get();}
135 virtual bool setTarget(Target* target)
136 {
137 _target = dynamic_cast<TargetType*>(target);
138 return _target.get() == target;
139 }
140
141 SamplerType* getOrCreateSampler()
142 {
143 if (!_sampler.valid())
144 _sampler = new SamplerType;
145 return _sampler.get();
146 }
147
148 Sampler* getSampler() { return _sampler.get(); }
149 const Sampler* getSampler() const { return _sampler.get(); }
150
151 SamplerType* getSamplerTyped() { return _sampler.get();}
152 const SamplerType* getSamplerTyped() const { return _sampler.get();}
153 void setSampler(SamplerType* sampler) { _sampler = sampler; }
154
155 TargetType* getTargetTyped() { return _target.get(); }
156 const TargetType* getTargetTyped() const { return _target.get(); }
157 void setTarget(TargetType* target) { _target = target; }
158
159 virtual double getStartTime() const { return _sampler->getStartTime(); }
160 virtual double getEndTime() const { return _sampler->getEndTime(); }
161
162 protected:
163 osg::ref_ptr<TargetType> _target;
164 osg::ref_ptr<SamplerType> _sampler;
165 };
166
167
168 typedef std::vector<osg::ref_ptr<osgAnimation::Channel> > ChannelList;
169
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;
176
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;
184
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;
190
191}
192
193#endif