osgEarth地球表面放置模型不依赖三方,计算姿态

一、效果演示

请添加图片描述

二、逻辑分析

不依赖osgEarth的内置放置模型的接口,自己实现模型放置姿态的计算。

三、整体代码实现

#include <iostream>

#include <osgEarth/MapNode>
#include <osgViewer/Viewer>
#include <osgEarthUtil/EarthManipulator>
#include <osgGA/GUIEventHandler>
#include <osg/Point>
#include <osg/LineWidth>
#include <osg/MatrixTransform>
#include <osg/Switch>

using namespace std;

osgEarth::MapNode *pMapNode;
osg::Group *pLineGroup = new osg::Group;

osg::ref_ptr<osg::Node> CreateRect()
{
    //初始化相关对象
    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);

    pVertexArray->push_back(osg::Vec3(-1.0, 1.0, 0.0));
    pVertexArray->push_back(osg::Vec3( 1.0, 1.0, 0.0));
    pVertexArray->push_back(osg::Vec3( 1.0, -1.0, 0.0));
    pVertexArray->push_back(osg::Vec3(-1.0, -1.0, 0.0));
    pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
    pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
    pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
    pColorArray->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));

    pGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));

    osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;
    pGeode->addDrawable(pGeom);

    //关闭光照,每个面看起来都一样
    osg::StateSet* stateSet = pGeode->getOrCreateStateSet();
    stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

    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);
            if(w.x() == 0)
            {
                return false;
            }
            //计算当前点相对地球地面高度为h的点
//            double h = 2000000;//相对高度,可以自由设置
            double h = 0;//相对高度,可以自由设置
            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);
            //进行姿态位置变换
            osg::Matrix mat;
            mat.translate(calV);
            osg::MatrixTransform* pTrans = new osg::MatrixTransform;
            //沿X轴转动的夹角
            double acosX = y > 0 ? (M_PI*2 - acos(z/sqrt(y*y+y*y+z*z))) : acos(z/sqrt(y*y+y*y+z*z));
            /***
             * 沿Y轴转动的夹角,原点O到当前点P的向量到yoz平面的投影向量长除以OP向量的长,
             * 然后利用反三角函数求出夹角:acos(sqrt(y*y+z*z)/sqrt(x*x+y*y+z*z))
             */
            double acosY = x > 0 ? acos(sqrt(y*y+z*z)/sqrt(x*x+y*y+z*z)) : (M_PI*2 - acos(sqrt(y*y+z*z)/sqrt(x*x+y*y+z*z)));
            pTrans->setMatrix(osg::Matrix::rotate(acosY, 0, 1, 0)*
                              osg::Matrix::rotate(acosX, 1, 0, 0)*
                              osg::Matrix::scale(1000000, 1000000, 1000000)*
                              osg::Matrix::translate(calV));

//            osg::Node* pNode1 = osgDB::readNodeFile("cessna.osg");
            osg::Node* pNode1 = osgDB::readNodeFile("axes.osgt");
            osg::StateSet* stateSet = pNode1->getOrCreateStateSet();
            stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
            pTrans->addChild(pNode1);
            pTrans->addChild(CreateRect());
            pLineGroup->addChild(pTrans);
        }
        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");
//    osg::Node* earthNode = osgDB::readNodeFile("cessna.osg");
    pMapNode = osgEarth::MapNode::findMapNode( earthNode );
    root->addChild(earthNode);
    root->addChild(pLineGroup);

    osg::Node* pNode1 = osgDB::readNodeFile("axes.osgt");
    osg::StateSet* stateSet = pNode1->getOrCreateStateSet();
    stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    osg::MatrixTransform* pTrans = new osg::MatrixTransform;
    pTrans->setMatrix(osg::Matrix::scale(10000000, 10000000, 10000000));
    pTrans->addChild(pNode1);
    root->addChild(pTrans);

    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow(100, 100, 800, 600);
    viewer.addEventHandler(new CCustomEvent);
    viewer.setSceneData( root );
    viewer.run();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

映月光

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

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

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

打赏作者

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

抵扣说明:

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

余额充值