QT内嵌OSG[osg][qt]

1 osgQT的移植

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

这里先给出结果图:gliderQT

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


#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 封装一个自己的类:
#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 调用这个类:
            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))); 

Guess you like

Origin blog.csdn.net/cxy_hust/article/details/117636718