From Cedric Pinson and Jeremey Moles, Changes to OpenSceneGraph-osgWidget-dev branch.

Notes from Robert Osfield, Merged changes to OpenSceneGraph-osgWidget-dev r9367 (prior to my botched attempt at merged svn/trunk into the branch).
This commit is contained in:
Robert Osfield
2008-12-16 20:29:00 +00:00
parent 3313327ab4
commit 60fc821764
36 changed files with 1218 additions and 836 deletions

View File

@@ -15,6 +15,12 @@
#ifndef OSGANIMATION_EASE_MOTION_H
#define OSGANIMATION_EASE_MOTION_H
#include <osg/Referenced>
#include <osg/ref_ptr>
#include <osg/Notify>
#include <osg/Math>
#include <vector>
namespace osgAnimation {
@@ -156,7 +162,7 @@ namespace osgAnimation {
class Motion
class Motion : public osg::Referenced
{
public:
typedef float value_type;
@@ -165,28 +171,36 @@ namespace osgAnimation {
CLAMP,
LOOP,
};
Motion(float startValue = 0, float duration = 1, float changeValue = 1, TimeBehaviour tb = CLAMP) : _time(0), _b(startValue), _c(changeValue), _d(duration), _behaviour(tb) {}
Motion(float startValue = 0, float duration = 1, float changeValue = 1, TimeBehaviour tb = CLAMP) : _time(0), _startValue(startValue), _changeValue(changeValue), _duration(duration), _behaviour(tb) {}
virtual ~Motion() {}
void reset() { setTime(0);}
float getTime() const { return _time; }
void update(float dt)
float evaluateTime(float time) const
{
_time += dt;
switch (_behaviour)
{
case CLAMP:
if (_time > _d)
_time = _d;
else if (_time < 0.0)
_time = 0.0;
if (time > _duration)
time = _duration;
else if (time < 0.0)
time = 0.0;
break;
case LOOP:
_time = fmodf(_time, _d);
if (time <= 0)
time = 0;
else
time = fmodf(time, _duration);
break;
}
return time;
}
void update(float dt)
{
_time = evaluateTime(_time + dt);
}
void setTime(float time) { _time = time; update(0);}
void setTime(float time) { _time = evaluateTime(time);}
void getValue(value_type& result) const { getValueAt(_time, result); }
value_type getValue() const
{
@@ -197,23 +211,24 @@ namespace osgAnimation {
void getValueAt(float time, value_type& result) const
{
getValueInNormalizedRange(time/_d, result);
result = result * _c + _b;
getValueInNormalizedRange(evaluateTime(time)/_duration, result);
result = result * _changeValue + _startValue;
}
value_type getValueAt(float time) const
{
value_type result;
getValueAt(time, result);
getValueAt(evaluateTime(time), result);
return result;
}
virtual void getValueInNormalizedRange(float t, value_type& result) const = 0;
float getDuration() const { return _duration;}
protected:
float _time;
float _b;
float _c;
float _d;
float _startValue;
float _changeValue;
float _duration;
TimeBehaviour _behaviour;
};
@@ -246,6 +261,39 @@ namespace osgAnimation {
}
};
struct CompositeMotion : public Motion
{
typedef std::vector<osg::ref_ptr<Motion> > MotionList;
MotionList _motions;
MotionList& getMotionList() { return _motions; }
const MotionList& getMotionList() const { return _motions; }
CompositeMotion(float startValue = 0, float duration = 1, float changeValue = 1, TimeBehaviour tb = CLAMP) : Motion(startValue, duration, changeValue, tb) {}
virtual void getValueInNormalizedRange(float t, value_type& result) const
{
if (_motions.empty())
{
result = 0;
osg::notify(osg::WARN) << "CompositeMotion::getValueInNormalizedRange no Motion in the CompositeMotion, add motion to have result" << std::endl;
return;
}
for (MotionList::const_iterator it = _motions.begin(); it != _motions.end(); it++)
{
const Motion* motion = static_cast<const Motion*>(it->get());
float durationInRange = motion->getDuration() / getDuration();
if (t < durationInRange)
{
float tInRange = t/durationInRange * motion->getDuration();
motion->getValueAt( tInRange, result);
return;
} else
t = t - durationInRange;
}
osg::notify(osg::WARN) << "CompositeMotion::getValueInNormalizedRange did find the value in range, something wrong" << std::endl;
result = 0;
}
};
// linear