OSG使用Geometry绘制自定义图形、更新修改绘制数据、绘制带边线图形(多 PrimitiveSet 绘制)

Geometry绘制自定义图形:
主要就设置顶点、颜色、法线以及 PrimitiveSet 绘制模式。

osg::Geode* geode = new osg::Geode;
osg::Geometry* geo = new osg::Geometry;
osg::Vec3Array* v = new osg::Vec3Array;
osg::Vec4Array* c = new osg::Vec4Array;
osg::Vec3Array* n = new osg::Vec3Array;
v->push_back(osg::Vec3f(0.0, 0.0, 0.0));
v->push_back(osg::Vec3f(1.0, 0.0, 0.0));
v->push_back(osg::Vec3f(0.0, 1.0, 0.0));
v->push_back(osg::Vec3f(-1.0, 0.0, 0.0));
v->push_back(osg::Vec3f(0.0, 0.0, 0.0));
geo->setVertexArray(v);
c->push_back(osg::Vec4f(1.0, 1.0, 1.0, 1.0));
geo->setColorArray(c, osg::Array::BIND_OVERALL);
n->push_back(osg::Vec3f(0.0, 0.0, 1.0));
geo->setNormalArray(n, osg::Array::BIND_OVERALL);
geo->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, v->size()));
geode->addDrawable(geo);
	
root->addChild(geode);

运行效果:

在这里插入图片描述

更新修改绘制数据:

osg::Vec4Array* color = dynamic_cast<osg::Vec4Array*>(geo->getColorArray());
(*color)[0] = osg::Vec4f(0.0, 1.0, 0.0, 1.0);
osg::Vec3Array* ver = dynamic_cast<osg::Vec3Array*>(geo->getVertexArray());
ver->clear();
ver->push_back(osg::Vec3f(0.0, 0.0, 0.0));
ver->push_back(osg::Vec3f(1.0, 0.0, 0.0));
ver->push_back(osg::Vec3f(1.0, 1.0, 0.0));
ver->push_back(osg::Vec3f(-1.0, 1.0, 0.0));
ver->push_back(osg::Vec3f(-1.0, 0.0, 0.0));
ver->push_back(osg::Vec3f(0.0, 0.0, 0.0));
//如果更新的顶点数与原始顶点数不同,则需要获取当前PrimitiveSet设置绘制的顶点数量
osg::DrawArrays* drawable = dynamic_cast<osg::DrawArrays*>(geo->getPrimitiveSet(0));
drawable->setCount(ver->size());
geo->dirtyDisplayList();

运行效果:

在这里插入图片描述

绘制带边线图形(多 PrimitiveSet 绘制):

osg::Geode* geode = new osg::Geode;
osg::Geometry* geo = new osg::Geometry;
osg::Vec3Array* v = new osg::Vec3Array;
osg::Vec4Array* c = new osg::Vec4Array;
osg::Vec3Array* n = new osg::Vec3Array;
v->push_back(osg::Vec3f(0.0, 0.0, 0.0));
v->push_back(osg::Vec3f(1.0, 0.0, 0.0));
v->push_back(osg::Vec3f(0.0, 1.0, 0.0));
v->push_back(osg::Vec3f(-1.0, 0.0, 0.0));
v->push_back(osg::Vec3f(0.0, 0.0, 0.0));
geo->setVertexArray(v);
//设置两种颜色,并以BIND_PER_PRIMITIVE_SET进行绑定
c->push_back(osg::Vec4f(1.0, 1.0, 1.0, 1.0));
c->push_back(osg::Vec4f(1.0, 0.0, 0.0, 1.0));
geo->setColorArray(c, osg::Array::BIND_PER_PRIMITIVE_SET);
n->push_back(osg::Vec3f(0.0, 0.0, 1.0));
geo->setNormalArray(n, osg::Array::BIND_OVERALL);
//设置两个PrimitiveSet,一个以LINE_STRIP绘制,一个以POLYGON绘制
geo->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, v->size()));
geo->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON, 0, v->size()));
//设置线宽
osg::StateSet* stateSet = geode->getOrCreateStateSet();
stateSet->setAttributeAndModes(new osg::LineWidth(3.0));
geode->addDrawable(geo);

root->addChild(geode);

运行效果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值