Osgearth Basic Primer

Source: Internet
Author: User
Tags addchild

Osgearth Basic Primer

March 21, 2015

16:19

Osgearth is based on three-dimensional engine OSG development of three-dimensional digital Earth engine library, in OSG based on the implementation of tile scheduling plug-ins, optional four fork tree scheduling plug-ins, more geographic data loading plug-ins (including Gdal,ogr,wms,tms,vpb,filesystem, etc.), Combined with a set of geographic projection conversion plug-in, so that the efficient processing of loading scheduling geographic data on three-dimensional Earth display, the realization of three-dimensional virtual Earth.

There are two ways to implement a simple Osgearth-based three-dimensional Earth, both of which are interoperable. An XML tag based on the earth file loading, the other is the use of C + + code, the essence is the same, Osgearth internal support directly parse XML tag file, converted to code implementation, specific reference tests folder example, code reference Application The following example. But in most cases, you need more flexibility to control more powerful code to achieve functionality, because only a three-dimensional Earth loaded with basic data can only be seen, not solve the actual problem, the need for the interface is usually QT, more three-dimensional rendering and simulation business by OSG to complete. So learning OSG is the basis of doing all this.

Osgearth Features: Support for loading common raster data (imagery and DEM), but big data must be built into pyramids, set to geographic projection, want to be efficient and best handled as tiles, so also easy to deploy on the server side. Vector data, it is best to simplify as much as possible, because the large vector will greatly affect the rendering speed, of course, can also speed up the vector raster processing, for the model, the big data must be done LOD or pagelod.

General flow of Osgearth programs:

Create Osgviewer----> Create mapnode----> Set earth operator----> Set scene parameters----->run

Mapnode is inherited from the OSG node, is the Osgearth in the Earth nodes, you add images, DEM, models are included in the Mapnode, because they are added to the map, map is similar to the two-dimensional map can add a variety of layers. The rest of the model nodes, either node or node, can be added directly to Mapnode or another group.

Earth operator is the same as other OSG operators, but specifically for the three-dimensional Earth browsing custom, specific parameters can be set.

The parameters of the scene are mainly the automatic terrain clipping, the minimum cropping pixels and other optimization scenarios.

Here is a brief description of a small example:

The code functionality primarily implements the ability to query real-time elevations and display XYZ coordinates.

Use the command App.exe Test.earth to get the following effect.

