在osgearth里头画线

线的画法和osg里头一样,可参看osggeometry.cpp,但是点的坐标需要经过转换,实践证明是可行的,应该也可以用到其它几何体上。新手上路,欢迎大牛们拍砖。

这部分放在main()或者InitSceneGraph( void )中
osg::ref_ptr geode = new osg::Geode();
mRoot->addChild(geode);
//测试画线
osg::Vec3d startline(-155.150257, 63.390041, 220230);
osg::Vec3d endline(-165.150257, 63.390041, 0);
createLine(startline, endline);

void OSGEarthEngine::createLine(osg::Vec3d startline, osg::Vec3d endline)
{
const SpatialReference* mapSRS = mapNode->getMapSRS();
// create Geometry object to store all the vertices and lines primitive.
osg::ref_ptr linesGeom = new osg::Geometry();

// this time we'll preallocate the vertex array to the size we
// need and then simple set them as array elements, 2 points
// makes 1 line segments.
osg::Vec3d startWorld;
osg::Vec3d endWorld;
osg::ref_ptr vertices = new osg::Vec3dArray(2);
mapNode->getMap()->toWorldPoint( GeoPoint(mapSRS,startline), startWorld );
mapNode->getMap()->toWorldPoint( GeoPoint(mapSRS,endline), endWorld );
(*vertices)[0] = startWorld;
(*vertices)[1] = endWorld;

// pass the created vertex array to the points geometry object.
linesGeom->setVertexArray(vertices);

// set the colors as before, plus using the above
osg::ref_ptr colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
linesGeom->setColorArray(colors);
linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);

// set the normal in the same way color.
osg::ref_ptr normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
linesGeom->setNormalArray(normals);
linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);

// This time we simply use primitive, and hardwire the number of coords to use
// since we know up front,
linesGeom->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet:INES,0,2));

// add the points geometry to the geode.
geode->addDrawable(linesGeom);
}

相关文档
最新文档