setlocale(LC_ALL,"chs");
osg::Group* pRoot = new osg::Group;
osg::Node*pNode = osgDB::readNodeFile("F:\\三维数据\\HY15\\现状\\现状建筑\\XZJZ01\\SubIve\\Level1\\sub1.osg");
pRoot->addChild(pNode);
osg::ref_ptr<osg::Geode> geode = dynamic_cast<osg::Geode*>(pNode);
int num = geode->getNumDrawables();
osg::ref_ptr<osg::Geometry> mGeometry =new osg::Geometry();
mGeometry->setDataVariance( osg::Object::DYNAMIC );
osg::StateSet* stateset = new osg::StateSet;
osg::Material *pMaterial = new osg::Material;
pMaterial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(0.0, 0.0, 0.0, 1.0)));
pMaterial->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(0.0, 0.0, 0.0, 1.0)));
pMaterial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(0.0, 0.0, 0.0, 1.0)));
pMaterial->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(1.0, 1.0, 1.0, 1.0)));
pMaterial->setShininess(osg::Material::FRONT_AND_BACK, 0.0f);
stateset->setAttribute(pMaterial);
mGeometry->setStateSet(stateset);
osg::ref_ptr<osg::Vec3Array > mVertexArray = new osg::Vec3Array();
osg::ref_ptr<osg::Vec3Array > mNormalArray = new osg::Vec3Array();
stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON );
stateset->setMode( GL_CULL_FACE, osg::StateAttribute::ON );
int t =0;
int vectorNum = 0;
for (int i =0;i<num;i++)
{
osg::ref_ptr<osg::Geometry> pGeometry= dynamic_cast<osg::Geometry*>(geode->getDrawable(i));
osg::StateSet* ss = pGeometry->getStateSet();
osg::StateSet::TextureAttributeList texAbl = ss->getTextureAttributeList();
for(osg::StateSet::TextureAttributeList::size_type i=0; i<texAbl.size(); ++i)
{
osg::StateAttribute* sa = ss->getTextureAttribute(i, osg::StateAttribute::TEXTURE);
osg::Texture2D* tex2d = dynamic_cast<osg::Texture2D*>(sa);
if(tex2d==NULL)continue;
stateset->setTextureAttributeAndModes(t,tex2d,osg::StateAttribute::ON);
mGeometry->setTexCoordArray(t,pGeometry->getTexCoordArray(i));
t++;
}
for (int j =0;j<pGeometry->getNumPrimitiveSets();j++)
{
if (pGeometry->getPrimitiveSet(j)->getType() == osg::PrimitiveSet::DrawElementsUShortPrimitiveType )
{
osg::DrawElementsUShort*ss1 = dynamic_cast<osg::DrawElementsUShort*> (pGeometry->getPrimitiveSet(j));
if (ss1)
{
for (int p =0 ;p<ss1->getNumIndices();p++)
{
if (ss1)
{
int Primitive = ss1->getElement(p)+vectorNum;
//std::cout<<Primitive<<std::endl;
ss1->setElement(p,Primitive);
}
}
mGeometry->addPrimitiveSet(pGeometry->getPrimitiveSet(j));
}
}
else if (pGeometry->getPrimitiveSet(j)->getType() == osg::PrimitiveSet::DrawArraysPrimitiveType)
{
osg::DrawArrays*ss1 = dynamic_cast<osg::DrawArrays*> (pGeometry->getPrimitiveSet(j));
if (ss1)
{
int Primitive = ss1->getFirst()+vectorNum;//
ss1->setFirst(Primitive);
mGeometry->addPrimitiveSet(pGeometry->getPrimitiveSet(j));
}
}
}
mGeometry->setNormalBinding(pGeometry->getNormalBinding());
int v =0;
osg::Vec3Array * pVertexArray = (osg::Vec3Array*)(pGeometry->getVertexArray());
vectorNum +=pVertexArray->size();
for (v = 0;v<pVertexArray->size();v++)
{
mVertexArray->push_back(pVertexArray->at(v));
}
osg::Vec3Array * pNormalArray = (osg::Vec3Array*)(pGeometry->getNormalArray());
for (v = 0;v<pNormalArray->size();v++)
{
mNormalArray->push_back(pNormalArray->at(v));
}
}
mGeometry->setVertexArray(mVertexArray);
mGeometry->setNormalArray(mNormalArray);
geode->removeDrawables(0,num);//删掉原始结点
geode->addDrawable(mGeometry.get());
osg::Group* pRoot = new osg::Group;
osg::Node*pNode = osgDB::readNodeFile("F:\\三维数据\\HY15\\现状\\现状建筑\\XZJZ01\\SubIve\\Level1\\sub1.osg");
pRoot->addChild(pNode);
osg::ref_ptr<osg::Geode> geode = dynamic_cast<osg::Geode*>(pNode);
int num = geode->getNumDrawables();
osg::ref_ptr<osg::Geometry> mGeometry =new osg::Geometry();
mGeometry->setDataVariance( osg::Object::DYNAMIC );
osg::StateSet* stateset = new osg::StateSet;
osg::Material *pMaterial = new osg::Material;
pMaterial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(0.0, 0.0, 0.0, 1.0)));
pMaterial->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(0.0, 0.0, 0.0, 1.0)));
pMaterial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(0.0, 0.0, 0.0, 1.0)));
pMaterial->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(osg::Vec4(1.0, 1.0, 1.0, 1.0)));
pMaterial->setShininess(osg::Material::FRONT_AND_BACK, 0.0f);
stateset->setAttribute(pMaterial);
mGeometry->setStateSet(stateset);
osg::ref_ptr<osg::Vec3Array > mVertexArray = new osg::Vec3Array();
osg::ref_ptr<osg::Vec3Array > mNormalArray = new osg::Vec3Array();
stateset->setMode( GL_LIGHTING, osg::StateAttribute::ON );
stateset->setMode( GL_CULL_FACE, osg::StateAttribute::ON );
int t =0;
int vectorNum = 0;
for (int i =0;i<num;i++)
{
osg::ref_ptr<osg::Geometry> pGeometry= dynamic_cast<osg::Geometry*>(geode->getDrawable(i));
osg::StateSet* ss = pGeometry->getStateSet();
osg::StateSet::TextureAttributeList texAbl = ss->getTextureAttributeList();
for(osg::StateSet::TextureAttributeList::size_type i=0; i<texAbl.size(); ++i)
{
osg::StateAttribute* sa = ss->getTextureAttribute(i, osg::StateAttribute::TEXTURE);
osg::Texture2D* tex2d = dynamic_cast<osg::Texture2D*>(sa);
if(tex2d==NULL)continue;
stateset->setTextureAttributeAndModes(t,tex2d,osg::StateAttribute::ON);
mGeometry->setTexCoordArray(t,pGeometry->getTexCoordArray(i));
t++;
}
for (int j =0;j<pGeometry->getNumPrimitiveSets();j++)
{
if (pGeometry->getPrimitiveSet(j)->getType() == osg::PrimitiveSet::DrawElementsUShortPrimitiveType )
{
osg::DrawElementsUShort*ss1 = dynamic_cast<osg::DrawElementsUShort*> (pGeometry->getPrimitiveSet(j));
if (ss1)
{
for (int p =0 ;p<ss1->getNumIndices();p++)
{
if (ss1)
{
int Primitive = ss1->getElement(p)+vectorNum;
//std::cout<<Primitive<<std::endl;
ss1->setElement(p,Primitive);
}
}
mGeometry->addPrimitiveSet(pGeometry->getPrimitiveSet(j));
}
}
else if (pGeometry->getPrimitiveSet(j)->getType() == osg::PrimitiveSet::DrawArraysPrimitiveType)
{
osg::DrawArrays*ss1 = dynamic_cast<osg::DrawArrays*> (pGeometry->getPrimitiveSet(j));
if (ss1)
{
int Primitive = ss1->getFirst()+vectorNum;//
ss1->setFirst(Primitive);
mGeometry->addPrimitiveSet(pGeometry->getPrimitiveSet(j));
}
}
}
mGeometry->setNormalBinding(pGeometry->getNormalBinding());
int v =0;
osg::Vec3Array * pVertexArray = (osg::Vec3Array*)(pGeometry->getVertexArray());
vectorNum +=pVertexArray->size();
for (v = 0;v<pVertexArray->size();v++)
{
mVertexArray->push_back(pVertexArray->at(v));
}
osg::Vec3Array * pNormalArray = (osg::Vec3Array*)(pGeometry->getNormalArray());
for (v = 0;v<pNormalArray->size();v++)
{
mNormalArray->push_back(pNormalArray->at(v));
}
}
mGeometry->setVertexArray(mVertexArray);
mGeometry->setNormalArray(mNormalArray);
geode->removeDrawables(0,num);//删掉原始结点
geode->addDrawable(mGeometry.get());
osgDB::writeNodeFile((*geode),"F:\\三维数据\\HY15\\现状\\现状建筑\\XZJZ01\\SubIve\\Level1\\sub1.osg");
===============================
思路:将多个geometry合并成一个geometry,用primitiveset控制形状