OSG/osgEarth相关功能函数汇总

1、字符串转double、float

double osg::asciiToFloat(const char* str);//位于\src\osg\Math.h
double osg::asciiToDouble(const char* str);//位于\src\osg\Math.cpp

2、快速生成Geometry、Geode

Geode* osg::createGeodeForImage(osg::Image* image);//位于\src\osg\image.cpp
Geometry* osg::createTexturedQuadGeometry(const Vec3&corner, const Vec3& widthVec,const Vec3& heightVec,float s=1.0f, flaot t=1.0f);//位于\src\osg\Geometry
osg::Geode* geode = new osg::Geode();
osg::ShapeDrawable* drawable = new osg::ShapeDrawable(new osg::Box(osg::Vec3(1,1,1), 1));
geode->addDrawable(drawable);
_root->addChild(geode);


3、检测一个路径是否是网络路径

bool osgDB::containsServerAddress( const std::string& filename);//位于\src\osgDB\FileNameUtils.cpp

相关函数:获取网络协议和网络地址:getServerProtocol()和getServerAddress()

4、文件、文件夹操作。osgEarth处理XML文档

osgDB\FileUtils.h

//新建目录
extern bool makeDirectory(const std::string &directiryPath);
//判断一个文件路径是否存在
extern bool fileExists(const std::string &fileName);
//获取一个目录下的文件列表
typedef std::vector<std::string> DirectoryContents;
extern DirectoryContents getDirectoryContents(const std::string &dirName);
//判断一个filename是文件还是目录
enum FileType{ FILE_NOT_FOUND, REGULAR_FILE, DIRECTORY};
extern FileType fileType(const std::string& filename);
//读取XML文档,osgEarth/XmlUtils
osgEarth::XmlDocument* doc=new osgEarth::XmlDocument::load(const std::string &xmlfile);
osgEarth::Config docConfig=doc->getConfig();
if(!config.empty())
{
   std::string name=config.value("name");
   int age=config.value<int>("age",0);
   //需要注意的是key字符串必须都是小写,例如源XML中为MySet标签,只能写config.find("myset");不能 
   写config.find("MySet");
   osgEarth::Config* mySet=config.find("myset");
   if(config.hasChild("child1"))
   {
      osgEarth::Config child=config.child("child1");
   }
}

5、在地球上放置模型

注意从osgEarth2.4版本开始,删除了osgEarth::Util::ObjectPlacer类,

以下代码适用于2.3之前的版本

osgEarth::Map* map = new osgEarth::Map();
osgEarth::MapNode* mapNode = new osgEarth::MapNode(map);
osgEarth::Util::ObjectPlacer* objPlacer = new osgEarth::Util::ObjectPlacer( mapNode );
osg::Node* model = osgDB::readNodeFile("teapot.3ds");
osg::Node* modelOnEarth = objPlacer->placeNode( model, lat, lon, elevation );//lat,lon表示模型在地球的纬度和经度,elevation是海拔
root->addChild( modelOnEarth );//添加到场景的根

对于2.4版本

osgEarth::GeoPoint point(mapNode->getMapSRS(), lon, lat, elevation, osgEarth::ALTMODE_ABSLOTE);
osg::Matrix m;
point.createLocalToWorld(m);
osg::MatrixTransform* mt = new osg::MatrixTransform(m);
mt->addChild(model);

6、获取地球半径

double equatorRadius=map->getSRS()->getEllipsoid()->getRadiusEquator();//6378137.0赤道半径


7、设定Home视点

osgEarth::Util::EarthManipulator* em = new osgEarth::Util::EarthManipulator();
em->setHomeViewpoint( osgEarth::Util::Viewpoint(116,40,0,0,-90,equqtorRadius*4);//正对北京地面,视点高度离地面高度为4个地球半径
viewer->setCameraManipulator(em);
//设置初始视点
em->setViewpoint(osgEarth::Util::Viewpoint(126,43,0,0,-90,5e4), 5);//5s,定位大东北

8、视点经过屏幕鼠标的射线与远、近裁剪面的交点

#include <osgManipulator/Dragger>

osgManipulator::PointerInfo pi;
pi.setCamera(camera);
pi.setMousePosition(x,y);//x,y非归一化坐标,例如(10,30)
osg::Vec3 nearPoint,farPoint;//射线和远近裁剪面的交点
pi.getNearFarPoints(nearPoint,farPoint);

9、OSG三个坐标轴向量

 

osg\Vec3f文件末尾,直接使用,例如osg::Vec3f v=osg::X_AXIS;
const Vec3f X_AXIS(1.0,0.0,0.0);
const Vec3f Y_AXIS(0.0,1.0,0.0);
const Vec3f Z_AXIS(0.0,0.0,1.0);

10、反锯齿

osg::DisplaySettings::instance()->setNumMultiSamples(4);

11、PageLOD最大节点数量(默认是300)

viewer->getDatabasePager()->setTargetMaximumNumbeOfPageLOD(100);

12、获取摄像机(视点)在世界坐标中的位置

osg::Vec3 vPosEye, vCenter, vUp;
camera->getViewMatrixAsLookAt( vPosEye, vCenter, vUp);

osg::ref_ptr<osg::Camera> cameraMaster = viewer->getCamera();         
osg::Matrix _inverseMV;
_inverseMV.invert( cameraMaster->getViewMatrix());
osg::Vec3 ptEye= osg::Vec3(  0, 0, 0) * _inverseMV;
/*获取世界坐标系下的视点坐标:世界坐标系中某点Pworld在视点坐标系中为Pview,则Pview= Pworld * MV。则Pworld=Pview * MV逆,则视点坐标系下的视点(0,0,0)在世界坐标系下为:ptEye=(0,0,0)* MV逆。

13、(x,y,z)<-->(lon, lat, elevation)

 

mapNode->getMapSRS()->getEllipsoid()->convertLatLongHeightToXYZ( osg::DegreesToRadians( lat ), osg::DegreesToRadians( lon), alt, x, y, z);

//To Map coordinates
GeoPoint map;
map.fromWorld( mapNode->getMapSRS(), x, y, z );//map.xyz is now lon, lat, alt 

//To world coordinates
GeoPoint map( mapNode->getMapSRS(), lon, lat, alt, ALTMODE_ABSOLUTE);
osg::Vec3d world;
map.toWorld( world );


14、使用osgEarth::ModelLayer添加模型

osgEarth::Drivers::SimpleModelOptions opt;
opt.url()="c:/glider.osg";
opt.location()=osg::Vec3d(lon, lat, elevation);//lon和lat单位是度,elevation单位是米,例如(112, 36, 1000)
map->addModelLayer(new osgEarth::ModelLayer("model", opt));

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值