2013-06-10 22:34:25 +08:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QGridLayout>
|
2011-04-19 19:40:22 +08:00
|
|
|
|
|
|
|
#include <osgViewer/CompositeViewer>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
|
|
|
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <osgQt/GraphicsWindowQt>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
|
|
|
|
{
|
|
|
|
public:
|
2011-05-16 17:15:02 +08:00
|
|
|
ViewerWidget(osgViewer::ViewerBase::ThreadingModel threadingModel=osgViewer::CompositeViewer::SingleThreaded) : QWidget()
|
2011-04-19 19:40:22 +08:00
|
|
|
{
|
2011-05-16 17:15:02 +08:00
|
|
|
setThreadingModel(threadingModel);
|
2013-06-10 22:34:25 +08:00
|
|
|
|
|
|
|
// disable the default setting of viewer.done() by pressing Escape.
|
|
|
|
setKeyEventSetsDone(0);
|
2011-04-19 19:40:22 +08:00
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("cow.osgt") );
|
|
|
|
QWidget* widget2 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("glider.osgt") );
|
|
|
|
QWidget* widget3 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("axes.osgt") );
|
|
|
|
QWidget* widget4 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("fountain.osgt") );
|
2011-04-19 19:40:22 +08:00
|
|
|
QWidget* popupWidget = addViewWidget( createCamera(900,100,320,240,"Popup window",true),
|
2011-06-15 00:54:20 +08:00
|
|
|
osgDB::readNodeFile("dumptruck.osgt") );
|
2011-04-19 19:40:22 +08:00
|
|
|
popupWidget->show();
|
|
|
|
|
|
|
|
QGridLayout* grid = new QGridLayout;
|
|
|
|
grid->addWidget( widget1, 0, 0 );
|
|
|
|
grid->addWidget( widget2, 0, 1 );
|
|
|
|
grid->addWidget( widget3, 1, 0 );
|
|
|
|
grid->addWidget( widget4, 1, 1 );
|
|
|
|
setLayout( grid );
|
|
|
|
|
|
|
|
connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
|
|
|
|
_timer.start( 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* addViewWidget( osg::Camera* camera, osg::Node* scene )
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
|
|
|
view->setCamera( camera );
|
|
|
|
addView( view );
|
|
|
|
|
|
|
|
view->setSceneData( scene );
|
|
|
|
view->addEventHandler( new osgViewer::StatsHandler );
|
|
|
|
view->setCameraManipulator( new osgGA::TrackballManipulator );
|
|
|
|
|
|
|
|
osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
|
2011-05-16 17:15:02 +08:00
|
|
|
return gw ? gw->getGLWidget() : NULL;
|
2011-04-19 19:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
|
|
|
|
{
|
|
|
|
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
|
|
|
|
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
|
|
|
traits->windowName = name;
|
|
|
|
traits->windowDecoration = windowDecoration;
|
|
|
|
traits->x = x;
|
|
|
|
traits->y = y;
|
|
|
|
traits->width = w;
|
|
|
|
traits->height = h;
|
|
|
|
traits->doubleBuffer = true;
|
|
|
|
traits->alpha = ds->getMinimumNumAlphaBits();
|
|
|
|
traits->stencil = ds->getMinimumNumStencilBits();
|
|
|
|
traits->sampleBuffers = ds->getMultiSamples();
|
|
|
|
traits->samples = ds->getNumMultiSamples();
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
|
|
|
|
camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
return camera.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void paintEvent( QPaintEvent* event )
|
|
|
|
{ frame(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
QTimer _timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
2011-05-16 17:15:02 +08:00
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
|
|
|
osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
|
|
|
|
while (arguments.read("--SingleThreaded")) threadingModel = osgViewer::ViewerBase::SingleThreaded;
|
|
|
|
while (arguments.read("--CullDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
|
|
|
|
while (arguments.read("--DrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::DrawThreadPerContext;
|
|
|
|
while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext;
|
|
|
|
|
2011-04-19 19:40:22 +08:00
|
|
|
QApplication app(argc, argv);
|
2011-05-16 17:15:02 +08:00
|
|
|
ViewerWidget* viewWidget = new ViewerWidget(threadingModel);
|
2011-04-19 19:40:22 +08:00
|
|
|
viewWidget->setGeometry( 100, 100, 800, 600 );
|
|
|
|
viewWidget->show();
|
|
|
|
return app.exec();
|
|
|
|
}
|