osgearth3.2绘制云的代码,支持云厚度和颜色的调整功能
时间: 2024-03-05 14:47:56 浏览: 164
使用Qt+osgEarth绘制标牌
5星 · 资源好评率100%
以下是使用osgEarth 3.2绘制云的代码示例,支持云厚度和颜色的调整功能:
```cpp
#include <osgEarth/MapNode>
#include <osgEarthUtil/SkyNode>
#include <osgEarthUtil/Controls>
using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Util::Controls;
// 定义绘制云的函数
void drawCloud(osg::Group* root, float thickness, const osg::Vec4& color)
{
// 创建一个平面,用于绘制云
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, 0.0f, 1.0f));
vertices->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
geom->setVertexArray(vertices.get());
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
colors->push_back(color);
geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
osg::ref_ptr<osg::DrawElementsUInt> primitive =
new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
primitive->push_back(0);
primitive->push_back(1);
primitive->push_back(2);
primitive->push_back(3);
geom->addPrimitiveSet(primitive.get());
// 创建材质属性
osg::ref_ptr<osg::Material> mat = new osg::Material();
mat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
mat->setAlpha(osg::Material::FRONT_AND_BACK, color.a());
// 创建节点
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(geom.get());
geode->getOrCreateStateSet()->setAttributeAndModes(mat.get(), osg::StateAttribute::ON);
root->addChild(geode.get());
// 设置云的厚度
osg::ref_ptr<osg::Uniform> thicknessUniform = new osg::Uniform("thickness", thickness);
geode->getOrCreateStateSet()->addUniform(thicknessUniform.get());
}
int main(int argc, char** argv)
{
// 创建地图节点
osg::ref_ptr<MapNode> mapNode = MapNode::create();
osg::ref_ptr<SkyNode> skyNode = SkyNode::create(mapNode.get());
osg::ref_ptr<osg::Group> root = new osg::Group();
root->addChild(mapNode.get());
root->addChild(skyNode.get());
// 创建调整工具栏
osg::ref_ptr<HBox> toolbar = new HBox();
osg::ref_ptr<Label> thicknessLabel = new Label("Thickness:");
thicknessLabel->setMargin(5.0f);
osg::ref_ptr<HSliderControl> thicknessSlider = new HSliderControl(1.0f, 10.0f, 1.0f);
thicknessSlider->setWidth(150.0f);
thicknessSlider->addEventHandler(new ControlValueChangedHandler([&](Control* control) {
drawCloud(root.get(), thicknessSlider->getValue(), osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
}));
osg::ref_ptr<Label> colorLabel = new Label("Color:");
colorLabel->setMargin(5.0f);
osg::ref_ptr<ColorPickerControl> colorPicker = new ColorPickerControl();
colorPicker->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
colorPicker->addEventHandler(new ControlValueChangedHandler([&](Control* control) {
drawCloud(root.get(), thicknessSlider->getValue(), colorPicker->getColor());
}));
toolbar->addControl(thicknessLabel.get());
toolbar->addControl(thicknessSlider.get());
toolbar->addControl(colorLabel.get());
toolbar->addControl(colorPicker.get());
// 绘制云
drawCloud(root.get(), thicknessSlider->getValue(), colorPicker->getColor());
// 创建视图
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
viewer.getCamera()->setClearColor(osg::Vec4(0.2f, 0.2f, 0.6f, 1.0f));
viewer.getCamera()->setProjectionMatrixAsPerspective(
30.0f, static_cast<double>(viewer.getCamera()->getGraphicsContext()->getScreenWidth()) /
static_cast<double>(viewer.getCamera()->getGraphicsContext()->getScreenHeight()), 1.0f, 1000.0f);
viewer.setUpViewInWindow(50, 50, 800, 600);
viewer.addEventHandler(new osgViewer::StatsHandler());
viewer.addEventHandler(new osgViewer::WindowSizeHandler());
viewer.addEventHandler(new osgViewer::ThreadingHandler());
viewer.addEventHandler(new osgViewer::LODScaleHandler());
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
viewer.addEventHandler(new osgViewer::RecordCameraPathHandler());
viewer.addEventHandler(new osgViewer::ScreenCaptureHandler());
viewer.addEventHandler(new osgViewer::HelpHandler());
viewer.addEventHandler(new osgViewer::StatsHandler());
viewer.addEventHandler(new osgViewer::WindowSizeHandler());
// 运行程序
return viewer.run();
}
```
在上述代码中,我们定义了一个名为`drawCloud`的函数,用于绘制云。该函数接受三个参数:
- `root`:云节点的父节点。
- `thickness`:云的厚度。
- `color`:云的颜色。
在函数中,我们创建了一个平面作为云的形状,设置了材质属性和颜色,并将它们添加到了云节点中。在最后,我们将云的厚度设置为指定的值。
在主函数中,我们创建了一个地图节点和一个天空节点,并将它们添加到根节点中。然后,我们创建了一个调整工具栏,其中包含一个滑块和一个颜色选择器,用于调整云的厚度和颜色。我们使用`drawCloud`函数绘制了一个初始的云,并将它添加到根节点中。最后,我们创建了一个视图并运行程序。
你可以根据自己的需求调整代码,实现你想要的效果。
阅读全文