OSGEARTH3 加载一个地球
方法一: 读取*.earth文件
读取*.earth文件加载,改文件格式是xml形式,每个节点代表指定的图层:
- FreeEarth_flat_simple.earth文件内容,加载一个基础图层,即使用一张全球大图作为底图:
world.tif的路径是相对于*.earth文件的。
<!--
osgEarth Sample - simple image
-->
<map name="Demo: simple image">
<image name="default" driver="gdal">
<url>../world.tif</url>
<shader>
<uniform name="gamma" value="1.5"/>
<![CDATA[
#pragma vp_entryPoint gammaCorrection
#pragma vp_location fragment
uniform float gamma;
void gammaCorrection(inout vec4 color)
{
color.rgb = pow(color.rgb, vec3(1.0/gamma));
}
]]>
</shader>
</image>
</map>
- 代码内加载*.earth文件
- 初始化GDAL;
- 初始化OSGEARTH;
- 加载地球, 读取earth文件
- 运行
#include <osgEarth/Common>
#include <gdal_priv.h>
#include <ogr_api.h>
#include <ogr_core.h>
#include <ogr_feature.h>
#include <ogr_geometry.h>
#include <ogrsf_frmts.h>
#include <iostream>
#include <osgViewer/Viewer>
#include <osg/Vec3d>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgEarth/EarthManipulator>
#include <osgEarth/MapNode>
#include <osgEarth/GDAL>
#include <osgEarth/OGRFeatureSource>
#include <osgEarth/FeatureImageLayer>
#include <osgEarth/GeoTransform>
int main(int argc, char** argv)
{
OGRRegisterAll();
GDALAllRegister();
CPLSetConfigOption("GDAL_DATA", "../../Data/gdal_data");
CPLSetConfigOption("CPL_DEBUG", "YES");
CPLSetConfigOption("CPL_LOG", "../LOG/gdal.log");
osgEarth::initialize();
{
// map
osg::Node* globe = osgDB::readNodeFile("../../Data/3d-data/Data/earth/FreeEarth_flat_simple.earth");
osgEarth::MapNode* mapNode = osgEarth::MapNode::get(globe);
// viewer
osgViewer::Viewer viewer;
viewer.setSceneData(mapNode);
// manipulator
osg::ref_ptr<osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
viewer.setCameraManipulator(mainManipulator);
// run
viewer.setUpViewInWindow(100, 100, 800, 600);
viewer.run();
}
return 0;
}
方法二: 图层加载
直接通过代码的形式创建layer, 进行加载:.
world.tif路径相对于exe.
1. 初始化GDAL;
2. 初始化OSGEARTH;
3. 加载地球.
4. 加载基础图层
5. 运行
#include <osgEarth/Common>
#include <gdal_priv.h>
#include <ogr_api.h>
#include <ogr_core.h>
#include <ogr_feature.h>
#include <ogr_geometry.h>
#include <ogrsf_frmts.h>
#include <iostream>
#include <osgViewer/Viewer>
#include <osg/Vec3d>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgEarth/EarthManipulator>
#include <osgEarth/MapNode>
#include <osgEarth/GDAL>
#include <osgEarth/OGRFeatureSource>
#include <osgEarth/FeatureImageLayer>
#include <osgEarth/GeoTransform>
int main(int argc, char** argv)
{
OGRRegisterAll();
GDALAllRegister();
CPLSetConfigOption("GDAL_DATA", "../../Data/gdal_data");
CPLSetConfigOption("CPL_DEBUG", "YES");
CPLSetConfigOption("CPL_LOG", "../LOG/gdal.log");
osgEarth::initialize();
{
// map(empty)
osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map;
osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map);
// base layer
osg::ref_ptr<osgEarth::GDALImageLayer> baselayer = new osgEarth::GDALImageLayer();
baselayer->setURL("../../Data/3d-data/Data/world.tif");
map->addLayer(baselayer);
// viewer
osgViewer::Viewer viewer;
viewer.setSceneData(mapNode);
osg::ref_ptr<osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
viewer.setCameraManipulator(mainManipulator);
// run
viewer.setUpViewInWindow(100, 100, 800, 600);
viewer.run();
}
return 0;
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容