Channel 6.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*  -*-c++-*- 
 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
 *
 * Authors:
 *         Cedric Pinson <cedric.pinson@plopbyte.net>
 *         Michael Platings <mplatings@pixelpower.com>
 */

#ifndef OSGANIMATION_CHANNEL
#define OSGANIMATION_CHANNEL 1

#include <osgAnimation/Export>
#include <osgAnimation/Sampler>
#include <osgAnimation/Target>
#include <osg/Referenced>
#include <string>

namespace osgAnimation
{

    class OSGANIMATION_EXPORT Channel : public osg::Referenced
    {
    public:

        Channel();
        Channel(const Channel& channel);
        virtual ~Channel();
        virtual Channel* clone() const = 0;

        virtual void update(double time, float weight, int priority) = 0;
        virtual void reset() = 0;
        virtual Target* getTarget() = 0;
        virtual bool setTarget(Target*) = 0;

        const std::string& getName() const;
        void setName(const std::string& name);

        virtual double getStartTime() const = 0;
        virtual double getEndTime() const = 0;

        const std::string& getTargetName() const;
        void setTargetName(const std::string& name);

        virtual Sampler* getSampler() = 0;
        virtual const Sampler* getSampler() const = 0;

        // create a keyframe container from current target value
        // with one key only, can be used for debug or to create
        // easily a default channel from an existing one
        virtual bool createKeyframeContainerFromTargetValue() = 0;

    protected:
    
        std::string _targetName;
        std::string _name;
    };


    template <typename SamplerType>
    class TemplateChannel : public Channel
    {
    public:

        typedef typename SamplerType::UsingType UsingType;
        typedef TemplateTarget<UsingType> TargetType;
        typedef TemplateKeyframeContainer<typename SamplerType::KeyframeType> KeyframeContainerType;
        Channel* clone() const { return new TemplateChannel<SamplerType>(*this); }

        TemplateChannel (const TemplateChannel& channel) :
            Channel(channel)
        {
            if (channel.getTargetTyped())
                _target = new TargetType(*channel.getTargetTyped());

            if (channel.getSamplerTyped())
                _sampler = new SamplerType(*channel.getSamplerTyped());
        }

        TemplateChannel (SamplerType* s = 0,TargetType* target = 0)
        {
            if (target)
                _target = target;
            else
                _target = new TargetType;
            _sampler = s;
        }

        virtual bool createKeyframeContainerFromTargetValue()
        {
            if (!_target.valid()) // no target it does not make sense to do it
            {
                return false;
            }

            // create a key from current target value
            typename KeyframeContainerType::KeyType key(0, _target->getValue());
            // recreate the keyframe container
            getOrCreateSampler()->setKeyframeContainer(0);
            getOrCreateSampler()->getOrCreateKeyframeContainer();
            // add the key
            _sampler->getKeyframeContainerTyped()->push_back(key);
            return true;
        }

        virtual ~TemplateChannel() {}
        virtual void update(double time, float weight, int priority) 
        {
            // skip if weight == 0
            if (weight < 1e-4)
                return;
            typename SamplerType::UsingType value;
            _sampler->getValueAt(time, value);
            _target->update(weight, value, priority);
        }
        virtual void reset() { _target->reset(); }
        virtual Target* getTarget() { return _target.get();}
        virtual bool setTarget(Target* target)
        {
            _target = dynamic_cast<TargetType*>(target);
            return _target.get() == target;
        }

        SamplerType* getOrCreateSampler()
        {
            if (!_sampler.valid())
                _sampler = new SamplerType;
            return _sampler.get();
        }

        Sampler* getSampler() { return _sampler.get(); }
        const Sampler* getSampler() const { return _sampler.get(); }

        SamplerType* getSamplerTyped() { return _sampler.get();}
        const SamplerType* getSamplerTyped() const { return _sampler.get();}
        void setSampler(SamplerType* sampler) { _sampler = sampler; }

        TargetType* getTargetTyped() { return _target.get(); }
        const TargetType* getTargetTyped() const { return _target.get(); }
        void setTarget(TargetType* target) { _target = target; }

        virtual double getStartTime() const { return _sampler->getStartTime(); }
        virtual double getEndTime() const { return _sampler->getEndTime(); }

    protected:
        osg::ref_ptr<TargetType> _target;
        osg::ref_ptr<SamplerType> _sampler;
    };


    typedef std::vector<osg::ref_ptr<osgAnimation::Channel> > ChannelList;

    typedef TemplateChannel<DoubleStepSampler> DoubleStepChannel;
    typedef TemplateChannel<FloatStepSampler> FloatStepChannel;
    typedef TemplateChannel<Vec2StepSampler> Vec2StepChannel;
    typedef TemplateChannel<Vec3StepSampler> Vec3StepChannel;
    typedef TemplateChannel<Vec4StepSampler> Vec4StepChannel;
    typedef TemplateChannel<QuatStepSampler> QuatStepChannel;

    typedef TemplateChannel<DoubleLinearSampler> DoubleLinearChannel;
    typedef TemplateChannel<FloatLinearSampler> FloatLinearChannel;
    typedef TemplateChannel<Vec2LinearSampler> Vec2LinearChannel;
    typedef TemplateChannel<Vec3LinearSampler> Vec3LinearChannel;
    typedef TemplateChannel<Vec4LinearSampler> Vec4LinearChannel;
    typedef TemplateChannel<QuatSphericalLinearSampler> QuatSphericalLinearChannel;
    typedef TemplateChannel<MatrixLinearSampler> MatrixLinearChannel;

    typedef TemplateChannel<FloatCubicBezierSampler> FloatCubicBezierChannel;
    typedef TemplateChannel<DoubleCubicBezierSampler> DoubleCubicBezierChannel;
    typedef TemplateChannel<Vec2CubicBezierSampler> Vec2CubicBezierChannel;
    typedef TemplateChannel<Vec3CubicBezierSampler> Vec3CubicBezierChannel;
    typedef TemplateChannel<Vec4CubicBezierSampler> Vec4CubicBezierChannel;

}

#endif