本文主要介绍通过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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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(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 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
运行,即可看到下列结果:
编程技术与生活(ID:hw_cchang)