OSGEARTH3 加载一个地球


在这里插入图片描述

方法一: 读取*.earth文件

读取*.earth文件加载,改文件格式是xml形式,每个节点代表指定的图层:

  1. 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>
  1. 代码内加载*.earth文件
    1. 初始化GDAL;
    2. 初始化OSGEARTH;
    3. 加载地球, 读取earth文件
    4. 运行
#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
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容