osg系列文章目录
前言
在osg3.6.5 osgEarth3.2中,加载tif格式文件,遇到“由于找不到libprotobufd.dll,无法继续执行代码。重新安装程序可能解决此问题”,而且有时候在exe文件同目录下会改变某些动态库,比如:libprotobufd.dll、libprotobuf.dll、sqlite3.dll等,按照常理应该不会。我是把exe生成在osgEarth库的bin目录下,当我清理解决方案时,难道会影响到相关的dll吗。也有另外一种可能,会不会是我机器上windows自带的杀毒软件把bin下面dll当做病毒误删了。反正每过一段时间,就会发现bin目录下会少一些dll,百思不得其解,如果哪位大佬知道,请为小弟指点迷津哈,万分感谢
一、加载tif文件
实现思路
初始化 osgEarth:首先需要初始化 osgEarth 库。
创建地图对象:创建一个 osgEarth::Map 对象来管理和组织图层。
创建地图节点:使用地图对象创建一个 osgEarth::MapNode 节点,作为场景图的一部分。
加载图层:使用 osgEarth::GDALImageLayer 加载 TIFF 文件,并将其添加到地图对象中。
设置查看器:创建一个 osgViewer::Viewer 对象来显示场景,并设置相应的相机操控器。
设置初始视点:设置初始视点,使查看器能够正确地显示加载的图层。
运行查看器:将地图节点添加到查看器的场景数据中,并启动查看器。
// LoadTif.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <Windows.h>
#include <iostream>
#include <string>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgEarth/MapNode>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarth/ImageLayer>
#include <osgEarth/EarthManipulator>
#include <osgEarth/OGRFeatureSource>
#include <osgEarth/FeatureModelLayer>
#include <osgEarth/FeatureImageLayer>
#include <osgEarth/ECEF>
#include <osgEarth/GeoData>
#include <osgEarth/Viewpoint>
#include <osgEarth/TerrainOptions>
#include <osgGA/SphericalManipulator>
#include <osgEarth/Symbol>
#include <osgEarth/Feature>
#include <osgEarth/FeatureModelSource>
#include <osgUtil/Tessellator>
#include <osg/LineWidth>
#include <osg/Depth>
#include <osg/PolygonMode>
#include <osgEarth/ImageOverlay>
#include <osgEarth/GDAL>
//#include <osgEarthDrivers/tms/TMSOptions>
using namespace std;
#include <iostream>
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::Node> mp = osgDB::readNodeFile("../vs2022_64bit_3rdParty_osg365_oe32/runtime/test/earthFile/china-simple.earth");
osg::ref_ptr<osg::Group> gpRoot = new osg::Group;
gpRoot->addChild(mp);
osg::ref_ptr<osgEarth::MapNode> mapNode = dynamic_cast<osgEarth::MapNode*>(mp.get());
if (mapNode)
{
osg::ref_ptr<osgEarth::Map> map = mapNode->getMap();
osg::ref_ptr<osgEarth::GDALImageLayer> baselayer = new osgEarth::GDALImageLayer();
baselayer->setURL("world.tif"); //world.tif
map->addLayer(baselayer);
}
// Use EarthManipulator to control the camera
osg::ref_ptr<osgEarth::EarthManipulator> manipulator = new osgEarth::EarthManipulator;
manipulator->getSettings()->setMinMaxPitch(-90.0, 90.0);
manipulator->getSettings()->setThrowingEnabled(false);
manipulator->getSettings()->setLockAzimuthWhilePanning(false);
viewer->setCameraManipulator(manipulator);
// Set the initial viewpoint to look at Chengdu
osgEarth::Viewpoint vp("Chengdu", 104.0668, 30.5728, 1000000.0, 0.0, -45.0, 25000000.0);
manipulator->setHomeViewpoint(vp);
//osg::DisplaySettings::instance()->setNumMultiSamples(8); // 8x MSAA
// Run the viewer
viewer->setUpViewInWindow(100, 100, 1600, 1200);
viewer->setSceneData(gpRoot);
viewer->run();
return 0;
}