osg 图元重启


osg::Geometry* createLine2(const std::vector<osg::Vec3d>& allPTs, osg::Vec4 color)
{
	int nCount = allPTs.size();

	osg::ref_ptr<osg::Geometry> pGeometry = new osg::Geometry();

	osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array;
	pGeometry->setVertexArray(verts.get());
	for (int i = 0; i < allPTs.size(); i++)
	{
		verts->push_back(allPTs[i]);
	}

	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
	colors->push_back(color);
	pGeometry->setColorArray(colors.get());
	pGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);

	const int							   kLastIndex = allPTs.size() - 1;
	osg::ref_ptr<osg::ElementBufferObject> ebo		  = new osg::ElementBufferObject;
	osg::ref_ptr<osg::DrawElementsUInt>	indices	= new osg::DrawElementsUInt(osg::PrimitiveSet::LINE_STRIP);

	for (unsigned int i = 0; i < allPTs.size(); i++)
	{
		if (allPTs[i].x() == -1 && allPTs[i].y() == -1)
		{
			indices->push_back(kLastIndex); 
		}
		else
		{
			indices->push_back(i);
		}
	}

	indices->setElementBufferObject(ebo);
	pGeometry->addPrimitiveSet(indices.get());
	pGeometry->setUseVertexBufferObjects(true); //不启用VBO的话,图元重启没效果

	osg::StateSet* stateset = pGeometry->getOrCreateStateSet();
	stateset->setAttributeAndModes(new osg::LineWidth(2), osg::StateAttribute::ON);
	stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
	stateset->setMode(GL_PRIMITIVE_RESTART, osg::StateAttribute::ON);
	//PrimitiveRestartIndex索引如果取很大数的话,计算包围盒的时候会崩溃
	stateset->setAttributeAndModes(new osg::PrimitiveRestartIndex(kLastIndex), osg::StateAttribute::ON);

	return pGeometry.release();
}
osg::Node* create_lines()
{
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	vector<osg::Vec3d>		 PTs;

	for (int j = 0; j < 2; j++)
	{
		for (int i = 0; i < 100; i++)
		{
			PTs.push_back(osg::Vec3d(i * 10, 0, 0));

			if (i % 4 < 2)
				PTs.back() += osg::Vec3(0, 50, 0);

			PTs.back() += osg::Vec3(0, j * 150,  0);
		}
		PTs.push_back(osg::Vec3(-1,-1, -1));  //这个点不会显示,但OSG计算包围盒的时候还是会考虑它
	}

	osg::Geometry* n = createLine2(PTs, osg::Vec4(1, 0, 0, 1));
	geode->addDrawable(n);
	
	return geode.release();
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值