本文目录
1. 工具准备
1.1 OSG3.6.4
关于OSG的编译我之前的博客已经写的很详细,这里贴出链接。
1.2 osgQt
osgQt的编译过程也在我之前的博客中,这里接着贴链接。
2. 正文开始
2.1 需求分析
在做osgb三维模型的分割处理的时候,因为要查看处理之后的三维模型,每次双击打开osgb文件再选择osgviewer.exe实在是麻烦,而且命令行使用osgviewer来同时查看多个模型,也着实麻烦。
基于此,就想着写个程序,能同时选中多个osgb文件,并在一个窗口中显示。
因为是模型展示工具,因此只需要能显示选中的三维模型就行。
2.2 源码分析
2.2.1 Widget.h
Widget类是继承自QWidget的类,而要使用的osgQt则是导入的外部库osgQOpenGL.lib。该库是结合Qt和OSG的第三方库,其中要用到的osgQOpenGLWidget也是继承自QWidget,因此该类使用起来和常规的窗口类一样。
具体代码如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QFileDialog>
#include <QDir>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Texture2D>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
osg::Camera *backGround(QString sImagePath, int iWidth, int iHeight);
void resizeEvent(QResizeEvent *event) override;
protected slots:
void onChoseFileButtonClicked();
void onChoseDirButtonClicked();
void onShowModelButtonClicked();
void onClearButtonClicked();
void initOsgWindow();
private:
Ui::Widget *ui;
osgQOpenGLWidget* _pOsgWidget;
osg::ref_ptr<osgViewer::Viewer> _viewer;
QStringList _lFileNames;
};
#endif // WIDGET_H
基本思路为:将选中的osgb文件通过OSG的组节点将模型读入到内存,然后使用osgQOpenGLWidget获取该窗口组件的viewer,将存储模型数据的group组节点设置为当前viewer的场景数据。
具体实现如下。
2.2.2 Widget.cpp
#include "Widget.h"
#include "ui_Widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
_pOsgWidget = new osgQOpenGLWidget(this);
_pOsgWidget->setGeometry(220, 0, 580, 580);
// _pOsgWidget->setHidden(true);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(onChoseFileButtonClicked()));
connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(onChoseDirButtonClicked()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(onShowModelButtonClicked()));
connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(onClearButtonClicked()));
connect(_pOsgWidget, SIGNAL(initialized()), this, SLOT(initOsgWindow()));
}
Widget::~Widget()
{
delete ui;
}
osg::

本文介绍如何利用OSG和osgQt库实现三维模型的批量加载与展示,包括工具准备、需求分析、源码解析等内容。
最低0.47元/天 解锁文章
1161

被折叠的 条评论
为什么被折叠?



