2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-02-27 08:58:54 +08:00
|
|
|
#include <osg/AnimationPath>
|
2003-01-03 04:10:04 +08:00
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
2002-02-27 08:58:54 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2002-10-10 22:58:44 +08:00
|
|
|
void AnimationPath::insert(double time,const ControlPoint& controlPoint)
|
2002-02-27 08:58:54 +08:00
|
|
|
{
|
2002-10-10 22:58:44 +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;
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
time = getFirstTime()+(fraction_part*2.0) * getPeriod();
|
2002-08-13 23:31:10 +08:00
|
|
|
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);
|
2002-11-06 18:24:33 +08:00
|
|
|
time = getFirstTime()+fraction_part * getPeriod();
|
2002-08-13 23:31:10 +08:00
|
|
|
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
|
|
|
}
|
2003-01-03 04:10:04 +08:00
|
|
|
|
|
|
|
|
2003-11-04 07:13:31 +08:00
|
|
|
void AnimationPath::read(std::istream& in)
|
|
|
|
{
|
|
|
|
while (!in.eof())
|
|
|
|
{
|
|
|
|
double time;
|
|
|
|
osg::Vec3 position;
|
|
|
|
osg::Quat rotation;
|
|
|
|
in >> time >> position.x() >> position.y() >> position.z() >> rotation.x() >> rotation.y() >> rotation.z() >> rotation.w();
|
|
|
|
if(!in.eof())
|
|
|
|
insert(time,osg::AnimationPath::ControlPoint(position,rotation));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationPath::write(std::ostream& fout)
|
|
|
|
{
|
|
|
|
const TimeControlPointMap& tcpm = getTimeControlPointMap();
|
|
|
|
for(TimeControlPointMap::const_iterator tcpmitr=tcpm.begin();
|
|
|
|
tcpmitr!=tcpm.end();
|
|
|
|
++tcpmitr)
|
|
|
|
{
|
|
|
|
const ControlPoint& cp = tcpmitr->second;
|
|
|
|
fout<<tcpmitr->first<<" "<<cp._position<<" "<<cp._rotation<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-04 05:42:02 +08:00
|
|
|
class AnimationPathCallbackVisitor : public NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2003-11-04 07:13:31 +08:00
|
|
|
AnimationPathCallbackVisitor(const AnimationPath::ControlPoint& cp, bool useInverseMatrix):
|
|
|
|
_cp(cp),
|
|
|
|
_useInverseMatrix(useInverseMatrix) {}
|
2003-01-04 05:42:02 +08:00
|
|
|
|
|
|
|
virtual void apply(MatrixTransform& mt)
|
|
|
|
{
|
|
|
|
Matrix matrix;
|
2003-11-04 07:13:31 +08:00
|
|
|
if (_useInverseMatrix)
|
|
|
|
_cp.getInverse(matrix);
|
|
|
|
else
|
|
|
|
_cp.getMatrix(matrix);
|
|
|
|
|
2003-01-04 05:42:02 +08:00
|
|
|
mt.setMatrix(matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void apply(PositionAttitudeTransform& pat)
|
|
|
|
{
|
2003-11-04 07:13:31 +08:00
|
|
|
if (_useInverseMatrix)
|
|
|
|
{
|
|
|
|
Matrix matrix;
|
|
|
|
_cp.getInverse(matrix);
|
|
|
|
pat.setPosition(matrix.getTrans());
|
|
|
|
pat.setAttitude(_cp._rotation.inverse());
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pat.setPosition(_cp._position);
|
|
|
|
pat.setAttitude(_cp._rotation);
|
|
|
|
}
|
2003-01-04 05:42:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimationPath::ControlPoint _cp;
|
2003-11-04 07:13:31 +08:00
|
|
|
bool _useInverseMatrix;
|
2003-01-04 05:42:02 +08:00
|
|
|
};
|
|
|
|
|
2003-01-03 04:10:04 +08:00
|
|
|
void AnimationPathCallback::operator()(Node* node, NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
if (_animationPath.valid() &&
|
|
|
|
nv->getVisitorType()==NodeVisitor::UPDATE_VISITOR &&
|
|
|
|
nv->getFrameStamp())
|
|
|
|
{
|
|
|
|
double time = nv->getFrameStamp()->getReferenceTime();
|
2003-11-04 07:13:31 +08:00
|
|
|
_latestTime = time;
|
|
|
|
|
|
|
|
if (!_pause)
|
2003-01-04 05:42:02 +08:00
|
|
|
{
|
2004-01-12 21:53:04 +08:00
|
|
|
// Only update _firstTime the first time, when its value is still DBL_MAX
|
|
|
|
if (_firstTime==DBL_MAX) _firstTime = time;
|
2003-11-04 07:13:31 +08:00
|
|
|
update(*node);
|
2003-01-04 05:42:02 +08:00
|
|
|
}
|
2003-01-03 04:10:04 +08:00
|
|
|
}
|
2003-11-04 07:13:31 +08:00
|
|
|
|
2003-01-03 04:10:04 +08:00
|
|
|
// must call any nested node callbacks and continue subgraph traversal.
|
|
|
|
NodeCallback::traverse(node,nv);
|
|
|
|
}
|
2003-11-04 07:13:31 +08:00
|
|
|
|
|
|
|
void AnimationPathCallback::update(osg::Node& node)
|
|
|
|
{
|
|
|
|
double animationTime = ((_latestTime-_firstTime)-_timeOffset)*_timeMultiplier;
|
|
|
|
|
|
|
|
AnimationPath::ControlPoint cp;
|
|
|
|
if (_animationPath->getInterpolatedControlPoint(animationTime,cp))
|
|
|
|
{
|
|
|
|
AnimationPathCallbackVisitor apcv(cp,_useInverseMatrix);
|
|
|
|
node.accept(apcv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AnimationPathCallback::reset()
|
|
|
|
{
|
|
|
|
_firstTime = _latestTime;
|
|
|
|
_pauseTime = _latestTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationPathCallback::setPause(bool pause)
|
|
|
|
{
|
|
|
|
if (_pause==pause)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_pause = pause;
|
|
|
|
if (_pause)
|
|
|
|
{
|
|
|
|
_pauseTime = _latestTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_firstTime += (_latestTime-_pauseTime);
|
|
|
|
}
|
|
|
|
}
|