查看: 1462|回复: 1

osgEarth设置模型旋转角度

[复制链接]
  • TA的每日心情
    开心
    2020-3-20 17:50
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2020-4-28 11:35:59 | 显示全部楼层 |阅读模式
    本帖最后由 liyihongcug 于 2020-4-29 09:21 编辑

    GeoPoint(114,36,10000)

    #include<windows.h>
    #include <osgViewer/Viewer>
    #include <osgEarthDrivers/gdal/GDALOptions>
    #include <osg/ShapeDrawable>
    #include <osgEarthUtil/EarthManipulator>
    #include <osg/MatrixTransform>
    #include <osgEarthFeatures/ConvertTypeFilter>
    #include <osgEarthDrivers/model_simple/SimpleModelOptions>
    #include <osgViewer/ViewerEventHandlers>
    #include <osgGA/StateSetManipulator>

    #ifdef _DEBUG
    #pragma comment(lib, "osgd.lib")
    //#pragma comment(lib, "osgDBd.lib")
    #pragma comment(lib, "osgViewerd.lib")
    #pragma comment(lib, "osgGAd.lib")
    #pragma comment(lib, "osgEarthd.lib")
    #pragma comment(lib, "osgEarthUtild.lib")
    #else
    #pragma comment(lib, "osg.lib")
    #pragma comment(lib, "osgDB.lib")
    #pragma comment(lib, "osgViewer.lib")
    #pragma comment(lib, "osgGA.lib")
    #endif // DEBUG


    // lonlat1:雷达波圆锥顶点
    // lonlat2:轨迹点
    void rotateCone(osg::MatrixTransform* mt, const osgEarth::SpatialReference* sr, osg::Vec3d lonlat1, osg::Vec3d lonlat2)
    {
        // 雷达波模型所在位置
        osgEarth::GeoPoint geoPoint1(
            sr,
            lonlat1.x(),
            lonlat1.y(),
            lonlat1.z(),
            osgEarth::ALTMODE_ABSOLUTE);
        osg::Matrixd matrix1;
        // 获取雷达波模型从原点变换到lonlat1位置的变换矩阵
        geoPoint1.createLocalToWorld(matrix1);
        // 经纬度高程到xyz的变换
        osg::Vec3d world1, world2;
        // geoPoint1.toWorld(world1);//等同于    sr->transformToWorld(lonlat1,world1);
        sr->transformToWorld(lonlat2, world2);
        // 计算轨迹点在雷达波模型坐标系下的位置
        osg::Vec3 point2InRadarCoordinateSystem = world2*osg::Matrix::inverse(matrix1);
        // 在雷达波模型坐标系下,对Z轴进行旋转,与连接原点指向轨迹点方向的矢量重合,计算出此旋转矩阵
        osg::Matrixd rotMat = osg::Matrixd::rotate(osg::Z_AXIS, point2InRadarCoordinateSystem-osg::Vec3(0,0,0));
        // 将计算出的旋转矩阵赋给雷达波模型所在的mt
        mt->setMatrix(rotMat);
    }
    int main(int argc, char** argv)
    {
        osgViewer::Viewer viewer;
        std::string world_tif = "data/world.tif";
        osgEarth::Map* map = new osgEarth::Map();
        // Start with a basemap imagery layer; we'll be using the GDAL driver
        // to load a local GeoTIFF file:
        osgEarth:rivers::GDALOptions basemapOpt;
        basemapOpt.url() = world_tif;
        map->addImageLayer(new osgEarth::ImageLayer(osgEarth::ImageLayerOptions("basemap", basemapOpt)));

        osgEarth::MapNodeOptions mapNodeOptions;
        mapNodeOptions.enableLighting() = false;
        osgEarth::MapNode* mapNode = new osgEarth::MapNode(map, mapNodeOptions);

        osgEarth::Drivers::SimpleModelOptions opt;
        opt.location() = osg::Vec3(118, 40, 10000);
        //opt.url() = "cow.osg.1000,1000,1000.scale";
        osg::Geode* geode = new osg::Geode;
        osg::ShapeDrawable* cone = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(), 10000, 50000));
        //osg::ShapeDrawable* cone = new osg::ShapeDrawable(new osg::Box(osg::Vec3(), 50000));
        geode->addDrawable(cone);
        osg::MatrixTransform* mtCone = new osg::MatrixTransform;
        mtCone->addChild(geode);
        opt.node() = mtCone;
        map->addModelLayer(new osgEarth::ModelLayer("", opt));

        rotateCone(mtCone, map->getProfile()->getSRS(), osg::Vec3(118, 40, 100), osg::Vec3(120, 40, 100));

        osg::Group* root = new osg::Group();
        root->addChild(mapNode);
        viewer.setSceneData(root);
        viewer.setCameraManipulator(new osgEarth::Util::EarthManipulator());

        // Process cmdline args
        //MapNodeHelper().parse(mapNode, arguments, &viewer, root, new LabelControl("Features Demo"));

        //视点定位模型所在位置
        osgEarth::Viewpoint vp("", 118, 40, 1000.0, -2.50, -90.0, 1.5e6);
        (dynamic_cast<osgEarth::Util::EarthManipulator*>(viewer.getCameraManipulator()))->setViewpoint(vp);

        // add some stock OSG handlers:
        viewer.addEventHandler(new osgViewer::StatsHandler());
        viewer.addEventHandler(new osgViewer::WindowSizeHandler());
        viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
        viewer.setUpViewInWindow(100, 500, 1024, 768);
        return viewer.run();
    }


    const osgEarth::Util::EarthManipulator* manip = dynamic_cast<const osgEarth::Util::EarthManipulator*>(_viewer->getCameraManipulator());
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    OSG中国官方论坛-有您OSG在中国才更好

    网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

    联系我们

    • 工作时间:09:00--18:00
    • 反馈邮箱:1315785073@qq.com
    快速回复 返回顶部 返回列表