本人的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);
}
运行结果