OSG Study Notes-Group(2-3-7)-CoordinateSystemNode

2.3.17 Coordinate System Node

The coordinate system node (osg::CoordinateSystemNode) inherits from the osg::Group node, and its main function is to associate a scene object with a coordinate system. Common coordinate system types include WKT, PROJ4, and USGS, which are usually used together with the osg::EllipsoidModel node. The osg::EllipsoidModel node (ellipsoid model node) is mainly used to simulate celestial bodies, such as the sun, planets, and moon. The default The case is the globe. The osg::EllipsoidModel node also has the function of realizing the conversion between latitude and longitude and coordinates, so that the precise positioning of the child nodes of the celestial ellipsoid model can be realized. When building the earth database, this node will be very useful, and the precise positioning of the child nodes can be realized according to the earth ellipsoid model.

Many people may not understand the WKT coordinate system. Its definition includes the following aspects:

  • A global coordinate system name
  • the name of a geographic coordinate system
  • a data identifier
  • The name, semi-major axis, and flattening of an ellipsoid.
  • The name of the prime meridian and its offset from Greenwich
  • Projection type (e.g. Transverse Mercator)
  • Projection parameters (such as central meridian)
  • Unit name, conversion factor to meters or radians
  • Axis names and order
  • Codes for most coordinate systems predefined by authorized organizations

2.3.18 Coordinate system node sample code

#include<osgViewer/Viewer>
#include<osg/Node>
#include<osg/Geode>
#include<osg/Group>
#include<osg/CoordinateSystemNode>
#include<osg/ShapeDrawable>
#include<osgDB/ReadFile>
#include<osgDB/WriteFile>
#include<osgUtil/Optimizer>
//--------------------------------------------------
// 绘制一个地球
osg::ref_ptr<osg::Node> createEarth()
{
    // 创建一个地球
    osg::ref_ptr<osg::ShapeDrawable> sd = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0,0.0,0.0),osg::WGS_84_RADIUS_POLAR));
    
    // 添加到叶节点
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    geode->addDrawable(sd.get());
    
    // 设置纹理
    std::string filename("Image/land_shallow_topo_2048.jpg");
    geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,new osg::Texture2D(osgDB::readImageFile(filename)));
    
    // 创建坐标系节点
    osg::ref_ptr<osg::CoordinateSystemNode> csn = new osg::CoordinateSystemNode;
    
    // 设置椭圆体模型,默认的坐标系WGS-84
    csn->setEllipsoidModel(new osg::EllipsoidModel());
    csn->addChild(geode.get());
    
    return csn.get();
}
//-----------------------------------------------------
int main()
{
    // 创建Viewer对象,场景浏览器
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
    osg::ref_ptr<osg::Group>root = new osg::Group();
    
    // 添加到场景
    root->addChild(createEarth());
    
    // 优化场景数据
    osgUtil::Optimizer = optimizer;
    optimizer.optimize(root.get());
    
    viewer->setSceneData(root.get());
    viewer->realize();
    viewer->run();
    
    return 0;
}

Guess you like

Origin blog.csdn.net/liangfei868/article/details/123632690