|
楼主 |
发表于 2017-2-17 14:35:03
|
显示全部楼层
顶点访问器的程序设计如下:
struct TriangleIndexCollector
{
TriangleIndexCollector()
{
indexArray = new osg::UIntArray;
}
void operator()(const unsigned int& v1,const unsigned int& v2,const unsigned int& v3)
{
if(v1==v2||v1==v3||v2==v3)
return;
indexArray->push_back(v1);
indexArray->push_back(v2);
indexArray->push_back(v3);
}
osg::ref_ptr<osg::UIntArray> indexArray;
};
struct TriangleCollector
{
TriangleCollector()
{
Triangle = new osg::Vec3Array;
}
void operator()(const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3 v3, bool temp )
{
if(v1==v2 || v1==v3 || v2==v3) return;
Triangle->push_back(v1);
Triangle->push_back(v2);
Triangle->push_back(v3);
}
osg::ref_ptr<osg::Vec3Array> Triangle;
};
VertexVisitor::VertexVisitor()sg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
extracted_verts = new osg::Vec3Array;
index_array = new osg::UIntArray;
triangle = new osg::Vec3Array;
VertexCount = 0;
IndexCount = 0;
}
void VertexVisitor:: apply(osg::Geode& geode)
{
//得到每一个drawable
for(unsigned int i=0;i<geode.getNumDrawables();i++)
{
osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i)); //得到几何体
if(!geom)
{
std::cout<<"几何体错误"<<std::endl;
continue;
}
osg::Vec3Array* verts = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray()); //得到顶点数组
if(!verts)
{
std::cout<<"顶点数组错误"<<std::endl;
continue;
}
extracted_verts->insert(extracted_verts->end(),verts->begin(),verts->end());
//得到三角形顶点信息
osg::TriangleFunctor<TriangleCollector> tf;
geom->accept(tf);
triangle->insert(triangle->end(),tf.Triangle->begin(),tf.Triangle->end());
//得到三角形顶点索引信息
osg::TriangleIndexFunctor<TriangleIndexCollector> tif;
geom->accept(tif);
index_array->insert(index_array->end(),tif.indexArray->begin(),tif.indexArray->end());
}
}
int VertexVisitor::getVertexCount()
{
VertexCount = extracted_verts->size();
return VertexCount;
}
没时间回答你 提供一些源代码 希望对你有帮助 |
|