Show
Ignore:
Timestamp:
04/05/03 21:40:18 (12 years ago)
Author:
robert
Message:

Added a couple ability of offset the images relative to each other
horizontally and vertically to allow better comenstation for mismatched
imagery.

Files:
1 modified

Legend:

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

    r1799 r1808  
    3737 
    3838 
    39     void set(osg::Switch* sw, osg::TexMat* texmat,float timePerSlide, bool autoSteppingActive); 
     39    void set(osg::Switch* sw, float offsetX, float offsetY, osg::TexMat* texmatLeft, osg::TexMat* texmatRight, float timePerSlide, bool autoSteppingActive); 
    4040 
    4141    virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); } 
     
    5353    void scaleImage(float s); 
    5454     
     55    void offsetImage(float ds,float dt); 
     56 
    5557    void rotateImage(float rx,float ry); 
    5658 
     59    void initTexMatrices(); 
    5760 
    5861protected: 
     
    6265 
    6366    osg::ref_ptr<osg::Switch>   _switch; 
    64     osg::ref_ptr<osg::TexMat>   _texmat; 
     67    osg::ref_ptr<osg::TexMat>   _texmatLeft; 
     68    osg::ref_ptr<osg::TexMat>   _texmatRight; 
    6569    bool                        _firstTraversal; 
    6670    unsigned int                _activeSlide; 
     
    6872    double                      _timePerSlide; 
    6973    bool                        _autoSteppingActive; 
     74    float                       _initSeperationX; 
     75    float                       _currentSeperationX; 
     76    float                       _initSeperationY; 
     77    float                       _currentSeperationY; 
    7078         
    7179}; 
     
    7381SlideEventHandler::SlideEventHandler(): 
    7482    _switch(0), 
    75     _texmat(0), 
     83    _texmatLeft(0), 
     84    _texmatRight(0), 
    7685    _firstTraversal(true), 
    7786    _activeSlide(0), 
     
    8291} 
    8392 
    84 void SlideEventHandler::set(osg::Switch* sw, osg::TexMat* texmat,float timePerSlide, bool autoSteppingActive) 
     93void SlideEventHandler::set(osg::Switch* sw, float offsetX, float offsetY, osg::TexMat* texmatLeft, osg::TexMat* texmatRight, float timePerSlide, bool autoSteppingActive) 
    8594{ 
    8695    _switch = sw; 
    8796    _switch->setUpdateCallback(this); 
    8897 
    89     _texmat = texmat; 
     98    _texmatLeft = texmatLeft; 
     99    _texmatRight = texmatRight; 
    90100 
    91101    _timePerSlide = timePerSlide; 
    92102    _autoSteppingActive = autoSteppingActive;     
     103     
     104    _initSeperationX = offsetX; 
     105    _currentSeperationX = _initSeperationX; 
     106 
     107    _initSeperationY = offsetY; 
     108    _currentSeperationY = _initSeperationY; 
     109 
     110    initTexMatrices(); 
     111 
    93112} 
    94113 
     
    115134                return true; 
    116135            } 
    117             else if (ea.getKey()=='z') 
     136            else if (ea.getKey()=='w') 
    118137            { 
    119138                scaleImage(0.99f); 
    120139                return true; 
    121140            } 
    122             else if (ea.getKey()=='x') 
     141            else if (ea.getKey()=='s') 
    123142            { 
    124143                scaleImage(1.01f); 
    125144                return true; 
    126145            } 
     146            else if (ea.getKey()=='j') 
     147            { 
     148                offsetImage(-0.001f,0.0f); 
     149                return true; 
     150            } 
     151            else if (ea.getKey()=='k') 
     152            { 
     153                offsetImage(0.001f,0.0f); 
     154                return true; 
     155            } 
     156            else if (ea.getKey()=='i') 
     157            { 
     158                offsetImage(0.0f,-0.001f); 
     159                return true; 
     160            } 
     161            else if (ea.getKey()=='m') 
     162            { 
     163                offsetImage(0.0f,0.001f); 
     164                return true; 
     165            } 
    127166            else if (ea.getKey()==' ') 
    128167            { 
    129                 if (_texmat.valid()) _texmat->setMatrix(osg::Matrix::identity()); 
     168                initTexMatrices(); 
    130169                return true; 
    131170            } 
     
    135174        case(osgGA::GUIEventAdapter::MOVE): 
    136175        { 
    137             static int px = ea.getX(); 
    138             static int py = ea.getY(); 
    139              
    140             int dx = ea.getX()-px; 
    141             int dy = ea.getY()-py; 
     176            static float px = ea.getX(); 
     177            static float py = ea.getY(); 
     178             
     179            float dx = ea.getX()-px; 
     180            float dy = ea.getY()-py; 
    142181             
    143182            px = ea.getX(); 
     
    160199    usage.addKeyboardMouseBinding("n","Advance to next image"); 
    161200    usage.addKeyboardMouseBinding("p","Move to previous image"); 
    162     usage.addKeyboardMouseBinding("z","Zoom into the image"); 
    163     usage.addKeyboardMouseBinding("x","Zoom out of the image"); 
     201    usage.addKeyboardMouseBinding("q","Zoom into the image"); 
     202    usage.addKeyboardMouseBinding("a","Zoom out of the image"); 
     203    usage.addKeyboardMouseBinding("j","Reduce horizontal offset"); 
     204    usage.addKeyboardMouseBinding("k","Increase horizontal offset"); 
     205    usage.addKeyboardMouseBinding("m","Reduce vertical offset"); 
     206    usage.addKeyboardMouseBinding("i","Increase vertical offset"); 
    164207} 
    165208 
     
    209252void SlideEventHandler::scaleImage(float s) 
    210253{ 
    211     if (_texmat.valid()) 
    212     { 
    213         _texmat->setMatrix(_texmat->getMatrix()*osg::Matrix::translate(-0.5f,-0.5f,0.0f)*osg::Matrix::scale(s,s,1.0f)*osg::Matrix::translate(0.5f,0.5f,0.0f)); 
    214     } 
     254    _texmatLeft->setMatrix(_texmatLeft->getMatrix()*osg::Matrix::translate(-0.5f,-0.5f,0.0f)*osg::Matrix::scale(s,s,1.0f)*osg::Matrix::translate(0.5f,0.5f,0.0f)); 
     255    _texmatRight->setMatrix(_texmatRight->getMatrix()*osg::Matrix::translate(-0.5f,-0.5f,0.0f)*osg::Matrix::scale(s,s,1.0f)*osg::Matrix::translate(0.5f,0.5f,0.0f)); 
     256} 
     257 
     258void SlideEventHandler::offsetImage(float ds,float dt) 
     259{ 
     260    _currentSeperationX+=ds; 
     261    _currentSeperationY+=dt; 
     262    osg::notify(osg::NOTICE)<<"image offset x = "<<_currentSeperationX<<"  y ="<<_currentSeperationY<<std::endl; 
     263    _texmatLeft->setMatrix(_texmatLeft->getMatrix()*osg::Matrix::translate(ds,dt,0.0f)); 
     264    _texmatRight->setMatrix(_texmatRight->getMatrix()*osg::Matrix::translate(-ds,-dt,0.0f)); 
    215265} 
    216266 
    217267void SlideEventHandler::rotateImage(float rx,float ry) 
    218268{ 
    219     if (_texmat.valid()) 
    220     { 
    221         const float scale = 0.5f; 
    222         _texmat->setMatrix(_texmat->getMatrix()*osg::Matrix::translate(-rx*scale,ry*scale,0.0f)); 
    223     } 
    224 } 
    225  
    226 osg::Geode* createSectorForImage(osg::Image* image,float s,float t, float radius, float height, float length) 
     269    const float scale = 0.5f; 
     270    _texmatLeft->setMatrix(_texmatLeft->getMatrix()*osg::Matrix::translate(-rx*scale,ry*scale,0.0f)); 
     271    _texmatRight->setMatrix(_texmatRight->getMatrix()*osg::Matrix::translate(-rx*scale,ry*scale,0.0f)); 
     272} 
     273 
     274void SlideEventHandler::initTexMatrices() 
     275{ 
     276    _texmatLeft->setMatrix(osg::Matrix::translate(_initSeperationX,_initSeperationY,0.0f)); 
     277    _texmatRight->setMatrix(osg::Matrix::translate(-_initSeperationX,-_initSeperationY,0.0f)); 
     278} 
     279 
     280 
     281osg::Geode* createSectorForImage(osg::Image* image, osg::TexMat* texmat, float s,float t, float radius, float height, float length) 
    227282{ 
    228283 
     
    244299    dstate->setMode(GL_LIGHTING,osg::StateAttribute::OFF); 
    245300    dstate->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); 
     301    dstate->setTextureAttribute(0, texmat); 
    246302 
    247303    // set up the geoset. 
     
    290346// create a switch containing a set of child each containing a  
    291347// stereo image pair. 
    292 osg::Switch* createScene(const FileList& fileList, float radius, float height, float length) 
     348osg::Switch* createScene(const FileList& fileList, osg::TexMat* texmatLeft, osg::TexMat* texmatRight, float radius, float height, float length) 
    293349{ 
    294350    osg::Switch* sw = new osg::Switch; 
     
    304360            float average_t = (imageLeft->t()+imageRight->t())*0.5f; 
    305361 
    306             osg::Geode* geodeLeft = createSectorForImage(imageLeft.get(),average_s,average_t, radius, height, length); 
     362            osg::Geode* geodeLeft = createSectorForImage(imageLeft.get(),texmatLeft,average_s,average_t, radius, height, length); 
    307363            geodeLeft->setNodeMask(0x01); 
    308364 
    309             osg::Geode* geodeRight = createSectorForImage(imageRight.get(),average_s,average_t, radius, height, length); 
     365            osg::Geode* geodeRight = createSectorForImage(imageRight.get(),texmatRight,average_s,average_t, radius, height, length); 
    310366            geodeRight->setNodeMask(0x02); 
    311367 
     
    343399    arguments.getApplicationUsage()->addCommandLineOption("-d <float>","Time delay in sceonds between the display of successive image pairs when in auto advance mode."); 
    344400    arguments.getApplicationUsage()->addCommandLineOption("-a","Enter auto advance of image pairs on start up."); 
     401    arguments.getApplicationUsage()->addCommandLineOption("-x <float>","Horizontal offset of left and right images."); 
     402    arguments.getApplicationUsage()->addCommandLineOption("-y <float>","Vertical offset of left and right images."); 
    345403    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); 
    346404     
     
    367425    while (arguments.read("-a")) autoSteppingActive = true; 
    368426 
     427    float offsetX=0.0f; 
     428    while (arguments.read("-x",offsetX)) {} 
     429 
     430    float offsetY=0.0f; 
     431    while (arguments.read("-y",offsetY)) {} 
    369432 
    370433    // if user request help write it out to cout. 
     
    420483    float length = osg::PI*radius;  // half a cylinder. 
    421484 
     485    // use a texure matrix to control the placement of the image. 
     486    osg::TexMat* texmatLeft = new osg::TexMat; 
     487    osg::TexMat* texmatRight = new osg::TexMat; 
     488 
    422489    // creat the scene from the file list. 
    423     osg::ref_ptr<osg::Switch> rootNode = createScene(fileList,radius,height,length); 
    424  
    425     // use a texure matrix to control the placement of the image. 
    426     osg::StateSet* stateset = rootNode->getOrCreateStateSet(); 
    427     osg::TexMat* texmat = new osg::TexMat; 
    428     stateset->setTextureAttribute(0,texmat); 
     490    osg::ref_ptr<osg::Switch> rootNode = createScene(fileList,texmatLeft,texmatRight,radius,height,length); 
     491 
    429492 
    430493    //osgDB::writeNodeFile(*rootNode,"test.osg"); 
     
    449512 
    450513    // set up the SlideEventHandler. 
    451     seh->set(rootNode.get(),texmat,timeDelayBetweenSlides,autoSteppingActive); 
     514    seh->set(rootNode.get(),offsetX,offsetY,texmatLeft,texmatRight,timeDelayBetweenSlides,autoSteppingActive); 
    452515     
    453516