osg画线_在osgearth里头画线
时间: 2023-07-24 19:51:49 浏览: 133
在OSGEarth中画线有多种方法,以下是其中一种基本方法:
1. 创建线段的几何体
```c++
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3d(0.0, 0.0, 0.0));
vertices->push_back(osg::Vec3d(0.0, 0.0, 1.0));
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setVertexArray(vertices.get());
osg::ref_ptr<osg::DrawArrays> drawArrays = new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2);
geom->addPrimitiveSet(drawArrays.get());
```
2. 创建线段的颜色
```c++
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
```
3. 创建线段的外观
```c++
osg::ref_ptr<osg::LineWidth> lineWidth = new osg::LineWidth(3.0f);
osg::ref_ptr<osg::StateSet> stateSet = geom->getOrCreateStateSet();
stateSet->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON);
```
4. 创建节点并添加几何体
```c++
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(geom.get());
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(geode.get());
```
5. 将节点加入场景图
```c++
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
viewer.run();
```
以上代码将会在场景中画一条从 (0,0,0) 到 (0,0,1) 的红色线段。
阅读全文