OSG和osgQt编译教程,实测通过
一、下载OpenSceneGraph
OpenSceneGraphhttps://github.com/openscenegraph/OpenSceneGraph
二、使用CMAKE编译OpenSceneGraph
1.打开cmake,配置源代码目录
2. CMAKE_INSTALL_PREFIX设置为install文件夹,生成VS工程
3. 使用VS2022打开工程,编译ALL_BUILD和INSTALL,在OpenSceneGraph-master\install目录下生成头文件、lib文件和动态库
三、下载osgQt
osgQthttps://github.com/openscenegraph/osgQt
四、使用CMAKE编译osgQt
1.修改osgQt-master/CMakeLists.txt,配置OSG和QT依赖(重点,不修改cmake会报错)
找到以下的行,在FIND_PACKAGE(OpenSceneGraph...上面添加一行,set(CMAKE_PREFIX_PATH "path1;paht2"),注意path1和ath2使用分号分隔,path1为上面的OSG生成目录,即OpenSceneGraph-master/install,path2为QT的安装目录,本案例为Qt/5.15.2/msvc2019_64。版本号3.7.0为OSG的版本号
如下:
PROJECT(osgQt)
set(CMAKE_PREFIX_PATH "C:/Users/115627/Desktop/OpenSceneGraph-master/OpenSceneGraph-master/install;C:/Qt/5.15.2/msvc2019_64")
FIND_PACKAGE(OpenSceneGraph 3.7.0 REQUIRED osgDB osgGA osgUtil osgText osgViewer osgWidget)
SET(OPENSCENEGRAPH_SOVERSION 145)
SET(OSG_PLUGINS osgPlugins-${OPENSCENEGRAPH_VERSION})
SET(OSG_PLUGIN_PREFIX "")
IF (CYGWIN)
SET(OSG_PLUGIN_PREFIX "cygwin_")
ENDIF()
IF(MINGW)
SET(OSG_PLUGIN_PREFIX "mingw_")
ENDIF()
2..打开cmake,配置源代码目录
3. CMAKE_INSTALL_PREFIX设置为install文件夹,生成VS工程
4. 使用VS2022打开工程,编译ALL_BUILD和INSTALL,在install目录下生成头文件、lib文件和动态库
五、配置环境变量
将OSG和osgQt的动态库加入到系统环境变量,方便后续测试测序找到
六、Qt 测试程序
启动qt creator,新建测试项目,配置头文件路径, lib路径即lib文件,pro配置如下:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17 opengl
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH += C:/Users/115627/Desktop/OpenSceneGraph-master/OpenSceneGraph-master/install/include \
C:/Users/115627/Desktop/OpenSceneGraph-master/osgQt-master/install/include
CONFIG(debug,debug|release){
LIBS += -LC:\Users\115627\Desktop\OpenSceneGraph-master\OpenSceneGraph-master\install\lib \
-LC:\Users\115627\Desktop\OpenSceneGraph-master\osgQt-master\install\lib \
-lOpenThreadsd \
-losgd \
-losgDBd \
-losgGAd \
-losgUtild \
-losgViewerd \
-losgQOpenGLd
}else {
LIBS += -LC:\Users\115627\Desktop\OpenSceneGraph-master\OpenSceneGraph-master\install\lib \
-LC:\Users\115627\Desktop\OpenSceneGraph-master\osgQt-master\install\lib \
-lOpenThreads \
-losg \
-losgDB \
-losgGA \
-losgUtil \
-losgViewer \
-losgQOpenGL
}
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <osgQOpenGL/osgQOpenGLWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected slots:
void initOSG();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
osgQOpenGLWidget* osgWidget;
};
#endif // WIDGET_H
CPP文件
#include "widget.h"
#include "ui_widget.h"
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osg/MatrixTransform>
#include <osg/ShapeDrawable>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
osgWidget = new osgQOpenGLWidget(ui->widget); // 指定osg窗口显示位置
osgWidget->setGeometry(ui->widget->geometry()); // 指定osg窗口显示大小
// connect(osgWidget, SIGNAL(initialized()), this, SLOT(initOSG()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::initOSG()
{
osgViewer::Viewer* pViewer = osgWidget->getOsgViewer();
pViewer->setCameraManipulator(new osgGA::TrackballManipulator);
auto root = new osg::Group;
auto transform = new osg::MatrixTransform;
osg::Cylinder* cylinder = new osg::Cylinder( osg::Vec3( 0.f, 0.f, 0.f ), 1.f, 1.f );
osg::ShapeDrawable* sd = new osg::ShapeDrawable( cylinder );
sd->setColor( osg::Vec4( 0.8f, 0.5f, 0.2f, 1.f ) );
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(sd);
pViewer->setSceneData(geode);
}
void Widget::on_pushButton_clicked()
{
initOSG();
}
运行结果