osgearth3.2生成云的代码,有调节云的颜色和厚度功能,有CreateCloud函数
时间: 2024-03-05 22:48:18 浏览: 118
osgEarth 3.2并没有内置生成云的函数,需要自行实现。以下是一个简单的示例代码,可以生成一个云层:
```cpp
osg::Node* createCloud(osg::Vec3 center, float radius, osg::Vec4 color, float thickness)
{
osg::Geode* geode = new osg::Geode();
osg::Geometry* geometry = new osg::Geometry();
geode->addDrawable(geometry);
osg::Vec3Array* vertices = new osg::Vec3Array();
vertices->push_back(osg::Vec3(center.x(), center.y(), center.z()));
for (float angle = 0.0f; angle < osg::PI * 2.0f; angle += osg::PI / 8.0f)
{
float x = center.x() + radius * sin(angle);
float y = center.y() + radius * cos(angle);
vertices->push_back(osg::Vec3(x, y, center.z()));
}
geometry->setVertexArray(vertices);
osg::Vec4Array* colors = new osg::Vec4Array();
colors->push_back(color);
geometry->setColorArray(colors, osg::Array::BIND_OVERALL);
osg::DrawElementsUShort* primitive = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_FAN, 0);
for (unsigned int i = 1; i < vertices->size(); ++i)
{
primitive->push_back(0);
primitive->push_back(i);
primitive->push_back(i == vertices->size() - 1 ? 1 : i + 1);
}
geometry->addPrimitiveSet(primitive);
osgEarth::AltitudeSymbol* alt = geode->getOrCreateStateSet()->getOrCreateUniform<osgEarth::AltitudeSymbol>("oe_layer_altitude", osgEarth::AltitudeSymbol::create());
alt->clamping() = osgEarth::AltitudeSymbol::CLAMP_TO_TERRAIN;
alt->technique() = osgEarth::AltitudeSymbol::TECHNIQUE_DRAPE;
osgEarth::ModelLayer* cloudLayer = new osgEarth::ModelLayer();
cloudLayer->setName("Clouds");
cloudLayer->setEnabled(true);
cloudLayer->setMaxVisibleRange(2000, 100000);
osgEarth::ModelSourceOptions cloudModelOptions;
cloudModelOptions.color() = color;
cloudModelOptions.alphaBlending() = true;
cloudModelOptions.alphaBlending().setFunction(osg::StateAttribute::FUNCTION_ADD, osg::StateAttribute::FUNCTION_ADD);
osg::MatrixTransform* transform = new osg::MatrixTransform();
transform->addChild(geode);
transform->setMatrix(osg::Matrix::scale(1.0f, 1.0f, thickness));
cloudLayer->getModelSourceOptions() = cloudModelOptions;
cloudLayer->addChild(transform);
osgEarth::Registry::instance()->getMap()->addModelLayer(cloudLayer);
return transform;
}
```
其中,`center`表示云层的中心点,`radius`表示云层的半径,`color`表示云层的颜色,`thickness`表示云层的厚度。可以根据实际需求调节这些参数以生成不同形状、颜色和厚度的云层。
阅读全文