一、效果演示
二、逻辑分析
osgEarth计算鼠标与地球交点的位置,相对地面指定高度的对应点的坐标。
鄙人不善言辞,代码整体就100行左右,有需要的直接看代码吧
三、整体代码实现
#include <iostream>
#include <osgEarth/MapNode>
#include <osgViewer/Viewer>
#include <osgEarthUtil/EarthManipulator>
#include <osgGA/GUIEventHandler>
#include <osg/Point>
#include <osg/LineWidth>
using namespace std;
osgEarth::MapNode *pMapNode;
osg::Group *pLineGroup = new osg::Group;
bool flag = true;
osg::ref_ptr<osg::Node> CreateLine(vector<osg::Vec3f> points)
{
//初始化相关对象
osg::ref_ptr<osg::Vec3Array> pVertexArray = new osg::Vec3Array;
osg::ref_ptr<osg::Vec4Array> pColorArray = new osg::Vec4Array;
osg::ref_ptr<osg::Geometry> pGeom = new osg::Geometry;
pGeom->setVertexArray(pVertexArray.get());
pGeom->setColorArray(pColorArray.get());
pGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
pGeom->setDataVariance(osg::Object::STATIC);
pGeom->setUseVertexBufferObjects(true);
for(int i = 0; i < (int)points.size(); i++)
{
pVertexArray->push_back(points.at(i));
pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
}
pGeom->addPrimitiveSet(new osg::DrawArrays(flag ? osg::PrimitiveSet::LINES
: osg::PrimitiveSet::POINTS, 0, (int)points.size()));
osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
pGeode->addDrawable(pGeom);
//关闭光照,每个面看起来都一样
osg::StateSet* stateSet = pGeode->getOrCreateStateSet();
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
// stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
// stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
//设置点的大小
osg::LineWidth* pWidth = new osg::LineWidth;
pWidth->setWidth(2);
stateSet->setAttribute(pWidth);
//设置点的大小
osg::Point* pPoint = new osg::Point;
pPoint->setSize(10);
stateSet->setAttribute(pPoint);
return pGeode.get();
}
class CCustomEvent : public osgGA::GUIEventHandler
{
public:
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override
{
if(ea.getEventType() == osgGA::GUIEventAdapter::PUSH
&& ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)
{
//根据窗口视图位置和地图地形标准获取当前鼠标与地球交点的位置的世界坐标
osg::Vec3d w;
pMapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), w);
//计算当前点相对地球地面高度为h的点
double h = 2000000;//相对高度,可以自由设置
double r = sqrt(pow(w.x(), 2) + pow(w.y(), 2) + pow(w.z(), 2));
double x = (r+h)*w.x()/r;
double y = (r+h)*w.y()/r;
double z = (r+h)*w.z()/r;
osg::Vec3d calV(x, y, z);
//将计算的点进行绘制
vector<osg::Vec3f> points;
points.push_back(w);
points.push_back(calV);
pLineGroup->addChild(CreateLine(points));
}
if(ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN
&& ea.getKey() == osgGA::GUIEventAdapter::KEY_K)
{
flag = !flag;
}
return false;
}
};
int main()
{
osg::Group* root = new osg::Group();
osg::Node* earthNode = osgDB::readNodeFile("D:\\QtCode\\GGD_PRO_20240105\\GGD_YF\\data\\globel3d.earth");
pMapNode = osgEarth::MapNode::findMapNode( earthNode );
root->addChild(pMapNode);
root->addChild(pLineGroup);
osgViewer::Viewer viewer;
viewer.setUpViewInWindow(100, 100, 800, 600);
viewer.addEventHandler(new CCustomEvent);
viewer.setSceneData( root );
viewer.run();
return 0;
}