std::vector<glm::dvec3> pts;
std::vector<glm::dvec3> normals;
std::vector<glm::ivec3> triangles;
_mesh.GetMeshGeometry(pts, normals, triangles);
int nV = pts.size();
int nF = triangles.size();
int nFacePt = nF * 3;
osg::ref_ptr<osg::Group> root = dynamic_cast<osg::Group*>(m_pViewer->getSceneData());
if (root == NULL)
{
root = new osg::Group;
root->setDataVariance(osg::Object::DYNAMIC);
}
int nChild = root->getNumChildren();
if (nChild == 0)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> meshGeometry = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(nV);
osg::ref_ptr<osg::Vec3Array> normArray = new osg::Vec3Array(nV);
for (int i = 0; i < nV; i++)
{
(*vertices)[i].set(pts[i].x, pts[i].y, pts[i].z);
(*normArray)[i].set(normals[i].x, normals[i].y, normals[i].z);
}
osg::ref_ptr<osg::DrawElementsUInt> meshFace = new osg::DrawElementsUInt(osg::DrawElementsUInt::TRIANGLES, nFacePt);
for (int i = 0; i < nF; i++)
{
meshFace->push_back(triangles[i].x);
meshFace->push_back(triangles[i].y);
meshFace->push_back(triangles[i].z);
}
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(0.25, 0.0, 0.0, 1.0));
meshGeometry->setVertexArray(vertices);
meshGeometry->setColorArray(colors.get());
meshGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
meshGeometry->setNormalArray(normArray.get());
meshGeometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
meshGeometry->addPrimitiveSet(meshFace.get());
geode->addDrawable(meshGeometry);
root->addChild(geode);
}
else
{
for (int iChild = 0; iChild < nChild; iChild++)
{
osg::ref_ptr<osg::Geode> geoNode = dynamic_cast<osg::Geode*>(root->getChild(iChild));
if (geoNode)
{
osg::ref_ptr<osg::Geometry> meshGeometry = geoNode->getDrawable(0)->asGeometry();
osg::ref_ptr<osg::Vec3Array> old_vertices = dynamic_cast<osg::Vec3Array*>(meshGeometry->getVertexArray());
osg::ref_ptr<osg::Vec3Array> old_normArray = dynamic_cast<osg::Vec3Array*>(meshGeometry->getNormalArray());
if (old_vertices)
{
old_vertices->clear();
for (int i = 0; i < nV; i++)
{
old_vertices->push_back(osg::Vec3(pts[i].x, pts[i].y, pts[i].z));
old_normArray->push_back(osg::Vec3(normals[i].x, normals[i].y, normals[i].z));
}
old_vertices->dirty();
old_normArray->dirty();
}
osg::ref_ptr<osg::DrawElementsUInt> meshFace = new osg::DrawElementsUInt(osg::DrawElementsUInt::TRIANGLES, nFacePt);
for (int i = 0; i < nF; i++)
{
meshFace->push_back(triangles[i].x);
meshFace->push_back(triangles[i].y);
meshFace->push_back(triangles[i].z);
}
int nPri = meshGeometry->getNumPrimitiveSets();
for (int i = 0; i < nPri; i++)
{
meshGeometry->removePrimitiveSet(i);
}
meshGeometry->addPrimitiveSet(meshFace.get());
meshGeometry->dirtyDisplayList();
break;
}
}
}
m_pViewer->setSceneData(root);