第21节 实例 您所在的位置:网站首页 ppt制作开门关门效果 第21节 实例

第21节 实例

2023-07-07 12:08| 来源: 网络整理| 查看: 265

致谢

感谢网友提出这个问题。大家有问题也可以在评论区提出,有问必有答。

问题描述

“啥时候出个动态更新某个指定节点的例子,比如一个节点,加载的时候是状态1..通过操作,在相同的位置替换成状态2的模型,比如,开门关门”

于是我就做了这个例子,功能是双击门,门就会开,开了之后再双击一下就会关。截图如下:

image.png 本节资源

本文集包括本节所有资源包括模型代码都在此下载,按节的序号有文件或文件夹: 注意:务必使用浏览器打开: 链接:https://pan.baidu.com/s/13gwJLwo_LbRnN3Bl2NXXXw 提取码:xrf5

实现思路与具体实现

1.在3DMAX中,我让设计师做了门的动画,且只有这个门有动画。大家可以使用osgviewer men.ive 可以看到这个门是有动画的。然后我使用NodeVisitor可以遍历出这个动画。写个men的类来对动画进行控制。

2.这个动画是开门带关门的,我要判断哪个时刻门正好完全打开,我就要将动画的路径导出,看看Path文件,简单分析一下可能正好完全打开的时间点。将animationPath写出到路径path文件的代码如下:

if (_apc != nullptr) { //将路径导出 osg::AnimationPath* ap = _apc->getAnimationPath(); osgDB::ofstream out("ap.path"); ap->write(out); ap->setLoopMode(osg::AnimationPath::NO_LOOPING); _apc->setPause(true); }

从ap.path中可以看到门完全打开的时间差不多在2.0,可以从其旋转的量可以看出来。到达一个高峰,超过这个时间就又转回去了。

3.写个函数名称叫做KaiMen,用来当鼠标点击门的时候,进行开门,开门就是动画重新设置在原始位置,然后开始启动_state是记录了三种状态,初始状态0,开门状态1,关门状态2:

void KaiMen() { _apc->reset(); _apc->setPause(false); _state = 1; }

仿照开门我们再写个关门,因为关门的时候门已经完全打开了,因此只需要将动画播完即可:

void GuanMen() { _apc->setPause(false); _state = 2; }

4. 再讲一点如果找到结点里动画,使用一个NodeVisitor进行逐结点的判断:

class AnimationManagerFinder : public osg::NodeVisitor { public: AnimationManagerFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), _apc(nullptr), _menNode(nullptr) {} void apply(osg::Node& node) { if (node.getUpdateCallback()) { osg::AnimationPathCallback* b = dynamic_cast(node.getUpdateCallback()); if (b) { _apc = b; _menNode = &node; return; } } traverse(node); } osg::AnimationPathCallback* _apc; osg::Node* _menNode; };

5.最后如何让门停在动画运行2秒的时候呢,就是在事件中每一帧都判断当前是在开门的状态,且动画运行的时间:

if (ea.getEventType() == ea.FRAME) { if (_men->_apc) { if (_men->_state == 0) { //静止状态,最终要让动画在静止状态 } else if (_men->_state == 1) { if (_men->_apc->getAnimationTime() >= 2.0) { _men->_apc->setPause(true); } } else { //关门时就让门关完 } } }

好了,代码主要的部分差不多就是这些。以下是全部代码,是不是又学到了一些小知识呀:)

// jianshu.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include #include #include #include class AnimationManagerFinder : public osg::NodeVisitor { public: AnimationManagerFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), _apc(nullptr), _menNode(nullptr) {} void apply(osg::Node& node) { if (node.getUpdateCallback()) { osg::AnimationPathCallback* b = dynamic_cast(node.getUpdateCallback()); if (b) { _apc = b; _menNode = &node; return; } } traverse(node); } osg::AnimationPathCallback* _apc; osg::Node* _menNode; }; class Men : public osg::Group { public: Men() { _node = osgDB::readNodeFile("men.ive"); AnimationManagerFinder amf; _node->accept(amf); _apc = amf._apc; _menNode = amf._menNode; if (_apc != nullptr) { //将路径导出 osg::AnimationPath* ap = _apc->getAnimationPath(); osgDB::ofstream out("ap.path"); ap->write(out); ap->setLoopMode(osg::AnimationPath::NO_LOOPING); _apc->setPause(true); } addChild(_node); _state = 0; //0是静止,1是开门,2是关门 } void KaiMen() { _apc->reset(); _apc->setPause(false); _state = 1; } void GuanMen() { _apc->setPause(false); _state = 2; } osg::Node* _node; osg::AnimationPathCallback* _apc; int _state; osg::Node* _menNode; }; Men* _men = new Men; class MyGUIEventHandler : public osgGA::GUIEventHandler { public: MyGUIEventHandler(){} virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) { if (ea.getEventType() == ea.FRAME) { if (_men->_apc) { if (_men->_state == 0) { //静止状态,最终要让动画在静止状态 } else if (_men->_state == 1) { if (_men->_apc->getAnimationTime() >= 2.0) { _men->_apc->setPause(true); } } else { //关门时就让门关完 } } } if (ea.getEventType() == ea.DOUBLECLICK) { if (ea.getButton() == ea.LEFT_MOUSE_BUTTON) { //与地板求交 osgUtil::LineSegmentIntersector::Intersections intersections; osgViewer::View* view = dynamic_cast(&aa); if (view->computeIntersections(ea, intersections)) { for (osgUtil::LineSegmentIntersector::Intersections::iterator iter = intersections.begin(); iter != intersections.end(); iter++) { osg::NodePath np = iter->nodePath; for (int i = 0; i < np.size(); i++) { if (_men->_menNode == np[i]) { //击中门了,当前在停着就开门,当前开着就关门 int menstate = _men->_state; if (menstate == 0) { _men->KaiMen(); return false; } else if (menstate == 1) { _men->GuanMen(); return false; } else { //当前在关门,什么也不做 _men->KaiMen(); return false; } } } } } } } return false; } }; int main() { osgViewer::Viewer viewer; viewer.setSceneData(_men); viewer.addEventHandler(new MyGUIEventHandler()); return viewer.run(); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有