| 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_SAMPLER |
|---|
| 20 | #define OSGANIMATION_SAMPLER 1 |
|---|
| 21 | |
|---|
| 22 | #include <vector> |
|---|
| 23 | #include <iostream> |
|---|
| 24 | #include <osg/Referenced> |
|---|
| 25 | #include <osg/ref_ptr> |
|---|
| 26 | #include <osgAnimation/Keyframe> |
|---|
| 27 | #include <osgAnimation/Interpolator> |
|---|
| 28 | |
|---|
| 29 | namespace osgAnimation |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | class Sampler : public osg::Referenced |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | virtual KeyframeContainer* getKeyframeContainer() = 0; |
|---|
| 36 | virtual const KeyframeContainer* getKeyframeContainer() const = 0; |
|---|
| 37 | protected: |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | // Sampler generic |
|---|
| 41 | template <class F> |
|---|
| 42 | class TemplateSampler : public Sampler |
|---|
| 43 | { |
|---|
| 44 | public: |
|---|
| 45 | typedef typename F::KeyframeType KeyframeType; |
|---|
| 46 | typedef TemplateKeyframeContainer<KeyframeType> KeyframeContainerType; |
|---|
| 47 | typedef typename F::UsingType UsingType; |
|---|
| 48 | typedef F FunctorType; |
|---|
| 49 | |
|---|
| 50 | TemplateSampler() {} |
|---|
| 51 | ~TemplateSampler() {} |
|---|
| 52 | |
|---|
| 53 | void getValueAt(double time, UsingType& result) const { _functor.getValue(*_keyframes, time, result);} |
|---|
| 54 | void setKeyframeContainer(KeyframeContainerType* kf) { _keyframes = kf;} |
|---|
| 55 | |
|---|
| 56 | virtual KeyframeContainer* getKeyframeContainer() { return _keyframes.get(); } |
|---|
| 57 | virtual const KeyframeContainer* getKeyframeContainer() const { return _keyframes.get();} |
|---|
| 58 | |
|---|
| 59 | KeyframeContainerType* getKeyframeContainerTyped() { return _keyframes.get();} |
|---|
| 60 | const KeyframeContainerType* getKeyframeContainerTyped() const { return _keyframes.get();} |
|---|
| 61 | KeyframeContainerType* getOrCreateKeyframeContainer() |
|---|
| 62 | { |
|---|
| 63 | if (_keyframes != 0) |
|---|
| 64 | return _keyframes.get(); |
|---|
| 65 | _keyframes = new KeyframeContainerType; |
|---|
| 66 | return _keyframes.get(); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | double getStartTime() const |
|---|
| 70 | { |
|---|
| 71 | if (!_keyframes || _keyframes->empty()) |
|---|
| 72 | return 0.0; |
|---|
| 73 | return _keyframes->front().getTime(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | double getEndTime() const |
|---|
| 77 | { |
|---|
| 78 | if (!_keyframes || _keyframes->empty()) |
|---|
| 79 | return 0.0; |
|---|
| 80 | return _keyframes->back().getTime(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | protected: |
|---|
| 84 | |
|---|
| 85 | FunctorType _functor; |
|---|
| 86 | osg::ref_ptr<KeyframeContainerType> _keyframes; |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | template<typename VALUESAMPLERTYPE, typename TIMESAMPLERTYPE> |
|---|
| 91 | class TemplateCompositeSampler : public osg::Referenced |
|---|
| 92 | { |
|---|
| 93 | VALUESAMPLERTYPE& _value; |
|---|
| 94 | TIMESAMPLERTYPE& _time; |
|---|
| 95 | |
|---|
| 96 | public: |
|---|
| 97 | typedef typename VALUESAMPLERTYPE::FunctorType::UsingType UsingType; |
|---|
| 98 | typedef typename VALUESAMPLERTYPE::FunctorType::KeyframeType KeyframeType; |
|---|
| 99 | |
|---|
| 100 | TemplateCompositeSampler(VALUESAMPLERTYPE& value, TIMESAMPLERTYPE& time) : _value(value), _time(time) |
|---|
| 101 | { |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void getValueAt(double time, typename VALUESAMPLERTYPE::FunctorType::UsingType& result) |
|---|
| 105 | { |
|---|
| 106 | double newtime; |
|---|
| 107 | _time.getValueAt(time, newtime); |
|---|
| 108 | _value.getValueAt(newtime, result); |
|---|
| 109 | } |
|---|
| 110 | float getStartTime() const {return _time.getStartTime(); } |
|---|
| 111 | float getEndTime() const {return _time.getEndTime();} |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 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; |
|---|
| 121 | |
|---|
| 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; |
|---|
| 129 | |
|---|
| 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; |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | #endif |
|---|