class InOutCircFunction : public EasingFunction
InOutCircFunction是OpenSceneGraph osgAnimation库中实现的一种缓动函数(Easing Function),该函数可用于简单的动画效果的生成。
在缓动函数中,输入参数为t,输出参数为val,其中t在0到1之间变化。val的变化取决于所选的缓动函数类型,InOutCircFunction的计算公式如下:
if (t < 0.5)
{
t *= 2.0;
return -0.5 * (sqrt(1 - t * t) - 1);
}
else
{
t = (t - 0.5) * 2.0;
return 0.5 * (sqrt(1 - t * t) + 1);
}
该函数的作用是产生一种类似于从慢到快,再从快到慢的过渡效果。
接口:
class InOutCircFunction : public EasingFunction
{
public:
InOutCircFunction(): EasingFunction() {}
InOutCircFunction(const InOutCircFunction& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY) : EasingFunction(copy, copyop) {}
META_Object(osgAnimation, InOutCircFunction)
virtual double operator()(double t)
{
if (t < 0.5)
{
t *= 2.0;
return -0.5 * (sqrt(1 - t*t) - 1);
}
else
{
t = (t - 0.5) * 2.0;
return 0.5 * (sqrt(1 - t*t) + 1);
}
}
protected:
virtual ~InOutCircFunction() {}
};
构造函数:
InOutCircFunction()
:构造一个新的InOutCircFunction对象。InOutCircFunction(const InOutCircFunction& copy, const osg::CopyOp& copyop)
:根据传入的InOutCircFunction对象和常规拷贝选项创建一个新的InOutCircFunction对象。虚函数:
virtual double operator()(double t)
:计算经过缓动函数处理的输出值。传入的参数t
取值在0到1之间,表示输入值的比例。保护虚析构函数:
virtual ~InOutCircFunction()
:析构函数。元数据:
META_Object(osgAnimation, InOutCircFunction)
:对象类型的元数据。示例代码:
#include <osgAnimation/EaseMotion>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
int main(int argc, char **argv)
{
//创建根节点
osg::ref_ptr<osg::Group> root = new osg::Group;
//读取飞机模型
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cessna.osg.0,0,90.rot");
//为飞机设置动画路径
osg::ref_ptr<osgAnimation::Motion> motion = osgAnimation::createDiscreteMotion(0, 10, 6);
osg::ref_ptr<osgAnimation::Vec3LinearSampler> keys = new osgAnimation::Vec3LinearSampler;
keys->addTime(0.0, osg::Vec3(0.0, 0.0, 0.0));
keys->addTime(4.0, osg::Vec3(40.0, 0.0, 0.0));
keys->addTime(6.0, osg::Vec3(60.0, 0.0, 20.0));
motion->setPlayMode(osgAnimation::Motion::LOOP);
motion->addSampler(keys);
//创建飞机动画组,并将模型和动画路径添加到其中
osg::ref_ptr<osg::AnimationPathCallback> apc = new osg::AnimationPathCallback(motion);
osg::ref_ptr<osg::Group> animGroup = new osg::Group;
animGroup->addChild(model.get());
animGroup->setUpdateCallback(apc.get());
//为动画组设置缓动函数
osg::ref_ptr<osgAnimation::InOutCircFunction> easing = new osgAnimation::InOutCircFunction;
osg::ref_ptr<osgAnimation::EaseMotion> easeMotion = new osgAnimation::EaseMotion(easing.get(), motion.get());
apc->setAnimationPath(easeMotion.get());
//添加动画组到根节点
root->addChild(animGroup.get());
//创建查看器
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
//运行查看器
return viewer.run();
}