Alle-z-y 2020-05-09 10:10 采纳率: 0%
浏览 626

关于osgEarth的earth文件搭建地球的问题

本人的osgearth2.10.1,osg3.6.4,vs2017
我照着osgearth2.10文档的用户指南的规则写了earth文件,在VS里读取结果运行没有结果。请大佬指教一下。

我的earth文件内容

<map name="mymap" type="geocentric" version="2">

<image name="bluemarble" driver="gdal">
   <url>D:/01-Software/osgEarth/Projects/DigitalEarth/data/image/image.tif</url>
</image>

<elevation name="srtm" driver="gdal">
    <url>D:/01-Software/osgEarth/Projects/DigitalEarth/data/heightfiel    d/heightfield.tif</url>
</elevation>

<options>
    <cache type="filesystem">
        <path>D:/01-Software/osgEarth/Projects/DigitalEarth/data/FileCache</path>
    </cache>


#VS里面的代码,首先是osgobject.h

 #pragma once

    #include <osgDB/ReadFile>
    #include <osgGA/TrackballManipulator>
    #include <osgViewer/api/win32/GraphicsWindowWin32>
    #include <osgViewer/Viewer>
    #include <osgEarth/MapNode>
    #include <osgEarthUtil/EarthManipulator>
    #include <osgEarthUtil/ExampleResources>
    #include <osgEarthUtil/AutoClipPlaneHandler>
    class COSGObject
    {
    public:

        COSGObject(HWND hWnd);
        ~COSGObject(void);

    public:
        void InitOSG();//初始化osg
        void InitSceneGraph();  //初始化场景
        void InitCameraConfig();//初始化相机
        void PreFrameUpdate();  //渲染前更新
        void PostFrameUpdate(); //渲染后更新
        static void Render(void * ptr);//渲染线程
        void InitOsgEarth();
        osgViewer::Viewer *getViewer();
    private:
        HWND m_hWnd;
        osgViewer::Viewer *mViewer;
        osg::ref_ptr<osg::Group> mRoot;
        osg::ref_ptr<osgEarth::MapNode> mapNode;
        osg::ref_ptr<osgEarth::Util::EarthManipulator> em;
    };

osgobject.cpp代码

    #include "pch.h"
    #include "OSGObject.h"
    COSGObject::COSGObject(HWND hWnd)
    {
    m_hWnd = hWnd;
    }
    COSGObject::~COSGObject(void)
    {
    }
    void COSGObject::InitOSG()
    {
        InitSceneGraph();
        InitCameraConfig();
        InitOsgEarth();
    }
    void COSGObject::InitSceneGraph() {

    mRoot = new osg::Group;
    osg::ref_ptr<osg::Node> mp = osgDB::readNodeFile("D:/01-Software/osgEarth/Projects/DigitalEart/earthfile/chinasimple.earth");
    mRoot->addChild(mp);    
    mapNode = dynamic_cast<osgEarth::MapNode*>(mp.get());   

    }
    void COSGObject::InitCameraConfig()
    {

    RECT rect;
    mViewer = new osgViewer::Viewer;
    ::GetWindowRect(m_hWnd, &rect);
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
    traits->x = 0;
    traits->y = 0;
    traits->width = rect.right - rect.left;
    traits->height = rect.bottom - rect.top;
    traits->windowDecoration = false;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    traits->setInheritedWindowPixelFormat = true;
    traits->inheritedWindowData = windata;
    osg::GraphicsContext * gc = osg::GraphicsContext::createGraphicsContext(traits);

    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    camera->setGraphicsContext(gc);
    camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height));
    camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width) / static_cast<double>(traits->height), 1.0, 1000.0);

    mViewer->setCamera(camera);
    //mViewer->setCameraManipulator(new osgGA::TrackballManipulator);
    mViewer->setSceneData(mRoot);
    mViewer->realize();
    //近地面自动裁剪AutoClipPlaneCullCallback
    mViewer->getCamera()->addCullCallback(new osgEarth::Util::AutoClipPlaneCullCallback(mapNode));

    mViewer->getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
    mViewer->getCamera()->setNearFarRatio(0.000003f);
    }
    void COSGObject::PreFrameUpdate()
    {
    }
    void COSGObject::PostFrameUpdate()
    {
    }
    void COSGObject::Render(void * ptr) 
    { 
    COSGObject *osg = (COSGObject*)ptr; 
    osgViewer::Viewer * viewer = osg->getViewer();  
    while (!viewer->done()) 
    { 
        osg->PreFrameUpdate();
        viewer->frame();
        osg->PostFrameUpdate();
    }   
    _endthread(); 
    }
    osgViewer::Viewer *COSGObject::getViewer()
    {
        return mViewer;
    }
    void COSGObject::InitOsgEarth()
    {
    //初始化操作器    
    em = new osgEarth::Util::EarthManipulator;
    if (mapNode.valid())
    {
        em->setNode(mapNode);
    }
    em->getSettings()->setArcViewpointTransitions(true);
    mViewer->setCameraManipulator(em);

    }

运行结果
图片说明

  • 写回答

1条回答 默认 最新

  • yu1069153913 2020-09-25 17:06
    关注

    你把这句话去掉试试:
    mViewer->getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES)

    我也遇到了这个问题,然后试了半天,发现去掉之后地球就有了,我的是osg3.6.3 + osgEarth 2.10
    原来在低版本的osgEarth上面加这句话没问题,高版本的我也不知道为什么就会出问题

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程