3D进阶之OSG:从VS2019编译osgEarth开始

本文详细介绍了在VS2019环境下编译OpenSceneGraph (OSG) 的过程,重点讨论了遇到的protobuf相关问题及解决方法,包括编译OSG Earth时的依赖安装、CMake配置和编译步骤,以及protobuf头文件的生成与链接。
摘要由CSDN通过智能技术生成

3D库比较多,一般学习的有PCL的VTK,库比较大,集成了相当多不错的算法,对开发水平是个考验;OGRE由于其插件式架构,使得在机器人开发方面应用广泛;相对而言,OSG比较小众,可以用来构建模拟仿真的的场景图。

当然,不论你学习哪个库,道理都是一样的,就是要去熟悉那些API,快速上手应用。

OSG的编译在这里,

3D进阶之OSG: VS2019编译OpenSceneGraph_高精度计算机视觉的博客-CSDN博客首先,这里已经有人给出了个省事的解决方案,我给个链接,OSG3.6.5+VS2019:免编译,在VS2019中新建OSG项目运行OSG_乔木cc的博客-CSDN博客_vs2019配置osg本博客资源链接_杨石兴的博客-CSDN博客链接:https://pan.baidu.com/s/101IXFgvKQhQOEbfLa-ztZg提取码:osgb到这个链接里,找到vs2019的编译库和第三方依赖,就可以开始工作了。然后,我想说的是, 如果你想编译这些第三方依赖库,和OSG,其实也不难,.https://blog.csdn.net/tanmx219/article/details/123943131你可以选择自己编译,也可以vcpkg安装编译,也有已经编译好的二进制可执行文件。

言归正传,下面我们开始OSG的开发之旅。

什么是osgEarth

这玩意就是把osg的很多功能做了一次封装。

安装 osgEarth

使用vcpkg的话,只需要一句指令,

vcpkg install osgearth:x64-windows

官方参考地址,

cInstalling osgEarth — osgEarth 3.1 documentation

编译osgEarth

作为开发者,我总想自己编译看看问题所在。官方地址是,

Building osgEarth — osgEarth 3.1 documentation

开发默认是vs2017,使用包管理工具vcpkg。关于如何使用vcpkg+vs2019可以参考我前面的博文,VS2017类似,

Tools_vcpkg包管理工具在VS2019项目开发中的使用_高精度计算机视觉的博客-CSDN博客

虽然本人的最终项目计划用qtcreator来实现;不过,在学习的过程中,VS还是必须的。下面我用VS2019实现编译。编译过程这段摘自官网,我做了一些注释,

(方法一)按照官网的办法

Building with vcpkg

vcpkg is an extremely useful package manager. It works on Windows, Linux and MacOS but for this guide we will focus on Windows.

Step 1 - Configure vcpkg

First, download and bootstrap vcpkg following the instructions on the page.

Next install the dependencies required to build a fully functional osgEarth. This example assume s 64-bit Windows build; you can alter that to correspond to your platform/architecture of choice.

Install the required dependencies:

vcpkg install osg:x64-windows gdal:x64-windows curl:x64-windows

For full functionality, you can install optional dependences as well:

vcpkg install sqlite3:x64-windows protobuf:x64-windows geos:x64-windows blend2d:x64-windows libwebp:x64-windows basisu:x64-windows draco:x64-windows libzip:x64-windows

This will take awhile the first time you run it as this pulls down lots of dependencies, so go get a cup of coffee.

Once all the dependencies are built, you’ll need to actually build osgEarth.

说明:

在这一步里,我实际只安装osg和gdal这两个必要的包,curl这样的工具在系统里已经安装 了,所以我直接跳过。

另外一点就是,如果后面发现需要安装其他包的时候,回过来再安装也不迟。

嗯,这杯咖啡耗时比较长。

Step 2 - Clone the repository

Pull down the source from GitHub and create a build folder for your out-of-source build. We always recommend doing an out-of-source build to avoid problems down the road!

git clone --recurse-submodules https://github.com/gwaldron/osgearth.git osgearth
mkdir build

This will clone the repository into a folder called osgearth and pull down all the submodules.

说明

科学上网,git直接下载就好了。

Step 3 - Configure CMake

vcpkg provides a CMake toolchain file that helps osgEarth find all of its dependencies.

