Channel 7.0 KB

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