Show
Ignore:
Timestamp:
01/11/07 16:19:59 (7 years ago)
Author:
robert
Message:

Ported following examples to osgViewer:

osgteapot
osgtessellate
osgtext
osgtexture1D
osgtexture2D
osgtexture3D
osgtexturerectangle
osgvertexprogram
osgvolume
osgwindows
osgsimple
osgkeyboardmouse

Files:
1 modified

Legend:

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

    r5832 r5962  
    44// example that provides the user with control over view position with basic picking. 
    55 
    6 #include <Producer/RenderSurface> 
    7 #include <Producer/KeyboardMouse> 
    8 #include <Producer/Trackball> 
    9  
    106#include <osg/Timer> 
    117#include <osg/io_utils> 
     
    2319 
    2420#include <osgViewer/SimpleViewer> 
     21#include <osgViewer/GraphicsWindow> 
    2522 
    2623#include <osgFX/Scribe> 
    2724 
    28 // ----------- Begining of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events. 
    29 class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback 
    30 { 
    31 public: 
    32  
    33     MyKeyboardMouseCallback(osgGA::EventQueue* eventQueue) : 
    34         _done(false), 
    35         _eventQueue(eventQueue) 
    36     { 
    37     } 
    38  
    39     virtual void shutdown() 
    40     { 
    41         _done = true;  
    42     } 
    43  
    44     virtual void specialKeyPress( Producer::KeyCharacter key ) 
    45     { 
    46         if (key==Producer::KeyChar_Escape) 
    47                     shutdown(); 
    48  
    49         _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); 
    50     } 
    51  
    52     virtual void specialKeyRelease( Producer::KeyCharacter key ) 
    53     { 
    54         _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); 
    55     } 
    56  
    57     virtual void keyPress( Producer::KeyCharacter key) 
    58     { 
    59         _eventQueue->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key ); 
    60     } 
    61  
    62     virtual void keyRelease( Producer::KeyCharacter key) 
    63     { 
    64         _eventQueue->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key ); 
    65     } 
    66  
    67     virtual void mouseMotion( float mx, float my )  
    68     { 
    69         _eventQueue->mouseMotion( mx, my ); 
    70     } 
    71  
    72     virtual void buttonPress( float mx, float my, unsigned int mbutton )  
    73     { 
    74         _eventQueue->mouseButtonPress(mx, my, mbutton); 
    75     } 
    76      
    77     virtual void buttonRelease( float mx, float my, unsigned int mbutton )  
    78     { 
    79         _eventQueue->mouseButtonRelease(mx, my, mbutton); 
    80     } 
    81  
    82     bool done() { return _done; } 
    83  
    84 private: 
    85  
    86     bool                                _done; 
    87     osg::ref_ptr<osgGA::EventQueue>     _eventQueue; 
    88 }; 
    89  
    90 class MyActionAdapter : public osgGA::GUIActionAdapter, public osg::Referenced 
    91 { 
    92 public: 
    93     // Override from GUIActionAdapter 
    94     virtual void requestRedraw() {} 
    95  
    96     // Override from GUIActionAdapter 
    97     virtual void requestContinuousUpdate(bool =true) {} 
    98  
    99     // Override from GUIActionAdapter 
    100     virtual void requestWarpPointer(float ,float ) {} 
    101      
    102 }; 
    103  
    104 // ----------- End of glue classes to adapter Producer's keyboard mouse events to osgGA's abstraction events. 
    105  
     25#include <iostream> 
    10626 
    10727class CreateModelToSaveVisitor : public osg::NodeVisitor 
     
    13656}; 
    13757 
     58class ExitHandler : public osgGA::GUIEventHandler  
     59{ 
     60public:  
     61 
     62    ExitHandler(): 
     63        _done(false) {} 
     64         
     65    bool done() const { return _done; }     
     66 
     67    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) 
     68    { 
     69        switch(ea.getEventType()) 
     70        { 
     71            case(osgGA::GUIEventAdapter::KEYUP): 
     72            { 
     73                if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Escape) 
     74                { 
     75                    _done = true; 
     76                } 
     77                return false; 
     78            } 
     79            case(osgGA::GUIEventAdapter::CLOSE_WINDOW): 
     80            case(osgGA::GUIEventAdapter::QUIT_APPLICATION): 
     81            { 
     82                _done = true; 
     83            } 
     84            default: break; 
     85        } 
     86         
     87        return false; 
     88    } 
     89     
     90    bool _done; 
     91}; 
     92 
     93 
     94 
    13895// class to handle events with a pick 
    139 class PickHandler : public osgGA::GUIEventHandler { 
     96class PickHandler : public osgGA::GUIEventHandler  
     97{ 
    14098public:  
    14199 
     
    331289     
    332290    // create the window to draw to. 
    333     osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface; 
    334     renderSurface->setWindowName("osgkeyboardmouse"); 
    335     renderSurface->setWindowRectangle(100,100,800,600); 
    336     renderSurface->useBorder(true); 
    337     renderSurface->realize(); 
    338      
     291    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
     292    traits->x = 200; 
     293    traits->y = 200; 
     294    traits->width = 800; 
     295    traits->height = 600; 
     296    traits->windowDecoration = true; 
     297    traits->doubleBuffer = true; 
     298    traits->sharedContext = 0; 
     299 
     300    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
     301    osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get()); 
     302    if (!gw) 
     303    { 
     304        osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl; 
     305        return 1; 
     306    } 
     307 
     308    gw->realize(); 
     309    gw->makeCurrent(); 
    339310 
    340311    // create the view of the scene. 
     
    342313    viewer.setSceneData(loadedModel.get()); 
    343314     
    344     // set up a KeyboardMouse to manage the events comming in from the RenderSurface 
    345     osg::ref_ptr<Producer::KeyboardMouse>  kbm = new Producer::KeyboardMouse(renderSurface.get()); 
    346  
    347     // create a KeyboardMouseCallback to handle the mouse events within this applications 
    348     osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback(viewer.getEventQueue()); 
     315    viewer.setEventQueue(gw->getEventQueue()); 
     316    viewer.getEventQueue()->windowResize(traits->x,traits->y,traits->width,traits->height); 
    349317 
    350318    // create a tracball manipulator to move the camera around in response to keyboard/mouse events 
     
    357325    // add the pick handler 
    358326    viewer.addEventHandler(new PickHandler()); 
    359      
    360     // set the window dimensions  
    361     viewer.getEventQueue()->getCurrentEventState()->setWindowRectangle(100,100,800,600); 
    362  
    363     // set the mouse input range. 
    364     // 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. 
    365     viewer.getEventQueue()->setUseFixedMouseInputRange(true); 
    366     viewer.getEventQueue()->setMouseInputRange(-1.0, -1.0, 1.0, 1.0); 
    367  
    368     // Producer has the y axis increase upwards, like OpenGL, and contary to most Windowing toolkits. 
    369     // we need to construct the event queue so that it knows about this convention. 
    370     viewer.getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); 
    371      
     327 
     328 
     329    // add the exit handler' 
     330    ExitHandler* exitHandler = new ExitHandler; 
     331    viewer.addEventHandler(exitHandler); 
    372332 
    373333    viewer.init(); 
    374334 
    375335    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.) 
    376     while( renderSurface->isRealized() && !kbmcb->done()) 
    377     { 
    378         // update the window dimensions, in case the window has been resized. 
    379          viewer.getEventQueue()->windowResize(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight()); 
    380  
    381         // pass any keyboard mouse events onto the local keyboard mouse callback. 
    382         kbm->update( *kbmcb ); 
     336    while( gw->isRealized() && !exitHandler->done()) 
     337    { 
     338        gw->checkEvents(); 
    383339         
    384340        viewer.frame(); 
    385341 
    386342        // Swap Buffers 
    387         renderSurface->swapBuffers(); 
     343        gw->swapBuffers(); 
    388344    } 
    389345