osgQT

1 osgQT的移植

首先参考官方项目:
https://github.com/openscenegraph/osgQt

这里先给出结果图:
https://cdn.jsdelivr.net/gh/xychen5/blogImgs@main/imgs/gliderQT.1w4nlzuxphsw.png

1.1 修改osgviewerqt.cpp如下: 这样你就能自己读模型了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

#include <osgQOpenGL/osgQOpenGLWidget>

#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>

#include <osg/Switch>
#include <osg/Types>
#include <osgViewer/Viewer>
#include <osgText/Text>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>

#include <osgGA/Device>

#include <QApplication>
#include <QSurfaceFormat>

#include <iostream>


int main( int argc, char** argv )
{


QSurfaceFormat format = QSurfaceFormat::defaultFormat();

#ifdef OSG_GL3_AVAILABLE
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setOption(QSurfaceFormat::DebugContext);
#else
format.setVersion(2, 0);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setOption(QSurfaceFormat::DebugContext);
#endif
format.setDepthBufferSize(24);
//format.setAlphaBufferSize(8);
format.setSamples(8);
format.setStencilBufferSize(8);
format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
QSurfaceFormat::setDefaultFormat(format);

QApplication app(argc, argv);

// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc, argv);

arguments.getApplicationUsage()->setApplicationName(
arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() +
" is the standard OpenSceneGraph example which loads and visualises 3d models.");
arguments.getApplicationUsage()->setCommandLineUsage(
arguments.getApplicationName() + " [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("--image <filename>",
"Load an image and render it on a quad");
arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>",
"Load an image/DEM and render it on a HeightField");
arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>",
"Provide authentication information for http file access.");
arguments.getApplicationUsage()->addCommandLineOption("-p <filename>",
"Play specified camera path animation file, previously saved with 'z' key.",
"F:/dataSets/OpenSceneGraph-Data-3.4.0/OpenSceneGraph-Data/glider.osg");
arguments.getApplicationUsage()->addCommandLineOption("--speed <factor>",
"Speed factor for animation playing (1 == normal speed).");
arguments.getApplicationUsage()->addCommandLineOption("--device <device-name>",
"add named device to the viewer");

osgQOpenGLWidget widget(&arguments);

if (true) {

osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("F:/dataSets/OpenSceneGraph-Data-3.4.0/OpenSceneGraph-Data/glider.osg");

if (!loadedModel)
{
std::cout << arguments.getApplicationName() << ": No data loaded" << std::endl;
return 1;
}

// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();

// report any errors if they have occurred when parsing the program arguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}

widget.show();

// optimize the scene graph, remove redundant nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel);

// mViewer = new osgViewer::Viewer();
osg::ref_ptr<osgViewer::Viewer> mViewer = widget.getOsgViewer();

osg::ref_ptr<osg::Camera> camera = mViewer->getCamera();

camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f));

// Add the Camera to the Viewer
mViewer->setCamera(camera.get());

// Add the Camera Manipulator to the Viewer
// mViewer->setCameraManipulator(keyswitchManipulator.get());
osg::ref_ptr<osgGA::TrackballManipulator> trackball = new osgGA::TrackballManipulator();
mViewer->setCameraManipulator(trackball.get());

mViewer->setSceneData(loadedModel);

// // Realize the Viewer's graphics context, which already done in the default pWidget
// mViewer->realize();

return app.exec();
}
}

1.2 qt中使用

  • 1 封装一个自己的类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    #include <iostream>
    #include <memory>

    #include "osgHeaders.h"

    /*
    * this class is mainly for initialize the pWidget
    */
    class QOsgWidget {
    public:
    // essential widget, use this ptr to be the real widget
    osgQOpenGLWidget* pWidget = nullptr;
    // QOsgWidget(QWidget* parent = nullptr);
    QOsgWidget(const std::string& modelPath, QWidget* parent = nullptr);
    ~QOsgWidget();

    // osg base vars
    osg::ref_ptr<osg::Group> mRoot = nullptr;
    osg::ref_ptr<osg::Camera> camera = nullptr;
    osg::ref_ptr<osgViewer::Viewer> mViewer = nullptr;
    osg::ref_ptr<osgGA::TrackballManipulator> trackball = nullptr;
    osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = nullptr;

    // for experiment:
    osg::ref_ptr<osgViewer::StatsHandler> pStat = nullptr;
    osg::ref_ptr<osgGA::TrackballManipulator> pTrackball = nullptr;
    osg::ref_ptr<osgViewer::Viewer> pmViewer = nullptr;

    // osg base funcs
    void InitManipulators();
    // load model into the mRoot
    void InitModel(const std::string& modelPathm, osg::ref_ptr<osg::Group>& mRoot);
    // init the cmaera
    void InitCameraConfig();
    osg::ref_ptr<osgViewer::Viewer> getViewer() { return mViewer; }

    // QObject::connect(&widget, &osgQOpenGLWidget::initialized);
    };
  • 2 调用这个类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    QOsgWidget *bBoxEdit = new QOsgWidget(modelPath, static_cast<QWidget*>(this));

    // use an ArgumentParser object to manage the program arguments.
    bBoxEdit->pWidget = new osgQOpenGLWidget(&arguments, this);
    bBoxEdit->pWidget->show();

    // init the manipulators
    bBoxEdit->InitManipulators();
    // init Scene Graph, that is to load the model
    bBoxEdit->InitModel(modelPath, bBoxEdit->mRoot);
    // init camera config
    bBoxEdit->InitCameraConfig();

    // 在这一行,将这个worksapceWidget添加到你想要添加的位置上就ok
    workspaceWidget = static_cast<QOpenGLWidget*>(&(*(bBoxEdit->pWidget)));