OSG更新几何节点的方法

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);

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值