Target 3.83 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
/*  -*-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.
*/


#ifndef OSGANIMATION_TARGET
#define OSGANIMATION_TARGET 1

#include <vector>
#include <osg/Quat>
#include <osg/Vec3>
#include <osg/Vec2>
#include <osg/Vec4>
#include <osg/Referenced>
#include <osgAnimation/Export>

namespace osgAnimation 
{

    class Channel;

    class OSGANIMATION_EXPORT Target : public osg::Referenced
    {
    public:
            
        Target();
        virtual ~Target() {}
        void reset() { _weight = 0; _priorityWeight = 0; }
        int getCount() const { return referenceCount(); }
        float getWeight() const { return _weight; }
    protected:
        float _weight;
        float _priorityWeight;
        int _lastPriority;
    };


    template <class T>
    class TemplateTarget : public Target
    {
    public:
            
        TemplateTarget() : _target() {}
        TemplateTarget(const T& v) { setValue(v); }
        TemplateTarget(const TemplateTarget& v) { setValue(v.getValue()); }

        inline void lerp(float t, const T& a, const T& b);

        /**
         *  The priority is used to detect a change of priority
         *  It's important to update animation target in priority
         *  order. eg:
         *  all animation with priority 1
         *  all animation with priority 0
         *  all animation with priority -1
         *  ...
         */
        void update(float weight, const T& val, int priority)
        {
            if (_weight || _priorityWeight)
            {
                if (_lastPriority != priority)
                {
                    // change in priority
                    // add to weight with the same previous priority cumulated weight
                    _weight += _priorityWeight * (1.0 - _weight);
                    _priorityWeight = 0;
                    _lastPriority = priority;
                }

                _priorityWeight += weight;
                float t = (1.0 - _weight) * weight / _priorityWeight;
                lerp(t, _target, val);
            }
            else
            {
                _priorityWeight = weight;
                _lastPriority = priority;
                _target = val;
            }
        }
        const T& getValue() const { return _target; }

        void setValue(const T& value) { _target = value; }

    protected:

        T _target;
    };

    template <class T>
    inline void TemplateTarget<T>::lerp(float t, const T& a, const T& b)
    {
        _target = a * (1.0f - t) + b * t;
    }

    template <>
    inline void TemplateTarget<osg::Quat>::lerp(float t, const osg::Quat& a, const osg::Quat& b)
    {
        if (a.asVec4() * b.asVec4() < 0.0)
        {
            _target = a * (1.0f - t) + b * -t;
        }
        else
        {
            _target = a * (1.0f - t) + b * t;
        }

        osg::Quat::value_type len2 = _target.length2();
        if ( len2 != 1.0 && len2 != 0.0) 
            _target *= 1.0/sqrt(len2);
    }

    typedef TemplateTarget<osg::Matrixf> MatrixTarget;
    typedef TemplateTarget<osg::Quat> QuatTarget;
    typedef TemplateTarget<osg::Vec3> Vec3Target;
    typedef TemplateTarget<osg::Vec4> Vec4Target;
    typedef TemplateTarget<osg::Vec2> Vec2Target;
    typedef TemplateTarget<float> FloatTarget;
    typedef TemplateTarget<double> DoubleTarget;
  
}

#endif