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::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");
相关文章推荐
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- PUSH模式动态水晶报表的实现-网格线如何实现呢
- 17. 首先我们获取曼哈顿距离:如何在一个网格里测量距离
- GMF:如何让网格显示在background,而不是foreground
- Bootstrap流式网格系统如何使用及注意事项
- 如何对ArcSDE空间网格大小进行优化?
- OSG基本几何体的透明效果和网格模型
- 如何通过三维网格自动生成纸制模型
- OSG点击场景中的某个物体,如何判断点击的物体,及处理此模型相关变换
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- Unity 改变一个模型 如何同时改变该模型的 网格碰撞器大小
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- Dynamics CRM 365 子网格上"添加"按钮旁的“查看关联视图”按钮如何控制显示和隐藏?
- OSG学习之示例遇到的问题二图形不能显示网格模式
- 如何设置 jqplot 图表插件的轴和网格
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- 如何通过jQuery实现网格中的行的颜…
- OSG 显示网格和状态信息
- 如何使用CSS网格创建一个自适应布局