Show
Ignore:
Timestamp:
12/19/06 17:12:29 (8 years ago)
Author:
robert
Message:

Added osgViewer::Viewer / osgViewer::GraphicsWindowsX11 usage.

Files:
1 modified

Legend:

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

    r5822 r5824  
    1717#include <osgDB/Registry> 
    1818 
     19#include <osgViewer/View> 
     20 
    1921#include <map> 
    2022#include <list> 
     
    2224 
    2325 
     26#if !defined(_WIN32) 
     27 
     28#include <osgViewer/GraphicsWindowX11> 
     29#include <osgViewer/Viewer> 
     30 
     31void renderCamera(osg::Camera* camera) 
     32{ 
     33    osg::GraphicsContext* gc = camera->getGraphicsContext(); 
     34    if (!gc) return; 
     35     
     36    osgUtil::SceneView* sceneView = dynamic_cast<osgUtil::SceneView*>(camera->getRenderingCache(0)); 
     37    if (!sceneView) return; 
     38 
     39    gc->makeCurrent(); 
     40     
     41    sceneView->cull(); 
     42    sceneView->draw(); 
     43     
     44    gc->swapBuffers(); 
     45} 
     46 
     47void setUpFrameStamp(osg::Camera* camera, osg::FrameStamp* frameStamp, osg::Node* loadedModel) 
     48{ 
     49    osgUtil::SceneView* sceneView = dynamic_cast<osgUtil::SceneView*>(camera->getRenderingCache(0)); 
     50    if (!sceneView) return; 
     51     
     52    sceneView->setFrameStamp(frameStamp); 
     53     
     54    sceneView->setSceneData(loadedModel); 
     55     
     56} 
     57 
     58int main( int argc, char **argv ) 
     59{ 
     60    if (argc<2)  
     61    { 
     62        std::cout << argv[0] <<": requires filename argument." << std::endl; 
     63        return 1; 
     64    } 
     65 
     66    // load the scene. 
     67    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); 
     68    if (!loadedModel)  
     69    { 
     70        std::cout << argv[0] <<": No data loaded." << std::endl; 
     71        return 1; 
     72    } 
     73     
     74    // initialize the view to look at the center of the scene graph 
     75    const osg::BoundingSphere& bs = loadedModel->getBound(); 
     76    osg::Matrix viewMatrix; 
     77    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f)); 
     78 
     79    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; 
     80    viewer->setUpViewAcrossAllScreens(); 
     81    viewer->realize(); 
     82     
     83    viewer->getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f)); 
     84     
     85    osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp; 
     86 
     87    unsigned int frameNum = 0; 
     88 
     89    osgUtil::UpdateVisitor updateVisitor; 
     90    updateVisitor.setFrameStamp(frameStamp.get()); 
     91     
     92    setUpFrameStamp(viewer->getCamera(), frameStamp.get(), loadedModel.get()); 
     93    for(unsigned i=0; i<viewer->getNumSlaves(); ++i) 
     94    { 
     95        osg::View::Slave& slave = viewer->getSlave(i); 
     96        setUpFrameStamp(slave._camera.get(), frameStamp.get(), loadedModel.get()); 
     97    } 
     98 
     99    // record the timer tick at the start of rendering.     
     100    osg::Timer_t start_tick = osg::Timer::instance()->tick(); 
     101    osg::Timer_t previous_tick = start_tick; 
     102 
     103    while(true) 
     104    { 
     105        osg::Timer_t current_tick = osg::Timer::instance()->tick(); 
     106 
     107        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,current_tick)); 
     108        frameStamp->setFrameNumber(frameNum++); 
     109         
     110        //std::cout<<"Frame rate "<<1.0/osg::Timer::instance()->delta_s(previous_tick,current_tick)<<std::endl; 
     111        previous_tick = current_tick; 
     112 
     113        // do the update traversal. 
     114        loadedModel->accept(updateVisitor); 
     115 
     116 
     117        viewer->getCamera()->setViewMatrix(viewMatrix); 
     118 
     119        viewer->updateSlaves(); 
     120 
     121        if (viewer->getCamera() && viewer->getCamera()->getGraphicsContext()) renderCamera(viewer->getCamera()); 
     122         
     123        for(unsigned i=0; i<viewer->getNumSlaves(); ++i) 
     124        { 
     125            osg::View::Slave& slave = viewer->getSlave(i); 
     126            if (slave._camera.valid() && slave._camera->getGraphicsContext()) renderCamera(slave._camera.get()); 
     127        } 
     128         
     129    } 
     130} 
     131 
     132 
     133#else 
     134 
     135 
     136////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
     137//   
     138//  ORIGINAL osgcamera example code. 
    24139 
    25140//////////////////////////////////////////////////////////////////////////////// 
     
    156271    } 
    157272 
     273 
     274    osg::ref_ptr<osgViewer::View> view = new osgViewer::View; 
     275    view->setUpViewAcrossAllScreens(); 
     276 
     277 
     278 
     279 
    158280    unsigned int numScreens = wsi->getNumScreens(); 
    159     for(unsigned int i=0; i<numScreens; ++i) 
    160     { 
    161         osg::GraphicsContext::ScreenIdentifier si; 
    162         si._screenNum = 0; 
    163          
    164         unsigned int width, height; 
    165         wsi->getScreenResolution(si, width, height); 
    166          
    167         std::cout<<"screen= "<<i<<" width="<<width<<" height="<<height<<std::endl; 
    168     } 
    169          
    170281 
    171282    unsigned int numberCameras = numScreens; 
     
    410521    return 0; 
    411522} 
     523 
     524#endif