2002-02-27 08:58:54 +08:00
|
|
|
#include <osg/AnimationPath>
|
2002-04-22 06:05:26 +08:00
|
|
|
#include <osg/NodeVisitor>
|
2002-02-27 08:58:54 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
void AnimationPath::insert(double time,const ControlPoint& ControlPoint)
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
_timeControlPointMap[time] = ControlPoint;
|
2002-02-27 08:58:54 +08:00
|
|
|
}
|
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
bool AnimationPath::getInterpolatedControlPoint(double time,ControlPoint& controlPoint) const
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
if (_timeControlPointMap.empty()) return false;
|
|
|
|
|
|
|
|
switch(_loopMode)
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
case(SWING):
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
double modulated_time = (time - getFirstTime())/(getPeriod()*2.0);
|
|
|
|
double fraction_part = modulated_time - floor(modulated_time);
|
|
|
|
if (fraction_part>0.5) fraction_part = 1.0-fraction_part;
|
|
|
|
|
|
|
|
time = (fraction_part*2.0) * getPeriod();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(LOOP):
|
2002-03-01 17:29:56 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
double modulated_time = (time - getFirstTime())/getPeriod();
|
|
|
|
double fraction_part = modulated_time - floor(modulated_time);
|
|
|
|
time = fraction_part * getPeriod();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(NO_LOOPING):
|
|
|
|
// no need to modulate the time.
|
|
|
|
break;
|
2002-03-01 17:29:56 +08:00
|
|
|
}
|
2002-08-13 23:31:10 +08:00
|
|
|
|
|
|
|
|
2002-08-13 21:22:46 +08:00
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
TimeControlPointMap::const_iterator second = _timeControlPointMap.lower_bound(time);
|
|
|
|
if (second==_timeControlPointMap.begin())
|
2002-04-22 06:05:26 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
controlPoint = second->second;
|
2002-04-22 06:05:26 +08:00
|
|
|
}
|
2002-08-13 23:31:10 +08:00
|
|
|
else if (second!=_timeControlPointMap.end())
|
2002-04-22 06:05:26 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
TimeControlPointMap::const_iterator first = second;
|
2002-08-13 21:22:46 +08:00
|
|
|
--first;
|
|
|
|
|
|
|
|
// we have both a lower bound and the next item.
|
|
|
|
|
|
|
|
// deta_time = second.time - first.time
|
|
|
|
double delta_time = second->first - first->first;
|
|
|
|
|
|
|
|
if (delta_time==0.0)
|
2002-08-13 23:31:10 +08:00
|
|
|
controlPoint = first->second;
|
2002-08-13 21:22:46 +08:00
|
|
|
else
|
2002-04-22 06:05:26 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
controlPoint.interpolate((time - first->first)/delta_time,
|
2002-08-13 21:22:46 +08:00
|
|
|
first->second,
|
|
|
|
second->second);
|
|
|
|
}
|
2002-04-22 06:05:26 +08:00
|
|
|
}
|
2002-08-13 23:31:10 +08:00
|
|
|
else // (second==_timeControlPointMap.end())
|
2002-08-13 21:22:46 +08:00
|
|
|
{
|
2002-08-13 23:31:10 +08:00
|
|
|
controlPoint = _timeControlPointMap.rbegin()->second;
|
2002-08-13 21:22:46 +08:00
|
|
|
}
|
|
|
|
return true;
|
2002-04-22 06:05:26 +08:00
|
|
|
}
|