Note: You’ll need to specify a different build directory based on your build configuration (Release, RelWIthDebInfo, Debug) and specify the build type using -DCMAKE_BUILD_TYPE. This is because some dependencies of osgEarth don’t pick up both debug and release versions without specifying the build type. Hopefully this will be fixed in future CMake versions.

Most developers will use a RelWithDebInfo build, like so:

cmake -S osgearth -B build -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DWIN32_USE_MP=ON -DCMAKE_INSTALL_PREFIX=[installroot] -DCMAKE_TOOLCHAIN_FILE=[vcpkgroot]\scripts\buildsystems\vcpkg.cmake

说明

这里面有些参数要改成自己的,例如,我用的VS2019,

cmake -S osgearth -B build -G "Visual Studio 16 2019 Win64" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DWIN32_USE_MP=ON -DCMAKE_INSTALL_PREFIX=[installroot] -DCMAKE_TOOLCHAIN_FILE=D:\vs3D\vcpkg\scripts\buildsystems\vcpkg.cmake

Step 4 - Build and install osgEarth

You can build and install osgEarth on the command line using CMake or you can open up the Visual Studio solution and build it from there.

cmake --build build --target INSTALL --config RelWithDebInfo

说明

这里会生成的项目,一般会有报错,可以用VS2019打开进行编译与调试。

Step 5 - Set up your runtime environment

You’ll need to make sure that the vcpkg dependencies and osgEarth are in your path:

set PATH=%PATH%;c:\vcpkg\installed\x64-windows\bin
set PATH=%PATH%;c:\vcpkg\installed\x64-windows\tools\osg
set PATH=%PATH%;[installroot]

碰到的问题

报错如下所示

1>------ 已启动生成: 项目: osgEarth, 配置: RelWithDebInfo x64 ------
1>MapboxGLImageLayer.cpp
1>MapboxGLGlyphManager.cpp
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLGlyphManager.cpp(25,10): fatal error C1083: 无法打开包括文件: “glyphs.pb.h”: No such file or directory
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(258,27): error C2065: “MVTFeatureSource”: 未声明的标识符
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(258,18): error C2923: “osg::ref_ptr”: 对于参数“T”,“MVTFeatureSource”不是有效的 模板 类型变量
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(258): message : 参见“MVTFeatureSource”的声明
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(258,66): error C2061: 语法错误: 标识符“MVTFeatureSource”
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(258,60): error C2512: “osg::ref_ptr”: 没有合适的默认构造函数可用
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(32,1): message : 参见“osg::ref_ptr”的声明 (编译源文件 D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp)
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(259,26): error C2678: 二进制“->”: 没有找到接受“osg::ref_ptr”类型的左操作数的运算符(或没有可接受的转换)
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(106,12): message : 可能是“T *osg::ref_ptr::operator ->(void) const” (编译源文件 D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp)
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(259,26): message : 尝试匹配参数列表“(osg::ref_ptr)”时
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(259,28): error C2039: "setReadOptions": 不是 "osg::ref_ptr" 的成员
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(32): message : 参见“osg::ref_ptr”的声明
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(260,26): error C2678: 二进制“->”: 没有找到接受“osg::ref_ptr”类型的左操作数的运算符(或没有可接受的转换)
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(106,12): message : 可能是“T *osg::ref_ptr::operator ->(void) const” (编译源文件 D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp)
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(260,26): message : 尝试匹配参数列表“(osg::ref_ptr)”时
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(260,28): error C2039: "setURL": 不是 "osg::ref_ptr" 的成员
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(32): message : 参见“osg::ref_ptr”的声明
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(261,26): error C2678: 二进制“->”: 没有找到接受“osg::ref_ptr”类型的左操作数的运算符(或没有可接受的转换)
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(106,12): message : 可能是“T *osg::ref_ptr::operator ->(void) const” (编译源文件 D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp)
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(261,26): message : 尝试匹配参数列表“(osg::ref_ptr)”时
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(261,28): error C2039: "open": 不是 "osg::ref_ptr" 的成员
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(32): message : 参见“osg::ref_ptr”的声明
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(262,48): error C2662: “T *osg::ref_ptr::get(void) const”: 不能将“this”指针从“osg::ref_ptr”转换为“const osg::ref_ptr &”
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(262,48): message : 原因如下: 无法从“osg::ref_ptr”转换为“const osg::ref_ptr”
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(262,30): message : 转换要求第二个用户定义的转换运算符或构造函数
1>D:\vs3D\vcpkg\installed\x64-windows\include\osg/ref_ptr(117,12): message : 参见“osg::ref_ptr::get”的声明 (编译源文件 D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp)
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(1274,48): warning C4018: “>=”: 有符号/无符号不匹配
1>D:\osgprj\osgearth\src\osgEarth\MapboxGLImageLayer.cpp(1274,73): warning C4018: “>=”: 有符号/无符号不匹配
1>已完成生成项目“osgEarth.vcxproj”的操作 - 失败。

