目录
1. 前言
2. 生成动画路径文件
2.1. 粗糙方式
2.2. 精确方式
在进行osg的开发中,有时需要对模型按某个路径或规则进行动画,如下:
奶牛在10秒时间段从起始的osg::Vec3d(0.0, 18, 1.0)位置 匀速直线运动到osg::Vec3d(0.0, -8, 1.0)位置处。又比如下:
飞机以和Y轴成倾斜45°角绕着Z轴在地面一定高度做圆周盘旋运动。
一般地,为相机设置osgGA::AnimationPathManipulator操控器,或者为osg::PositionAttitudeTransform、osg::MatrixTransform节点通过调用setUpdateCallback函数设置动画路径回调函数osg::AnimationPathCallback类对象就可以达到动画的效果。当动画生成的时候,需要将动画路径的数据如控制点等信息,以文件形式保存下来,以供其它程序使用。如何生成这样的动画路径文件?
如果需求对动画路径要求不精确,如:没有要求具体的时间段、没有要求具体的转动角度、移动距离等, 仅仅只是要求移动或转动到估计的位置或角度就行,则可以用osg自带的在OsgViewerd.exe(release版为OsgViewer.exe)按下述步骤生成动画路径文件(以Windows为例说明):
1. 打开cmd,输入:
E:\osg\build_osg3.6.2\bin\OsgViewerd.exe E:\osg\OpenSceneGraph-Data\cow.osg
其中的两个路径,请根据实际更改。如下为cmd输入的命令行:
2. 按下键盘小写键z,开始记录动画路径,依照需求,在场景视图上按住鼠标移动或转动奶牛模型,直到达到预想的动画,然后按住大写"Z"保存路径文件,按住ESC键关闭场景视图窗口,此时就会在上述红色框的目录即启动cmd.exe的目录下生成saved_animation.path的动画路径文件,文件内容类似如下:
0.0018253 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.0667249 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.1337192 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.1998216 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.2653721 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.3338356 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.4002481 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.4664029 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.5332971 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.5996751 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.6665445 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.7353267 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.7997468 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.8663566 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.9328836 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
0.9993953 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.0663357 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.1383031 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.2032655 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.2654628 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.3326584 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.3995767 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.4658965 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.5320493 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.5997768 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.6665459 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.7327271 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.7986692 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.8654239 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
1.9356403 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
2.0001472 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
2.0880553 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
2.1488845 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
这样动画路径文件就生成了。
对于2.1节提到的方法,仅仅只能用于粗略估计的平移和旋转,对于精确的,则必须通过编程及数学算出每个时刻及该时刻的控制点,然后保存到文件。以前面的奶牛需求为例子,代码实现如下:
#include
#include
#include
#include
#include
#include
osg::AnimationPath* createAnimationPath(const osg::Vec3d& startPos, const osg::Vec3d& endPos, double time)
{
auto pAnimationPath = new osg::AnimationPath;
pAnimationPath->setLoopMode(osg::AnimationPath::LOOP);
auto timeDeta = 0.2;
const auto loopCount = time / timeDeta;
// 这里假定模型在x、y、z方向上运动的速度都一样且是匀速直线运动
auto xStep = (endPos.x() - startPos.x()) / loopCount; // 每个时间段前进的距离
auto yStep = (endPos.y() - startPos.y()) / loopCount;
auto zStep = (endPos.z() - startPos.z()) / loopCount;
double angle = 0.0;
double twoPi = 2 * 3.1415926;
osg::Vec3d lastPos = startPos;
osg::Vec3d pos = startPos;
for (auto timeOffset = 0.0; timeOffset <= time; timeOffset += timeDeta)
{
if (0.0 < timeOffset)
{
pos.x() = pos.x() + xStep;
pos.y() = pos.y() + yStep;
pos.z() = pos.z() + zStep;
auto xOffset = pos.x() - lastPos.x();
auto yOffset = pos.y() - lastPos.y();
angle = atan2f(yOffset, xOffset); // 算出偏转方向
angle = twoPi + angle;
}
osg::Quat roateByZAxis(0/*angle*/, osg::Vec3(0, 0, 1));
osg::AnimationPath::ControlPoint ctrlPoint(pos, roateByZAxis); // 动画控制点
pAnimationPath->insert(timeOffset, ctrlPoint);
lastPos = pos;
}
// 保存动画路径到文件
osgDB::ofstream out("cow_animationpath.txt");
pAnimationPath->write(out);
return pAnimationPath;
}
int main(int argc, char *argv[])
{
auto pCow = osgDB::readRefNodeFile(R"(E:\osg\OpenSceneGraph-Data\cow.osg)");
if (nullptr == pCow)
{
OSG_WARN << "cow node is nullpr!" << std::endl;
return 1;
}
osgViewer::Viewer viewer;
auto pRoot = new osg::Group();
auto pPosAttTransform = new osg::PositionAttitudeTransform;
auto initPos = osg::Vec3d(0.0, 18, 1.0);
pPosAttTransform->setPosition(initPos);
pPosAttTransform->addChild(pCow);
auto pAnimationCallback = new osg::AnimationPathCallback(createAnimationPath(initPos, osg::Vec3d(0.0, -8, 1.0), 10.0));
pPosAttTransform->setUpdateCallback(pAnimationCallback);
pRoot->addChild(pPosAttTransform);
viewer.setSceneData(pRoot);
return viewer.run();
}
运行该程序,则在本工程目录下,会生成cow_animationpath.txt文件,内容如下:
0 0 18 1 0 0 0 1
0.2 0 17.48 1 0 0 0 1
0.4 0 16.96 1 0 0 0 1
0.6 0 16.44 1 0 0 0 1
0.8 0 15.92 1 0 0 0 1
1 0 15.4 1 0 0 0 1
1.2 0 14.88 1 0 0 0 1
1.4 0 14.36 1 0 0 0 1
1.6 0 13.84 1 0 0 0 1
1.8 0 13.32 1 0 0 0 1
2 0 12.8 1 0 0 0 1
2.2 0 12.28 1 0 0 0 1
2.4 0 11.76 1 0 0 0 1
2.6 0 11.24 1 0 0 0 1
2.8 0 10.72 1 0 0 0 1
3 0 10.2 1 0 0 0 1
3.2 0 9.68000000000001 1 0 0 0 1
3.4 0 9.16000000000001 1 0 0 0 1
3.6 0 8.64000000000001 1 0 0 0 1
3.8 0 8.12000000000001 1 0 0 0 1
4 0 7.60000000000001 1 0 0 0 1
4.2 0 7.08000000000001 1 0 0 0 1
4.4 0 6.56000000000001 1 0 0 0 1
4.6 0 6.04000000000001 1 0 0 0 1
4.8 0 5.52000000000001 1 0 0 0 1
5 0 5.00000000000001 1 0 0 0 1
5.2 0 4.48000000000001 1 0 0 0 1
5.4 0 3.96000000000001 1 0 0 0 1
5.6 0 3.44000000000001 1 0 0 0 1
5.8 0 2.92000000000001 1 0 0 0 1
6 0 2.40000000000001 1 0 0 0 1
6.2 0 1.88000000000001 1 0 0 0 1
6.4 0 1.36000000000001 1 0 0 0 1
6.6 0 0.840000000000011 1 0 0 0 1
6.8 0 0.320000000000011 1 0 0 0 1
7 0 -0.199999999999989 1 0 0 0 1
7.2 0 -0.719999999999989 1 0 0 0 1
7.4 0 -1.23999999999999 1 0 0 0 1
7.6 0 -1.75999999999999 1 0 0 0 1
7.8 0 -2.27999999999999 1 0 0 0 1
8 0 -2.79999999999999 1 0 0 0 1
8.2 0 -3.31999999999999 1 0 0 0 1
8.4 0 -3.83999999999999 1 0 0 0 1
8.6 0 -4.35999999999999 1 0 0 0 1
8.8 0 -4.87999999999999 1 0 0 0 1
9 0 -5.39999999999999 1 0 0 0 1
9.2 0 -5.91999999999999 1 0 0 0 1
9.4 0 -6.43999999999999 1 0 0 0 1
9.6 0 -6.95999999999999 1 0 0 0 1
9.8 0 -7.47999999999999 1 0 0 0 1
10 0 -7.99999999999999 1 0 0 0 1