OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Channel.h
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 namespace osgAnimation
29 {
30 
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;
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 
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:
157  };
158 
159 
160  typedef std::vector<osg::ref_ptr<osgAnimation::Channel> > ChannelList;
161 
168 
176 
182 
183 }
184 
185 #endif
SamplerType::UsingType UsingType
Definition: Channel.h:74
#define OSGANIMATION_EXPORT
Definition: Export.h:40
TemplateChannel< FloatCubicBezierSampler > FloatCubicBezierChannel
Definition: Channel.h:177
void setSampler(SamplerType *sampler)
Definition: Channel.h:145
virtual Target * getTarget()
Definition: Channel.h:126
TemplateKeyframeContainer< typename SamplerType::KeyframeType > KeyframeContainerType
Definition: Channel.h:76
TemplateChannel< DoubleStepSampler > DoubleStepChannel
Definition: Channel.h:162
bool valid() const
Definition: ref_ptr.h:95
virtual bool setTarget(Target *target)
Definition: Channel.h:127
TemplateChannel< MatrixLinearSampler > MatrixLinearChannel
Definition: Channel.h:175
TemplateChannel< FloatStepSampler > FloatStepChannel
Definition: Channel.h:163
TemplateChannel(const TemplateChannel &channel)
Definition: Channel.h:79
Channel * clone() const
Definition: Channel.h:77
const Sampler * getSampler() const
Definition: Channel.h:141
TemplateTarget< UsingType > TargetType
Definition: Channel.h:75
T * clone(const T *t, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition: Object.h:242
TemplateChannel< Vec4CubicBezierSampler > Vec4CubicBezierChannel
Definition: Channel.h:181
TemplateChannel(SamplerType *s=0, TargetType *target=0)
Definition: Channel.h:89
TemplateChannel< Vec2CubicBezierSampler > Vec2CubicBezierChannel
Definition: Channel.h:179
const SamplerType * getSamplerTyped() const
Definition: Channel.h:144
SamplerType * getSamplerTyped()
Definition: Channel.h:143
TemplateChannel< Vec3StepSampler > Vec3StepChannel
Definition: Channel.h:165
osg::ref_ptr< SamplerType > _sampler
Definition: Channel.h:156
TemplateChannel< DoubleCubicBezierSampler > DoubleCubicBezierChannel
Definition: Channel.h:178
SamplerType * getOrCreateSampler()
Definition: Channel.h:133
const TargetType * getTargetTyped() const
Definition: Channel.h:148
TargetType * getTargetTyped()
Definition: Channel.h:147
TemplateChannel< Vec2LinearSampler > Vec2LinearChannel
Definition: Channel.h:171
std::string _name
Definition: Channel.h:65
T * get() const
Definition: ref_ptr.h:92
virtual double getStartTime() const
Definition: Channel.h:151
TemplateChannel< FloatLinearSampler > FloatLinearChannel
Definition: Channel.h:170
std::string _targetName
Definition: Channel.h:64
std::vector< osg::ref_ptr< osgAnimation::Channel > > ChannelList
Definition: Channel.h:160
TemplateChannel< Vec2StepSampler > Vec2StepChannel
Definition: Channel.h:164
TemplateChannel< DoubleLinearSampler > DoubleLinearChannel
Definition: Channel.h:169
void setTarget(TargetType *target)
Definition: Channel.h:149
TemplateChannel< Vec4LinearSampler > Vec4LinearChannel
Definition: Channel.h:173
TemplateChannel< Vec3CubicBezierSampler > Vec3CubicBezierChannel
Definition: Channel.h:180
virtual double getEndTime() const
Definition: Channel.h:152
TemplateChannel< QuatStepSampler > QuatStepChannel
Definition: Channel.h:167
osg::ref_ptr< TargetType > _target
Definition: Channel.h:155
virtual void update(double time, float weight, int priority)
Definition: Channel.h:116
TemplateChannel< Vec3LinearSampler > Vec3LinearChannel
Definition: Channel.h:172
TemplateChannel< Vec4StepSampler > Vec4StepChannel
Definition: Channel.h:166
TemplateChannel< QuatSphericalLinearSampler > QuatSphericalLinearChannel
Definition: Channel.h:174
virtual bool createKeyframeContainerFromTargetValue()
Definition: Channel.h:98