我们一个个来,

(1)fatal error C1083: 无法打开包括文件: “glyphs.pb.h”: No such file or directory

这个一看就知道是pb.h这种需要protobuf生成的文件,我搜索了一下,找到了2个proto文件,为了避免找不到生成文件,单独搞了文件夹,

D:\osgprj\temp\glyphs.proto
D:\osgprj\temp\vector_tile.proto

然后找到protoc.exe运行下面的脚本,我用的protobuf3.7,自己在电脑上编译的,当然也可以直接到官网上去下载,用起来都一样,

下载地址在这里,

https://github.com/protocolbuffers/protobuf/releases?page=7

命令使用的方式如下, 

protoc --proto_path=D:/osgprj/temp --cpp_out=D:/osgprj/temp D:/osgprj/temp/glyphs.proto
protoc --proto_path=D:/osgprj/temp --cpp_out=D:/osgprj/temp D:/osgprj/temp/vector_tile.proto

protobuf的版本要保持和OSG一致,比如在我下载的第三方库地址是,

E:\vOSG\3rdpartyVs2019x64\include\google\protobuf\port_def.inc

里面定义了

#define PROTOBUF_VERSION 3007000

当然,另一个办法是你随便用一个版本生成glyphs.pb.h和vector_file.pb.h之后,把里面的版本信息改成一致(显然不是一个好办法)。

最后会生成

glyphs.pb.h,  glyphs.pb.cpp,vector_tile.pb.h, vector_tile.pb.cpp

把生成文件拷贝到到项目中去,再编译就OK了。当然,要记得把libprotobuf.lib链接添加进项目里去。

(2)MapboxGLImageLayer.cpp(258,27): error C2065: “MVTFeatureSource”: 未声明的标识符

这个在项目预处理器中定义一个参数OSGEARTH_HAVE_MVT就好了。

(3)错误    LNK2001    无法解析的外部符号 "class google::protobuf::internal::ExplicitlyConstructed<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > google::protobuf::internal::fixed_address_empty_string" (?fixed_address_empty_string@internal@protobuf@google@@3V?$ExplicitlyConstructed@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@123@A)    osgEarth    D:\osgprj\build\src\osgEarth    D:\osgprj\build\src\osgEarth\glyphs.pb.obj    1    

这个是protobuf的编译方式引起的,如果你用的static静态库, 一般就不会有这个错误;因为我用的是DLL动态库,所以在生成的.pb.h头文件中需要加上这么一句,

#ifndef PROTOBUF_glyphs_2eproto__INCLUDED
#define PROTOBUF_glyphs_2eproto__INCLUDED

#define PROTOBUF_USE_DLLS  // 这个是新添加的

#include <string>

(方法二)cmake-gui编译

编译时用到的第三方库参考此贴

3D进阶之OSG: VS2019编译OpenSceneGraph_高精度计算机视觉的博客-CSDN博客首先,这里已经有人给出了个省事的解决方案,我给个链接,OSG3.6.5+VS2019:免编译,在VS2019中新建OSG项目运行OSG_乔木cc的博客-CSDN博客_vs2019配置osg本博客资源链接_杨石兴的博客-CSDN博客链接:https://pan.baidu.com/s/101IXFgvKQhQOEbfLa-ztZg提取码:osgb到这个链接里,找到vs2019的编译库和第三方依赖,就可以开始工作了。然后,我想说的是, 如果你想编译这些第三方依赖库,和OSG,其实也不难,.https://blog.csdn.net/tanmx219/article/details/123943131同样,配置看图,

Configure + Generate之后,打开vs2019,一路编译过来,基本没有问题。

 本文结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值