您的位置:首页 > 其它

osg如何画网格

2015-05-22 16:22 197 查看
osg画网格只要是通过画线来实现的的,代码的的结构主要在于如何构造图元信息,及相应的顶点信息,将Geometry添加至节点即可实现,代码如下:

osg::ref_ptr<osg::MatrixTransform> transformGrid = new osg::MatrixTransform();
osg::Geode *geode = new osg::Geode();
osg::Geometry *geom = new osg::Geometry();
//构造顶点信息
osg::Vec3Array *vertices = new osg::Vec3Array();
int i;
int num = 10;
float rad = 5;
vertices->push_back( osg::Vec3(0.0f,-rad,0.0f) );
vertices->push_back( osg::Vec3(0.0f, rad,0.0f) );
vertices->push_back( osg::Vec3(-rad,0.0f,0.0f) );
vertices->push_back( osg::Vec3( rad,0.0f,0.0f) );
for (i = 1; i<=num; i++)
{
vertices->push_back( osg::Vec3(i,-rad,0.0f) );
vertices->push_back( osg::Vec3(i,rad,0.0f) );
vertices->push_back( osg::Vec3(-i,-rad,0.0f) );
vertices->push_back( osg::Vec3(-i,rad,0.0f) );
vertices->push_back( osg::Vec3(-rad,i,0.0f) );
vertices->push_back( osg::Vec3(rad,i,0.0f) );
vertices->push_back( osg::Vec3(-rad,-i,0.0f) );
vertices->push_back( osg::Vec3(rad,-i,0.0f) );
}
geom->setVertexArray(vertices);
//指定颜色数组
osg::Vec4Array* colors = new osg::Vec4Array();
colors->push_back( osg::Vec4(.9f,.9f,.9f,1.0f) );
colors->push_back( osg::Vec4(.9f,.9f,.9f,1.0f) );
geom->setColorArray(colors);
geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);

//指定法线
osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back( osg::Vec3(0.0f,-1.0f,0.0f) );
geom->setNormalArray(normals);
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);

//将图元添加至geometry
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES,0,4) );
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES,4,vertices->size() - 4) );
geode->addDrawable(geom);
osg::StateSet *set = new osg::StateSet();
set->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
set->setAttributeAndModes(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON);
set->setMode(GL_LINE_SMOOTH,osg::StateAttribute::ON);
geode->setStateSet(set);
transformGrid->addChild(geode);
transformGrid->setName("transformGrid");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  osg 网格