2008-11-22 20:14:19 +08:00
|
|
|
/* -*-c++-*-
|
2009-06-15 22:48:37 +08:00
|
|
|
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
|
2008-11-22 20:14:19 +08:00
|
|
|
*
|
|
|
|
* 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_H
|
|
|
|
#define OSGANIMATION_TARGET_H
|
|
|
|
|
|
|
|
#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();
|
2009-08-31 17:40:56 +08:00
|
|
|
virtual ~Target() {}
|
2009-08-26 17:24:02 +08:00
|
|
|
void reset() { _weight = 0; _priorityWeight = 0; }
|
2008-11-22 20:14:19 +08:00
|
|
|
int getCount() const { return referenceCount(); }
|
2009-08-26 17:24:02 +08:00
|
|
|
float getWeight() const { return _weight; }
|
2008-11-22 20:14:19 +08:00
|
|
|
protected:
|
|
|
|
float _weight;
|
2009-08-26 17:24:02 +08:00
|
|
|
float _priorityWeight;
|
|
|
|
int _lastPriority;
|
2008-11-22 20:14:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class TemplateTarget : public Target
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-10-02 01:08:16 +08:00
|
|
|
TemplateTarget() : _target() {}
|
2008-11-22 20:14:19 +08:00
|
|
|
TemplateTarget(const T& v) { setValue(v); }
|
2009-09-09 17:52:54 +08:00
|
|
|
TemplateTarget(const TemplateTarget& v) { setValue(v.getValue()); }
|
2008-11-22 20:14:19 +08:00
|
|
|
|
2009-08-26 17:24:02 +08:00
|
|
|
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)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-08-26 17:24:02 +08:00
|
|
|
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);
|
|
|
|
}
|
2008-11-22 20:14:19 +08:00
|
|
|
else
|
|
|
|
{
|
2009-08-26 17:24:02 +08:00
|
|
|
_priorityWeight = weight;
|
|
|
|
_lastPriority = priority;
|
|
|
|
_target = val;
|
2008-11-22 20:14:19 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-26 17:24:02 +08:00
|
|
|
const T& getValue() const { return _target; }
|
2008-11-22 20:14:19 +08:00
|
|
|
|
2009-08-26 17:24:02 +08:00
|
|
|
void setValue(const T& value) { _target = value; }
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
T _target;
|
|
|
|
};
|
|
|
|
|
2009-08-26 17:24:02 +08:00
|
|
|
template <class T>
|
|
|
|
inline void TemplateTarget<T>::lerp(float t, const T& a, const T& b)
|
|
|
|
{
|
|
|
|
_target = a * (1.0f - t) + b * t;
|
|
|
|
}
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
template <>
|
2009-08-26 17:24:02 +08:00
|
|
|
inline void TemplateTarget<osg::Quat>::lerp(float t, const osg::Quat& a, const osg::Quat& b)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-09-09 17:52:54 +08:00
|
|
|
if (a.asVec4() * b.asVec4() < 0.0)
|
|
|
|
{
|
|
|
|
_target = a * (1.0f - t) + b * -t;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_target = a * (1.0f - t) + b * t;
|
|
|
|
}
|
|
|
|
|
2009-08-26 17:24:02 +08:00
|
|
|
osg::Quat::value_type len2 = _target.length2();
|
|
|
|
if ( len2 != 1.0 && len2 != 0.0)
|
|
|
|
_target *= 1.0/sqrt(len2);
|
|
|
|
}
|
2009-09-09 17:52:54 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
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
|