OsgEarth開發筆記(三):Osg3.6.3+OsgEarth3.1+vs2019x64開發環境搭建(下)

若該文爲原創文章,轉載請註明原文出處 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/113541346

長期持續帶來更多項目與技術分享,諮詢請加QQ:21497936、微信:yangsir198808

紅胖子(紅模仿)的博文大全:開發技術集合(包含Qt實用技術、樹莓派、三維、OpenCV、OpenGL、ffmpeg、OSG、單片機、軟硬結合等等)持續更新中…(點擊傳送門)

OSG與OsgEarth三維開發專欄

上一篇:《OsgEarth開發筆記(二):Osg3.6.3+OsgEarth3.1+vs2019x64開發環境搭建(中)》 下一篇:敬請期待...

<br>

前言

  上一篇編譯了proj6.2.0、gdal3.2.1,本篇繼續。

<br>

OsgEarth編譯過程簡介

  OsgEarth的編譯,是基於Osg和OsgEarth結合在一起的,先要編譯Osg,然後編譯OsgEarth。OsgEarth的依賴庫較多,分爲上、中、下三篇,然後單獨有一篇如何將編譯好的osgEarth集成到Qt中。

<br>

目標:Qt5.15.x + VS2019 x64版本

<br>

演示Demo

  Demo基於Qt5.15.2 + vs2019 x64 + osg3.6.3 + osgEarth3.1。   在這裏插入圖片描述   演示環境Demo下載地址:https://download.csdn.net/download/qq21497936/14984791

<br>

編譯OsgEarth 3.1

步驟一:下載解壓

   在這裏插入圖片描述   (備註:博主QQ羣提供文件下載,博客首頁有掃碼加羣)

步驟二:CMake配置,添加Curl

  在這裏插入圖片描述

補充:沒有添加Curl,則會報錯如下:

  在這裏插入圖片描述

步驟三:CMake配置,添加DGAL

   在這裏插入圖片描述

補充:沒有添加GDAL,則會報錯如下:

  在這裏插入圖片描述

步驟四:CMake配置,添加libZip

  在這裏插入圖片描述

補充:沒有添加libzip,則會報錯如下:

  在這裏插入圖片描述

步驟五:CMake配置,添加OSG

  在這裏插入圖片描述

步驟六:CMake配置

  在這裏插入圖片描述

步驟七:CMake生成工程

  在這裏插入圖片描述

步驟八:打開工程編譯

  在這裏插入圖片描述

步驟九:編譯錯誤“未定義GL_DYNAMIC_STORAGE_BIT”

  在這裏插入圖片描述   直接自己加個定義把,先找到他的定義:   在這裏插入圖片描述   直接修改源碼:   在這裏插入圖片描述

步驟十:編譯成功

  在這裏插入圖片描述    在這裏插入圖片描述

<br>

Demo源碼

#include <osg/Notify>
#include <osgGA/StateSetManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/WriteFile>

#include <osgEarth/MapNode>
#include <osgEarth/GDAL>
#include <osgEarth/ExampleResources>
#include <osgEarth/EarthManipulator>

#include <osgEarth/Style>
#include <osgEarth/OGRFeatureSource>
#include <osgEarth/FeatureModelLayer>
#include <osgEarth/FeatureImageLayer>

using namespace osgEarth;
using namespace osgEarth::Util;

