osg如何画网格

来源:互联网 发布:汉中交通网络教学平台 编辑:程序博客网 时间:2024/04/28 11:23

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);//将图元添加至geometrygeom->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");


0 0
原创粉丝点击