若该文为原创文章,转载请注明原文出处
本文章博客地址: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开发环境搭建(中)》
下一篇:敬请期待...
前言
上一篇编译了proj6.2.0、gdal3.2.1,本篇继续。
OsgEarth编译过程简介
OsgEarth的编译,是基于Osg和OsgEarth结合在一起的,先要编译Osg,然后编译OsgEarth。OsgEarth的依赖库较多,分为上、中、下三篇,然后单独有一篇如何将编译好的osgEarth集成到Qt中。
目标:Qt5.15.x + VS2019 x64版本
演示Demo
Demo基于Qt5.15.2 + vs2019 x64 + osg3.6.3 + osgEarth3.1。
演示环境Demo下载地址:https://download.csdn.net/download/qq21497936/14984791
编译OsgEarth 3.1
步骤一:下载解压
(备注:博主QQ群提供文件下载,博客首页有扫码加群)
步骤二:CMake配置,添加Curl
补充:没有添加Curl,则会报错如下:
步骤三:CMake配置,添加DGAL
补充:没有添加GDAL,则会报错如下:
步骤四:CMake配置,添加libZip
补充:没有添加libzip,则会报错如下:
步骤五:CMake配置,添加OSG
步骤六:CMake配置
步骤七:CMake生成工程
步骤八:打开工程编译
步骤九:编译错误“未定义GL_DYNAMIC_STORAGE_BIT”
直接自己加个定义把,先找到他的定义:
直接修改源码:
步骤十:编译成功
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();
}
}
上一篇:《OsgEarth开发笔记(二):Osg3.6.3+OsgEarth3.1+vs2019x64开发环境搭建(中)》
下一篇:敬请期待...
若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/113541346
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。