前言
osgearth_htm示例,围绕US绘制线框,且线框组成的三角形内,会放置一个“模型”,当拉进距离时,线框会1分裂4,同时模型也会分裂,如果最外层是个单角,则删去。不太明白htm模式有什么用处。
执行命令
osgearth_htmd.exe earth_image\world.earth --model ..\..\..\data\red_flag.osg
执行效果
上图左侧放大过程中,三角形裂变,且最外层三角形消失。
代码分析
#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarth/MapNode>
#include <osgEarth/ThreadingUtils>
#include <osgEarth/Metrics>
#include <iostream>
#include <osgEarthUtil/HTM>
#include <osgEarthAnnotation/PlaceNode>
#include <osgEarth/Random>
#define LC "[viewer] "
using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Annotation;
int
usage(const char* name, const char* msg =NULL)
{
OE_NOTICE
<< (msg ? msg : "")
<< "\nUsage: " << name << " file.earth --model <file> [--num <number>] [--debug]" << std::endl
<< MapNodeHelper().usage() << std::endl;
return 0;
}
int
main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc,argv);
if ( arguments.read("--help") )
return usage(argv[0]);
// Viewer setup 创建视景器
osgViewer::Viewer viewer(arguments);
viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy( true, false );
osgDB::Registry::instance()->getObjectWrapperManager()->findWrapper("osg::Image");
viewer.setCameraManipulator( new EarthManipulator(arguments) );
viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
viewer.getCamera()->setNearFarRatio(0.0001);
// 读取模型
std::string modelPath;
if (arguments.read("--model", modelPath) == false)
return usage(argv[0]);
osg::ref_ptr<osg::Node> model = osgDB::readRefNodeFile(modelPath);
if (model.valid() == false)
return usage(argv[0], "Cannot load model file");
int numObjects = 10000;
arguments.read("--num", numObjects);
// 此参数并未被采用???
bool debug = arguments.read("--debug");
// Load the earth file
osg::Node* node = MapNodeHelper().load(arguments, &viewer);
if (!node)
return usage(argv[0], "Cannot load earth file");
viewer.setSceneData( node );
MapNode* mapNode = MapNode::get(node);
// Randomly place object instances around the US.
// 围绕US国,随机生成
const SpatialReference* wgs84 = SpatialReference::get("wgs84");
Random prng;
HTMGroup* htm = new HTMGroup();// 自动组织其内容以提高剔除性能的 osg::Group。
htm->setMaximumObjectsPerCell(250);// 设置单个单元格中可以存在的最大对象数。
htm->setMaximumCellSize(500000);// 能够包含最大个数的HTM单元格尺寸(两倍半径的包围球)
htm->setMinimumCellSize(25000);// HTM单元格最小尺寸
htm->setRangeFactor(5);// 随机因子
mapNode->addChild(htm);// 将设置好的htm对象放入mapNode
for (unsigned i = 0; i < numObjects; ++i)
{
GeoTransform* xform = new GeoTransform();
xform->addChild(model.get());
// 生成随机位置,在US附近
double lon = -115 + prng.next() * 40;
double lat = 25 + prng.next() * 25;
xform->setPosition(GeoPoint(wgs84, lon, lat, 0, ALTMODE_ABSOLUTE));
htm->addChild(xform);
if (i%1000 == 0)
std::cout << "\r" << i << "/" << numObjects << std::flush;
}
std::cout << std::endl;
return viewer.run();
}