root/OpenSceneGraph/trunk/examples/osgviewerQt/osgviewerQt.cpp @ 12529

Revision 12529, 4.2 kB (checked in by robert, 4 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
Line 
1#include <QtCore/QTimer>
2#include <QtGui/QApplication>
3#include <QtGui/QGridLayout>
4
5#include <osgViewer/CompositeViewer>
6#include <osgViewer/ViewerEventHandlers>
7
8#include <osgGA/TrackballManipulator>
9
10#include <osgDB/ReadFile>
11
12#include <osgQt/GraphicsWindowQt>
13
14#include <iostream>
15
16class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
17{
18public:
19    ViewerWidget(osgViewer::ViewerBase::ThreadingModel threadingModel=osgViewer::CompositeViewer::SingleThreaded) : QWidget()
20    {
21        setThreadingModel(threadingModel);
22
23        QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("cow.osgt") );
24        QWidget* widget2 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("glider.osgt") );
25        QWidget* widget3 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("axes.osgt") );
26        QWidget* widget4 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("fountain.osgt") );
27        QWidget* popupWidget = addViewWidget( createCamera(900,100,320,240,"Popup window",true),
28                                            osgDB::readNodeFile("dumptruck.osgt") );
29        popupWidget->show();
30
31        QGridLayout* grid = new QGridLayout;
32        grid->addWidget( widget1, 0, 0 );
33        grid->addWidget( widget2, 0, 1 );
34        grid->addWidget( widget3, 1, 0 );
35        grid->addWidget( widget4, 1, 1 );
36        setLayout( grid );
37
38        connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
39        _timer.start( 10 );
40    }
41   
42    QWidget* addViewWidget( osg::Camera* camera, osg::Node* scene )
43    {
44        osgViewer::View* view = new osgViewer::View;
45        view->setCamera( camera );
46        addView( view );
47       
48        view->setSceneData( scene );
49        view->addEventHandler( new osgViewer::StatsHandler );
50        view->setCameraManipulator( new osgGA::TrackballManipulator );
51       
52        osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
53        return gw ? gw->getGLWidget() : NULL;
54    }
55   
56    osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
57    {
58        osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
59        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
60        traits->windowName = name;
61        traits->windowDecoration = windowDecoration;
62        traits->x = x;
63        traits->y = y;
64        traits->width = w;
65        traits->height = h;
66        traits->doubleBuffer = true;
67        traits->alpha = ds->getMinimumNumAlphaBits();
68        traits->stencil = ds->getMinimumNumStencilBits();
69        traits->sampleBuffers = ds->getMultiSamples();
70        traits->samples = ds->getNumMultiSamples();
71       
72        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
73        camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );
74       
75        camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
76        camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
77        camera->setProjectionMatrixAsPerspective(
78            30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
79        return camera.release();
80    }
81   
82    virtual void paintEvent( QPaintEvent* event )
83    { frame(); }
84
85protected:
86   
87    QTimer _timer;
88};
89
90int main( int argc, char** argv )
91{
92    osg::ArgumentParser arguments(&argc, argv);
93
94    osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
95    while (arguments.read("--SingleThreaded")) threadingModel = osgViewer::ViewerBase::SingleThreaded;
96    while (arguments.read("--CullDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
97    while (arguments.read("--DrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::DrawThreadPerContext;
98    while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext;
99   
100    QApplication app(argc, argv);
101    ViewerWidget* viewWidget = new ViewerWidget(threadingModel);
102    viewWidget->setGeometry( 100, 100, 800, 600 );
103    viewWidget->show();
104    return app.exec();
105}
Note: See TracBrowser for help on using the browser.