osg::Node获取所有面片顶点坐标索引的集合,所有面片纹理坐标索引的集合
时间: 2024-02-24 08:00:45 浏览: 90
同一个osg::Geometry或osg::Geode或osg::Group对象 在不同的位置 用不同的颜色 大小 角度绘制
可以使用以下代码来获取osg::Node中所有Drawable的顶点坐标索引和纹理坐标索引集合:
```cpp
// 获取所有Drawable
osg::Node* node = ...;
osg::ref_ptr<osg::Group> group = node->asGroup();
if (group)
{
osg::ref_ptr<osg::Node> child;
osg::Drawable* drawable = nullptr;
osg::Geometry* geometry = nullptr;
osg::Vec3Array* vertices = nullptr;
osg::Vec2Array* texcoords = nullptr;
std::vector<unsigned int> vertexIndices;
std::vector<unsigned int> texcoordIndices;
for (unsigned int i = 0; i < group->getNumChildren(); ++i)
{
child = group->getChild(i);
drawable = child->asDrawable();
if (drawable)
{
geometry = drawable->asGeometry();
if (geometry)
{
vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
if (vertices)
{
// 获取顶点索引
osg::UIntArray* indices = dynamic_cast<osg::UIntArray*>(geometry->getPrimitiveSetList()[0]->getIndices());
if (indices)
{
for (unsigned int j = 0; j < indices->size(); ++j)
{
vertexIndices.push_back((*indices)[j]);
}
}
}
texcoords = dynamic_cast<osg::Vec2Array*>(geometry->getTexCoordArray(0));
if (texcoords)
{
// 获取纹理坐标索引
osg::UIntArray* indices = dynamic_cast<osg::UIntArray*>(geometry->getPrimitiveSetList()[0]->getIndices());
if (indices)
{
for (unsigned int j = 0; j < indices->size(); ++j)
{
texcoordIndices.push_back((*indices)[j]);
}
}
}
}
}
}
}
```
需要注意的是,此代码只适用于具有单个PrimitiveSet的Geometry。如果存在多个PrimitiveSet,需要对每个PrimitiveSet进行遍历并分别获取顶点和纹理坐标索引。
阅读全文