Show
Ignore:
Timestamp:
09/25/06 18:25:53 (8 years ago)
Author:
robert
Message:

Added bare bones osgGA::SimpleViewer? class to help simplify OSG setup when embedding the OSG
into existing GUI applications, and for one one a single camera, single window is required.

Files:
1 modified

Legend:

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

    r5174 r5592  
    1818#include <osgDB/WriteFile> 
    1919 
    20 #include <osgGA/EventQueue> 
    21 #include <osgGA/EventVisitor> 
     20#include <osgGA/SimpleViewer> 
    2221#include <osgGA/TrackballManipulator> 
    2322#include <osgGA/StateSetManipulator> 
     
    286285 
    287286    // create the view of the scene. 
    288     osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView; 
    289     sceneView->setDefaults(); 
    290     sceneView->setSceneData(loadedModel.get()); 
    291      
    292     // create the event queue, note that Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits, so 
    293     // we need to construct the event queue so that it knows about this convention. 
    294     osg::ref_ptr<osgGA::EventQueue> eventQueue = new osgGA::EventQueue(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); 
    295  
     287    osgGA::SimpleViewer viewer; 
     288    viewer.setSceneData(loadedModel.get()); 
     289     
     290    // create the event queue,  
     291     
    296292    // set up a KeyboardMouse to manage the events comming in from the RenderSurface 
    297293    osg::ref_ptr<Producer::KeyboardMouse>  kbm = new Producer::KeyboardMouse(renderSurface.get()); 
    298294 
    299295    // create a KeyboardMouseCallback to handle the mouse events within this applications 
    300     osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(eventQueue.get()); 
     296    osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(viewer.getEventQueue()); 
    301297 
    302298    // create a tracball manipulator to move the camera around in response to keyboard/mouse events 
    303     osg::ref_ptr<osgGA::TrackballManipulator> cameraManipulator = new osgGA::TrackballManipulator; 
    304  
    305     // keep a list of event handlers to manipulate the application/scene with in response to keyboard/mouse events 
    306     typedef std::list< osg::ref_ptr<osgGA::GUIEventHandler> > EventHandlers; 
    307     EventHandlers eventHandlers; 
    308      
     299    viewer.setCameraManipulator( new osgGA::TrackballManipulator ); 
     300 
    309301    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; 
    310     statesetManipulator->setStateSet(sceneView->getGlobalStateSet()); 
    311     eventHandlers.push_back(statesetManipulator.get()); 
    312     eventHandlers.push_back(new PickHandler(sceneView.get())); 
    313      
    314     // create an event visitor to pass the events down to the scene graph nodes 
    315     osg::ref_ptr<osgGA::EventVisitor> eventVisitor = new osgGA::EventVisitor; 
    316  
    317     // create an action adapter to allow event handlers to request actions from the GUI. 
    318     osg::ref_ptr<MyActionAdapter> actionAdapter = new MyActionAdapter; 
    319  
    320     // record the timer tick at the start of rendering.     
    321     osg::Timer_t start_tick = osg::Timer::instance()->tick(); 
    322      
    323     unsigned int frameNum = 0; 
    324  
    325     eventQueue->setStartTick(start_tick); 
    326  
    327     // set the mouse input range (note WindowSize name in appropriate here so osgGA::GUIEventAdapter API really needs looking at, Robert Osfield, June 2006). 
     302    statesetManipulator->setStateSet(viewer.getSceneView()->getGlobalStateSet()); 
     303    viewer.addEventHandler(statesetManipulator.get()); 
     304 
     305    // add the pick handler 
     306    viewer.addEventHandler(new PickHandler(viewer.getSceneView())); 
     307     
     308    // set the window dimensions  
     309    viewer.getEventQueue()->getCurrentEventState()->setWindowRectangle(100,100,800,600); 
     310 
     311    // set the mouse input range. 
    328312    // Producer defaults to using non-dimensional units, so we pass this onto osgGA, most windowing toolkits use pixel coords so use the window size instead. 
    329     eventQueue->getCurrentEventState()->setWindowSize(-1.0, -1.0, 1.0, 1.0); 
    330  
    331  
    332     // home the manipulator. 
    333     osg::ref_ptr<osgGA::GUIEventAdapter> dummyEvent =  eventQueue->createEvent(); 
    334     cameraManipulator->setNode(sceneView->getSceneData()); 
    335     cameraManipulator->home(*dummyEvent, *actionAdapter); 
    336  
     313    viewer.getEventQueue()->getCurrentEventState()->setInputRange(-1.0, -1.0, 1.0, 1.0); 
     314 
     315    // Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits. 
     316    // we need to construct the event queue so that it knows about this convention. 
     317    viewer.getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); 
     318 
     319    viewer.init(); 
    337320 
    338321    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.) 
    339322    while( renderSurface->isRealized() && !kbmcb->done()) 
    340323    { 
    341         // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly 
    342         osg::FrameStamp* frameStamp = new osg::FrameStamp; 
    343         frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick())); 
    344         frameStamp->setFrameNumber(frameNum++); 
    345          
    346         // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp 
    347         sceneView->setFrameStamp(frameStamp); 
     324        // update the window dimensions, in case the window has been resized. 
     325         viewer.getEventQueue()->windowResize(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight(), false); 
    348326 
    349327        // pass any keyboard mouse events onto the local keyboard mouse callback. 
    350328        kbm->update( *kbmcb ); 
    351329         
    352         // create an event to signal the new frame. 
    353         eventQueue->frame(frameStamp->getReferenceTime()); 
    354  
    355         // get the event since the last frame. 
    356         osgGA::EventQueue::Events events; 
    357         eventQueue->takeEvents(events); 
    358  
    359         if (eventVisitor.valid()) 
    360         { 
    361             eventVisitor->setTraversalNumber(frameStamp->getFrameNumber()); 
    362         } 
    363  
    364         // dispatch the events in order of arrival. 
    365         for(osgGA::EventQueue::Events::iterator event_itr = events.begin(); 
    366             event_itr != events.end(); 
    367             ++event_itr) 
    368         { 
    369             bool handled = false; 
    370  
    371             if (eventVisitor.valid() && sceneView->getSceneData()) 
    372             { 
    373                 eventVisitor->reset(); 
    374                 eventVisitor->addEvent(event_itr->get()); 
    375                 sceneView->getSceneData()->accept(*eventVisitor); 
    376                 if (eventVisitor->getEventHandled()) 
    377                     handled = true; 
    378             } 
    379  
    380             if (cameraManipulator.valid() && !handled) 
    381             { 
    382                 /*handled =*/ cameraManipulator->handle(*(*event_itr), *actionAdapter); 
    383             } 
    384  
    385             for(EventHandlers::iterator handler_itr=eventHandlers.begin(); 
    386                 handler_itr!=eventHandlers.end() && !handled; 
    387                 ++handler_itr) 
    388             {    
    389                 handled = (*handler_itr)->handle(*(*event_itr),*actionAdapter,0,0); 
    390             } 
    391  
    392             // osg::notify(osg::NOTICE)<<"  Handled event "<<(*event_itr)->getTime()<<" "<< handled<<std::endl; 
    393  
    394         } 
    395  
    396  
    397         // update view matrices 
    398         if (cameraManipulator.valid()) 
    399         { 
    400             sceneView->setViewMatrix(cameraManipulator->getInverseMatrix()); 
    401         } 
    402  
    403         // update the viewport dimensions, incase the window has been resized. 
    404         sceneView->setViewport(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight()); 
    405  
    406         // do the update traversal the scene graph - such as updating animations 
    407         sceneView->update(); 
    408          
    409         // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins 
    410         sceneView->cull(); 
    411          
    412         // draw the rendering bins. 
    413         sceneView->draw(); 
     330        viewer.frame(); 
    414331 
    415332        // Swap Buffers