最新版的osg中不附带osgQt源码,所以需要单独下载编译
在编译osgQt前需要先编译osg源码,osg编译安装看这里
编译osgQt的环境与之前编译osg源码一样
github
可使用git下载:
git clone git@github.com:openscenegraph/osgQt.git
osg:编译好后按OSG编译安装-MSVC中方式配置环境变量
msvc2017_64:由于这里使用msvc64编译,所以需要添加相应的环境变量
打开环境变量窗口
加入如下路径(需要修改为自己的Qt安装路径)
D:\Qt\5.14.2\msvc2017_64\bin D:\Qt\5.14.2\msvc2017_64\lib
环境变量配置后重启电脑使环境变量生效
具体步骤和编译osg差不多
将CMakelists.txt拖入CMake中,设置好编译后的路径build
点击Configure,这里的选择必须与osg编译一样(如果没配置环境变量,则这一步会报错,显示找不到Qt5Config.cmake
等文件)
修改CMAKE_INSTALL_PREFIX
点击Configure -> Generate -> Open Project
后续步骤就和编译osg一样,编译好osgQt后,有如下4个文件夹,将文件夹中内容复制到osg安装路径下对应文件夹下即可(编译后会有两个lib)
pro文件添加
INCLUDEPATH += D:/OSG/install/include DEPENDPATH += D:/OSG/install/include win32:CONFIG(release, debug|release): LIBS += -LD:/OSG/install/lib/ -losgQOpenGL else:win32:CONFIG(debug, debug|release): LIBS += -LD:/OSG/install/lib/ -losgQOpenGLd win32:CONFIG(release, debug|release): LIBS += -LD:/OSG/install/lib/ -losgViewer else:win32:CONFIG(debug, debug|release): LIBS += -LD:/OSG/install/lib/ -losgViewerd win32:CONFIG(release, debug|release): LIBS += -LD:/OSG/install/lib/ -losgGA else:win32:CONFIG(debug, debug|release): LIBS += -LD:/OSG/install/lib/ -losgGAd win32:CONFIG(release, debug|release): LIBS += -LD:/OSG/install/lib/ -losgDB else:win32:CONFIG(debug, debug|release): LIBS += -LD:/OSG/install/lib/ -losgDBd win32:CONFIG(release, debug|release): LIBS += -LD:/OSG/install/lib/ -losg else:win32:CONFIG(debug, debug|release): LIBS += -LD:/OSG/install/lib/ -losgd
ui文件添加一个button,一个widget
widget.h
#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; QString m_fileName; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <osgViewer/Viewer> #include <osg/Node> #include <osgDB/ReadFile> #include <osgGA/TrackballManipulator> 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窗口显示大小 m_fileName = "D:/OSG/OpenSceneGraph-Data/avatar.osg"; // 显示模型路径 connect(osgWidget, SIGNAL(initialized()), this, SLOT(initOSG())); } Widget::~Widget() { delete ui; if(osgWidget != NULL) { delete osgWidget; } } void Widget::initOSG() { osgViewer::Viewer* pViewer = osgWidget->getOsgViewer(); pViewer->setCameraManipulator(new osgGA::TrackballManipulator); osg::Node* node = osgDB::readNodeFile(m_fileName.toStdString()); pViewer->setSceneData(node); } void Widget::on_pushButton_clicked() { m_fileName = "D:/OSG/OpenSceneGraph-Data/bignathan.osg"; initOSG(); }
链接:https://pan.baidu.com/s/1UfNbJ0fUAzKfe3XrWNVTDA
提取码:66tf