Show
Ignore:
Timestamp:
10/27/05 15:53:55 (9 years ago)
Author:
robert
Message:

Added option to osgdepthpartion for loading the model specified on the commandline.

Files:
1 modified

Legend:

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

    r4582 r4626  
    77// graphics drawing with Producer::RenderSurface 
    88 
    9 #include <stdio.h> 
    10  
    11 #include <Producer/Camera> 
    12 #include <Producer/CameraConfig> 
    13 #include <Producer/InputArea> 
    14 #include <Producer/KeyboardMouse> 
    15 #include <Producer/Trackball> 
    16  
    17 #include <osg/Timer> 
    18  
    199#include <osgUtil/UpdateVisitor> 
    2010 
     
    2414#include <osg/PositionAttitudeTransform> 
    2515 
    26 #include <osgProducer/OsgCameraGroup> 
    27 #include <osgProducer/OsgSceneHandler> 
     16#include <osgProducer/Viewer> 
    2817 
    2918#include "DepthPartitionNode.h" 
    30  
    31 class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback 
    32 { 
    33     public: 
    34  
    35         MyKeyboardMouseCallback() : 
    36                 Producer::KeyboardMouseCallback(), 
    37                 _mx(0.0f),_my(0.0f),_mbutton(0), 
    38                 _done(false)     
    39                 {} 
    40  
    41         virtual void specialKeyPress( Producer::KeyCharacter key ) 
    42         { 
    43                 if (key==Producer::KeyChar_Escape) 
    44                     shutdown(); 
    45         } 
    46  
    47         virtual void shutdown() 
    48         { 
    49             _done = true;  
    50         } 
    51  
    52         virtual void keyPress( Producer::KeyCharacter ) 
    53         { 
    54         } 
    55  
    56         virtual void mouseMotion( float mx, float my )  
    57         { 
    58                 _mx = mx; 
    59                 _my = my; 
    60         } 
    61         virtual void buttonPress( float mx, float my, unsigned int mbutton )  
    62         { 
    63                 _mx = mx; 
    64                 _my = my; 
    65                 _mbutton |= (1<<(mbutton-1)); 
    66         } 
    67         virtual void buttonRelease( float mx, float my, unsigned int mbutton )  
    68         { 
    69                 _mx = mx; 
    70                 _my = my; 
    71                 _mbutton &= ~(1<<(mbutton-1)); 
    72         } 
    73  
    74         bool done() { return _done; } 
    75         float mx()  { return _mx; } 
    76         float my()  { return _my; } 
    77         unsigned int mbutton()  { return _mbutton; } 
    78  
    79     private: 
    80  
    81         float _mx, _my; 
    82         unsigned int _mbutton; 
    83         bool _done; 
    84 }; 
    8519 
    8620const double r_earth = 6378.137; 
     
    13670    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); 
    13771 
    138     // create the camera group. 
    139     osgProducer::OsgCameraGroup cg(arguments); 
     72    // construct the viewer. 
     73    osgProducer::Viewer viewer(arguments); 
     74 
     75    // set up the value with sensible default event handlers. 
     76    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); 
    14077 
    14178    // if user request help write it out to cout. 
     
    15693    } 
    15794 
    158     // set up the keyboard and mouse handling. 
    159     Producer::InputArea *ia = cg.getCameraConfig()->getInputArea(); 
    160     Producer::KeyboardMouse *kbm = ia ? 
    161                                    (new Producer::KeyboardMouse(ia)) :  
    162                                    (new Producer::KeyboardMouse(cg.getCamera(0)->getRenderSurface())); 
    16395 
     96    bool needToSetHomePosition = false; 
    16497 
    165     osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback; 
     98    // read the scene from the list of file specified commandline args. 
     99    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); 
     100 
     101    // if one hasn't been loaded create an earth and sun test model. 
     102    if (!scene)  
     103    { 
     104        scene = createScene();  
     105        needToSetHomePosition = true; 
     106    } 
    166107     
    167     // register the callback with the keyboard mouse manger. 
    168     kbm->setCallback( kbmcb.get() ); 
    169     kbm->startThread(); 
    170  
    171108    // Create a DepthPartitionNode to manage partitioning of the scene 
    172     DepthPartitionNode *dpn = new DepthPartitionNode; 
    173     osg::Node* scene = createScene(); 
    174     dpn->addChild(scene); 
     109    osg::ref_ptr<DepthPartitionNode> dpn = new DepthPartitionNode; 
     110    dpn->addChild(scene.get()); 
    175111    dpn->setActive(true); // Control whether the node analyzes the scene 
    176112         
    177     // set the scene to render 
    178     cg.setSceneData(dpn); 
     113    // pass the loaded scene graph to the viewer. 
     114    viewer.setSceneData(dpn.get()); 
    179115 
    180     // Set the view to be looking at the earth 
    181     osg::BoundingSphere bs; 
    182     bs._radius = r_earth; 
    183  
    184     osg::ref_ptr<Producer::Trackball> tb = new Producer::Trackball; 
    185     tb->setOrientation( Producer::Trackball::Z_UP ); 
    186     tb->setDistance(bs.radius()*10.0f); 
    187     tb->translate(-bs.center().x(),-bs.center().y(),-bs.center().z()); 
     116    if (needToSetHomePosition) 
     117    { 
     118        viewer.getKeySwitchMatrixManipulator()->setHomePosition(osg::Vec3d(0.0,-5.0*r_earth,0.0),osg::Vec3d(0.0,0.0,0.0),osg::Vec3d(0.0,0.0,1.0)); 
     119    } 
    188120 
    189121    // create the windows and run the threads. 
    190     cg.realize(); 
     122    viewer.realize(); 
    191123 
    192     osgUtil::UpdateVisitor update; 
    193  
    194     while( !kbmcb->done() ) 
     124    while( !viewer.done() ) 
    195125    { 
    196         // syncronize to the when cull and draw threads have completed. 
    197         cg.sync(); 
     126        // wait for all cull and draw threads to complete. 
     127        viewer.sync(); 
    198128 
    199129        // update the scene by traversing it with the the update visitor which will 
    200130        // call all node update callbacks and animations. 
    201         cg.getSceneData()->accept(update); 
    202  
    203         tb->input( kbmcb->mx(), kbmcb->my(), kbmcb->mbutton() ); 
    204  
    205         // update the main producer camera 
    206         cg.setViewByMatrix(tb->getMatrix()); 
     131        viewer.update(); 
    207132          
    208133        // fire off the cull and draw traversals of the scene. 
    209         cg.frame(); 
     134        viewer.frame(); 
     135         
    210136    } 
     137     
     138    // wait for all cull and draw threads to complete before exit. 
     139    viewer.sync(); 
    211140 
    212     // syncronize to the when cull and draw threads have completed. 
    213     cg.sync(); 
     141    // run a clean up frame to delete all OpenGL objects. 
     142    viewer.cleanup_frame(); 
     143 
     144    // wait for all the clean up frame to complete. 
     145    viewer.sync(); 
    214146 
    215147    return 0;