Osg使用osgQt与Qt结合使用(附项目)(2)

概述

在上一篇文章中:
https://blog.csdn.net/qq_34861102/article/details/80786031
已经介绍了验证Osg编译是否成功
现在这里介绍使用osgQt实现将Osg放入一个控件之中

结果示意图:
Osg使用osgQt与Qt结合使用(附项目)(2)_第1张图片


代码

  • 引入相关的包
#include 
#include >
#include 
#include 
#include 
#include 
#include 
  • 然后在main函数中声明
 QApplication app( argc, argv );
    osgQt::GraphicsWindowQt* gw = createGraphicsWindow( 50, 50, 640, 480 );
    osg::Node* scene = osgDB::readNodeFile("cow.osg");

    ViewerWidget* widget = new ViewerWidget(gw, scene);
    widget->setGeometry( 100, 100, 800, 600 );
    widget->show();
    return app.exec();
  • Node

    首先使用osg::Node* scene读取数据中已经存在的模型

  • GraphicsWindowQt

    osgQt中对应的一个类,可以在osggithub地址上查看(目前的3.6中是没有的,在编译的时候注意),主要是窗口的获得

  • 对于ViewerWidget
    这个是继承QWidget的一个子类:

    • 在构造函数中可以看到使用之前的模型
    • osgQt::GraphicsWindowQt实际上包含一个与OSG的渲染操作的图形上下文相关的Qt小部件(Widget)。 一旦设置了场景的相机,小部件将自动渲染场景。
    • 最后使用了定时器与信号绑定并进行定时更新
    • osgViewer和其中的camera 进行赋值即可放入
     ViewerWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )
      : QWidget()
      {
          const osg::GraphicsContext::Traits* traits = gw->getTraits();
    
          osg::Camera* camera = _viewer.getCamera();
          camera->setGraphicsContext( gw );
          camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
          camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
          camera->setProjectionMatrixAsPerspective(
              30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
    
          _viewer.setSceneData( scene );
          _viewer.addEventHandler( new osgViewer::StatsHandler );
          _viewer.setCameraManipulator( new osgGA::TrackballManipulator );
          _viewer.setThreadingModel( osgViewer::Viewer::SingleThreaded );
    
          QVBoxLayout* layout = new QVBoxLayout;
          layout->addWidget( gw->getGLWidget() );
          setLayout( layout );
    
          connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
          _timer.start( 40 );
      }
  • 最后启动即可:app.exec()

  • 完整代码:(注释掉的代码为上一个验证的
//#include 

//#include "drawtwoball.h"
//#include "pickhandler.h"

//int main()
//{
//    osg::ref_ptr viewer=new osgViewer::Viewer();
//    PickHandler* p=new PickHandler();
//    viewer->addEventHandler(p);
//    osg::ref_ptr root=new osg::Group();

//    //读取模型
//    osg::ref_ptr node=osgDB::readNodeFile("cow.osg");

//    //创建位置变换位置节点pat2
//    osg::ref_ptr pat2=new osg::PositionAttitudeTransform();
//    pat2->setPosition(osg::Vec3(10.0f,0.0f,0.0f));
//    pat2->addChild(node.get());

//    root->addChild(node.get());
//    root->addChild(pat2.get());

//    printf_s("X:%lf,Y:%lf,Z:%lf\n\n",node->getBound().center().x(),
//             node->getBound().center().y(),
//             node->getBound().center().z());
//    printf_s("X:%lf,Y:%lf,Z:%lf",pat2->getBound().center().x(),
//             pat2->getBound().center().y(),
//             pat2->getBound().center().z());

//    viewer->setUpViewInWindow(100,100,800,600);

//    //优化场景数据
//    osgUtil::Optimizer op;
//    op.optimize(root.get());

//    viewer->setSceneData(root.get());
//    viewer->realize();
//    viewer->run();

//    return 0;

//}

/* -*-c++-*- OpenSceneGraph Cookbook
* Chapter 9 Recipe 1
* Author: Wang Rui 
*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h )
{
    osg::ref_ptr traits = new osg::GraphicsContext::Traits;
    traits->windowDecoration = false;
    traits->x = x;
    traits->y = y;
    traits->width = w;
    traits->height = h;
    traits->doubleBuffer = true;

    return new osgQt::GraphicsWindowQt(traits.get());
}

class ViewerWidget : public QWidget
{
public:
    ViewerWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )
    : QWidget()
    {
        const osg::GraphicsContext::Traits* traits = gw->getTraits();

        osg::Camera* camera = _viewer.getCamera();
        camera->setGraphicsContext( gw );
        camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
        camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
        camera->setProjectionMatrixAsPerspective(
            30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );

        _viewer.setSceneData( scene );
        _viewer.addEventHandler( new osgViewer::StatsHandler );
        _viewer.setCameraManipulator( new osgGA::TrackballManipulator );
        _viewer.setThreadingModel( osgViewer::Viewer::SingleThreaded );

        QVBoxLayout* layout = new QVBoxLayout;
        layout->addWidget( gw->getGLWidget() );
        setLayout( layout );

        connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
        _timer.start( 40 );
    }

protected:
    virtual void paintEvent( QPaintEvent* event )
    { _viewer.frame(); }

    osgViewer::Viewer _viewer;
    QTimer _timer;
};

int main( int argc, char** argv )
{
    QApplication app( argc, argv );
    osgQt::GraphicsWindowQt* gw = createGraphicsWindow( 50, 50, 640, 480 );
    osg::Node* scene = osgDB::readNodeFile("cow.osg");

    ViewerWidget* widget = new ViewerWidget(gw, scene);
    widget->setGeometry( 100, 100, 800, 600 );
    widget->show();
    return app.exec();
}


Git项目地址:

https://github.com/Outliwer/osgDemo

你可能感兴趣的:(OpenSceneGraph)