|
楼主 |
发表于 2009-5-7 17:58:35
|
显示全部楼层
多谢!
但是还有新的问题。想通过NodeVisitor遍历所有的点,如下程序运行出错了。错误是在
extracted_verts->insert( extracted_verts->end(), verts->begin(), verts->end() );
这话一句,这是书上的例子啊,照抄下来的??
再次请教!
附上代码:
=====================================================================================
class VertexExtractor : public osg::NodeVisitor
{
public:
//所有的顶点
osg::ref_ptr<osg::Vec3Array> extracted_verts;
//构造函数,选择向下遍历全孩子的方式
VertexExtractor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
extracted_verts = new osg::Vec3Array;
}
//apply
void apply( osg::Geode& geode )
{
//看看有多少可绘制结点
int iii = geode.getNumDrawables();
for( unsigned int i=0; i < geode.getNumDrawables(); ++i )
{
osg::Geometry* geom = dynamic_cast<osg::Geometry*>( geode.getDrawable(i) );
if( !geom )
continue;
//得到可绘制结点的顶点序列
osg::Vec3Array* verts = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
if( !verts )
continue;
extracted_verts->insert( extracted_verts->end(), verts->begin(), verts->end() );
}
}
};
int main(int argc, char** argv)
{
osgViewer::Viewer viewer;
osg::Node* rootNode = osgDB::readNodeFile ("cow.osg");
VertexExtractor ivea ;
rootNode ->accept (ivea) ;
viewer.setSceneData(rootNode);
viewer.realize();
std:fstream out("C:\\test.vertexs") ;
int size_t = ivea.extracted_verts .get () ->size () ;
std::vector <osg::Vec3 > ::iterator iter = ivea.extracted_verts .get () ->begin () ;
for (int i =0 ; i<size_t ; i++)
{
out << iter->x () <<" "<<iter ->y () <<" " << iter ->z () << std::endl; iter ++ ;
} ;
std::cout <<"输出所有结点完毕"<<std::endl;
return 0;
}
====================================================================================================== |
|