在vs下的osg的qt窗口开发例子以及一些注意事项

1.运行结果图:

2.注意事项

(1)qt插件的设定一定要选择好

(2)包含目录和库目录的设置(我的是release库,不是debug库,设置请注意)

(3)链接器的附加依赖项中的库添加

我的库有(release库,非debug):

qtmain.lib
Qt5Core.lib
Qt5Gui.lib
Qt5Widgets.lib
OpenThreads.lib
osg.lib
osgDB.lib
osgUtil.lib
osgGA.lib
osgViewer.lib
osgText.lib
Qt5OpenGL.lib
opengl32.lib
glu32.lib
osgQt.lib

3.代码及其注释

  1 #include <QtCore/QtCore>
  2 #include <QtGui/QtGui>>
  3 #include <osgDB/ReadFile>
  4 #include <osgViewer/ViewerEventHandlers>
  5 #include <osgViewer/Viewer>
  6 #include <osgQt/GraphicsWindowQt>
  7 #include <QtCore/QTimer>  
  8 #include <QtWidgets/QApplication>  
  9 #include <QtWidgets/QVBoxLayout>  
 10 #include <QtWidgets/QWidget>  
 11 #include <osgViewer/CompositeViewer>  
 12 #include <osgViewer/ViewerEventHandlers>  
 13 #include <osgGA/TrackballManipulator>    
 14 #include <osgQt/GraphicsWindowQt>  
 15 #include <iostream>  
 16 #include <osg/Node>
 17 #include <osg/Geode>
 18 #include <osg/Geometry>
 19 #include <osg/ShapeDrawable>
 20 #include <osg/Shape>
 21 #include <osgGA/StateSetManipulator>
 22 #include <osgQt/GraphicsWindowQt> 
 23 #include <QGLWidget>
 24 #include <QtWidgets/QMainWindow>
 25 #include "ui_qttest.h"
 26 //创建球模型
 27 osg::ref_ptr<osg::Node> createShape()
 28 {
 29      //几何组结点
 30          osg::ref_ptr<osg::Geode> geode = new osg::Geode;
 31      float radius = 0.08f;
 32      float height = 0.1f;
 33      osg::TessellationHints* hints = new osg::TessellationHints;
 34      hints->setDetailRatio(0.5f);
 35      geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), radius), hints));
 36      return geode.get();
 37 }
 38 //创建渲染器的相机
 39 osg::Camera* createCamera(int x, int y, int w, int h)
 40 {
 41     osg::DisplaySettings* ds =
 42         osg::DisplaySettings::instance().get();
 43     osg::ref_ptr<osg::GraphicsContext::Traits> traits =
 44         new osg::GraphicsContext::Traits; //声明一个嵌入窗口的变量,只要是有窗口必须使用此对象进行设置
 45     traits->windowDecoration = false;//是否显示窗口的描述,默认为false不显示 
 46     traits->x = x;//设置显示的x坐标
 47     traits->y = y;//设置显示的y坐标
 48     traits->width = w;//设置窗口的宽度
 49     traits->height = h;//设置窗口的高度
 50     traits->doubleBuffer = true;//创建新的图形窗口是否使用双缓冲特性 
 51     osg::ref_ptr<osg::Camera> camera = new osg::Camera;
 52     //任意图形子系统的抽象层接口,它提供了统一的图形设备操作函数,用来实现渲染结果和底层设备的交互
 53     camera->setGraphicsContext(
 54         new osgQt::GraphicsWindowQt(traits.get()));
 55     //设置渲染器清除色
 56     camera->setClearColor(osg::Vec4(0.2, 0.2, 0.6, 1.0));
 57     //图形输出设置当前视口
 58     camera->setViewport(new osg::Viewport(
 59         0, 0, traits->width, traits->height));
 60     //标识摄像机在世界坐标系下的位置
 61     camera->setProjectionMatrixAsPerspective(
 62         30.0f, static_cast<double>(traits->width) /
 63         static_cast<double>(traits->height), 1.0f, 10000.0f);
 64     return camera.release();
 65 }
 66 
 67 class ViewerWidget : public QWidget
 68 {
 69 public:
 70     ViewerWidget(osg::Camera* camera, osg::Group* scene)
 71         //: QWidget()
 72         {
 73             _viewer.setCamera(camera);   //将相机添加到渲染器中
 74             _viewer.setSceneData(scene); //将模型添加到渲染器中
 75             _viewer.addEventHandler(new osgViewer::StatsHandler);
 76             //以下是创建了渲染器的一些操作
 77             _viewer.setCameraManipulator(new osgGA::TrackballManipulator);
 78             _viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);//创建为单线程    
 79             _viewer.addEventHandler(new osgGA::StateSetManipulator(_viewer.getCamera()->getOrCreateStateSet()));
 80             //窗口大小变化事件
 81             _viewer.addEventHandler(new osgViewer::WindowSizeHandler);
 82             //添加一些常用状态设置
 83             _viewer.addEventHandler(new osgViewer::StatsHandler);
 84             //其实说gw就是要显示的窗口
 85             osgQt::GraphicsWindowQt* gw =
 86                     dynamic_cast<osgQt::GraphicsWindowQt*>(
 87                     camera->getGraphicsContext());
 88                 if (gw)
 89                 {
 90                     QVBoxLayout* layout = new QVBoxLayout;
 91                     layout->addWidget(gw->getGLWidget());
 92                     setLayout(layout);
 93                 }
 94             connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
 95             _timer.start(40);//关联定时器计满信号和响应的槽函数
 96         }
 97 
 98 protected:
 99     virtual void paintEvent(QPaintEvent* event)
100     {
101         _viewer.frame();//实时进行渲染
102     } 
103     osgViewer::Viewer _viewer;//创建一个渲染器
104     QTimer _timer;//创建一个定时器
105 };
106 int main(int argc, char **argv)
107 {
108     QApplication app(argc, argv);
109     osg::Camera* camera = createCamera(50, 50, 640, 480);
110     osg::Group * scene = new osg::Group();
111     scene->addChild(createShape().get());
112 
113     ViewerWidget* widget = new ViewerWidget(camera, scene);
114     widget->setGeometry(100, 100, 800, 600);
115     widget->show();
116     return app.exec();
117 }

 

 

posted @ 2018-03-09 11:34  masking_timeflows  阅读(4372)  评论(0编辑  收藏  举报