查看: 3089|回复: 2

qt结合osg viewer设置多线程问题!

[复制链接]

该用户从未签到

发表于 2012-3-25 10:53:26 | 显示全部楼层 |阅读模式
QQ截图20120325105120.png adapterwidget.h
  1. #ifndef ADAPTERWIDGET_H
  2. #define ADAPTERWIDGET_H

  3. #include <QGLWidget>
  4. #include <osgViewer/GraphicsWindow>
  5. #include <QKeyEvent>

  6. class AdapterWidget : public QGLWidget
  7. {
  8.         Q_OBJECT

  9. public:
  10.         AdapterWidget(QWidget *parent = 0);
  11.         ~AdapterWidget();
  12.         QSize minimumSizeHint() const;
  13.         QSize sizeHint() const;

  14.         osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
  15.         const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }

  16. protected:
  17. //        virtual void initializeGL();
  18. //        virtual void paintGL();
  19.         virtual void resizeGL(int width, int height);

  20.         virtual void keyPressEvent( QKeyEvent* event );
  21.         virtual void keyReleaseEvent( QKeyEvent* event );
  22.         virtual void mousePressEvent( QMouseEvent* event );
  23.         virtual void mouseReleaseEvent( QMouseEvent* event );
  24.         virtual void mouseMoveEvent( QMouseEvent* event );

  25.         osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
  26. };

  27. #endif // ADAPTERWIDGET_H
复制代码
adapterwidget.cpp
  1. #include "adapterwidget.h"

  2. AdapterWidget::AdapterWidget(QWidget *parent)
  3.         : QGLWidget(parent)
  4. {
  5.         _gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
  6.         setFocusPolicy(Qt::ClickFocus);
  7. }

  8. AdapterWidget::~AdapterWidget()
  9. {

  10. }

  11. QSize AdapterWidget::minimumSizeHint() const
  12. {
  13.         return QSize(50, 50);
  14. }

  15. QSize AdapterWidget::sizeHint() const
  16. {
  17.         return QSize(400, 400);
  18. }

  19. void AdapterWidget::resizeGL(int width, int height)
  20. {
  21.         _gw->getEventQueue()->windowResize(0, 0, width, height );
  22.         _gw->resized(0,0,width,height);
  23. }

  24. void AdapterWidget::keyPressEvent( QKeyEvent* event )
  25. {
  26.         _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
  27. }

  28. void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
  29. {
  30.         _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
  31. }

  32. void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
  33. {
  34.         _gw->getEventQueue()->mouseMotion(event->x(), event->y());
  35. }

  36. void AdapterWidget::mousePressEvent( QMouseEvent* event )
  37. {
  38.          int button = 0;
  39.     switch(event->button())
  40.     {
  41.         case(Qt::LeftButton): button = 1; break;
  42.         case(Qt::MidButton): button = 2; break;
  43.         case(Qt::RightButton): button = 3; break;
  44.         case(Qt::NoButton): button = 0; break;
  45.         default: button = 0; break;
  46.     }
  47.     _gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
  48. }

  49. void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
  50. {
  51.         int button = 0;
  52.     switch(event->button())
  53.     {
  54.         case(Qt::LeftButton): button = 1; break;
  55.         case(Qt::MidButton): button = 2; break;
  56.         case(Qt::RightButton): button = 3; break;
  57.         case(Qt::NoButton): button = 0; break;
  58.         default: button = 0; break;
  59.     }
  60.     _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
  61. }
复制代码
osgwidget.h
  1. #ifndef OSGWIDGET_H
  2. #define OSGWIDGET_H

  3. #include <osgViewer/Viewer>
  4. #include "adapterwidget.h"

  5. class OSGWidget : public AdapterWidget, public osgViewer::Viewer
  6. {
  7.         Q_OBJECT

  8. public:
  9.         OSGWidget(QWidget *parent = 0);
  10.         ~OSGWidget();

  11. protected:
  12.         void paintGL();

  13. private:
  14.        
  15. };

  16. #endif // OSGWIDGET_H
复制代码
osgwidget.cpp
  1. #include "osgwidget.h"

  2. OSGWidget::OSGWidget(QWidget *parent)
  3.         : AdapterWidget(parent)
  4. {
  5.         getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
  6.         getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
  7.         getCamera()->setGraphicsContext(getGraphicsWindow());

  8.         setThreadingModel(osgViewer::Viewer::SingleThreaded); // 这里有问题!!!!!
  9.         setGeometry(0,0,600,600);

  10. }

  11. OSGWidget::~OSGWidget()
  12. {

  13. }

  14. void OSGWidget::paintGL()
  15. {
  16.         frame();
  17.         update();
  18. }
复制代码
setThreadingModel(osgViewer::Viewer::SingleThreaded);  只有设置单线程没有问题,问题如下,osg不能显示,,是不是因为我只给这个场景添加了一个cow.osg 的问题啊!

该用户从未签到

发表于 2012-3-29 17:58:20 | 显示全部楼层
我没什么时间把所有程序都读一遍,不过您的问题看起来像是没有正确设置opengl上下文就开始渲染了。您应该考虑osgQt中的方案

该用户从未签到

 楼主| 发表于 2012-3-29 19:02:19 | 显示全部楼层
array 发表于 2012-3-29 17:58
我没什么时间把所有程序都读一遍,不过您的问题看起来像是没有正确设置opengl上下文就开始渲染了。您应该考 ...

谢谢!我再看看!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表