Show
Ignore:
Timestamp:
11/02/06 13:27:15 (7 years ago)
Author:
robert
Message:

Moved SimpleViewer? and GraphicsWindow? into their own osgViewer library, updated simpleviewer examples to reflect this change

Files:
1 modified

Legend:

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

    r5677 r5679  
    11// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL. 
    22// 
    3 // Simple example of use of Producer::RenderSurface + KeyboardMouseCallback + SceneView 
     3// Simple example of use of Producer::RenderSurface + KeyboardMouseCallback + SimpleViewer 
    44// example that provides the user with control over view position with basic picking. 
    55 
     
    1212#include <osg/observer_ptr> 
    1313 
    14 #include <osgUtil/SceneView> 
    15 #include <osgUtil/IntersectVisitor> 
    1614#include <osgUtil/IntersectionVisitor> 
    1715 
     
    1917#include <osgDB/WriteFile> 
    2018 
    21 #include <osgGA/SimpleViewer> 
    2219#include <osgGA/TrackballManipulator> 
    2320#include <osgGA/StateSetManipulator> 
     21 
     22#include <osgViewer/SimpleViewer> 
    2423 
    2524#include <osgFX/Scribe> 
     
    139138public:  
    140139 
    141     PickHandler(osgUtil::SceneView* sceneView): 
    142         _sceneView(sceneView), 
     140    PickHandler(): 
    143141        _mx(0.0),_my(0.0) {} 
    144142 
    145143    ~PickHandler() {} 
    146144 
    147     bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) 
    148     { 
     145    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) 
     146    { 
     147        osgViewer::SimpleViewer* viewer = dynamic_cast<osgViewer::SimpleViewer*>(&aa); 
     148 
    149149        switch(ea.getEventType()) 
    150150        { 
    151151            case(osgGA::GUIEventAdapter::KEYUP): 
    152152            { 
    153                 if (ea.getKey()=='s') 
     153                if (ea.getKey()=='s' && viewer) 
    154154                { 
    155                     saveSelectedModel(); 
     155                    saveSelectedModel(viewer->getSceneData()); 
    156156                } 
    157157                return false; 
     
    169169                { 
    170170                    // only do a pick if the mouse hasn't moved 
    171                     pick(ea); 
     171                    pick(ea,viewer); 
    172172                } 
    173173                return true; 
     
    179179    } 
    180180 
    181     void pick(const osgGA::GUIEventAdapter& ea) 
    182     { 
    183  
    184         osg::Node* scene = _sceneView.valid() ? _sceneView->getSceneData() : 0; 
     181    void pick(const osgGA::GUIEventAdapter& ea, osgViewer::SimpleViewer* viewer) 
     182    { 
     183        osg::Node* scene = viewer->getSceneData(); 
    185184        if (!scene) return; 
    186185 
    187         // remap the mouse x,y into viewport coordinates. 
    188186         
    189187 
     
    192190#if 0 
    193191        // use non dimension coordinates - in projection/clip space 
    194         osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),0.0), osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),1.0) ); 
     192        osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),-1.0), osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),1.0) ); 
    195193#else 
    196194        // use window coordinates 
    197         float mx = _sceneView->getViewport()->x() + (int)((float)_sceneView->getViewport()->width()*(ea.getXnormalized()*0.5f+0.5f)); 
    198         float my = _sceneView->getViewport()->y() + (int)((float)_sceneView->getViewport()->height()*(ea.getYnormalized()*0.5f+0.5f)); 
     195        // remap the mouse x,y into viewport coordinates. 
     196        osg::Viewport* viewport = viewer->getCamera()->getViewport(); 
     197        float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); 
     198        float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); 
    199199        osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) ); 
     200 
    200201#endif 
    201202 
    202203        osgUtil::IntersectionVisitor iv(picker); 
    203204         
    204         _sceneView->getCamera()->accept(iv); 
    205          
    206         osg::notify(osg::NOTICE)<<"Done pick, "<<picker->containsIntersections()<<std::endl; 
    207          
     205        viewer->getCamera()->accept(iv); 
     206         
     207 
    208208        if (picker->containsIntersections()) 
    209209        { 
    210210            osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); 
    211             osg::notify(osg::NOTICE)<<"Picking "<<intersection.localIntersectionPoint<<std::endl; 
    212  
    213 #if 0 
     211            osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl; 
     212 
     213#if 1 
    214214            osg::NodePath& nodePath = intersection.nodePath; 
    215215            osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; 
     
    248248        } 
    249249         
    250  
    251         { 
    252             float mx = _sceneView->getViewport()->x() + (int)((float)_sceneView->getViewport()->width()*(ea.getXnormalized()*0.5f+0.5f)); 
    253             float my = _sceneView->getViewport()->y() + (int)((float)_sceneView->getViewport()->height()*(ea.getYnormalized()*0.5f+0.5f)); 
    254  
    255             // do the pick traversal use the other PickVisitor to double check results. 
    256             osgUtil::PickVisitor pick(_sceneView->getViewport(), 
    257                                       _sceneView->getProjectionMatrix(),  
    258                                       _sceneView->getViewMatrix(), mx, my); 
    259             scene->accept(pick); 
    260  
    261             osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList(); 
    262             if (!segHitList.empty() && !segHitList.begin()->second.empty()) 
    263             { 
    264  
    265                 // get the hits for the first segment 
    266                 osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second; 
    267  
    268                 // just take the first hit - nearest the eye point. 
    269                 osgUtil::Hit& hit = hits.front(); 
    270  
    271                 std::cout<<"Got hits"<<hit.getLocalIntersectPoint()<<std::endl; 
    272             } 
    273         } 
    274  
    275     } 
    276  
    277     void saveSelectedModel() 
    278     { 
     250    } 
     251 
     252    void saveSelectedModel(osg::Node* scene) 
     253    { 
     254        if (!scene) return; 
     255     
    279256        CreateModelToSaveVisitor cmtsv; 
    280         _sceneView->getSceneData()->accept(cmtsv); 
     257        scene->accept(cmtsv); 
    281258         
    282259        if (cmtsv._group->getNumChildren()>0) 
     
    289266protected: 
    290267 
    291     osg::observer_ptr<osgUtil::SceneView> _sceneView; 
    292268    float _mx,_my; 
    293269}; 
     
    318294 
    319295    // create the view of the scene. 
    320     osgGA::SimpleViewer viewer; 
     296    osgViewer::SimpleViewer viewer; 
    321297    viewer.setSceneData(loadedModel.get()); 
    322298     
     
    335311 
    336312    // add the pick handler 
    337     viewer.addEventHandler(new PickHandler(viewer.getSceneView())); 
     313    viewer.addEventHandler(new PickHandler()); 
    338314     
    339315    // set the window dimensions