版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_39611196/article/details/86562709
本文主要介绍通过VS配置编译好的osgEarth,并完成第一个小例子。
一、VS配置osgEarth
打开VS,新建win32控制台项目,选择空项目。
(1)配置包含目录
在工程上右键->属性->VC++目录
包含目录:E:\osg+osgearth_64\include
(2)配置库文件
在工程上右键->属性->VC++目录
库目录:E:\osg+osgearth_64\lib
右键->属性->链接器->输入->附加依赖项,输入如下文件:
(下列为debug模式的依赖库,若选择release模式,则去除库名称后的d即可,如osgd.lib->osg.lib)
osgd.lib
osgUtild.lib
osgDBd.lib
osgGAd.lib
osgViewerd.lib
osgEarthd.lib
osgEarthUtild.lib
osgEarthFeaturesd.lib
osgEarthSymbologyd.lib
OpenThreadsd.lib
二、小例子
新建cpp文件test.cpp,并输入如下代码:
#include <osgGA/GUIEventHandler>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgEarth/MapNode>
#include <osgEarth/Registry>
#include <osgEarth/ObjectIndex>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarthUtil/Controls>
#include <osgEarthUtil/RTTPicker>
#include <osgEarthFeatures/Feature>
#include <osgEarthFeatures/FeatureIndex>
#define LC "[feature_query] "
using namespace osgEarth::Util;
using namespace osgEarth::Util::Controls;
using namespace osgEarth::Features;
//-----------------------------------------------------------------------
/**
* Creates a simple user interface for the demo.
*/
Container* createUI()
{
VBox* vbox = new VBox();
vbox->setVertAlign(Control::ALIGN_TOP);
vbox->setHorizAlign(Control::ALIGN_RIGHT);
vbox->addControl(new LabelControl("Feature Query Demo", Color::Yellow));
vbox->addControl(new LabelControl("Click on a feature to see its attributes."));
return vbox;
}
//-----------------------------------------------------------------------
/**
* Query Callback that displays the targeted feature's attributes in a
* user interface grid control.
*/
class ReadoutCallback : public RTTPicker::Callback
{
public:
ReadoutCallback(ControlCanvas* container) : _lastFID(~0)
{
_grid = new Grid();
_grid->setBackColor(osg::Vec4(0, 0, 0, 0.7f));
container->addControl(_grid);
}
void onHit(ObjectID id)
{
FeatureIndex* index = Registry::objectIndex()->get<FeatureIndex>(id).get();
Feature* feature = index ? index->getFeature(id) : 0L;
if (feature && feature->getFID() != _lastFID)
{
_grid->clearControls();
unsigned r = 0;
_grid->setControl(0, r, new LabelControl("FID", Color::Red));
_grid->setControl(1, r, new LabelControl(Stringify() << feature->getFID(), Color::White));
++r;
const AttributeTable& attrs = feature->getAttrs();
for (AttributeTable::const_iterator i = attrs.begin(); i != attrs.end(); ++i, ++r)
{
_grid->setControl(0, r, new LabelControl(i->first, 14.0f, Color::Yellow));
_grid->setControl(1, r, new LabelControl(i->second.getString(), 14.0f, Color::White));
}
if (!_grid->visible())
_grid->setVisible(true);
_lastFID = feature->getFID();
}
}
void onMiss()
{
_grid->setVisible(false);
_lastFID = 0u;
}
bool accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa)
{
return ea.getEventType() == ea.RELEASE; // click
}
Grid* _grid;
FeatureID _lastFID;
};
//------------------------------------------------------------------------
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc, argv);
// a basic OSG viewer
osgViewer::Viewer viewer(arguments);
// install our default manipulator (do this before using MapNodeHelper)
viewer.setCameraManipulator(new EarthManipulator());
// load an earth file, and support all or our example command-line options
// and earth file <external> tags
osg::Group* root = MapNodeHelper().load(arguments, &viewer, createUI());
if (root)
{
viewer.setSceneData(root);
MapNode* mapNode = MapNode::findMapNode(root);
if (mapNode)
{
// Install the query tool.
RTTPicker* picker = new RTTPicker();
viewer.addEventHandler(picker);
picker->addChild(mapNode);
// Install a readout for feature metadata.
ControlCanvas* canvas = ControlCanvas::getOrCreate(&viewer);
picker->setDefaultCallback(new ReadoutCallback(canvas));
}
return viewer.run();
}
else
{
OE_NOTICE
<< "\nUsage: " << argv[0] << " file.earth" << std::endl
<< MapNodeHelper().usage() << std::endl;
}
}
复制下列文件至项目根目录:
cloud_combined_2048.jpg
clouds.clr
clouds.earth
设置命令行参数:
右键->属性->调试->命令参数->clouds.earth
运行,即可看到下列结果: