osg实现任一经纬度上添加一个小球

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>

#include <osgViewer/Viewer>
#include <osg/ShapeDrawable>
#include <osgDB/ReadFile>

#include <osgEarth/MapNode>
#include <osgEarth/ImageLayer>

#include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarthDrivers/feature_ogr/OGRFeatureOptions>

#include <osgEarthFeatures/FeatureSourceLayer>
#include <osgEarthFeatures/FeatureModelLayer>

#include <osgEarthUtil/EarthManipulator>
#include <osg/Node>
#include <osg/MatrixTransform>
using namespace std;


//绘制几何体
osg::Geode* createShpe()
{
    //
    osg::Geode *geode = new osg::Geode();
    //半径
    float radius = 50000.0f;
    //高度
    float height = 160.0f;

    //精细度
    osg::TessellationHints* hints1 = new osg::TessellationHints();
    //设置精细度
    hints1->setDetailRatio(1.1f);

    //创建球体
    osg::Sphere *sphere = new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius);
    osg::ShapeDrawable *draw1 = new osg::ShapeDrawable(sphere, hints1);

    geode->addDrawable(draw1);

    return geode;
}



//经纬度转世界坐标
osg::Vec3d LonLatAltToWorld(const osg::Vec3d lonLatAlt)
{
	osg::Vec3d world;
	osg::EllipsoidModel* em = new osg::EllipsoidModel();
	//Lat纬度Long经度
	em->convertLatLongHeightToXYZ(osg::DegreesToRadians(lonLatAlt.y()),
	osg::DegreesToRadians(lonLatAlt.x()), lonLatAlt.z(), world.x(), world.y(), world.z());

	return world;
}



void AddVector(osg::ref_ptr<osgEarth::Map> map)
{
	//
	std::string filePath = "C:/Users/lenovo/Downloads/OSGEarth2.10.1_VS2019_X64/OSGEARTH2.10_VS2019_x64/data/boston_buildings_utm19.shp";
	osgEarth::Drivers::OGRFeatureOptions featureData;
	featureData.url() = filePath;

	//	   如果缺少空间参考,可以手动指定	
	//    ifstream infile("C:/Data/vector/hs/23.prj");
	//    string line;
	//    getline(infile, line);
	//    featureData.profile()->srsString() = line;

	// Make a feature source layer and add it to the Map:
	osgEarth::Features::FeatureSourceLayerOptions ogrLayer;
	ogrLayer.name() = filePath + "_source";
	ogrLayer.featureSource() = featureData;
	osgEarth::Features::FeatureSourceLayer*  featureSourceLayer = new osgEarth::Features::FeatureSourceLayer(ogrLayer);
	map->addLayer(featureSourceLayer);
	osgEarth::Features::FeatureSource *features = featureSourceLayer->getFeatureSource();
	if (!features)
	{
		printf(("无法打开该矢量文件!"));
		return;
	}
	   
	//
	osgEarth::Features::FeatureModelLayerOptions fmlOpt;
	fmlOpt.name() = filePath;
	fmlOpt.featureSourceLayer() = filePath + "_source";
	fmlOpt.enableLighting() = false;

	osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
	map->addLayer(fml);
}

int main()
{
	osgEarth::ProfileOptions profileOpts;

	//地图配置:设置缓存目录
	osgEarth::Drivers::FileSystemCacheOptions cacheOpts;
	string cacheDir = "D:/Work/OSGNewBuild/tmp";
	cacheOpts.rootPath() = cacheDir;

	//
	osgEarth::MapOptions mapOpts;
	mapOpts.cache() = cacheOpts;
	mapOpts.profile() = profileOpts;

	//创建地图节点
	osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(mapOpts);
	osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map);

	osgEarth::Drivers::GDALOptions gdal;
	gdal.url() = "C:/Users/lenovo/Downloads/OSGEarth2.10.1_VS2019_X64/OSGEARTH2.10_VS2019_x64/data/world.tif";
	osg::ref_ptr<osgEarth::ImageLayer> layer = new osgEarth::ImageLayer("BlueMarble", gdal);
	map->addLayer(layer);

	AddVector(map);


	//创建一个球
	osg::ref_ptr<osg::Geode>  nodee=createShpe();

	osg::Vec3d mm=LonLatAltToWorld(osg::Vec3d(77.4f,8.07f,0));
     osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform;
        trans->setMatrix(osg::Matrix::translate(mm));
        trans->addChild(nodee);

	mapNode->addChild(trans);


	osgViewer::Viewer viewer;
	viewer.setSceneData(mapNode);

	osg::ref_ptr< osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
	viewer.setCameraManipulator(mainManipulator);

	viewer.setUpViewInWindow(100, 100, 800, 600);

	return viewer.run();
}

FR:徐海涛(hunk Xu) 
QQ技术交流群:386476712

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值