Futher improvements and fixes to osg::AnimationPath, and the osglight
demo to show it action.
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
|
||||
#include <osg/Matrix>
|
||||
#include <osg/Quat>
|
||||
#include <osg/Transform>
|
||||
|
||||
#include <map>
|
||||
|
||||
@@ -17,29 +16,27 @@ namespace osg {
|
||||
* Subclassed from Transform::ComputeTransformCallback allows AnimationPath to
|
||||
* be attached directly to Transform nodes to move subgraphs around the scene.
|
||||
*/
|
||||
class SG_EXPORT AnimationPath : public Transform::ComputeTransformCallback
|
||||
class SG_EXPORT AnimationPath : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
|
||||
AnimationPath() {}
|
||||
AnimationPath():_loopMode(LOOP) {}
|
||||
|
||||
/** get the local transformation matrix for a point in time.*/
|
||||
virtual bool getMatrix(double time,Matrix& matrix) const;
|
||||
|
||||
/** get the local inverse transformation matrix for a point in time.*/
|
||||
virtual bool getInverse(double time,Matrix& matrix) const;
|
||||
|
||||
/** Get the transformation matrix which moves from local coords to world coords.*/
|
||||
virtual const bool computeLocalToWorldMatrix(Matrix& matrix,const Transform* transform, NodeVisitor* nv) const;
|
||||
|
||||
/** Get the transformation matrix which moves from world coords to local coords.*/
|
||||
virtual const bool computeWorldToLocalMatrix(Matrix& matrix,const Transform* transform, NodeVisitor* nv) const;
|
||||
|
||||
struct Key
|
||||
struct ControlPoint
|
||||
{
|
||||
Key() {}
|
||||
ControlPoint() {}
|
||||
|
||||
Key(const osg::Vec3& position, const osg::Quat& rotation, const osg::Vec3& scale):
|
||||
ControlPoint(const osg::Vec3& position):
|
||||
_position(position),
|
||||
_rotation(),
|
||||
_scale() {}
|
||||
|
||||
ControlPoint(const osg::Vec3& position, const osg::Quat& rotation):
|
||||
_position(position),
|
||||
_rotation(rotation),
|
||||
_scale() {}
|
||||
|
||||
ControlPoint(const osg::Vec3& position, const osg::Quat& rotation, const osg::Vec3& scale):
|
||||
_position(position),
|
||||
_rotation(rotation),
|
||||
_scale(scale) {}
|
||||
@@ -48,7 +45,7 @@ class SG_EXPORT AnimationPath : public Transform::ComputeTransformCallback
|
||||
osg::Quat _rotation;
|
||||
osg::Vec3 _scale;
|
||||
|
||||
inline void interpolate(const float ratio,const Key& first, const Key& second)
|
||||
inline void interpolate(const float ratio,const ControlPoint& first, const ControlPoint& second)
|
||||
{
|
||||
float one_minus_ratio = 1.0f-ratio;
|
||||
_position = first._position*one_minus_ratio + second._position*ratio;
|
||||
@@ -71,16 +68,53 @@ class SG_EXPORT AnimationPath : public Transform::ComputeTransformCallback
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
void insert(double time,const Key& key);
|
||||
/** get the local ControlPoint frame for a point in time.*/
|
||||
virtual bool getInterpolatedControlPoint(double time,ControlPoint& ControlPoint) const;
|
||||
|
||||
void insert(double time,const ControlPoint& ControlPoint);
|
||||
|
||||
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; }
|
||||
|
||||
LoopMode getLoopMode() const { return _loopMode; }
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~AnimationPath() {}
|
||||
|
||||
typedef std::map<double,Key> TimeKeyMap;
|
||||
typedef std::map<double,ControlPoint> TimeControlPointMap;
|
||||
|
||||
TimeKeyMap _timeKeyMap;
|
||||
TimeControlPointMap _timeControlPointMap;
|
||||
LoopMode _loopMode;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -84,30 +84,11 @@ class SG_EXPORT Billboard : public Geode
|
||||
virtual const bool removeDrawable( Drawable *gset );
|
||||
|
||||
|
||||
/** Callback attached to an Billboard which allows the users to customize the billboard orientation calculation during cull traversal.*/
|
||||
struct ComputeBillboardCallback : public osg::Referenced
|
||||
{
|
||||
/** Get the transformation matrix which moves from local coords to world coords.*/
|
||||
virtual const bool computeMatrix(Matrix& modelview, const Billboard* billboard, const Vec3& eye_local, const Vec3& pos_local) const = 0;
|
||||
};
|
||||
|
||||
/** Set the ComputeBillboardCallback which allows users to attach custom computation of the local transformation as
|
||||
* seen by cull traversers and alike.*/
|
||||
void setComputeBillboardCallback(ComputeBillboardCallback* ctc) { _computeBillboardCallback=ctc; }
|
||||
|
||||
/** Get the non const ComputeBillboardCallback.*/
|
||||
ComputeBillboardCallback* getComputeBillboardCallback() { return _computeBillboardCallback.get(); }
|
||||
|
||||
/** Get the const ComputeBillboardCallback.*/
|
||||
const ComputeBillboardCallback* getComputeBillboardCallback() const { return _computeBillboardCallback.get(); }
|
||||
|
||||
|
||||
inline const bool getMatrix(Matrix& modelview, const Vec3& eye_local, const Vec3& pos_local) const
|
||||
{
|
||||
if (_computeBillboardCallback.valid())
|
||||
return _computeBillboardCallback->computeMatrix(modelview,this,eye_local,pos_local);
|
||||
else
|
||||
return computeMatrix(modelview,eye_local,pos_local);
|
||||
return computeMatrix(modelview,eye_local,pos_local);
|
||||
}
|
||||
|
||||
virtual const bool computeMatrix(Matrix& modelview, const Vec3& eye_local, const Vec3& pos_local) const;
|
||||
@@ -131,7 +112,6 @@ class SG_EXPORT Billboard : public Geode
|
||||
Vec3 _axis;
|
||||
Vec3 _normal;
|
||||
PositionList _positionList;
|
||||
ref_ptr<ComputeBillboardCallback> _computeBillboardCallback;
|
||||
|
||||
// used internally as cache of which what _axis is aligned to help
|
||||
// deicde which method of rotation to use.
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define OSG_MATRIXTRANSFORM 1
|
||||
|
||||
#include <osg/Transform>
|
||||
#include <osg/AnimationPath>
|
||||
|
||||
namespace osg {
|
||||
|
||||
@@ -78,6 +79,23 @@ class SG_EXPORT MatrixTransform : public Transform
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Callback which can be attached to a MatrixTransform as an app
|
||||
* callback to allow it to follow the path defined by a AnimationPath.*/
|
||||
class AnimationPathCallback : public NodeCallback
|
||||
{
|
||||
public:
|
||||
|
||||
AnimationPathCallback(AnimationPath* ap):
|
||||
_animationPath(ap),
|
||||
_firstTime(0.0) {}
|
||||
|
||||
/** implements the callback*/
|
||||
virtual void operator()(Node* node, NodeVisitor* nv);
|
||||
|
||||
ref_ptr<AnimationPath> _animationPath;
|
||||
double _firstTime;
|
||||
};
|
||||
|
||||
protected :
|
||||
|
||||
virtual ~MatrixTransform();
|
||||
|
||||
@@ -7,11 +7,12 @@
|
||||
|
||||
#include <osg/Group>
|
||||
#include <osg/Transform>
|
||||
#include <osg/AnimationPath>
|
||||
#include <osg/Quat>
|
||||
|
||||
namespace osg {
|
||||
|
||||
/** PositionAttitideTransform - is Transfrom the set the coordinates transform
|
||||
/** PositionAttitideTransform - is Transform the set the coordinates transform
|
||||
up via a Vec3 position and Quat attitude.
|
||||
*/
|
||||
class SG_EXPORT PositionAttitudeTransform : public Transform
|
||||
@@ -48,6 +49,26 @@ class SG_EXPORT PositionAttitudeTransform : public Transform
|
||||
|
||||
virtual const bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor* nv) const;
|
||||
|
||||
|
||||
/** Callback which can be attached to a PositionAttitudeTransform
|
||||
* as an app callback to allow it to follow the path defined by a
|
||||
* AnimationPath.*/
|
||||
class AnimationPathCallback : public NodeCallback
|
||||
{
|
||||
public:
|
||||
|
||||
AnimationPathCallback(AnimationPath* ap):
|
||||
_animationPath(ap),
|
||||
_firstTime(0.0) {}
|
||||
|
||||
/** implements the callback*/
|
||||
virtual void operator()(Node* node, NodeVisitor* nv);
|
||||
|
||||
ref_ptr<AnimationPath> _animationPath;
|
||||
double _firstTime;
|
||||
};
|
||||
|
||||
|
||||
protected :
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user