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

Revision 13482, 4.3 kB (checked in by robert, 5 hours ago)

Added comment about using -DCMAKE_DISABLE_FINDPACKAGE_* command line.

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