From 1bdebcf174136fc7d3f8bc9b297dc951f5c8cfa1 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 27 Feb 2002 00:58:54 +0000 Subject: [PATCH] Added new osg::AnimationPath which is contaner for a set of key frames for defining an animation path. To be used with osg::Transform etc. --- VisualStudio/osg/osg.dsp | 8 ++++ include/osg/AnimationPath | 77 +++++++++++++++++++++++++++++++++++++++ include/osg/Quat | 13 ++++++- src/osg/AnimationPath.cpp | 49 +++++++++++++++++++++++++ src/osg/Makefile | 2 + 5 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 include/osg/AnimationPath create mode 100644 src/osg/AnimationPath.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index aeeef1472..538fd25e7 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -97,6 +97,10 @@ SOURCE=..\..\src\osg\AlphaFunc.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\AnimationPath.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\Billboard.cpp # End Source File # Begin Source File @@ -333,6 +337,10 @@ SOURCE=..\..\Include\Osg\AlphaFunc # End Source File # Begin Source File +SOURCE=..\..\Include\Osg\AnimationPath +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\Billboard # End Source File # Begin Source File diff --git a/include/osg/AnimationPath b/include/osg/AnimationPath new file mode 100644 index 000000000..36f62686e --- /dev/null +++ b/include/osg/AnimationPath @@ -0,0 +1,77 @@ +//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield +//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 +#include + +#include + +namespace osg { + +/** Animation Path for specify the time varying transformation pathway to use when update camera and model objects. +*/ +class SG_EXPORT AnimationPath : public osg::Referenced +{ + public: + + AnimationPath() {} + + virtual bool getMatrix(double time,Matrix& matrix); + virtual bool getInverse(double time,Matrix& matrix); + + struct Key + { + Key() {} + + Key(const osg::Vec3& position, const osg::Quat& rotation, const osg::Vec3& scale): + _position(position), + _rotation(rotation), + _scale(scale) {} + + osg::Vec3 _position; + osg::Quat _rotation; + osg::Vec3 _scale; + + inline void interpolate(const float ratio,const Key& first, const Key& second) + { + 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; + } + + inline void getMatrix(Matrix& matrix) + { + matrix.makeScale(_scale); + matrix.postMult(_rotation.getMatrix()); + matrix.postMult(osg::Matrix::translate(_position)); + } + + inline void getInverse(Matrix& matrix) + { + 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)); + } + }; + + + void insert(double time,const Key& key); + + protected: + + virtual ~AnimationPath() {} + + typedef std::map TimeKeyMap; + + TimeKeyMap _timeKeyMap; + +}; + +} + +#endif diff --git a/include/osg/Quat b/include/osg/Quat index 8ccf88681..bd2bbb3de 100644 --- a/include/osg/Quat +++ b/include/osg/Quat @@ -224,10 +224,19 @@ class SG_EXPORT Quat void slerp ( const float t, const Quat& from, const Quat& to); /** Set quaternion to be equivalent to specified matrix.*/ - void set( const osg::Matrix& m ); + void set( const Matrix& m ); /** Get the equivalent matrix for this quaternion.*/ - void get( osg::Matrix& m ) const; + void get( Matrix& m ) const; + + /** Get the equivalent matrix for this quaternion.*/ + Matrix getMatrix() const + { + Matrix matrix; + get(matrix); + return matrix; + } + friend inline std::ostream& operator << (std::ostream& output, const Quat& vec); diff --git a/src/osg/AnimationPath.cpp b/src/osg/AnimationPath.cpp new file mode 100644 index 000000000..382e6e222 --- /dev/null +++ b/src/osg/AnimationPath.cpp @@ -0,0 +1,49 @@ +#include + +using namespace osg; + +void AnimationPath::insert(double time,const Key& key) +{ + _timeKeyMap[time] = key; +} + +bool AnimationPath::getMatrix(double time,Matrix& matrix) +{ + if (_timeKeyMap.empty()) return false; + + TimeKeyMap::iterator second = _timeKeyMap.lower_bound(time); + if (second==_timeKeyMap.begin()) + { + second->second.getMatrix(matrix); + } + else if (second!=_timeKeyMap.end()) + { + TimeKeyMap::iterator first = second; + --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) + first->second.getMatrix(matrix); + else + { + Key key; + key.interpolate((time - first->first)/delta_time, + first->second, + second->second); + key.getMatrix(matrix); + } + } + else // (second==_timeKeyMap.end()) + { + _timeKeyMap.rbegin().base()->second.getMatrix(matrix); + } + return true; +} + +bool AnimationPath::getInverse(double time,Matrix& matrix) +{ +} diff --git a/src/osg/Makefile b/src/osg/Makefile index 14423ca59..844c2764c 100644 --- a/src/osg/Makefile +++ b/src/osg/Makefile @@ -3,6 +3,7 @@ include $(OSGHOME)/Make/makedefs C++FILES = \ AlphaFunc.cpp\ + AnimationPath.cpp\ Billboard.cpp\ BoundingBox.cpp\ BoundingSphere.cpp\ @@ -69,6 +70,7 @@ TARGET_LIB_FILES = lib$(TARGET_BASENAME).$(SO_EXT) TARGET_INCLUDE_FILES = \ osg/Notify\ osg/AlphaFunc\ + osg/AnimationPath\ osg/Billboard\ osg/BoundingBox\ osg/BoundingSphere\