2002-07-17 04:07:32 +08:00
|
|
|
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
|
2002-02-27 08:58:54 +08:00
|
|
|
//Distributed under the terms of the GNU Library General Public License (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
#ifndef OSG_ANIMATIONPATH
|
|
|
|
#define OSG_ANIMATIONPATH 1
|
|
|
|
|
|
|
|
#include <osg/Matrix>
|
|
|
|
#include <osg/Quat>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2002-04-22 06:05:26 +08:00
|
|
|
/** AnimationPath for specify the time varying transformation pathway to use when update camera and model objects.
|
|
|
|
* Subclassed from Transform::ComputeTransformCallback allows AnimationPath to
|
|
|
|
* be attached directly to Transform nodes to move subgraphs around the scene.
|
2002-02-27 08:58:54 +08:00
|
|
|
*/
|
2002-12-16 17:55:30 +08:00
|
|
|
class SG_EXPORT AnimationPath : public virtual osg::Object
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
AnimationPath():_loopMode(LOOP) {}
|
2002-04-22 06:05:26 +08:00
|
|
|
|
2002-12-16 17:55:30 +08:00
|
|
|
AnimationPath(const AnimationPath& ap, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
|
|
|
|
Object(ap,copyop),
|
|
|
|
_timeControlPointMap(ap._timeControlPointMap),
|
|
|
|
_loopMode(ap._loopMode) {}
|
|
|
|
|
|
|
|
META_Object(osg,AnimationPath)
|
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
struct ControlPoint
|
|
|
|
{
|
2002-10-08 04:01:28 +08:00
|
|
|
ControlPoint():
|
|
|
|
_scale(1.0f,1.0f,1.0f) {}
|
2002-04-22 06:05:26 +08:00
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
ControlPoint(const osg::Vec3& position):
|
|
|
|
_position(position),
|
|
|
|
_rotation(),
|
2002-10-08 04:01:28 +08:00
|
|
|
_scale(1.0f,1.0f,1.0f) {}
|
2002-04-22 06:05:26 +08:00
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
ControlPoint(const osg::Vec3& position, const osg::Quat& rotation):
|
|
|
|
_position(position),
|
|
|
|
_rotation(rotation),
|
2002-10-08 04:01:28 +08:00
|
|
|
_scale(1.0f,1.0f,1.0f) {}
|
2002-02-27 08:58:54 +08:00
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
ControlPoint(const osg::Vec3& position, const osg::Quat& rotation, const osg::Vec3& scale):
|
2002-02-27 08:58:54 +08:00
|
|
|
_position(position),
|
|
|
|
_rotation(rotation),
|
|
|
|
_scale(scale) {}
|
|
|
|
|
|
|
|
osg::Vec3 _position;
|
|
|
|
osg::Quat _rotation;
|
|
|
|
osg::Vec3 _scale;
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline void interpolate(float ratio,const ControlPoint& first, const ControlPoint& second)
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
|
|
|
float one_minus_ratio = 1.0f-ratio;
|
|
|
|
_position = first._position*one_minus_ratio + second._position*ratio;
|
|
|
|
_rotation.slerp(ratio,first._rotation,second._rotation);
|
|
|
|
_scale = first._scale*one_minus_ratio + second._scale*ratio;
|
|
|
|
}
|
|
|
|
|
2002-04-22 06:05:26 +08:00
|
|
|
inline void getMatrix(Matrix& matrix) const
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
|
|
|
matrix.makeScale(_scale);
|
|
|
|
matrix.postMult(_rotation.getMatrix());
|
|
|
|
matrix.postMult(osg::Matrix::translate(_position));
|
|
|
|
}
|
|
|
|
|
2002-04-22 06:05:26 +08:00
|
|
|
inline void getInverse(Matrix& matrix) const
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
|
|
|
matrix.makeScale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.y());
|
|
|
|
matrix.postMult(_rotation.inverse().getMatrix());
|
|
|
|
matrix.postMult(osg::Matrix::translate(-_position));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
|
|
|
|
/** get the transformation matrix for a point in time.*/
|
|
|
|
bool getMatrix(double time,Matrix& matrix) const
|
|
|
|
{
|
|
|
|
ControlPoint cp;
|
|
|
|
if (!getInterpolatedControlPoint(time,cp)) return false;
|
|
|
|
cp.getMatrix(matrix);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** get the inverse transformation matrix for a point in time.*/
|
|
|
|
bool getInverse(double time,Matrix& matrix) const
|
|
|
|
{
|
|
|
|
ControlPoint cp;
|
|
|
|
if (!getInterpolatedControlPoint(time,cp)) return false;
|
|
|
|
cp.getInverse(matrix);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** get the local ControlPoint frame for a point in time.*/
|
2002-10-10 22:58:44 +08:00
|
|
|
virtual bool getInterpolatedControlPoint(double time,ControlPoint& controlPoint) const;
|
2002-08-13 23:31:10 +08:00
|
|
|
|
2002-10-10 22:58:44 +08:00
|
|
|
void insert(double time,const ControlPoint& controlPoint);
|
2002-08-13 23:31:10 +08:00
|
|
|
|
|
|
|
double getFirstTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.begin()->first; else return 0.0;}
|
|
|
|
double getLastTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.rbegin()->first; else return 0.0;}
|
|
|
|
double getPeriod() const { return getLastTime()-getFirstTime();}
|
|
|
|
|
|
|
|
enum LoopMode
|
|
|
|
{
|
|
|
|
SWING,
|
|
|
|
LOOP,
|
|
|
|
NO_LOOPING
|
|
|
|
};
|
|
|
|
|
|
|
|
void setLoopMode(LoopMode lm) { _loopMode = lm; }
|
2002-02-27 08:58:54 +08:00
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
LoopMode getLoopMode() const { return _loopMode; }
|
2002-02-27 08:58:54 +08:00
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
|
|
|
|
typedef std::map<double,ControlPoint> TimeControlPointMap;
|
|
|
|
|
|
|
|
TimeControlPointMap& getTimeControlPointMap() { return _timeControlPointMap; }
|
|
|
|
|
|
|
|
const TimeControlPointMap& getTimeControlPointMap() const { return _timeControlPointMap; }
|
|
|
|
|
2002-02-27 08:58:54 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~AnimationPath() {}
|
|
|
|
|
2002-08-13 23:31:10 +08:00
|
|
|
TimeControlPointMap _timeControlPointMap;
|
|
|
|
LoopMode _loopMode;
|
2002-02-27 08:58:54 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|