Show
Ignore:
Timestamp:
01/02/07 18:39:31 (7 years ago)
Author:
robert
Message:

Added support for different threading models in osgViewer::Viewer

Files:
1 modified

Legend:

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

    r5870 r5872  
    88#include <osgViewer/Viewer> 
    99#include <osgGA/TrackballManipulator> 
     10#include <osgGA/AnimationPathManipulator> 
    1011#include <iostream> 
    1112 
    12 int main( int argc, char **argv ) 
     13void singleWindowMultipleCameras(osgViewer::Viewer& viewer) 
    1314{ 
    14     if (argc<2)  
    15     { 
    16         std::cout << argv[0] <<": requires filename argument." << std::endl; 
    17         return 1; 
    18     } 
    19  
    20  
    21     osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(2); 
    22     osg::Referenced::setThreadSafeReferenceCounting(true); 
    23  
    24     // load the scene. 
    25     osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); 
    26     if (!loadedModel)  
    27     { 
    28         std::cout << argv[0] <<": No data loaded." << std::endl; 
    29         return 1; 
    30     } 
    31  
    32     osgViewer::Viewer viewer; 
    33      
    34     viewer.setSceneData(loadedModel.get()); 
    35  
    36     viewer.setCameraManipulator(new osgGA::TrackballManipulator()); 
    37     viewer.getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f)); 
    38  
    39 #if 0 
    40  
    4115    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); 
    4216    if (!wsi)  
    4317    { 
    4418        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl; 
    45         return 0; 
     19        return; 
    4620    } 
    4721     
     
    9266    viewer.setUpRenderingSupport(); 
    9367    viewer.assignSceneDataToCameras(); 
     68} 
     69 
     70void multipleWindowMultipleCameras(osgViewer::Viewer& viewer) 
     71{ 
     72    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); 
     73    if (!wsi)  
     74    { 
     75        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl; 
     76        return; 
     77    } 
     78     
     79    unsigned int width, height; 
     80    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); 
     81 
     82 
     83    unsigned int numCameras = 2; 
     84    double aspectRatioScale = (double)numCameras; 
     85    double translate_x = double(numCameras)-1; 
     86    for(unsigned int i=0; i<numCameras;++i, translate_x -= 2.0) 
     87    { 
     88        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
     89        traits->x = (i*width)/numCameras; 
     90        traits->y = 0; 
     91        traits->width = width/numCameras-1; 
     92        traits->height = height; 
     93    #if 1 
     94        traits->windowDecoration = false; 
     95    #else 
     96        traits->windowDecoration = true; 
     97    #endif             
     98        traits->doubleBuffer = true; 
     99        traits->sharedContext = 0; 
     100 
     101        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
     102 
     103        osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get()); 
     104        if (gw) 
     105        { 
     106            osg::notify(osg::NOTICE)<<"  GraphicsWindow has been created successfully."<<gw<<std::endl; 
     107 
     108            gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, traits->width, traits->height ); 
     109        } 
     110        else 
     111        { 
     112            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl; 
     113        } 
     114 
     115        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     116        camera->setGraphicsContext(gc.get()); 
     117        camera->setViewport(new osg::Viewport(0,0, width/numCameras, height)); 
     118        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
     119        camera->setDrawBuffer(buffer); 
     120        camera->setReadBuffer(buffer); 
     121 
     122        viewer.addSlave(camera.get(), osg::Matrix::scale(aspectRatioScale, 1.0, 1.0)*osg::Matrix::translate(translate_x, 0.0, 0.0), osg::Matrix() ); 
     123    } 
     124 
     125    viewer.setUpRenderingSupport(); 
     126    viewer.assignSceneDataToCameras(); 
     127} 
     128 
     129int main( int argc, char **argv ) 
     130{ 
     131    // use an ArgumentParser object to manage the program arguments. 
     132    osg::ArgumentParser arguments(&argc,argv); 
     133 
     134    if (argc<2)  
     135    { 
     136        std::cout << argv[0] <<": requires filename argument." << std::endl; 
     137        return 1; 
     138    } 
     139 
     140 
     141    std::string pathfile; 
     142    osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0; 
     143    while (arguments.read("-p",pathfile)) 
     144    { 
     145        apm = new osgGA::AnimationPathManipulator(pathfile); 
     146        if(!apm.valid() || !(apm->valid()) )  
     147        { 
     148            apm = 0; 
     149        } 
     150    } 
     151 
     152    osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(2); 
     153    osg::Referenced::setThreadSafeReferenceCounting(true); 
     154 
     155    // load the scene. 
     156    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); 
     157    if (!loadedModel)  
     158    { 
     159        std::cout << argv[0] <<": No data loaded." << std::endl; 
     160        return 1; 
     161    } 
     162 
     163    osgViewer::Viewer viewer; 
     164     
     165    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); } 
     166    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerContext); } 
     167    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerCamera); } 
     168 
     169    viewer.setSceneData(loadedModel.get()); 
     170 
     171    if (apm.valid()) viewer.setCameraManipulator(apm.get()); 
     172    else viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); 
     173    viewer.getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f)); 
     174 
     175#if 1 
     176 
     177    // singleWindowMultipleCameras(viewer); 
     178     
     179    multipleWindowMultipleCameras(viewer); 
     180     
    94181     
    95182#else