Show
Ignore:
Timestamp:
08/17/05 18:13:06 (9 years ago)
Author:
robert
Message:

Replaced osgUtil::SceneView? usage by osg::CameraNode?

Files:
1 modified

Legend:

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

    r4435 r4439  
    66#include <osg/Timer> 
    77#include <osg/GraphicsContext> 
    8 #include <osgUtil/SceneView> 
     8 
     9#include <osgUtil/UpdateVisitor> 
     10#include <osgUtil/CullVisitor> 
     11 
    912#include <osgDB/ReadFile> 
     13 
    1014#include <iostream> 
     15 
     16#if 1 
    1117 
    1218int main( int argc, char **argv ) 
     
    4753 
    4854    // create the view of the scene. 
    49     osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView; 
    50     sceneView->setDefaults(); 
    51     sceneView->setSceneData(loadedModel.get()); 
     55    osg::ref_ptr<osg::CameraNode> camera = new osg::CameraNode; 
     56    camera->addChild(loadedModel.get()); 
    5257 
    5358    // initialize the view to look at the center of the scene graph 
     
    6368    // make the graphics context current 
    6469    gfxc->makeCurrent(); 
     70     
     71    osg::ref_ptr<osgUtil::UpdateVisitor> updateVisitor = new osgUtil::UpdateVisitor; 
     72    osg::ref_ptr<osgUtil::CullVisitor> cullVisitor = new osgUtil::CullVisitor; 
     73 
     74    cullVisitor->setRenderGraph(new osgUtil::RenderGraph); 
     75 
     76    osg::ref_ptr<osgUtil::RenderStage> renderStage = new osgUtil::RenderStage; 
     77    cullVisitor->setRenderStage(renderStage.get()); 
    6578 
    6679    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.) 
     
    7285        frameStamp->setFrameNumber(frameNum++); 
    7386         
     87        updateVisitor->reset(); 
     88        cullVisitor->reset(); 
     89         
     90        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp 
     91        updateVisitor->setFrameStamp(frameStamp.get()); 
     92        updateVisitor->setTraversalNumber(frameStamp->getFrameNumber()); 
     93 
     94        cullVisitor->setFrameStamp(frameStamp.get()); 
     95        cullVisitor->setTraversalNumber(frameStamp->getFrameNumber()); 
     96         
     97        // update the viewport dimensions, incase the window has been resized. 
     98        camera->setViewport(0,0,traits->_width,traits->_height); 
     99        renderStage->setViewport(camera->getViewport()); 
     100         
     101        cullVisitor->pushViewport(camera->getViewport()); 
     102         
     103        // set the view 
     104        camera->setViewMatrix(viewMatrix); 
     105 
     106        // do the update traversal the scene graph - such as updating animations 
     107        camera->accept(*updateVisitor); 
     108         
     109        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins 
     110        camera->accept(*cullVisitor); 
     111         
     112        cullVisitor->popViewport(); 
     113         
     114        // draw traversal 
     115        osgUtil::RenderLeaf* previous = NULL; 
     116        renderStage->draw(*(gfxc->getState()), previous); 
     117 
     118        // Swap Buffers 
     119        gfxc->swapBuffers(); 
     120    } 
     121 
     122    return 0; 
     123} 
     124 
     125#else 
     126 
     127int main( int argc, char **argv ) 
     128{ 
     129    if (argc<2)  
     130    { 
     131        std::cout << argv[0] <<": requires filename argument." << std::endl; 
     132        return 1; 
     133    } 
     134 
     135    // load the scene. 
     136    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]); 
     137    if (!loadedModel)  
     138    { 
     139        std::cout << argv[0] <<": No data loaded." << std::endl; 
     140        return 1; 
     141    } 
     142     
     143    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
     144    traits->_windowName = "osgcamera"; 
     145    traits->_x = 100; 
     146    traits->_y = 100; 
     147    traits->_width = 800; 
     148    traits->_height = 800; 
     149    traits->_windowDecoration = true; 
     150    traits->_doubleBuffer = true; 
     151     
     152    osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
     153     
     154    if (!gfxc) 
     155    { 
     156        std::cout<<"Unable to create window."<<std::endl; 
     157        return 1; 
     158    } 
     159     
     160    // realise the window 
     161    gfxc->realize(); 
     162 
     163    // create the view of the scene. 
     164    osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView; 
     165    sceneView->setDefaults(); 
     166    sceneView->setSceneData(loadedModel.get()); 
     167 
     168    // initialize the view to look at the center of the scene graph 
     169    const osg::BoundingSphere& bs = loadedModel->getBound(); 
     170    osg::Matrix viewMatrix; 
     171    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f)); 
     172 
     173    // record the timer tick at the start of rendering.     
     174    osg::Timer_t start_tick = osg::Timer::instance()->tick(); 
     175     
     176    unsigned int frameNum = 0; 
     177     
     178    // make the graphics context current 
     179    gfxc->makeCurrent(); 
     180 
     181    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.) 
     182    while( gfxc->isRealized() ) 
     183    { 
     184        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly 
     185        osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp; 
     186        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick())); 
     187        frameStamp->setFrameNumber(frameNum++); 
     188         
    74189        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp 
    75190        sceneView->setFrameStamp(frameStamp.get()); 
     
    96211    return 0; 
    97212} 
     213 
     214#endif