//QT += core gui opengl//LIBS += -losgViewer -losgDB -losgUtil -losg -lOpenThreads -losgGA -losgQt
#include #include#include#include#include#include#include#include#include#include#include#include#include#include#include#include
usingQt::WindowFlags;class AdapterWidget:publicQGLWidget
{public:
AdapterWidget(QWidget*parent=0,const char* name=0,const QGLWidget * shareWidget=0,WindowFlags f=0);virtual ~AdapterWidget()
{
}
osgViewer::GraphicsWindow*getGraphicsWindow()
{return _gw.get();
}const osgViewer::GraphicsWindow* getGraphicsWidow()const{return _gw.get();
}protected:voidinit();virtual void resizeGL(int width,intheight);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<:graphicswindowembedded>_gw;
};
AdapterWidget::AdapterWidget(QWidget*parent,const char* name,const QGLWidget *shareWidget,WindowFlags f):QGLWidget(parent,shareWidget,f)
{
_gw=new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
setFocusPolicy(Qt::ClickFocus);
}void AdapterWidget::resizeGL(int width, intheight)
{
_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::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);
}void AdapterWidget::mouseMoveEvent(QMouseEvent* event)
{
_gw->getEventQueue()->mouseMotion(event->x(),event->y());
}class ViewerQT : public osgViewer::Viewer, publicAdapterWidget
{public:
ViewerQT(QWidget* parent=0,const char * name=0,const QGLWidget * shareWidget=0,WindowFlags f=0):AdapterWidget(parent ,name,shareWidget ,f)
{
getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast(width())/static_cast(height()), 1.0f, 10000.0f);
getCamera()->setGraphicsContext(getGraphicsWindow());
setThreadingModel(osgViewer::Viewer::SingleThreaded);
connect(&_timer,SIGNAL(timeout()),this,SLOT(updateGL()));//并且把它的timeout()连接到适当的槽。当这段时间过去了,它将会发射timeout()信号。
_timer.start(10);//使用start()来开始
}virtual voidpaintGL()
{
frame();
}protected:
QTimer _timer;
};int main(int argc,char**argv)
{
QApplication a(argc,argv);
osg::ref_ptr<:node> loadedModel=osgDB::readNodeFile("cow.osg");
ViewerQT* ViewerWindow=newViewerQT;
ViewerWindow->setCameraManipulator(newosgGA::TrackballManipulator);
ViewerWindow->setSceneData(loadedModel.get());
QMainWindow* mw=newQMainWindow();
mw->showMaximized();
mw->setCentralWidget(ViewerWindow);
mw->show();
a.connect(&a,SIGNAL(lastWindowClosed()),&a,SLOT(quit()));returna.exec();
}