OSGEarth繪製隨模型位置變化而動態移動的線段

目錄

一、編寫Callback

二、初始化並綁定Callback


本文主要介紹在OSGEarth中繪製隨模型位置變化而動態移動的線段,即兩個三維模型通過線段進行連接,在模型移動的過程中,連接的線段跟着模型做相應的位移。

一、編寫Callback

#pragma once
class UpdateLink :public osg::NodeCallback
{
public:
	osg::Node *plane;
	osg::Node *car;
public:
	UpdateLink(osg::Node *p, osg::Node *c)
	{
		plane = p;
		car = c;
	}
	virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
	{
		osg::ref_ptr<osgEarth::Annotation::FeatureNode> m_pLakeFeatureNode =
			dynamic_cast<osgEarth::Annotation::FeatureNode*>(node);
		
		if (m_pLakeFeatureNode.get()) {
			std::vector<osg::Vec3> m_vecLakePoint;
			//獲取car節點所在的世界座標
			osgEarth::Features::Feature* m_pLakeFeature = m_pLakeFeatureNode->getFeature();//刪除所有的geomertry對象

			osgEarth::Symbology::Style m_lineLakeStyle;
			osg::Vec3 center = car->getBound().center() * car->getWorldMatrices()[0];
			double lon, lat, height;
			//將世界座標XYZ轉換成經度、緯度、高度信息
			osg::EllipsoidModel* emodel = new osg::EllipsoidModel();
			emodel->convertXYZToLatLongHeight(center.x(), center.y(), center.z(), lat, lon, height);
			//存儲當前點
			m_vecLakePoint.push_back(osg::Vec3(osg::RadiansToDegrees(lon), osg::RadiansToDegrees(lat), height));

			//獲取plane節點所在的世界座標
			osg::Vec3 center1 = plane->getBound().center() * plane->getWorldMatrices()[0];
			double lon1, lat1, height1;
			//將世界座標XYZ轉換成經度、緯度、高度信息
			emodel->convertXYZToLatLongHeight(center1.x(), center1.y(), center1.z(), lat1, lon1, height1);
			//存儲當前點
			m_vecLakePoint.push_back(osg::Vec3(osg::RadiansToDegrees(lon1), osg::RadiansToDegrees(lat1), height1));
			// 初始化繪圖參數
			m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
				->stroke()->color() = osgEarth::Symbology::Color::Blue;
			m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
				->stroke()->width() = 4.0;
			m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
				->tessellation() = 20.0;
			m_lineLakeStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
				->verticalOffset() = 0.1;
			m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
				->stroke()->stipple() = 255;
			//添加所要連接的點的經緯度和高度
			m_pLakeFeature->getGeometry()->clear();
			m_pLakeFeatureNode->setStyle(m_lineLakeStyle);
			for (int i = 0; i < m_vecLakePoint.size(); ++i)
			{

				m_pLakeFeature->getGeometry()->push_back(m_vecLakePoint[i]);
			}
			m_pLakeFeatureNode->init();

		}
		traverse(node, nv);
	}
};

二、初始化並綁定Callback

void drawLink()
{
	//初始化繪圖參數
	const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();
	//獲取car節點所在的世界座標// 初始化繪圖參數
	osgEarth::Symbology::Style m_lineLakeStyle;
	m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
		->stroke()->color() = osgEarth::Symbology::Color::Blue;
	m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
		->stroke()->width() = 4.0;
	m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
		->tessellation() = 20.0;
	m_lineLakeStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
		->verticalOffset() = 0.1;
	m_lineLakeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
		->stroke()->stipple() = 255;
	{	
		osgEarth::Features::Feature* m_pLakeFeature;
		std::vector<osg::Vec3> m_vecLakePoint;
		m_pLakeFeature = new osgEarth::Features::Feature(
			new osgEarth::Annotation::LineString,
			geoSRS, m_lineLakeStyle);
		m_pLakeFeatureNode = new osgEarth::Annotation::FeatureNode(
			mapNode, m_pLakeFeature);
		osg::Vec3 center = car->getBound().center() * car->getWorldMatrices()[0];
		double lon, lat, height;
		//將世界座標XYZ轉換成經度、緯度、高度信息
		csn->getEllipsoidModel()->convertXYZToLatLongHeight(center.x(), center.y(), center.z(), lat, lon, height);
		//存儲當前點
		m_vecLakePoint.push_back(osg::Vec3(osg::RadiansToDegrees(lon), osg::RadiansToDegrees(lat), height));

		//獲取plane節點所在的世界座標
		osg::Vec3 center1 = flyAirport->getBound().center() * flyAirport->getWorldMatrices()[0];
		double lon1, lat1, height1;
		//將世界座標XYZ轉換成經度、緯度、高度信息
		csn->getEllipsoidModel()->convertXYZToLatLongHeight(center1.x(), center1.y(), center1.z(), lat1, lon1, height1);
		//存儲當前點
		m_vecLakePoint.push_back(osg::Vec3(osg::RadiansToDegrees(lon1), osg::RadiansToDegrees(lat1), height1));

		//添加所要連接的點的經緯度和高度
		m_pLakeFeature->getGeometry()->clear();
		m_pLakeFeatureNode->setStyle(m_lineLakeStyle);
		for (int i = 0; i < m_vecLakePoint.size(); ++i)
		{
			m_pLakeFeature->getGeometry()->push_back(m_vecLakePoint[i]);
		}

		m_pLakeFeatureNode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);//關閉深度測試
		m_pLakeFeatureNode->init();
		m_pLakeFeatureNode->addUpdateCallback(new UpdateLink(flyAirport, car));
		mRoot->addChild(m_pLakeFeatureNode);
	}
}

 

發佈了277 篇原創文章 · 獲贊 222 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章