查看: 1555|回复: 1

osgEarth 如何在地球的上空添加一个模型

[复制链接]

该用户从未签到

发表于 2017-3-17 11:24:10 | 显示全部楼层 |阅读模式
我用addModelLayer为什么加不进去呢?用的是simple驱动。

该用户从未签到

发表于 2021-3-4 17:58:54 | 显示全部楼层

#include <vector>
#include <iostream>
#include <fstream>
#include <cstring>
#include <time.h>
#include <Windows.h>

#include <osgEarth/MapNode>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>

#include <osgViewer/Viewer>
#include <osgDB/WriteFile>

using namespace std;



int main(int argc, char* argv[])
{
        osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer;
        //操作器
        osg::ref_ptr<osgEarth::Util::EarthManipulator> earthManipulator = new osgEarth::Util::EarthManipulator;
        //设置相机操作器
        viewer->setCameraManipulator(earthManipulator);
        //根节点
        osg::ref_ptr<osg::Group>root = new osg::Group;

        //加载地球节点
        osg::Node* earthNode = osgDB::readNodeFile("H:/my-softwear/OSG_PATH/OsgEarth2.8_SDK/data/arcgisonline.earth");
        //将地球节点加入根节点
        root->addChild(earthNode);
        //设置现场数据
        viewer->setSceneData(root.get());
        //实现
        viewer->realize();


        osgEarth::MapNode* mapNode = osgEarth::MapNode::findMapNode(earthNode);
        //检测地图节点是否创建好
        if (!mapNode) return 0;


       
        //空间设置,水平和垂直
        const osgEarth::SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();


        //添加模型1
        osg::Node* model = osgDB::readNodeFile("H:/my-softwear/OSG_PATH/Osg3.4.0_SDK/data/cessna.osg");
        //osg中光照只会对有法线的模型起作用,而模型经过缩放后法线是不会变得,
        //所以需要手动设置属性,让法线随着模型大小变化而变化。GL_NORMALIZE 或 GL_RESCALE_NORMAL
        model->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, osg::StateAttribute::ON);

        osg::Matrix Lmatrix;
                                                                                   //角度to弧度
        geoSRS->getEllipsoid()->computeLocalToWorldTransformFromLatLongHeight(osg:egreesToRadians(40.0), osg::DegreesToRadians(116.0), 500000.0, Lmatrix);  //维度,经度,高度,localToWorld
        //放大一些,方便看到
        Lmatrix.preMult(osg::Matrix::scale(osg::Vec3(30000, 30000, 30000)));//x,y,z轴放大倍数

        osg::MatrixTransform* mt = new osg::MatrixTransform;
        mt->setMatrix(Lmatrix);
        mt->addChild(model);
        root->addChild(mt);
       

        viewer->setSceneData(root);
        //视点定位北京地区,此句代码运行后可以直接定位到该坐标,注释后仍能正常显示模型,不过不会自动定位
        earthManipulator->setViewpoint(osgEarth::Viewpoint("模拟无人机", 116, 40, 0.0, 0.0, -90.0, 1.5e7));//const char* name, double lon, double lat, double z, double heading, double pitch, double range
                                                                                                                                  //经度,维度,未知作用的参数,方位角(0度为正前方,90度为右侧,180度为后面,360度为左侧),俯仰角(-90~0,0度是水平方向,-90度是俯视),可选参数
       

        return viewer->run();
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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