OSG/osgEarth相關功能函數


轉自:http://www.cnblogs.com/coolbear/archive/2013/05/20/3088595.html

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);

或者(摘自tmljs1988的專欄)

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)

方法1: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));

 
針對第13個功能函數,osgearth2.1.1中有相應的函數
bool  mapPointToWorldPoint (const osg::Vec3d &input, osg::Vec3d &output) const
bool  worldPointToMapPoint (const osg::Vec3d &input, osg::Vec3d &output) const
 
轉自:http://updraft.github.io/osgearth-doc/html/classosgEarth_1_1Map.html#a3a7e1f28b755eb7ca5603bfbd44ada0d
 
mapPointToWorldPoint函數的繼承關系
mapPointToWorldPoint函數的內容為:
bool MapInfo::mapPointToWorldPoint(const osg::Vec3d & input,osg::Vec3d & output )const
{ if ( _isGeocentric ) { _profile->getSRS()->getEllipsoid()->convertLatLongHeightToXYZ( osg::DegreesToRadians( input.y() ), osg::DegreesToRadians( input.x() ), input.z(), output.x(), output.y(), output.z() ); } else { output = input; } return true; }

worldPointToMapPoint函數的繼承關系:
worldPointToMapPoint函數的內容為:
worldPointToMapPoint(const osg::Vec3d & input,osg::Vec3d & output )const
{
    if ( _isGeocentric )
    { 
        _profile->getSRS()->getEllipsoid()->convertXYZToLatLongHeight(
            input.x(), input.y(), input.z(),
            output.y(), output.x(), output.z() );

        output.y() = osg::RadiansToDegrees(output.y());
        output.x() = osg::RadiansToDegrees(output.x());
    }
    else { output = input; } return true; }

osgearth2.2與2.6的區別如網址:
osgEarth: headers diff between 2.2 and 2.6 versions 打開此網頁需要安裝lantern-installer-beta.exe軟件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM