本文主要是介绍Qt 集成OSG,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Qt 你好 | 专注于Qt的技术分享平台
一,新建一个 QOsgWidget 类,继承自osgQOpenGLWidget
#ifndef QOSGWIDGET_H #define QOSGWIDGET_H#include <QObject> #include <osgViewer/Viewer> #include <osgQOpenGL/osgQOpenGLWidget> class QOsgWidget: public osgQOpenGLWidget {public:explicit QOsgWidget(QWidget *parent = nullptr);QSize sizeHint() const;void InitQOsgWidget();};#endif // QOSGWIDGET_H
#include "qosgwidget.h" #include <QDebug> QOsgWidget::QOsgWidget(QWidget *parent):osgQOpenGLWidget(parent) {//多重采样QSurfaceFormat surfaceFormat;surfaceFormat.setSamples(6);setFormat(surfaceFormat); }QSize QOsgWidget::sizeHint() const {return QSize(this->width(),this->height()); }
二,UI中创建一个QWidget 并提升为QOsgWidget
三,主窗初始化这个QOsgWidget。 这里以一个 网格 节点为例。
#ifndef WIDGET_H #define WIDGET_H#include <osg/Node> #include <QWidget> #include "qosgwidget.h"QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;void InitQOsgWidget();osg::ref_ptr<osg::Group> _Root = nullptr;osg::ref_ptr<osgViewer::Viewer> _Viewer = nullptr; }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h"#include <osgGA/TrackballManipulator> #include <osgUtil/Optimizer> #include <osgGA/GUIEventHandler> #include <osgViewer/ViewerEventHandlers>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);connect(ui->widget, &osgQOpenGLWidget::initialized, this, &Widget::InitQOsgWidget); }Widget::~Widget() {delete ui; }void Widget::InitQOsgWidget() {_Root = new osg::Group();QSize size = ui->widget->size();osg::ref_ptr<osgGA::TrackballManipulator> trackball = new osgGA::TrackballManipulator;trackball->setAllowThrow(false);trackball->setAutoComputeHomePosition(true);trackball->setThreadSafeRefUnref(true);osgUtil::Optimizer optimizer;optimizer.optimize(_Root.get());_Viewer = ui->widget->getOsgViewer();_Viewer->addEventHandler(new osgViewer::StatsHandler);_Viewer->getCamera()->setClearColor(osg::Vec4(0.2, 0.2, 0.2, 1));_Viewer->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(size.width())/static_cast<double>(size.height()), 1.0f, 10000.0f );_Viewer->setCameraManipulator(trackball);_Viewer->setRunMaxFrameRate(60);osg::ref_ptr<osg::Geode> grid = new osg::Geode;osg::Geometry* geom = new osg::Geometry;grid->addChild(geom);osg::Vec3Array* vertex = new osg::Vec3Array;geom->setVertexArray(vertex);//沿xy平面画线,间隔500米,从-10000,画到100000for (int i = -10; i <= 10; i += 1){vertex->push_back(osg::Vec3(i, -10, 0));vertex->push_back(osg::Vec3(i, 10, 0));vertex->push_back(osg::Vec3(-10, i, 0));vertex->push_back(osg::Vec3(10, i, 0));}geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertex->size()));osg::Vec4Array* color = new osg::Vec4Array();color->push_back(osg::Vec4(0.7, 0.7, 0.7, 1.0));geom->setColorArray(color, osg::Array::BIND_OVERALL);geom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);_Root->addChild(grid);_Viewer->setSceneData(_Root.get()); }
四,效果
集成OSG | Qt 你好
这篇关于Qt 集成OSG的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!