赞
踩
使物体或者说Node隐藏方式有两种,一种是设置NodeMask,另外一种是使用osg的switch类来控制。
两者的区别是
前者只是看不到,数据还在场景中,隐藏了并不能影响渲染性能,不影响内存中的数据;
后者是从内存中暂时移除,会对性能有所影响,需要显示时再加载进场景。
回调的速度好像是每帧调用一次,这样程序运行起来,会看到球以很快的速度在闪。为了更方便的观察,我在回调中做了限制,每5000次取反一次进行显示。
osg::Switch隐藏和显示节点的接口有很多种组合方法,我在程序中有体现。将节点添加到Switch对象中,可以通过getChildIndex来获取当前节点在Switch对象中的索引。
如果需要将Switch对象中节点全部隐藏和显示,可以使用setAllChildrenOff和setAllChildrenOn接口。
完整程序:
- #include <osg/Geode>
- #include <osgViewer/Viewer>
- #include <osg/ShapeDrawable>
- #include <osg/Switch>
-
- using namespace osg;
- using namespace osgViewer;
-
- #pragma comment(lib,"osgd.lib")
- #pragma comment(lib,"osgViewerd.lib")
-
- class NodeVisiableCallback :public NodeCallback
- {
- public:
- NodeVisiableCallback(unsigned int index) :_bVisiable(false), _childIndex(index), _tick(0){}
- ~NodeVisiableCallback(){};
-
- virtual void operator()(Node* node, NodeVisitor* nv)
- {
- if (_tick < 5000)
- {
- _tick++;
- return;
- }
- else
- {
- _tick = 0;
- }
- ref_ptr<Switch> sw = dynamic_cast<Switch*>(node);
- if (sw)
- sw->setValue(_childIndex, _bVisiable = !_bVisiable);
- }
- protected:
- bool _bVisiable;
- unsigned int _tick;
- unsigned int _childIndex;
- };
-
- int main(int argc, char **argv)
- {
- Viewer viewer;
-
- ref_ptr<Group> group = new Group;
-
- ref_ptr<Geode> node1 = new Geode;
- ref_ptr<Geode> node2 = new Geode;
-
- ref_ptr<ShapeDrawable> sphere1 = new ShapeDrawable(new Sphere(Vec3(10, 0, 0), 2.f));
- ref_ptr<ShapeDrawable> sphere2 = new ShapeDrawable(new Sphere(Vec3(-10, 0, 0), 2.f));
-
- node1->addDrawable(sphere1);
- node2->addDrawable(sphere2);
-
- ref_ptr<Switch> sw = new Switch;
- //on
- sw->addChild(node1);
- sw->setUpdateCallback(new NodeVisiableCallback(sw->getChildIndex(node1)));
- //off 1
- //sw->addChild(node1,0);
- //off 2
- //sw->addChild(node1);
- //sw->setChildValue(node1, 0);
- //off 3
- //sw->addChild(node1);
- //sw->setValue(sw->getChildIndex(node1), 0);
- //off 4
- //sw->addChild(node1);
- //sw->setAllChildrenOff();
-
- group->addChild(sw);
- group->addChild(node2);
-
- viewer.setSceneData(group.get());
-
- viewer.realize();
- viewer.run();
- }
本文地址:http://blog.csdn.net/u011417605/article/details/70237688
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。