osgEarth计算鼠标与地球交点的位置,相对地面指定高度的对应点坐标

一、效果演示

请添加图片描述

二、逻辑分析

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;
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

映月光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值