OSGEarth绘制随模型位置变化而动态移动的线段
程序员文章站
2022-06-10 21:12:37
...
目录
本文主要介绍在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);
}
}
下一篇: osg设置模型纹理