Show
Ignore:
Timestamp:
03/01/11 15:50:25 (4 years ago)
Author:
robert
Message:

Added experiment depth partioning support in the form of an slave camera configuration.

Files:
1 modified

Legend:

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

    r12192 r12196  
    569569} 
    570570 
     571struct MyUpdateSlaveCallback : public osg::View::Slave::UpdateSlaveCallback 
     572{ 
     573    MyUpdateSlaveCallback(double nr, double fr):_zNear(nr),_zFar(fr) {} 
     574     
     575    virtual void updateSlave(osg::View& view, osg::View::Slave& slave) 
     576    { 
     577        slave.updateSlaveImplementation(view); 
     578        osg::Camera* camera = slave._camera.get(); 
     579 
     580        //camera->setProjectionMatrixAsOrtho(-1,1,-1,1,1,1000); 
     581 
     582        if (camera->getProjectionMatrix()(0,3)==0.0 && 
     583            camera->getProjectionMatrix()(1,3)==0.0 && 
     584            camera->getProjectionMatrix()(2,3)==0.0) 
     585        { 
     586            //OSG_NOTICE<<"MyUpdateSlaveCallback othographic camera"<<std::endl; 
     587            double left, right, bottom, top, zNear, zFar; 
     588            camera->getProjectionMatrixAsOrtho(left, right, bottom, top, zNear, zFar); 
     589            camera->setProjectionMatrixAsOrtho(left, right, bottom, top, _zNear, _zFar); 
     590        } 
     591        else 
     592        { 
     593            double left, right, bottom, top, zNear, zFar; 
     594            camera->getProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar); 
     595            //OSG_NOTICE<<"MyUpdateSlaveCallback perpective camera zNear="<<zNear<<", zFar="<<zFar<<std::endl; 
     596            double nr = _zNear / zNear; 
     597            camera->setProjectionMatrixAsFrustum(left * nr, right * nr, bottom * nr, top * nr, _zNear, _zFar); 
     598        } 
     599    } 
     600 
     601    double _zNear, _zFar; 
     602}; 
     603 
     604void setUpViewForDepthPartion(osgViewer::Viewer& viewer, double partitionPosition) 
     605{ 
     606    OSG_NOTICE<<"setUpViewForDepthPartion(Viewer, "<<partitionPosition<<std::endl; 
     607 
     608    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); 
     609    if (!wsi) 
     610    { 
     611        OSG_NOTICE<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl; 
     612        return; 
     613    } 
     614 
     615 
     616    osg::GraphicsContext::ScreenIdentifier si(0); 
     617    si.readDISPLAY(); 
     618 
     619    // displayNum has not been set so reset it to 0. 
     620    if (si.displayNum<0) si.displayNum = 0; 
     621    if (si.screenNum<0) si.screenNum = 0; 
     622 
     623    unsigned int width = 1280, height = 1024; 
     624    wsi->getScreenResolution(si, width, height); 
     625 
     626 
     627    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
     628    traits->screenNum = 0; 
     629    traits->x = 0; 
     630    traits->y = 0; 
     631    traits->width = width; 
     632    traits->height = height; 
     633    traits->windowDecoration = false; 
     634    traits->doubleBuffer = true; 
     635    traits->sharedContext = 0; 
     636 
     637    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
     638    if (gc.valid()) 
     639    { 
     640        osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl; 
     641    } 
     642    else 
     643    { 
     644        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl; 
     645    } 
     646 
     647#if 0 
     648 
     649    osg::Camera* camera = viewer.getCamera(); 
     650 
     651    camera->setGraphicsContext(gc); 
     652    camera->setViewport(new osg::Viewport(0,0, width, height)); 
     653    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
     654    camera->setDrawBuffer(buffer); 
     655    camera->setReadBuffer(buffer); 
     656 
     657#else 
     658 
     659    double zNear = 0.5; 
     660    double zMid = 10.0; 
     661    double zFar = 200.0; 
     662 
     663 
     664    // far camera 
     665    { 
     666        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     667        camera->setGraphicsContext(gc); 
     668        camera->setViewport(new osg::Viewport(0,0, width, height)); 
     669 
     670        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
     671        camera->setDrawBuffer(buffer); 
     672        camera->setReadBuffer(buffer); 
     673 
     674        double scale_z = 1.0; 
     675        double translate_z = 0.0; 
     676 
     677        camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); 
     678        camera->setCullingMode(osg::Camera::ENABLE_ALL_CULLING); 
     679 
     680        viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); 
     681 
     682        osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); 
     683        slave._updateSlaveCallback =  new MyUpdateSlaveCallback(zMid, zFar); 
     684    } 
     685 
     686    // near camera 
     687    { 
     688        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     689        camera->setGraphicsContext(gc); 
     690        camera->setViewport(new osg::Viewport(0,0, width, height)); 
     691 
     692        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
     693        camera->setDrawBuffer(buffer); 
     694        camera->setReadBuffer(buffer); 
     695 
     696        double scale_z = 1.0; 
     697        double translate_z = 0.0; 
     698 
     699        camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); 
     700        camera->setCullingMode(osg::Camera::ENABLE_ALL_CULLING); 
     701        camera->setClearMask(GL_DEPTH_BUFFER_BIT); 
     702 
     703        viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); 
     704 
     705        osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); 
     706        slave._updateSlaveCallback =  new MyUpdateSlaveCallback(zNear, zMid); 
     707    } 
     708 
     709 
     710#endif 
     711 
     712} 
     713 
     714 
    571715int main(int argc, char** argv) 
    572716{ 
     
    626770        return 1; 
    627771    } 
    628      
     772 
     773    double partitionPosition = 0.1; 
     774    if (arguments.read("--depth-partition",partitionPosition) || arguments.read("--dp")) 
     775    { 
     776        setUpViewForDepthPartion(viewer,partitionPosition); 
     777    } 
     778 
    629779    float fov = 0.0; 
    630780    while (arguments.read("--fov",fov)) {}