|
adapterwidget.h- #ifndef ADAPTERWIDGET_H
- #define ADAPTERWIDGET_H
- #include <QGLWidget>
- #include <osgViewer/GraphicsWindow>
- #include <QKeyEvent>
- class AdapterWidget : public QGLWidget
- {
- Q_OBJECT
- public:
- AdapterWidget(QWidget *parent = 0);
- ~AdapterWidget();
- QSize minimumSizeHint() const;
- QSize sizeHint() const;
- osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
- const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
- protected:
- // virtual void initializeGL();
- // virtual void paintGL();
- virtual void resizeGL(int width, int height);
- virtual void keyPressEvent( QKeyEvent* event );
- virtual void keyReleaseEvent( QKeyEvent* event );
- virtual void mousePressEvent( QMouseEvent* event );
- virtual void mouseReleaseEvent( QMouseEvent* event );
- virtual void mouseMoveEvent( QMouseEvent* event );
- osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
- };
- #endif // ADAPTERWIDGET_H
复制代码 adapterwidget.cpp- #include "adapterwidget.h"
- AdapterWidget::AdapterWidget(QWidget *parent)
- : QGLWidget(parent)
- {
- _gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
- setFocusPolicy(Qt::ClickFocus);
- }
- AdapterWidget::~AdapterWidget()
- {
- }
- QSize AdapterWidget::minimumSizeHint() const
- {
- return QSize(50, 50);
- }
- QSize AdapterWidget::sizeHint() const
- {
- return QSize(400, 400);
- }
- void AdapterWidget::resizeGL(int width, int height)
- {
- _gw->getEventQueue()->windowResize(0, 0, width, height );
- _gw->resized(0,0,width,height);
- }
- void AdapterWidget::keyPressEvent( QKeyEvent* event )
- {
- _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
- }
- void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
- {
- _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
- }
- void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
- {
- _gw->getEventQueue()->mouseMotion(event->x(), event->y());
- }
- void AdapterWidget::mousePressEvent( QMouseEvent* event )
- {
- int button = 0;
- switch(event->button())
- {
- case(Qt::LeftButton): button = 1; break;
- case(Qt::MidButton): button = 2; break;
- case(Qt::RightButton): button = 3; break;
- case(Qt::NoButton): button = 0; break;
- default: button = 0; break;
- }
- _gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
- }
- void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
- {
- int button = 0;
- switch(event->button())
- {
- case(Qt::LeftButton): button = 1; break;
- case(Qt::MidButton): button = 2; break;
- case(Qt::RightButton): button = 3; break;
- case(Qt::NoButton): button = 0; break;
- default: button = 0; break;
- }
- _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
- }
复制代码 osgwidget.h- #ifndef OSGWIDGET_H
- #define OSGWIDGET_H
- #include <osgViewer/Viewer>
- #include "adapterwidget.h"
- class OSGWidget : public AdapterWidget, public osgViewer::Viewer
- {
- Q_OBJECT
- public:
- OSGWidget(QWidget *parent = 0);
- ~OSGWidget();
- protected:
- void paintGL();
- private:
-
- };
- #endif // OSGWIDGET_H
复制代码 osgwidget.cpp- #include "osgwidget.h"
- OSGWidget::OSGWidget(QWidget *parent)
- : AdapterWidget(parent)
- {
- getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
- getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
- getCamera()->setGraphicsContext(getGraphicsWindow());
- setThreadingModel(osgViewer::Viewer::SingleThreaded); // 这里有问题!!!!!
- setGeometry(0,0,600,600);
- }
- OSGWidget::~OSGWidget()
- {
- }
- void OSGWidget::paintGL()
- {
- frame();
- update();
- }
复制代码 setThreadingModel(osgViewer::Viewer::SingleThreaded); 只有设置单线程没有问题,问题如下,osg不能显示,,是不是因为我只给这个场景添加了一个cow.osg 的问题啊! |
|