const char* styles_css =
R"(
    p {
        altitude-clamping: terrain-drape;
        render-backface-culling: false;
    }
    p1: p{ fill: #ff3f3f9f; }
    p2: p{ fill: #3fff3f9f; }
    p3: p{ fill: #3f3fff9f; }
    p4: p{ fill: #ff3fff9f; }
    p5: p{ fill: #ffff3f9f; }
)";

const char* script_source =
R"(
    function getStyleClass()
    {
        // Exclude any countries beginning with the letter A: 
        if ( feature.properties.name.charAt(0) === 'A' )
            return null;
                        
        // If it starts with the letter C, return an inline style:
        if ( feature.properties.name.charAt(0) == 'C' )
            return '{ _fill: #ffc838; stroke: #8f8838; extrusion-height: 250000; }';
                        
        // Otherwise, return a named style based on some calculations:
        var pop = parseFloat(feature.properties.pop);
        if      ( pop <= 14045470 )  return "p1";
        else if ( pop <= 43410900 )  return "p2";
        else if ( pop <= 97228750 )  return "p3";
        else if ( pop <= 258833000 ) return "p4";
        else                         return "p5";
    }
)";

int main(int argc, char** argv)
{
    osgEarth::initialize();

    osg::ArgumentParser arguments(&argc, argv);

    bool useRaster = arguments.read("--rasterize");
    bool useMem = arguments.read("--mem");
    bool useLabels = arguments.read("--labels");
    bool useDraping = arguments.read("--drape");
    bool useClamping = arguments.read("--clamp");
    bool useScript = arguments.read("--script");

    std::string outfile;
    arguments.read("--out", outfile);

    osgViewer::Viewer viewer(arguments);

    osg::ref_ptr<Map> map = new Map();

    GDALImageLayer* basemap = new GDALImageLayer();
    basemap->setURL("world.tif");
    map->addLayer(basemap);

    // Next we add a layer to provide the feature data.
    OGRFeatureSource* features = new OGRFeatureSource();
    features->setName("vector-data");

    if (useMem)
    {
        // the --mem options tells us to just make an in-memory geometry:
        Ring* line = new Ring();
        line->push_back(osg::Vec3d(-60, 20, 0));
        line->push_back(osg::Vec3d(-120, 20, 0));
        line->push_back(osg::Vec3d(-120, 60, 0));
        line->push_back(osg::Vec3d(-60, 60, 0));
        features->setGeometry(line);
    }
    else
    {
        features->setURL("world.shp");
    }
    map->addLayer(features);

    Style style;

    LineSymbol* ls = style.getOrCreateSymbol<LineSymbol>();
    ls->stroke()->color() = Color::Yellow;
    ls->stroke()->width() = 2.0f;
    ls->tessellationSize()->set(100, Units::KILOMETERS);

    if (useDraping)
    {
        AltitudeSymbol* alt = style.getOrCreate<AltitudeSymbol>();
        alt->clamping() = alt->CLAMP_TO_TERRAIN;
        alt->technique() = alt->TECHNIQUE_DRAPE;
    }

    else if (useClamping)
    {
        AltitudeSymbol* alt = style.getOrCreate<AltitudeSymbol>();
        alt->clamping() = alt->CLAMP_TO_TERRAIN;
        alt->technique() = alt->TECHNIQUE_GPU;

        ls->tessellationSize()->set(100, Units::KILOMETERS);

        RenderSymbol* render = style.getOrCreate<RenderSymbol>();
        render->depthOffset()->enabled() = true;
    }

    if (useRaster)
    {
        FeatureImageLayer* layer = new FeatureImageLayer();
        layer->setFeatureSource(features);
        StyleSheet* sheet = new StyleSheet();
        sheet->addStyle(style);
        layer->setStyleSheet(sheet);
        map->addLayer(layer);
    }

    else
    {
        FeatureModelLayer* layer = new FeatureModelLayer();
        layer->setFeatureSource(features);

        StyleSheet* styleSheet = new StyleSheet();

        if (useScript)
        {
            styleSheet->addStylesFromCSS(styles_css);
            styleSheet->setScript(new StyleSheet::ScriptDef(script_source));
            styleSheet->addSelector(StyleSelector("default", StringExpression("getStyleClass()")));
        }
        else
        {
            styleSheet->addStyle(style);
        }

        layer->setStyleSheet(styleSheet);
        map->addLayer(layer);
    }

    if (useLabels && !useRaster)
    {
        Style labelStyle;

        TextSymbol* text = labelStyle.getOrCreateSymbol<TextSymbol>();
        text->content() = StringExpression("[name]");
        text->priority() = NumericExpression("[pop]");
        text->size() = 16.0f;
        text->alignment() = TextSymbol::ALIGN_CENTER_CENTER;
        text->fill()->color() = Color::White;
        text->halo()->color() = Color::DarkGray;

        StyleSheet* sheet = new StyleSheet();
        sheet->addStyle(labelStyle);

        FeatureModelLayer* fml = new FeatureModelLayer();
        fml->setName("Labels");
        fml->setFeatureSource(features);
        fml->setStyleSheet(sheet);
        map->addLayer(fml);
    }

    LayerVector layers;
    map->getLayers(layers);
    for (LayerVector::const_iterator i = layers.begin(); i != layers.end(); ++i)
    {
        Layer* layer = i->get();
        if (layer->getStatus().isError() &&
            layer->getEnabled())
        {
            OE_WARN << layer->getName() << " : " << layer->getStatus().toString() << std::endl;
        }
    }

    MapNode* mapNode = new MapNode(map.get());

    if (!outfile.empty())
    {
        OE_NOTICE << "Writing to " << outfile << std::endl;
        osgDB::writeNodeFile(*mapNode, outfile);
    }
    else
    {
        viewer.setSceneData(mapNode);
        viewer.setCameraManipulator(new EarthManipulator());

        MapNodeHelper().configureView(&viewer);

        return viewer.run();
    }
}

<br>

上一篇:《OsgEarth開發筆記(二):Osg3.6.3+OsgEarth3.1+vs2019x64開發環境搭建(中)》 下一篇:敬請期待...

<br>

若該文爲原創文章,轉載請註明原文出處 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/113541346

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章