GIS开发笔记(9)结合osg及osgEarth实现三维球经纬网格绘制及显隐
一、实现效果
二、实现原理
按照5°的间隔分别创建经纬线的节点,挂在到组合节点,组合节点挂接到根节点。可以根据需要设置间隔度数和线宽、线的颜色。
三、参考代码
//创建经纬线的节点
osg::Node *GlobeWidget::createGraticuleGeometry(float interval, const osg::Vec4 &color)
{osg::ref_ptr<osg::Geode> geode = new osg::Geode;osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;// 设置颜色colors->push_back(color);geom->setColorArray(colors, osg::Array::BIND_OVERALL);// 生成经线(间隔为interval度)for (float lon = -180.0f; lon <= 180.0f; lon += interval){for (float lat = -89.9f; lat <= 89.9f; lat += 1.0f){osgEarth::GeoPoint pt(osgEarth::SpatialReference::get("wgs84"), lon, lat, 0);osg::Vec3d world;pt.toWorld(world);vertices->push_back(world);}geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,vertices->size() - 180, 180));}// 生成纬线(间隔为interval度)for (float lat = -90.0f; lat <= 90.0f; lat += interval){for (float lon = -180.0f; lon <= 180.0f; lon += 1.0f){osgEarth::GeoPoint pt(osgEarth::SpatialReference::get("wgs84"), lon, lat, 0);osg::Vec3d world;pt.toWorld(world);vertices->push_back(world);}geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,vertices->size() - 361, 361));}geom->setVertexArray(vertices);// 设置线宽osg::ref_ptr<osg::LineWidth> lw = new osg::LineWidth(1.5f);geom->getOrCreateStateSet()->setAttribute(lw);// 配置渲染状态(防止被地形遮挡)osg::StateSet* ss = geom->getOrCreateStateSet();ss->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);// 修复:使用全限定类名 osg::Depthss->setAttribute(new osg::Depth(osg::Depth::LEQUAL, 0, 1, false));ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);geode->addDrawable(geom);return geode.release();
}
//控制经纬线显示隐藏
void GlobeWidget::gridVisible(bool visible)
{if (m_graticuleGroup) {m_graticuleGroup->setNodeMask(visible ? 0xFFFFFFFF : 0x0);}
}