利用QT在界面显示OSG模型,之前写过一篇文章OSG在QT的中显示,现在更新一下。
#include <QtWidgets/QMainWindow> #include "ui_QtGuiOSG.h" #include "osgInc.h" class QtGuiOSG : public QMainWindow { Q_OBJECT public: QtGuiOSG(QWidget *parent = Q_NULLPTR); private: void init(); osg::LightSource* createLight(); private slots: void slotTimeUpdate(); private: Ui::QtGuiOSGClass ui; osgViewer::Viewer* _viewer = nullptr; unsigned int _screenW, _screenH; QTimer* _timer; };
#include "QtGuiOSG.h" #include <osg/GraphicsContext> #include "GraphicsWindowQt.h" #include <QTimer> QtGuiOSG::QtGuiOSG(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); init(); } void QtGuiOSG::init() { osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); if (!wsi) { osg::notify(osg::NOTICE) << "Error, no WindowSystemInterface available, cannot create windows." << std::endl; return ; } wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), _screenW, _screenH); osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->x = 0; traits->y = 0; traits->width = _screenW; traits->height = _screenH; traits->windowDecoration = false; traits->doubleBuffer = true; traits->sharedContext = 0; traits->alpha = ds->getMinimumNumAlphaBits(); traits->stencil = ds->getMinimumNumStencilBits(); traits->sampleBuffers = ds->getMultiSamples(); traits->samples = 16; traits->vsync = false; osg::GraphicsContext* gc = new GraphicsWindowQt(traits.get()); osg::ref_ptr<osg::Camera> camera = new osg::Camera; camera->setGraphicsContext(gc); camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); camera->setViewport(new osg::Viewport(0, 0, _screenW, _screenH)); camera->setClearColor(osg::Vec4(128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 0.8)); camera->setProjectionMatrixAsPerspective(30.0, static_cast<double>(_screenW) / static_cast<double>(_screenH), 1.0, 10000.0); _viewer = new osgViewer::Viewer; osg::ref_ptr<osg::Group> root = new osg::Group; root->addChild(osgDB::readNodeFile("cow.osgt"));//注意:这两句话的先后顺序 先添加模型在添加相机 root->addChild(createLight()); _viewer->setCamera(camera);//这两句话的先后顺序 _viewer->setSceneData(root); _viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); _viewer->setCameraManipulator(new osgGA::TrackballManipulator); _viewer->addEventHandler(new osgViewer::WindowSizeHandler()); GraphicsWindowQt* gcQT = dynamic_cast<GraphicsWindowQt*>(gc); if (gcQT){ QWidget *pWgt = gcQT->getGLWidget(); ui.verticalLayout->addWidget(pWgt); } _timer = new QTimer; connect(_timer, SIGNAL(timeout()), this, SLOT(slotTimeUpdate())); _timer->start(5); } osg::LightSource* QtGuiOSG::createLight() { osg::LightSource* ls = new osg::LightSource; osg::ref_ptr<osg::Light> light = new osg::Light; light->setLightNum(0); light->setPosition(osg::Vec4(0.0, -3.0, 0.0, 0.0)); light->setDirection(osg::Vec3(0.0, -1.0, 0.0)); light->setDiffuse(osg::Vec4(0.0, 0.0, 1.0, 1.0)); ls->setLight(light); return ls; } void QtGuiOSG::slotTimeUpdate() { _viewer->frame(); }
资源下载 aaa