virtual void apply(osg::Geode& node)
{
for (int i = 0; i < node.getNumDrawables(); i++)
{
osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(node.getDrawable(i));
if (geometry)
{
//apply(*g);
//***********************************************
//解析顶点
osg::Array* vertexArray = geometry->getVertexArray();
if (vertexArray == NULL)
return;
//顶点数组
osg::Vec3Array* verts = dynamic_cast<osg::Vec3Array*>(vertexArray);
long lVertNum = verts->size();
std::vector<osg::Vec3 >::iterator iter_ver = verts->begin();
//遍历顶点值
for (; iter_ver != verts->end(); iter_ver++)
{
double x = iter_ver->x();
double y = iter_ver->y();
double z = iter_ver->z();
}
//纹理
osg::Texture2D* tex2D = dynamic_cast<osg::Texture2D*>(geometry->getStateSet()->getTextureAttribute(0, osg::StateAttribute::TEXTURE));
osg::Image* image = tex2D->getImage();
osgDB::writeImageFile(*image, "abc.jpg");
int width = image->s();
int height = image->t();
/*osg::Vec2 color;
osg::Vec4 c = image->getColor(color);*/
//UV
osg::Array* uvArry = geometry->getTexCoordArray(0);
osg::Vec2Array* vertsUV = dynamic_cast<osg::Vec2Array*>(uvArry);
std::vector<osg::Vec2 >::iterator iter_verUV = vertsUV->begin();
std::vector<int> greenPointIndices;
int i = 0;
for (; iter_verUV != vertsUV->end(); iter_verUV++)
{
double u = iter_verUV->x();
double v = iter_verUV->y();
osg::Vec2 color(u, v);
osg::Vec4 c = image->getColor(color);
float r = c.r() * 255;
float g = c.g() * 255;
float b = c.b() * 255;
if (r < 100 && g < 100 && b < 100 )
greenPointIndices.push_back(i);
i++;
}
//索引
int numP = geometry->getNumPrimitiveSets();
osg::ref_ptr<osg::DrawElementsUInt> drawElemUInt = new osg::DrawElementsUInt(GL_TRIANGLES);
for (unsigned int ipr = 0; ipr < numP; ipr++)
{
osg::PrimitiveSet* prset = geometry->getPrimitiveSet(ipr);
unsigned int ncnt = prset->getNumIndices();
for (unsigned int ic = 0; ic * 3 < prset->getNumIndices(); ic++)
{
unsigned int iIndex0 = prset->index(ic * 3);
unsigned int iIndex1 = prset->index(ic * 3 + 1);
unsigned int iIndex2 = prset->index(ic * 3 + 2);
bool find = false;
for (int i = 0; i < greenPointIndices.size(); i++)
{
if (greenPointIndices[i] == iIndex0 || greenPointIndices[i] == iIndex1 || greenPointIndices[i] == iIndex2)
{
find = true;
break;
}
}
if (find)
continue;
else
{
drawElemUInt->push_back(prset->index(ic * 3));
drawElemUInt->push_back(prset->index(ic * 3 + 1));
drawElemUInt->push_back(prset->index(ic * 3 + 2));
}
}
}
osg::ref_ptr<osg::Geometry> geometry_new = new osg::Geometry();
geometry_new->setVertexArray(verts);
geometry_new->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex2D, osg::StateAttribute::ON);
geometry_new->setTexCoordArray(0, uvArry);
geometry_new->addPrimitiveSet(drawElemUInt);
osg::Geode* pGeode_new = new osg::Geode;
pGeode_new->removeDrawables(0, pGeode_new->getNumDrawables());
pGeode_new->addDrawable(geometry_new);
osgDB::writeNodeFile(*pGeode_new, "test.osgb", new osgDB::Options("WriteImageHint=IncludeData Compressor=zlib"));
//***********************************************
}
}
}
geode中顶点对应UV色彩在阈值范围内的三角形删除,并生成新的geode保存成osgb文件输出