Hi list,

I've tried to use a custom manipulator in an osghud. The picking and
manipulation works but I'm a little stumped about the clipping of the
Translate1DAxisDragger. The surrounding geometry of the TrackballDragger
seems perfectly clipped but I can't get the clipping for the inner axis
right. Do I have a fundamental misunderstanding of glOrtho or is that
actually a bug?

Cheers,
Philipp Moeller

/* OpenSceneGraph example, osghud.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>

#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>

#include <osgGA/TrackballManipulator>

#include <osg/Material>
#include <osg/Geode>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/PolygonOffset>
#include <osg/MatrixTransform>
#include <osg/Camera>
#include <osg/RenderInfo>

#include <osgDB/WriteFile>

#include <osgText/Text>

#include <osgManipulator/TabBoxDragger>
#include <osgManipulator/Translate1DDragger>
#include <osgManipulator/TrackballDragger>

// a TrackballDragger without the sphere
class SpherelessTrackballDragger : public osgManipulator::TrackballDragger
{
public:
  SpherelessTrackballDragger() : osgManipulator::TrackballDragger(false) { }
  void setupDefaultGeometry() {
    osgManipulator::TrackballDragger::setupDefaultGeometry();
    _xyzDragger->removeChildren(0, _xyzDragger->getNumChildren());
  }

  virtual ~SpherelessTrackballDragger() {}
};

osg::Camera* createHUD()
{
    // create a camera to set up the projection and model view matrices, and the subgraph to draw in the HUD
    osg::Camera* camera = new osg::Camera;

    // set the projection matrix
    // camera->setProjectionMatrix(osg::Matrix::ortho(0,1280,0,1024,-1.0f,1.0));
    camera->setProjectionMatrix(osg::Matrix::ortho(-1.5, 20.0, -1.5, 20.0, -1.0, 1.0));

    // set the view matrix    
    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    camera->setViewMatrix(osg::Matrix::identity());

    // only clear the depth buffer
    camera->setClearMask(GL_DEPTH_BUFFER_BIT);

    // draw subgraph after main camera view.
    camera->setRenderOrder(osg::Camera::POST_RENDER);

    // we don't want the camera to grab event focus from the viewers main camera(s).
    camera->setAllowEventFocus(true);

    return camera;
}

struct SnapImage : public osg::Camera::DrawCallback
{
    SnapImage(const std::string& filename):
        _filename(filename),
        _snapImage(false)
    {
        _image = new osg::Image;        
    }

    virtual void operator () (osg::RenderInfo& renderInfo) const
    {

        if (!_snapImage) return;
    
        osg::notify(osg::NOTICE)<<"Camera callback"<<std::endl;

        osg::Camera* camera = renderInfo.getCurrentCamera();
        osg::Viewport* viewport = camera ? camera->getViewport() : 0;

        osg::notify(osg::NOTICE)<<"Camera callback "<<camera<<" "<<viewport<<std::endl;

        if (viewport && _image.valid())
        {
            _image->readPixels(int(viewport->x()),int(viewport->y()),int(viewport->width()),int(viewport->height()),
                               GL_RGBA,
                               GL_UNSIGNED_BYTE);
            osgDB::writeImageFile(*_image, _filename);
            
            osg::notify(osg::NOTICE)<<"Taken screenshot, and written to '"<<_filename<<"'"<<std::endl;             
        }
       
        _snapImage = false;
    }

    std::string                         _filename;
    mutable bool                        _snapImage;
    mutable osg::ref_ptr<osg::Image>    _image;
};

struct SnapeImageHandler : public osgGA::GUIEventHandler
{

    SnapeImageHandler(int key,SnapImage* si):
        _key(key),
        _snapImage(si) {}

    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
    {
        if (ea.getHandled()) return false;

        switch(ea.getEventType())
        {
            case(osgGA::GUIEventAdapter::KEYUP):
            {
                if (ea.getKey() == _key)
                {
                    osg::notify(osg::NOTICE)<<"event handler"<<std::endl;
                    _snapImage->_snapImage = true;
                    return true;
                }

                break;
            }
        default:
            break;
        }

        return false;
    }
    
    int                     _key;
    osg::ref_ptr<SnapImage> _snapImage;
};


int main( int argc, char **argv )
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);


    // read the scene from the list of file specified commandline args.
    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
    
    // if not loaded assume no arguments passed in, try use default model instead.
    if (!scene) scene = osgDB::readNodeFile("dumptruck.osg");
    
    
    if (!scene)
    {
        osg::notify(osg::NOTICE)<<"No model loaded"<<std::endl;
        return 1;
    }


    if (arguments.read("--Viewer"))
    {
        // construct the viewer.
        osgViewer::Viewer viewer;

        // create a HUD as slave camera attached to the master view.
        
        viewer.setUpViewAcrossAllScreens();

        osgViewer::Viewer::Windows windows;
        viewer.getWindows(windows);
        
        if (windows.empty()) return 1;
        
        osg::Camera* hudCamera = createHUD();
        
        // set up cameras to render on the first window available.
        hudCamera->setGraphicsContext(windows[0]);
        hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);

        viewer.addSlave(hudCamera, false);

        // set the scene to render
        viewer.setSceneData(scene.get());

        return viewer.run();

    }
    if (arguments.read("--CompositeViewer"))
    {
        // construct the viewer.
        osgViewer::CompositeViewer viewer;

        // create the main 3D view
        osgViewer::View* view = new osgViewer::View;
        viewer.addView(view);

        view->setSceneData(scene.get());
        view->setUpViewAcrossAllScreens();;
        view->setCameraManipulator(new osgGA::TrackballManipulator);

        // now create the HUD camera's view
        
        osgViewer::Viewer::Windows windows;
        viewer.getWindows(windows);
        
        if (windows.empty()) return 1;
        
        osg::Camera* hudCamera = createHUD();
        
        // set up cameras to render on the first window available.
        hudCamera->setGraphicsContext(windows[0]);
        hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);
        
        osgViewer::View* hudView = new osgViewer::View;
        hudView->setCamera(hudCamera);
        
        viewer.addView(hudView);

        return viewer.run();

    }
    else
    {
        // construct the viewer.
        osgViewer::Viewer viewer;
        
        SnapImage* postDrawCallback = new SnapImage("PostDrawCallback.png");
        viewer.getCamera()->setPostDrawCallback(postDrawCallback);        
        viewer.addEventHandler(new SnapeImageHandler('p',postDrawCallback));
        
        SnapImage* finalDrawCallback = new SnapImage("FinalDrawCallback.png");
        viewer.getCamera()->setFinalDrawCallback(finalDrawCallback);        
        viewer.addEventHandler(new SnapeImageHandler('f',finalDrawCallback));

        auto group  = new osg::MatrixTransform;

        viewer.realize();

        // add the HUD subgraph.    
        if (scene.valid()) group->addChild(scene.get());
        osg::Camera* hud = createHUD();

        osgManipulator::TrackballDragger* dragger = new SpherelessTrackballDragger;
        dragger->setupDefaultGeometry();
        dragger->setHandleEvents(true);

        osgManipulator::Dragger* axisDragger = new osgManipulator::Translate1DDragger;
        axisDragger->setupDefaultGeometry();
        axisDragger->setHandleEvents(true);

        dragger->addTransformUpdating(axisDragger);

        hud->addChild(dragger);
        hud->addChild(axisDragger);

        axisDragger->addTransformUpdating(group);
        
        hud->setViewport(viewer.getCamera()->getViewport());
        group->addChild(hud);

        // set the scene to render
        viewer.setSceneData(group);

        return viewer.run();
    }
    
}
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to