|
- class ChangeVertex : public osg::NodeCallback
- {
- public:
- ChangeVertex()
- {
- }
- virtual void operator()( osg::Node* node,
- osg::NodeVisitor* nv )
- {
-
- osg::Geode * geode = node->asGeode();
- osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode->getDrawable(0));
- // 现在有了osg::Geometry指针,可不可以在此处调用geom->addPrimitiveSet或者
- // geom->RemovePrimitiveSet?我测试的结果是不可以,但不明白原因,症状是调用
- // 有效但是,运行很短的时间以后程序崩溃,而且每次崩溃的时机都有一些区别,
- // 内存并没有耗多少,多核计算机崩溃,单核无事,应该是线程冲突,里面有什么
- // 玄机请高手指教。
- traverse( node, nv );
- }
- private:
- };
复制代码 完整代码如下,复制即可编译运行。
- #include <osg/Geode>
- #include <osg/Geometry>
- #include <osg/Point>
- #include <osg/StateSet>
- #include <osgViewer/Viewer>
- #ifdef _DEBUG
- #pragma comment(lib,"osgd.lib")
- #pragma comment(lib,"osgDBd.lib")
- #pragma comment(lib,"osgViewerd.lib")
- #endif
- #ifdef NDEBUG
- #pragma comment(lib,"osg.lib")
- #pragma comment(lib,"osgDB.lib")
- #pragma comment(lib,"osgViewer.lib")
- #endif
- class ChangeVertex : public osg::NodeCallback
- {
- public:
- ChangeVertex()
- {
- }
- virtual void operator()( osg::Node* node,
- osg::NodeVisitor* nv )
- {
- osg::Geode * geode = node->asGeode();
- osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode->getDrawable(0));
- int count = geom->getNumPrimitiveSets();
- if(count>0)
- geom->removePrimitiveSet(0,1);
- osg::Vec3Array* _vertices = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray());
- for(unsigned int i = 0;i<_vertices->size();i++)
- {
- (*_vertices)[i]._v[2] = sin(((rand()%314)-157)/314.0)*0.6;
- }
- _vertices->push_back(osg::Vec3( sin(((rand()%314)-157)/314.0)*0.6, sin(((rand()%314)-157)/314.0)*0.6, sin(((rand()%314)-157)/314.0)*0.6 ));
- geom->addPrimitiveSet( new osg::DrawArrays( GL_POINTS, 0, _vertices->size()) );
- traverse( node, nv );
- }
- private:
- };
- osg::ref_ptr<osg::Geode>
- createPoints()
- {
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
- geom->setVertexArray( v.get() );
- v->push_back( osg::Vec3( 0.f, 0.f, 0.f ) );
- v->push_back( osg::Vec3( 0.f, 0.f, 4.f ) );
- v->push_back( osg::Vec3( -1.f, 0.f, 0.f ) );
- v->push_back( osg::Vec3( 1.f, 0.f, 0.f ) );
- v->push_back( osg::Vec3( 1.f, 0.f, 4.f ) );
- v->push_back( osg::Vec3( -1.f, 0.f, 4.f ) );
- v->push_back( osg::Vec3( 0.f, -1.f, 0.f ) );
- v->push_back( osg::Vec3( 0.f, 1.f, 0.f ) );
- v->push_back( osg::Vec3( 0.f, 1.f, 4.f ) );
- v->push_back( osg::Vec3( 0.f, -1.f, 4.f ) );
- geom->setUseDisplayList(false);
- geom->addPrimitiveSet( new osg::DrawArrays( GL_POINTS, 0, 10 ) );
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable( geom.get() );
- osg::StateSet* state = geode->getOrCreateStateSet();
- state->setMode( GL_LIGHTING, osg::StateAttribute::OFF |
- osg::StateAttribute::PROTECTED );
- osg::ref_ptr<osg::Point> pt = new osg::Point;
- pt->setSize( 10.f );
- state->setAttribute( pt.get() );
- geode->setUpdateCallback(new ChangeVertex());
- return geode.get();
- }
- int
- main( int, char ** )
- {
- osgViewer::Viewer viewer;
- viewer.setSceneData( createPoints().get() );
- return viewer.run();
- }
复制代码 |
|