osgearth 圆形扩散 代码
时间: 2023-08-25 17:07:51 浏览: 49
以下是使用osgEarth实现圆形扩散的示例代码:
```c++
#include <osgEarth/MapNode>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgViewer/Viewer>
using namespace osgEarth;
using namespace osgEarth::Util;
osg::Geometry* createCircleGeometry(float radius)
{
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array();
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array();
float step = osg::PI / 180.0f;
for (float angle = 0.0f; angle <= 2.0f * osg::PI; angle += step)
{
float x = radius * std::cos(angle);
float y = radius * std::sin(angle);
vertices->push_back(osg::Vec3(x, y, 0.0f));
normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
texcoords->push_back(osg::Vec2(x / radius * 0.5f + 0.5f, y / radius * 0.5f + 0.5f));
}
geom->setVertexArray(vertices.get());
geom->setNormalArray(normals.get());
geom->setTexCoordArray(0, texcoords.get());
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN, 0, vertices->size()));
return geom.release();
}
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc, argv);
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(arguments);
osg::ref_ptr<MapNode> mapNode = MapNode::create(Map::create());
viewer->setSceneData(mapNode.get());
osg::ref_ptr<EarthManipulator> manipulator = new EarthManipulator();
viewer->setCameraManipulator(manipulator.get());
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(createCircleGeometry(1000.0f));
osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::ref_ptr<osg::Depth> depth = new osg::Depth();
depth->setWriteMask(false);
stateset->setAttributeAndModes(depth.get(), osg::StateAttribute::ON);
mapNode->addChild(geode.get());
return viewer->run();
}
```
这段代码首先创建了一个`osg::Geometry`对象,用于存储圆形的顶点、法线和纹理坐标信息。然后使用三角扇(primitive set)绘制圆形。最后将`osg::Geometry`对象添加到场景中。为了让圆形半透明,我们使用了状态集(state set)来设置`GL_BLEND`和`TRANSPARENT_BIN`模式。同时,禁用深度写入,以避免圆形被遮挡。