OSG Windows编译的几个坑 VS2019 OSG3.6.4 Qt5.15.2 CMake3.20
资源下载 VS2019已编译版本下载
使用在QtCreator上 qmake
1、打开源码的Cmakelist.txt ,加入下面的代码,Qt路径替换为自己的
set(DESIRED_QT_VERSION "5" CACHE STRING "")
set(CMAKE_PREFIX_PATH "D:/Qt/5.14.2/msvc2017_64" CACHE PATH "")
2、官网的第三方插件包写了MSVC2017的,也可以用在MSVC2019上,最后工程使用的时候也要把bin文件夹中的文件拷贝到运行目录,不分release/debug
3、打开CMAKE并configure后务必关注第三方插件库是否找到,几个重要的比如freetype,没有的话编译出来也不能画字,cmake一页红的话检查下路径
4、最好分开编译,不要使用批生成,我的习惯是第一次CMAKE中 CMAKE_BUILD_TYPE 写 "Debug" CMAKE_DEBUG_POSTFIX 写个"d",第二次改"Release" 把"d"去掉
5、用管理员权限打开VS2019,先点一次编译,再点一次install,拷贝到默认路径,C:\Program Files\OpenSceneGraph 每次大概一个多小时
6、注意这是纯OSG的编译,如果要作为Qt窗体显示,需要另一个东西 osgQt
7、注意整个过程中不要使用360,否则可能会出现 GetThreadContext failed 的编译报错 就可以重装系统了
下面是qmake的pro文件
QT += core
CONFIG += c++11
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Osg_RootDir = $$PWD/OpenSceneGraph
Osg_3rdParty = $${Osg_RootDir}/3rdParty
INCLUDEPATH += $${Osg_3rdParty}/include
INCLUDEPATH += $${Osg_RootDir}/3.6.4/include
OsgLibPath = $${Osg_RootDir}/3.6.4/lib
CONFIG(debug, debug|release){
LIBS += $${OsgLibPath}/debug/osgViewerd.lib
LIBS += $${OsgLibPath}/debug/osgDBd.lib
LIBS += $${OsgLibPath}/debug/OpenThreadsd.lib
LIBS += $${OsgLibPath}/debug/osgd.lib
LIBS += $${OsgLibPath}/debug/osgManipulatord.lib
LIBS += $${OsgLibPath}/debug/osgTextd.lib
LIBS += $${OsgLibPath}/debug/osgGAd.lib
LIBS += $${OsgLibPath}/debug/osgUtild.lib
LIBS += $${OsgLibPath}/debug/osgUId.lib
}
else{
LIBS += $${OsgLibPath}/release/osgViewer.lib
LIBS += $${OsgLibPath}/release/osgDB.lib
LIBS += $${OsgLibPath}/release/OpenThreads.lib
LIBS += $${OsgLibPath}/release/osg.lib
LIBS += $${OsgLibPath}/release/osgManipulator.lib
LIBS += $${OsgLibPath}/release/osgText.lib
LIBS += $${OsgLibPath}/release/osgGA.lib
LIBS += $${OsgLibPath}/release/osgUtil.lib
LIBS += $${OsgLibPath}/release/osgUI.lib
}
抄的测试代码,只有一个main文件
#include <osg/Geode>
#include <osg/Geometry>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <locale.h>
#include <osgDB/ReadFile>
using namespace osg;
using namespace osgViewer;
using namespace osgDB;
void setupProperties(osgText::Text& textObject,osgText::Font* font,float size,const osg::Vec3& pos )
{
textObject.setFont(font);//
textObject.setCharacterSize(size);//字体大小
textObject.setPosition(pos);
textObject.setColor(osg::Vec4(1.0,1.0,1.0,1.0));
textObject.setAlignment(osgText::Text::CENTER_BOTTOM);//文字显示方向
//textObject.setAxisAlignment(osgText::Text::SCREEN);//获取文字对称成方式正对屏幕方向
//textObject.setCharacterSizeMode(osgText::Text::SCREEN_COORDS);//跟随视角不断变化,离物体越远,文字越大
textObject.setAutoRotateToScreen(true);//跟随视角不断变化,但离物体越远,文字越小,和现实当中像类似
textObject.setBackdropType(osgText::Text::OUTLINE);//对文字进行描边
textObject.setBackdropColor(osg::Vec4(1.0,0.0,0.0,1.0));//描边颜色
textObject.setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX);//添加文字边框
textObject.setAxisAlignment(osgText::Text::XZ_PLANE);//获取文字对称成方式
}
void createContent(osgText::Text& textObject,const char* string)
{
int requiredSize=mbstowcs(NULL,string,0);//如果mbstowcs第一参数为NULL那么返回字符串的数目
wchar_t* wText=new wchar_t[requiredSize+1];
mbstowcs(wText,string,requiredSize+1);//由char转换成wchar类型
textObject.setText(wText);
delete wText;
}
int main()
{
setlocale(LC_ALL,".936");// 配置地域化信息
const char* titleString="hsj";
const char* textString="666666";
osgText::Font* fontHei=osgText::readFontFile("C:\\WINDOWS\\Fonts\\simhei.ttf");
osgText::Font* fontKai=osgText::readFontFile("C:\\WINDOWS\\Fonts\\simkai.ttf");
osg::ref_ptr<osgText::Text> title=new osgText::Text;
setupProperties(*title,fontHei,20.0f,osg::Vec3(0.0,0.0,0.0));
createContent(*title,titleString);
osg::ref_ptr<osgText::Text> text=new osgText::Text;
setupProperties(*text,fontKai,15.0,osg::Vec3(0.0,0.0,-80.0f));
createContent(*text,textString);
osg::ref_ptr<osg::Geode> geode=new osg::Geode;
geode->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(-150.0,1.0,-130.0),osg::Vec3(300.0,0.0,0.0),osg::Vec3(0.0,0.0,200.0),1.0,1.0));//创建一个写字板
geode->addDrawable(title.get());
geode->addDrawable(text.get());
osgViewer::Viewer viewer;
viewer.setSceneData(geode.get());
return viewer.run();
}
绘制: