天天看點

Qt加載Osg的新方式osgQOpenGL簡介一、概述二、運作效果圖三、簡要步驟四、Widget.h代碼五、Widget.cpp代碼六、Main.cpp代碼七、TestOsgQt.pro代碼

一、概述

Qt加載Osg的老方式是使用osg3.4以及以前的某些版本中提供的osgQt項目加載osg,不過這種方式,在3.6等新版本中不再支援,更改起來比較麻煩,osg::GraphicsContext類不再提供osg::GraphicsContext::setWindowingSystemInterface接口函數,無法編譯成功。不過目前有個替代方案,使用osgQOpenGL的osgQOpenGLWidget類進行三維模型的加載,下載下傳位址為:

https://github.com/OpenSceneGraph/osgQt

下面是使用osgQOpenGLWidget加載osg的示範例子。

二、運作效果圖

Qt加載Osg的新方式osgQOpenGL簡介一、概述二、運作效果圖三、簡要步驟四、Widget.h代碼五、Widget.cpp代碼六、Main.cpp代碼七、TestOsgQt.pro代碼

三、簡要步驟

  1. 建立osgQOpenGLWidget并加入到控件布局中
  2. 響應osgQOpenGLWidget的initialized信号,設定一個漫遊操作器,讀取osg模型檔案并加入到場景節點中。

四、Widget.h代碼

#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

class Widget : public QWidget

{

    Q_OBJECT

public:

    Widget(QWidget *parent = 0);

    ~Widget();

private slots:

void initOsg();

};

#endif // WIDGET_H

五、Widget.cpp代碼

#include "widget.h"

#include <osgQOpenGL/osgQOpenGLWidget>

#include <QBoxLayout>

#include <osgDB/ReadFile>

#include <osgViewer/Viewer>

#include <osgUtil/Optimizer>

#include <osgGA/TrackballManipulator>

#include <QLabel>

Widget::Widget(QWidget *parent)

    : QWidget(parent)

{

resize(400, 300);

QHBoxLayout *pLayout = new QHBoxLayout(this);

pLayout->setMargin(0);

osgQOpenGLWidget *pOsgW = new osgQOpenGLWidget;

pLayout->addWidget(pOsgW);

connect(pOsgW, SIGNAL(initialized()), this, SLOT(initOsg()));

}

Widget::~Widget()

{

}

void Widget::initOsg()

{

osgViewer::Viewer *pViewer = ((osgQOpenGLWidget *)sender())->getOsgViewer();

pViewer->setCameraManipulator(new osgGA::TrackballManipulator());

osg::Node *pNode = osgDB::readNodeFile("boxman.osg");

osgUtil::Optimizer optimizer;

optimizer.optimize(pNode);

pViewer->setSceneData(pNode);

}

六、Main.cpp代碼

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    Widget w;

    w.show();

    return a.exec();

}

七、TestOsgQt.pro代碼

QT       += core gui widgets

TARGET = TestOsgQt

TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

CONFIG += c++11

SOURCES += \

        main.cpp \

        widget.cpp

HEADERS += \

        widget.h

OsgDir = C:\r

CONFIG(release, debug|release) {

LIBS += -L$${OsgDir}/lib/ -losgQOpenGL -losgDB -losgViewer -losg -losgUtil -losgGA

} else {

LIBS += -L$${OsgDir}/lib/ -losgQOpenGLd -losgDBd -losgViewerd -losgd -losgUtild -losgGAd

}

INCLUDEPATH += $${OsgDir}/include

DEPENDPATH += $${OsgDir}/include

繼續閱讀