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_SAMPLER
20#define OSGANIMATION_SAMPLER 1
24#include <osg/Referenced>
26#include <osgAnimation/Keyframe>
27#include <osgAnimation/Interpolator>
32 class Sampler : public osg::Referenced
35 virtual KeyframeContainer* getKeyframeContainer() = 0;
36 virtual const KeyframeContainer* getKeyframeContainer() const = 0;
42 class TemplateSampler : public Sampler
45 typedef typename F::KeyframeType KeyframeType;
46 typedef TemplateKeyframeContainer<KeyframeType> KeyframeContainerType;
47 typedef typename F::UsingType UsingType;
48 typedef F FunctorType;
53 void getValueAt(double time, UsingType& result) const { _functor.getValue(*_keyframes, time, result);}
54 void setKeyframeContainer(KeyframeContainerType* kf) { _keyframes = kf;}
56 virtual KeyframeContainer* getKeyframeContainer() { return _keyframes.get(); }
57 virtual const KeyframeContainer* getKeyframeContainer() const { return _keyframes.get();}
59 KeyframeContainerType* getKeyframeContainerTyped() { return _keyframes.get();}
60 const KeyframeContainerType* getKeyframeContainerTyped() const { return _keyframes.get();}
61 KeyframeContainerType* getOrCreateKeyframeContainer()
64 return _keyframes.get();
65 _keyframes = new KeyframeContainerType;
66 return _keyframes.get();
69 double getStartTime() const
71 if (!_keyframes || _keyframes->empty())
73 return _keyframes->front().getTime();
76 double getEndTime() const
78 if (!_keyframes || _keyframes->empty())
80 return _keyframes->back().getTime();
86 osg::ref_ptr<KeyframeContainerType> _keyframes;
90 template<typename VALUESAMPLERTYPE, typename TIMESAMPLERTYPE>
91 class TemplateCompositeSampler : public osg::Referenced
93 VALUESAMPLERTYPE& _value;
94 TIMESAMPLERTYPE& _time;
97 typedef typename VALUESAMPLERTYPE::FunctorType::UsingType UsingType;
98 typedef typename VALUESAMPLERTYPE::FunctorType::KeyframeType KeyframeType;
100 TemplateCompositeSampler(VALUESAMPLERTYPE& value, TIMESAMPLERTYPE& time) : _value(value), _time(time)
104 void getValueAt(double time, typename VALUESAMPLERTYPE::FunctorType::UsingType& result)
107 _time.getValueAt(time, newtime);
108 _value.getValueAt(newtime, result);
110 float getStartTime() const {return _time.getStartTime(); }
111 float getEndTime() const {return _time.getEndTime();}
115 typedef TemplateSampler<DoubleStepInterpolator> DoubleStepSampler;
116 typedef TemplateSampler<FloatStepInterpolator> FloatStepSampler;
117 typedef TemplateSampler<Vec2StepInterpolator> Vec2StepSampler;
118 typedef TemplateSampler<Vec3StepInterpolator> Vec3StepSampler;
119 typedef TemplateSampler<Vec4StepInterpolator> Vec4StepSampler;
120 typedef TemplateSampler<QuatStepInterpolator> QuatStepSampler;
122 typedef TemplateSampler<DoubleLinearInterpolator> DoubleLinearSampler;
123 typedef TemplateSampler<FloatLinearInterpolator> FloatLinearSampler;
124 typedef TemplateSampler<Vec2LinearInterpolator> Vec2LinearSampler;
125 typedef TemplateSampler<Vec3LinearInterpolator> Vec3LinearSampler;
126 typedef TemplateSampler<Vec4LinearInterpolator> Vec4LinearSampler;
127 typedef TemplateSampler<QuatSphericalLinearInterpolator> QuatSphericalLinearSampler;
128 typedef TemplateSampler<MatrixLinearInterpolator> MatrixLinearSampler;
130 typedef TemplateSampler<FloatCubicBezierInterpolator> FloatCubicBezierSampler;
131 typedef TemplateSampler<DoubleCubicBezierInterpolator> DoubleCubicBezierSampler;
132 typedef TemplateSampler<Vec2CubicBezierInterpolator> Vec2CubicBezierSampler;
133 typedef TemplateSampler<Vec3CubicBezierInterpolator> Vec3CubicBezierSampler;
134 typedef TemplateSampler<Vec4CubicBezierInterpolator> Vec4CubicBezierSampler;