Show
Ignore:
Timestamp:
06/02/07 14:03:24 (8 years ago)
Author:
robert
Message:

Ported GLUT example to using osgViewer::Viewer rather than SimpleViewer? by using
a local GraphicsWindowEmbedded? class to help the adaption.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgviewerGLUT/osgviewerGLUT.cpp

    r6813 r6814  
    1414#endif 
    1515 
    16 #include <osgViewer/SimpleViewer> 
     16#include <osgViewer/Viewer> 
     17#include <osgViewer/StatsHandler> 
    1718#include <osgGA/TrackballManipulator> 
    1819#include <osgDB/ReadFile> 
    1920 
    20 osg::ref_ptr<osgViewer::SimpleViewer> viewer; 
     21osg::ref_ptr<osgViewer::Viewer> viewer; 
     22osg::ref_ptr<osgViewer::GraphicsWindow> window; 
    2123 
    2224void display(void) 
     
    3335{ 
    3436    // update the window dimensions, in case the window has been resized. 
    35     viewer->getEventQueue()->windowResize(0, 0, w, h ); 
     37    window->resized(window->getTraits()->x, window->getTraits()->y, w, h); 
     38    window->getEventQueue()->windowResize(window->getTraits()->x, window->getTraits()->y, w, h ); 
    3639} 
    3740 
    3841void mousebutton( int button, int state, int x, int y ) 
    3942{ 
    40     if (state==0) viewer->getEventQueue()->mouseButtonPress( x, y, button+1 ); 
    41     else viewer->getEventQueue()->mouseButtonRelease( x, y, button+1 ); 
     43    if (state==0) window->getEventQueue()->mouseButtonPress( x, y, button+1 ); 
     44    else window->getEventQueue()->mouseButtonRelease( x, y, button+1 ); 
    4245} 
    4346 
    4447void mousemove( int x, int y ) 
    4548{ 
    46     viewer->getEventQueue()->mouseMotion( x, y ); 
     49    window->getEventQueue()->mouseMotion( x, y ); 
    4750} 
    4851 
     
    5558            break; 
    5659        default: 
    57             viewer->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); 
    58             viewer->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); 
     60            window->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); 
     61            window->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); 
    5962            break; 
    6063    } 
    6164} 
     65 
     66class GraphicsWindowEmbedded : public osgViewer::GraphicsWindow 
     67{ 
     68public: 
     69     
     70    GraphicsWindowEmbedded(osg::GraphicsContext::Traits* traits=0) 
     71    { 
     72            _traits = traits; 
     73 
     74            init(); 
     75             
     76            if (valid()) 
     77            { 
     78                setState( new osg::State ); 
     79                getState()->setGraphicsContext(this); 
     80 
     81                if (_traits.valid() && _traits->sharedContext) 
     82                { 
     83                    getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); 
     84                    incrementContextIDUsageCount( getState()->getContextID() );    
     85                } 
     86                else 
     87                { 
     88                    getState()->setContextID( osg::GraphicsContext::createNewContextID() ); 
     89                } 
     90            } 
     91    } 
     92 
     93    void init() {} 
     94     
     95    virtual bool valid() const { return true; } 
     96    virtual bool realizeImplementation() { return true; } 
     97    virtual bool isRealizedImplementation() const  { return true; } 
     98    virtual void closeImplementation() {} 
     99    virtual bool makeCurrentImplementation() {} 
     100    virtual bool releaseContextImplementation() {} 
     101    virtual void swapBuffersImplementation() {} 
     102    virtual void grabFocus() {} 
     103    virtual void grabFocusIfPointerInWindow() {} 
     104}; 
    62105 
    63106int main( int argc, char **argv ) 
     
    89132    glutKeyboardFunc( keyboard ); 
    90133 
     134    osg::GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits; 
     135    traits->x = 100; 
     136    traits->y = 100; 
     137    traits->width = 800; 
     138    traits->height = 600; 
     139    window = new GraphicsWindowEmbedded(traits); 
     140 
    91141    // create the view of the scene. 
    92     viewer = new osgViewer::SimpleViewer; 
     142    viewer = new osgViewer::Viewer; 
     143    viewer->getCamera()->setGraphicsContext(window.get()); 
     144    viewer->getCamera()->setViewport(new osg::Viewport(0,0,800,600)); 
     145    viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); 
     146 
    93147    viewer->setSceneData(loadedModel.get()); 
    94148    viewer->setCameraManipulator(new osgGA::TrackballManipulator); 
     149    viewer->addEventHandler(new osgViewer::StatsHandler); 
     150    viewer->realize(); 
    95151 
    96152    glutMainLoop(); 
     153     
    97154    return 0; 
    98155}