//introduction of OSG and Osgearth header files and namespaces#include <osgGA/StateSetManipulator> #include <osgGA/GUIEventHandler> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgUtil/LineSegmentIntersector> #include < osgearth/mapnode> #include <osgEarth/TerrainEngineNode> #include <osgEarth/ElevationQuery> #include <osgEarth/StringUtils> #include <osgEarth/Terrain> #include <osgearthutil/earthmanipulator># Include <osgEarthUtil/Controls> #include <osgEarthUtil/LatLongFormatter> #include <iomanip>using namespaceOsgearth;using namespaceOsgearth::util;using namespaceOsgearth::util::controls;Staticmapnode* S_mapnode = 0L;Staticlabelcontrol* S_poslabel = 0L;Staticlabelcontrol* S_vdalabel = 0L;Staticlabelcontrol* S_msllabel = 0L;Staticlabelcontrol* S_haelabel = 0L;Staticlabelcontrol* S_maplabel = 0L;Staticlabelcontrol* S_reslabel = 0L;//An event handler that would print out the elevation at the clicked Pointan event callback for the query elevation, called when the scene has an event update trigger, and a detailed reference to OSG or Osgga::guieventhandlerstructQueryelevationhandler: PublicOsgga::guieventhandler {//ConstructorsQueryelevationhandler (): _mousedown (false), _terrain (S_mapnode->getterrain ()), _query (S_mapnode->getmap ()) {_map = S_mapnode->getmap ();//Initialize maximum query Lod level_query.setmaxtilestocache (10); _path.push_back (S_mapnode->getterrainengine ());}//Update callback, the specific content can refer to the parent class, passed in the parameters are screen coordinates xy, and OsgviewervoidUpdatefloatXfloatY, osgviewer::view* View) {BOOLYes =false;//Look under the mouse://Use the line to do collision detection of the Earth, according to the mouse click Point to detect, get the intersection point, is the current points of XYZOsg::vec3d world;osgutil::linesegmentintersector::intersections hits;//Determine if the result of the intersection is emptyif(View->computeintersections (x, Y, hits)) {//Get coordinates below the world coordinate system, which is the xyz coordinate of OSGWorld = Hits.begin ()->getworldintersectpoint ();//Convert to map coords://convert it to the geo-coordinates of the Earth, the conversion method can be copiedGeoPoint Mappoint;mappoint.fromworld (_terrain->getsrs (), world);//Do an elevation query:Doublequery_resolution = 0;//1/10th of a degreeDoubleOUT_HAMSL = 0.0;Doubleout_resolution = 0.0;//Query the elevation at the current point location according to the input parameters, you need to set the resolution, that is, the query precisionBOOLOK = _query.getelevation (mappoint,out_hamsl,query_resolution, &out_resolution);//If the query succeedsif(OK) {//Convert to geodetic to get the HAE:Mappoint.z () = OUT_HAMSL; GeoPoint mappointgeodetic (S_mapnode->getmapsrs ()->getgeodeticsrs (), mapPoint);///latitude/Longitude coordinate format tool, you can also use the string to splice the XYZ numberStaticLatlongformatter S_f;//Update the displayed XYZ value, label is the control passed inS_poslabel->settext (stringify () << std::fixed<< std::setprecision (2) << S_f.format (Mappointgeodetic.y ()) << ", "<< S_f.format (mappointgeodetic.x ()));//can also output resolution, ellipsoid information, etc.S_msllabel->settext (stringify () << out_hamsl) S_haelabel->settext (stringify () << Mappointgeodetic.z ()); S_reslabel->settext (Stringify () << out_resolution); Yes =true;}//Finally, get a normal isect HAE point.GeoPoint Isectpoint;isectpoint.fromworld (_terrain->getsrs ()->getgeodeticsrs (), world);s_maplabel-> SetText (stringify () << isectpoint.alt ());}//If the elevation is not queriedif(!yes) {S_poslabel->settext ("-"); S_msllabel->settext ("-"); S_haelabel->settext ("-"); S_reslabel->settext ("-" );}}//Parameter one is the action of the event, and the other is the corresponding operationBOOLHandleConstosgga::guieventadapter& ea, osgga::guiactionadapter& aa) {//Judge if the mouse event is moved to update the current coordinate displayif(Ea.geteventtype () = = Osgga::guieventadapter::move &&aa.asview ()->getframestamp ()->getFrameNumber () % = = 0) {osgviewer::view* View =static_cast<osgViewer::View*> (Aa.asview ()); Update (Ea.getx (), Ea.gety (), View);}return false;}//map ObjectConstmap* _map;//Terrain ObjectConstterrain* _terrain;BOOL_mousedown;//Query the objects used by elevationElevationquery _query;osg::nodepath _path;};//main function,intMainintargcChar* * argv) {//Here are two parameters, the first is the number of command parameters, followed by the string array input earth file path Osg::argumentparser arguments (&ARGC,ARGV); the scene of//OSGOsgviewer::viewer Viewer (arguments);//Construction mapnode,arguments with earth file path, command line inputS_mapnode = mapnode::load (arguments);//If the path is incorrect or earth file is wrong, no mapnode is constructed.if(!s_mapnode) {Oe_warn << "Unable to load Earth file."<< Std::endl;return-1;}//Set up a group nodeosg::group* root =NewOsg::group ();//Set the group node as the scene nodeViewer.setscenedata (root);//Install the programmable manipulator.//Set Earth operatorViewer.setcameramanipulator (NewOsgearth::util::earthmanipulator ());//The Mapnode would render the Map object in the scene graph.//Add Mapnode to the group nodeRoot->addchild (S_mapnode);//The following is a set of controls, grid means to use grids to layout the small controls inside//Make the readout:grid* Grid =NewGrid ();//Set several label text controls to appear in the scene in the first rowGrid->setcontrol (0,0,NewLabelcontrol ("Coords (Lat, Long):"); Grid->setcontrol (0,1,NewLabelcontrol ("Vertical Datum:"); Grid->setcontrol (0,2,NewLabelcontrol ("Height (MSL):"); Grid->setcontrol (0,3,NewLabelcontrol ("Height (HAE):"); Grid->setcontrol (0,4,NewLabelcontrol ("Isect (HAE):"); Grid->setcontrol (0,5,NewLabelcontrol ("Resolution:"));//Set several label text controls to appear in the scene in the first rowS_poslabel = Grid->setcontrol (1, 0,NewLabelcontrol ("")); S_vdalabel = Grid->setcontrol (1, 1,NewLabelcontrol ("")); S_msllabel = Grid->setcontrol (1, 2,NewLabelcontrol ("")); S_haelabel = Grid->setcontrol (1, 3,NewLabelcontrol ("")); S_maplabel = Grid->setcontrol (1, 4,NewLabelcontrol ("")); S_reslabel = Grid->setcontrol (1, 5,NewLabelcontrol (""));//Get the spatial reference, ellipsoid information, and display the corresponding label aboveConstspatialreference* Mapsrs = S_mapnode->getmapsrs (); S_vdalabel->settext (Mapsrs->getverticaldatum ()? mapSRS- >getverticaldatum ()->getname (): Stringify () << "Geodetic ("<< mapsrs->getellipsoid ()->getname () <<")" );//Control drawing containercontrolcanvas* Canvas =NewControlcanvas ();//The controls to be displayed are added to the root group nodeRoot->addchild (canvas); Canvas->addcontrol (grid);//Add an event callback for the query elevation that you just customized//An event handler that would respond to mouse clicks:Viewer.addeventhandler (NewQueryelevationhandler ());//Add Status Display, window change and other event callback//Add some stock OSG handlers:Viewer.addeventhandler (NewOsgviewer::statshandler ()); Viewer.addeventhandler (NewOsgviewer::windowsizehandler ()); Viewer.addeventhandler (NewOsgga::statesetmanipulator (Viewer.getcamera ()->getorcreatestateset ()));//runreturnViewer.run ();}

Osgearth Basic Primer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.