框架包括以下几部分:
1.set VertexArray 设置顶点
2.set Color 设置颜色
3.设置几何体(顶点、颜色、所有点颜色相同或者每个点一种颜色、点的关联方式GL_POINTS or GL_LINES or GL_LINE_STRIP or GL_LINE_LOOP )
4.生成叶节点
5.设置样式(线宽、点的大小等)
osg::ref_ptr<osg::Group> get_drawable(vector<_Point2> _savePoints,float size,osg::Vec4 color)
{
// Set Color
osg::Vec4Array * colorArray = new osg::Vec4Array;
if(color==osg::Vec4(-1,-1,-1,-1))
{
float r=(float)rand()/RAND_MAX;
float g=(float)rand()/RAND_MAX;
float b=(float)rand()/RAND_MAX;
colorArray->resize(1,osg::Vec4(r,g,b,1.0));
}
else
{
colorArray->resize(1,color);
}
// Set Vertex Array
int count=_savePoints.size();
osg::Vec3dArray* points=new osg::Vec3dArray(count);
for (int i=0;i<count;i++)
{
points->at(i)=osg::Vec3d(
_savePoints[i].x, //此处原来写的Finalpoint[i].x(),但由于类_Point的定义中x不是函数,而是变量,所以不能加().
_savePoints[i].y,
_savePoints[i].z); //此处虽然Finalpoint中只有二维坐标,但可以用0补上
}
// Set Geometry
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;//设置一个几何体
geom->setUseDisplayList(true);
geom->setUseVertexBufferObjects(true);//这两行可以不写
geom->setVertexArray(points);//设置点集
geom->setColorArray(colorArray);//设置颜色
geom->setColorBinding(osg::Geometry::BIND_OVERALL);//OVERALL表示所有的点都一个颜色,BIND_PER_VERTEX表示每个点各有一种颜色
geom->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_FAN,0,count));
osg::ref_ptr<osg::Geode> geode=new osg::Geode;
geode->addDrawable(geom); //生成叶节点
osg::ref_ptr<osg::Group> node=new osg::Group;
node->addChild(geode);
// Set StateSet
osg::StateSet* stateSet=node->getOrCreateStateSet();
//osg::Point* point=new osg::Point; //设置一些样式,如线宽等
//point->setSize(2);
///*point->setSize(size);*/
//stateSet->setAttribute(point);
osg::LineWidth* lineWidth=new osg::LineWidth;
lineWidth->setWidth(3);
stateSet->setAttribute(lineWidth);
return node;
}