• 如何生成osg的动画路径文件


    目录

    1. 前言

    2. 生成动画路径文件

    2.1. 粗糙方式

    2.2. 精确方式


    1. 前言

             在进行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类对象就可以达到动画的效果。当动画生成的时候,需要将动画路径的数据如控制点等信息,以文件形式保存下来,以供其它程序使用。如何生成这样的动画路径文件?

    2. 生成动画路径文件

    2.1. 粗糙方式

          如果需求对动画路径要求不精确,如:没有要求具体的时间段、没有要求具体的转动角度、移动距离等, 仅仅只是要求移动或转动到估计的位置或角度就行,则可以用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的动画路径文件,文件内容类似如下:

    1. 0.0018253 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    2. 0.0667249 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    3. 0.1337192 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    4. 0.1998216 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    5. 0.2653721 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    6. 0.3338356 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    7. 0.4002481 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    8. 0.4664029 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    9. 0.5332971 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    10. 0.5996751 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    11. 0.6665445 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    12. 0.7353267 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    13. 0.7997468 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    14. 0.8663566 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    15. 0.9328836 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    16. 0.9993953 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    17. 1.0663357 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    18. 1.1383031 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    19. 1.2032655 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    20. 1.2654628 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    21. 1.3326584 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    22. 1.3995767 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    23. 1.4658965 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    24. 1.5320493 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    25. 1.5997768 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    26. 1.6665459 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    27. 1.7327271 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    28. 1.7986692 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    29. 1.8654239 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    30. 1.9356403 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    31. 2.0001472 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    32. 2.0880553 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548
    33. 2.1488845 0.776124954223633 -25.6958980560303 -0 0.707106781186548 0 0 0.707106781186548

    这样动画路径文件就生成了。

    2.2. 精确方式

            对于2.1节提到的方法,仅仅只能用于粗略估计的平移和旋转,对于精确的,则必须通过编程及数学算出每个时刻及该时刻的控制点,然后保存到文件。以前面的奶牛需求为例子,代码实现如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. osg::AnimationPath* createAnimationPath(const osg::Vec3d& startPos, const osg::Vec3d& endPos, double time)
    8. {
    9. auto pAnimationPath = new osg::AnimationPath;
    10. pAnimationPath->setLoopMode(osg::AnimationPath::LOOP);
    11. auto timeDeta = 0.2;
    12. const auto loopCount = time / timeDeta;
    13. // 这里假定模型在x、y、z方向上运动的速度都一样且是匀速直线运动
    14. auto xStep = (endPos.x() - startPos.x()) / loopCount; // 每个时间段前进的距离
    15. auto yStep = (endPos.y() - startPos.y()) / loopCount;
    16. auto zStep = (endPos.z() - startPos.z()) / loopCount;
    17. double angle = 0.0;
    18. double twoPi = 2 * 3.1415926;
    19. osg::Vec3d lastPos = startPos;
    20. osg::Vec3d pos = startPos;
    21. for (auto timeOffset = 0.0; timeOffset <= time; timeOffset += timeDeta)
    22. {
    23. if (0.0 < timeOffset)
    24. {
    25. pos.x() = pos.x() + xStep;
    26. pos.y() = pos.y() + yStep;
    27. pos.z() = pos.z() + zStep;
    28. auto xOffset = pos.x() - lastPos.x();
    29. auto yOffset = pos.y() - lastPos.y();
    30. angle = atan2f(yOffset, xOffset); // 算出偏转方向
    31. angle = twoPi + angle;
    32. }
    33. osg::Quat roateByZAxis(0/*angle*/, osg::Vec3(0, 0, 1));
    34. osg::AnimationPath::ControlPoint ctrlPoint(pos, roateByZAxis); // 动画控制点
    35. pAnimationPath->insert(timeOffset, ctrlPoint);
    36. lastPos = pos;
    37. }
    38. // 保存动画路径到文件
    39. osgDB::ofstream out("cow_animationpath.txt");
    40. pAnimationPath->write(out);
    41. return pAnimationPath;
    42. }
    43. int main(int argc, char *argv[])
    44. {
    45. auto pCow = osgDB::readRefNodeFile(R"(E:\osg\OpenSceneGraph-Data\cow.osg)");
    46. if (nullptr == pCow)
    47. {
    48. OSG_WARN << "cow node is nullpr!" << std::endl;
    49. return 1;
    50. }
    51. osgViewer::Viewer viewer;
    52. auto pRoot = new osg::Group();
    53. auto pPosAttTransform = new osg::PositionAttitudeTransform;
    54. auto initPos = osg::Vec3d(0.0, 18, 1.0);
    55. pPosAttTransform->setPosition(initPos);
    56. pPosAttTransform->addChild(pCow);
    57. auto pAnimationCallback = new osg::AnimationPathCallback(createAnimationPath(initPos, osg::Vec3d(0.0, -8, 1.0), 10.0));
    58. pPosAttTransform->setUpdateCallback(pAnimationCallback);
    59. pRoot->addChild(pPosAttTransform);
    60. viewer.setSceneData(pRoot);
    61. return viewer.run();
    62. }

    运行该程序,则在本工程目录下,会生成cow_animationpath.txt文件,内容如下:

    1. 0 0 18 1 0 0 0 1
    2. 0.2 0 17.48 1 0 0 0 1
    3. 0.4 0 16.96 1 0 0 0 1
    4. 0.6 0 16.44 1 0 0 0 1
    5. 0.8 0 15.92 1 0 0 0 1
    6. 1 0 15.4 1 0 0 0 1
    7. 1.2 0 14.88 1 0 0 0 1
    8. 1.4 0 14.36 1 0 0 0 1
    9. 1.6 0 13.84 1 0 0 0 1
    10. 1.8 0 13.32 1 0 0 0 1
    11. 2 0 12.8 1 0 0 0 1
    12. 2.2 0 12.28 1 0 0 0 1
    13. 2.4 0 11.76 1 0 0 0 1
    14. 2.6 0 11.24 1 0 0 0 1
    15. 2.8 0 10.72 1 0 0 0 1
    16. 3 0 10.2 1 0 0 0 1
    17. 3.2 0 9.68000000000001 1 0 0 0 1
    18. 3.4 0 9.16000000000001 1 0 0 0 1
    19. 3.6 0 8.64000000000001 1 0 0 0 1
    20. 3.8 0 8.12000000000001 1 0 0 0 1
    21. 4 0 7.60000000000001 1 0 0 0 1
    22. 4.2 0 7.08000000000001 1 0 0 0 1
    23. 4.4 0 6.56000000000001 1 0 0 0 1
    24. 4.6 0 6.04000000000001 1 0 0 0 1
    25. 4.8 0 5.52000000000001 1 0 0 0 1
    26. 5 0 5.00000000000001 1 0 0 0 1
    27. 5.2 0 4.48000000000001 1 0 0 0 1
    28. 5.4 0 3.96000000000001 1 0 0 0 1
    29. 5.6 0 3.44000000000001 1 0 0 0 1
    30. 5.8 0 2.92000000000001 1 0 0 0 1
    31. 6 0 2.40000000000001 1 0 0 0 1
    32. 6.2 0 1.88000000000001 1 0 0 0 1
    33. 6.4 0 1.36000000000001 1 0 0 0 1
    34. 6.6 0 0.840000000000011 1 0 0 0 1
    35. 6.8 0 0.320000000000011 1 0 0 0 1
    36. 7 0 -0.199999999999989 1 0 0 0 1
    37. 7.2 0 -0.719999999999989 1 0 0 0 1
    38. 7.4 0 -1.23999999999999 1 0 0 0 1
    39. 7.6 0 -1.75999999999999 1 0 0 0 1
    40. 7.8 0 -2.27999999999999 1 0 0 0 1
    41. 8 0 -2.79999999999999 1 0 0 0 1
    42. 8.2 0 -3.31999999999999 1 0 0 0 1
    43. 8.4 0 -3.83999999999999 1 0 0 0 1
    44. 8.6 0 -4.35999999999999 1 0 0 0 1
    45. 8.8 0 -4.87999999999999 1 0 0 0 1
    46. 9 0 -5.39999999999999 1 0 0 0 1
    47. 9.2 0 -5.91999999999999 1 0 0 0 1
    48. 9.4 0 -6.43999999999999 1 0 0 0 1
    49. 9.6 0 -6.95999999999999 1 0 0 0 1
    50. 9.8 0 -7.47999999999999 1 0 0 0 1
    51. 10 0 -7.99999999999999 1 0 0 0 1

  • 相关阅读:
    读写分离的利器——MySQL Proxy
    docker保存镜像出错
    SpringBoot整合Mybatis逆向工程
    论文阅读 DyREP:Learning Representations Over Dynamic Graphs
    5 分钟,教你用 Docker 部署一个 Python 应用!
    QT实现截屏
    包装行业SCM协同系统解决方案:布局智慧供应体系,数字化整合包装行业供应链
    项目背景以及游戏平台简介
    多主架构:VLDB技术论文《Taurus MM: bringing multi-master to the cloud》解读
    vue之组件动态添加style样式的四种写法(齐全)
  • 原文地址:https://blog.csdn.net/danshiming/article/details/134005063