osgEarth基于callback更新绘制的图形(附带:osgEarth绘制多边形方法)

本文主要介绍在osgEarth中基于callback实现实时更新绘制的图形。
绘制图形和调用callback的代码:

void drawLakes()
{
	//初始化绘图参数
	const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();
	//湖面
	osgEarth::Symbology::Style m_lakeStyle;
	osgEarth::Annotation::FeatureNode* m_pLakeNode;
	osgEarth::Annotation::FeatureEditor* m_pPolygonEdit;
	
	m_lakeStyle.getOrCreate()
		->fill()->color() = osgEarth::Symbology::Color::Color(0.0, 0.27, 0.42, 1.0);
	m_lakeStyle.getOrCreate()
		->clamping() = osgEarth::Symbology::AltitudeSymbol::CLAMP_TO_TERRAIN;
	m_lakeStyle.getOrCreate()
		->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_DRAPE;
	m_lakeStyle.getOrCreate()
		->verticalOffset() = 0.1;

	osgEarth::Features::Feature* pFeature = new osgEarth::Features::Feature(
		new osgEarth::Symbology::Polygon,
		geoSRS, m_lakeStyle);
	m_pLakeNode = new osgEarth::Annotation::FeatureNode(
		mapNode, pFeature);


	//添加所要连接的点的经纬度和高度
	std::vector m_vecPoint;
	m_vecPoint.push_back(osg::Vec3(112.885, 21.9483, 130));
	m_vecPoint.push_back(osg::Vec3(112.840, 21.905, 200));
	m_vecPoint.push_back(osg::Vec3(112.820, 21.930, 200));
	m_vecPoint.push_back(osg::Vec3(112.820, 21.940, 200));
	m_vecPoint.push_back(osg::Vec3(112.885, 21.9483, 200));

	

	pFeature->getGeometry()->clear();
	m_pLakeNode->setStyle(m_lakeStyle);
	for (int i = 0; i < m_vecPoint.size(); ++i)
	{
		pFeature->getGeometry()->push_back(m_vecPoint[i]);
	}

	m_pLakeNode->init();
	m_pLakeNode->addUpdateCallback(new UpdateLake());
	mRoot->addChild(m_pLakeNode);
}

callback代码:

class UpdateLake :public osg::NodeCallback
{
public:
	UpdateLake() :mRadius(0.001)
	{//
	}
	virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
	{
		osg::ref_ptr m_pLakeNode =
			dynamic_cast(node);

		if (m_pLakeNode.get()) {
			osgEarth::Features::Feature* pFeature = m_pLakeNode->getFeature();//删除所有的geomertry对象
			mRadius = mRadius + 1e-6;

			osgEarth::Symbology::Style m_lakeStyle;
			m_lakeStyle.getOrCreate()
				->fill()->color() = osgEarth::Symbology::Color::Color(0.0, 0.27, 0.42, 1.0);
			m_lakeStyle.getOrCreate()
				->clamping() = osgEarth::Symbology::AltitudeSymbol::CLAMP_TO_TERRAIN;
			m_lakeStyle.getOrCreate()
				->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_DRAPE;
			m_lakeStyle.getOrCreate()
				->verticalOffset() = 0.1;

			//添加所要连接的点的经纬度和高度
			std::vector m_vecPoint;
			m_vecPoint.push_back(osg::Vec3(112.885, 21.9483, 130));
			m_vecPoint.push_back(osg::Vec3(112.840, 21.905, 200));
			m_vecPoint.push_back(osg::Vec3(112.820, 21.930, 200));
			m_vecPoint.push_back(osg::Vec3(112.820 + mRadius, 21.940 + mRadius, 200));
			m_vecPoint.push_back(osg::Vec3(112.885 + mRadius, 21.9483 + mRadius, 200));
			pFeature->getGeometry()->clear();
			m_pLakeNode->setStyle(m_lakeStyle);
			for (int i = 0; i < m_vecPoint.size(); ++i)
			{
				pFeature->getGeometry()->push_back(m_vecPoint[i]);
			}



			m_pLakeNode->init();
		}

		traverse(node, nv);


	}
public:
	float mRadius;
};

你可能感兴趣的:(OSG)