我有以下代码,它是 OpenSceneGraph(https://github.com/openscenegraph/osg/blob/master/examples/osgviewerQt/osgviewerQt.cpp)给出的代码的修改,用于将 OSG 与 Qt 集成:
OSGViewer.h
#ifndef OSGVIEWER_H
#define OSGVIEWER_H
#include <QTimer>
#include <QApplication>
#include <QGridLayout>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/MultiTouchTrackballManipulator>
#include <osgDB/ReadFile>
#include <osgQt/GraphicsWindowQt>
#include <QGLWidget>
class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
{
public:
ViewerWidget(QWidget* parent = 0, osgViewer::ViewerBase::ThreadingModel threadingModel=osgViewer::CompositeViewer::SingleThreaded);
QWidget* addViewWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene );
osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false );
virtual void paintEvent( QPaintEvent* event ) { frame(); }
QTimer _timer;
};
#endif // OSGVIEWER_H
OSGViewer.cpp
#include "OSGViewer.h"
ViewerWidget::ViewerWidget(QWidget* parent, osgViewer::ViewerBase::ThreadingModel threadingModel)
{
setThreadingModel(threadingModel);
connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
_timer.start( 10 );
}
QWidget* ViewerWidget::addViewWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )
{
osgViewer::View* view = new osgViewer::View;
addView( view );
osg::Camera* camera = view->getCamera();
camera->setGraphicsContext( gw );
const osg::GraphicsContext::Traits* traits = gw->getTraits();
camera->setClearColor( osg::Vec4(0, 0, 0, 1.0) );
camera->setViewport( new osg::Viewport(0, 0, this->width(), this->height()) );
camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
view->setSceneData( scene );
view->addEventHandler( new osgViewer::StatsHandler );
view->setCameraManipulator( new osgGA::MultiTouchTrackballManipulator );
gw->setTouchEventsEnabled( true );
return gw->getGLWidget();
}
osgQt::GraphicsWindowQt* ViewerWidget::createGraphicsWindow( int x, int y, int w, int h, const std::string& name, bool windowDecoration )
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
return new osgQt::GraphicsWindowQt(traits.get());
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "OSGViewer.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
//void onCreateView();
private:
Ui::MainWindow *ui;
osg::ref_ptr<ViewerWidget> m_osgViewer_right, m_osgViewer_left;
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_osgViewer_left = new ViewerWidget(this);
ui->left = m_osgViewer_left->addViewWidget(m_osgViewer_left->createGraphicsWindow(0,0,100,100), osgDB::readNodeFile("cessnafire.osg") );
m_osgViewer_left->show();
m_osgViewer_right = new ViewerWidget;
ui->right = m_osgViewer_right->addViewWidget(m_osgViewer_right->createGraphicsWindow(0,0,100,100), osgDB::readNodeFile("cow.osgt") );
m_osgViewer_right->show();
ui->gridLayout->addWidget( ui->left, 0, 0 );
ui->gridLayout->addWidget( ui->right, 0, 2 );
}
/* ------------------------------------------------------- */
MainWindow::~MainWindow()
{
delete ui;
}
虽然我可以在我的 MainWindow 中使用两个不同的 QWidget 来渲染两个对象,但我总是在 MainWindow 后面弹出另外两个单独的空窗口。如果我关闭它们,那么我将无法在我的 MainWindow 中渲染我的模型。任何想法如何解决这个问题?我是 OpenSceneGraph 的新手。