osg gdal加載tif數據文件


osg加載.tif地形數據文件

#ifdef _WIN32
#include <Windows.h>
#endif // _WIN32
#include <iostream>
//#include <math.h>

#include <osg/Node>
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/BoundingSphere>
#include <osg/LineWidth>
#include <osg/Point>
#include <osg/TexGen>
#include <osg/TexEnv>

//#include <osg/TessellationHints>
//#include <osg/NodePath>
#include <osgGA/GUIEventHandler>
#include <osgGA/GUIEventAdapter>

#include <osg/PositionAttitudeTransform>
#include <osgViewer/ViewerEventHandlers>
#include <osg/MatrixTransform>
#include <OpenThreads/Thread>
#include <osg/LightSource>
#include <osg/Light>

#include <gdal.h>

osg::ref_ptr<osg::Node> CreateNode()
{
    osg::ref_ptr<osg::Group> _root = new osg::Group;

    //定義並讀取高程文件
    //真實高程文件名稱為:ASTGTM2_N34E110_dem.tif
    //屬於特殊的tiff格式,GEOTiff
    //讀取的時候使用osg的gdal插件進行讀取,所以在路徑后面加上了.gdal
    //.gdal后綴名只要在這里加就可以了,真實的高程文件后綴名不需要修改
    //osg::ref_ptr<osg::HeightField> heightMap = osgDB::readHeightFieldFile("E:\\OpenSourceGraph\\osgearth_install20190830\\data\\world.tif.gdal");
    osg::ref_ptr<osg::HeightField> heightMap = osgDB::readHeightFieldFile("E:\\OpenSourceGraph\\osgearth_install20190830\\data\\1\\1_0_0_5.tif.gdal");


    //創建一個葉結點對象
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;

    if (heightMap != nullptr)
    {
        //由於原始數據過大,創建三維對象會失敗,所以重新構造一個對象
        //相當於數據抽稀了一次。當然,可以直接把原圖使用特殊工具裁了
        //創建一個新的HeightField對象,用來拷貝heightMap
        osg::ref_ptr<osg::HeightField> heightMap1 = new osg::HeightField;
        //從原對象中拷貝一些熟悉過來
        heightMap1->setOrigin(heightMap->getOrigin());
        heightMap1->setRotation(heightMap->getRotation());
        heightMap1->setSkirtHeight(heightMap->getSkirtHeight());
        //XY方向的間隔設置為原來的兩倍,
        heightMap1->setXInterval(heightMap->getXInterval() * 2);
        heightMap1->setYInterval(heightMap->getYInterval() * 2);
        //設置新的高程數據量的行列數目為原來的一半
        heightMap1->allocate(heightMap->getNumColumns() / 2, heightMap->getNumRows() / 2);

        //把真實的數據值放進來
        for (size_t r = 0; r < heightMap1->getNumRows(); ++r)
        {
            for (size_t c = 0; c < heightMap1->getNumColumns(); ++c)
            {
                //加載的數據中XY方向的間隔是0.0002左右(經緯度偏移),3600個格子,數量級太小,高程值動輒在千級別,如果沒有進行坐標轉換(GPS轉換成米),顯示出來之后結果會嚴重失常。所以此處簡單的給高度值除以50000(這個是按照這個tif文件來試出來的,不同高程文件可能不同)
                heightMap1->setHeight(c, r, heightMap->getHeight(c * 2, r * 2) / 500);
            }
        }

        //添加到葉子節點中
        geode->addDrawable(new osg::ShapeDrawable(heightMap1));


        osg::ref_ptr<osg::Material> material = new osg::Material;
        material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
        material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
        material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
        material->setShininess(osg::Material::FRONT_AND_BACK, 60);


        osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D;
        //設置紋理
        osg::ref_ptr<osg::Image> image1 = osgDB::readImageFile("D:\\image_1\\arm1.jpg");
        if (image1.valid())
        {
            texture2D->setImage(image1.get());
        }
        geode->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON);
        geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D, osg::StateAttribute::ON);

    }

    _root->addChild(geode.get());
    return _root.get();
}

osg::ref_ptr<osg::Light> createLight()
{
    osg::ref_ptr<osg::Light> l = new osg::Light;
    l->setLightNum(0);//啟用第幾個光源  OpenGL有8個光源
    l->setDirection(osg::Vec3(0, 0, -1));//方向
    l->setPosition(osg::Vec4(10.0, 10.0, 0.0, 0.0f));//位置
    //osg::LightSource* ls = new osg::LightSource();//此處用超級指針 返回會發生錯誤
    //ls->setLight(l);
    return l;
}



int main()
{
    osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group> group1 = new osg::Group;

    group1->addChild(CreateNode());
    viewer1->setSceneData(group1.get());
    viewer1->setUpViewInWindow(200, 200, 800, 600, 0);
    viewer1->setLight(createLight());


    return viewer1->run();
}

 


免責聲明!

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



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