Show
Ignore:
Timestamp:
03/03/11 13:33:57 (3 years ago)
Author:
robert
Message:

Introduced a new more generic function for settings up the depth partitioning of viewer cameras.

Files:
1 modified

Legend:

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

    r12201 r12202  
    721721}; 
    722722 
    723 void setUpViewForDepthPartion(osgViewer::Viewer& viewer, double partitionPosition) 
     723 
     724bool setUpDepthPartitionForCamera(osg::View& view, osg::Camera* cameraToPartition, DepthPartitionSettings* dps) 
    724725{ 
    725     OSG_NOTICE<<"setUpViewForDepthPartion(Viewer, "<<partitionPosition<<")"<<std::endl; 
    726  
    727     osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); 
    728     if (!wsi) 
    729     { 
    730         OSG_NOTICE<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl; 
    731         return; 
    732     } 
    733  
    734  
    735     osg::GraphicsContext::ScreenIdentifier si(0); 
    736     si.readDISPLAY(); 
    737  
    738     // displayNum has not been set so reset it to 0. 
    739     if (si.displayNum<0) si.displayNum = 0; 
    740     if (si.screenNum<0) si.screenNum = 0; 
    741  
    742     unsigned int width = 1280, height = 1024; 
    743     wsi->getScreenResolution(si, width, height); 
    744  
    745  
    746     osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
    747     traits->screenNum = 0; 
    748     traits->x = 0; 
    749     traits->y = 0; 
    750     traits->width = width; 
    751     traits->height = height; 
    752     traits->windowDecoration = false; 
    753     traits->doubleBuffer = true; 
    754     traits->sharedContext = 0; 
    755  
    756     osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
    757     if (gc.valid()) 
    758     { 
    759         osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl; 
     726    osg::ref_ptr<osg::GraphicsContext> context = cameraToPartition->getGraphicsContext(); 
     727    if (!context) return false; 
     728 
     729    osg::ref_ptr<osg::Viewport> viewport = cameraToPartition->getViewport(); 
     730    if (!viewport) return false; 
     731 
     732    cameraToPartition->setGraphicsContext(0); 
     733    cameraToPartition->setViewport(0); 
     734 
     735    bool useMastersSceneData = true; 
     736    osg::Matrixd projectionOffset; 
     737    osg::Matrixd viewOffset; 
     738 
     739    if (view.getCamera()==cameraToPartition) 
     740    { 
     741        // replace main camera with depth partition cameras 
     742        OSG_NOTICE<<"Replacing main Camera"<<std::endl; 
    760743    } 
    761744    else 
    762745    { 
    763         osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl; 
    764     } 
    765  
    766     osg::ref_ptr<DepthPartitionSettings> dps = new DepthPartitionSettings; 
    767  
    768     dps->_mode = DepthPartitionSettings::BOUNDING_VOLUME; 
    769     dps->_zNear = 0.5; 
    770     dps->_zMid = partitionPosition; 
    771     dps->_zFar = 1000.0; 
     746        unsigned int i = view.findSlaveIndexForCamera(cameraToPartition); 
     747        if (i>=view.getNumSlaves()) return false; 
     748 
     749        osg::View::Slave& slave = view.getSlave(i); 
     750 
     751        useMastersSceneData = slave._useMastersSceneData; 
     752        projectionOffset = slave._projectionOffset; 
     753        viewOffset = slave._viewOffset; 
     754 
     755        OSG_NOTICE<<"Replacing slave Camera"<<i<<std::endl; 
     756        view.removeSlave(i); 
     757    } 
    772758 
    773759    // far camera 
    774760    { 
    775761        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
    776         camera->setGraphicsContext(gc.get()); 
    777         camera->setViewport(new osg::Viewport(0,0, width, height)); 
    778  
    779         GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
    780         camera->setDrawBuffer(buffer); 
    781         camera->setReadBuffer(buffer); 
    782  
    783         double scale_z = 1.0; 
    784         double translate_z = 0.0; 
     762        camera->setGraphicsContext(context.get()); 
     763        camera->setViewport(viewport.get()); 
     764 
     765        camera->setDrawBuffer(cameraToPartition->getDrawBuffer()); 
     766        camera->setReadBuffer(cameraToPartition->getReadBuffer()); 
    785767 
    786768        camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); 
    787769        camera->setCullingMode(osg::Camera::ENABLE_ALL_CULLING); 
    788770 
    789         viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); 
    790  
    791         osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); 
    792         slave._updateSlaveCallback =  new MyUpdateSlaveCallback(dps.get(), 1); 
     771        view.addSlave(camera.get()); 
     772 
     773        osg::View::Slave& slave = view.getSlave(view.getNumSlaves()-1); 
     774 
     775        slave._useMastersSceneData = useMastersSceneData; 
     776        slave._projectionOffset = projectionOffset; 
     777        slave._viewOffset = viewOffset; 
     778        slave._updateSlaveCallback =  new MyUpdateSlaveCallback(dps, 1); 
    793779    } 
    794780 
     
    796782    { 
    797783        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
    798         camera->setGraphicsContext(gc.get()); 
    799         camera->setViewport(new osg::Viewport(0,0, width, height)); 
    800  
    801         GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
    802         camera->setDrawBuffer(buffer); 
    803         camera->setReadBuffer(buffer); 
    804  
    805         double scale_z = 1.0; 
    806         double translate_z = 0.0; 
     784        camera->setGraphicsContext(context.get()); 
     785        camera->setViewport(viewport.get()); 
     786 
     787        camera->setDrawBuffer(cameraToPartition->getDrawBuffer()); 
     788        camera->setReadBuffer(cameraToPartition->getReadBuffer()); 
    807789 
    808790        camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); 
     
    810792        camera->setClearMask(GL_DEPTH_BUFFER_BIT); 
    811793 
    812         viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); 
    813  
    814         osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); 
    815         slave._updateSlaveCallback =  new MyUpdateSlaveCallback(dps.get(), 0); 
    816     } 
     794        view.addSlave(camera.get()); 
     795 
     796        osg::View::Slave& slave = view.getSlave(view.getNumSlaves()-1); 
     797        slave._useMastersSceneData = useMastersSceneData; 
     798        slave._projectionOffset = projectionOffset; 
     799        slave._viewOffset = viewOffset; 
     800        slave._updateSlaveCallback =  new MyUpdateSlaveCallback(dps, 0); 
     801    } 
     802 
     803    return true; 
     804} 
     805 
     806 
     807typedef std::list< osg::ref_ptr<osg::Camera> > Cameras; 
     808 
     809Cameras getActiveCameras(osg::View& view) 
     810{ 
     811    Cameras activeCameras; 
     812 
     813    if (view.getCamera() && view.getCamera()->getGraphicsContext()) 
     814    { 
     815        activeCameras.push_back(view.getCamera()); 
     816    } 
     817 
     818    for(unsigned int i=0; i<view.getNumSlaves(); ++i) 
     819    { 
     820        osg::View::Slave& slave = view.getSlave(i); 
     821        if (slave._camera.valid() && slave._camera->getGraphicsContext()) 
     822        { 
     823            activeCameras.push_back(slave._camera.get()); 
     824        } 
     825    } 
     826    return activeCameras; 
     827} 
     828 
     829 
     830bool setUpDepthPartition(osgViewer::View& view, DepthPartitionSettings* dsp) 
     831{ 
     832    OSG_NOTICE<<"setUpDepthPartition(View,..)"<<std::endl; 
     833 
     834    Cameras originalCameras = getActiveCameras(view); 
     835    if (originalCameras.empty()) 
     836    { 
     837        OSG_NOTICE<<"setUpDepthPartition(View,..) doing view.setUpViewAcrossAllScreens()"<<std::endl; 
     838        view.setUpViewAcrossAllScreens(); 
     839 
     840        originalCameras = getActiveCameras(view); 
     841        if (originalCameras.empty()) 
     842        { 
     843            OSG_NOTICE<<"setUpDepthPartition(View,..) Unable to set up windows for viewer."<<std::endl; 
     844            return false; 
     845        } 
     846    } 
     847 
     848    bool threadsWereRunning = view.getViewerBase()->areThreadsRunning(); 
     849    if (threadsWereRunning) view.getViewerBase()->stopThreading(); 
     850 
     851    for(Cameras::iterator itr = originalCameras.begin(); 
     852        itr != originalCameras.end(); 
     853        ++itr) 
     854    { 
     855        setUpDepthPartitionForCamera(view, itr->get(), dsp); 
     856    } 
     857 
     858    if (threadsWereRunning) view.getViewerBase()->startThreading(); 
     859     
     860    return true; 
    817861} 
    818862 
     
    879923    if (arguments.read("--depth-partition",partitionPosition) || arguments.read("--dp")) 
    880924    { 
    881         setUpViewForDepthPartion(viewer,partitionPosition); 
     925        // set up depth partitioning 
     926        osg::ref_ptr<DepthPartitionSettings> dps = new DepthPartitionSettings; 
     927        dps->_mode = DepthPartitionSettings::BOUNDING_VOLUME; 
     928        dps->_zNear = 0.5; 
     929        dps->_zMid = partitionPosition; 
     930        dps->_zFar = 1000.0; 
     931        setUpDepthPartition(viewer, dps.get()); 
    882932    } 
    883933 
     
    11641214    // create the windows and run the threads. 
    11651215    viewer.realize(); 
    1166      
     1216 
    11671217    if (fov!=0.0) 
    11681218    {