root/OpenSceneGraph/trunk/examples/osgvnc/osgvnc.cpp @ 9090

Revision 9090, 3.6 kB (checked in by robert, 6 years ago)

Refactored osgvnc example to utilise the new vnc plugin

Line 
1#include <osg/Image>
2#include <osg/Geometry>
3#include <osg/Texture2D>
4
5#include <osgGA/TrackballManipulator>
6
7#include <osgViewer/Viewer>
8#include <osgViewer/ViewerEventHandlers>
9
10#include <iostream>
11#include <osg/io_utils>
12
13#include <osgDB/ReadFile>
14
15
16osg::Node* createInteractiveQuad(const osg::Vec3& origin, osg::Vec3& widthAxis, osg::Vec3& heightAxis,
17                                 osg::Image* image)
18{
19    bool flip = image->getOrigin()==osg::Image::TOP_LEFT;
20
21    osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(origin, widthAxis, heightAxis,
22                                       0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f);
23
24    osg::Texture2D* texture = new osg::Texture2D(image);
25    texture->setResizeNonPowerOfTwoHint(false);
26    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
27    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
28    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
29   
30    pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
31                texture,
32                osg::StateAttribute::ON);
33               
34    pictureQuad->setEventCallback(new osgViewer::InteractiveImageHandler(image));
35
36    osg::Geode* geode = new osg::Geode;
37    geode->addDrawable(pictureQuad);
38   
39    return geode;
40}
41
42class EscapeHandler : public osgGA::GUIEventHandler
43{
44    public:
45   
46        EscapeHandler() {}
47
48        bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
49        {
50            if (ea.getHandled()) return false;
51
52            switch(ea.getEventType())
53            {
54                case(osgGA::GUIEventAdapter::KEYUP):
55                {
56                    if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Escape)
57                    {
58                        osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
59                        if (view) view->getViewerBase()->setDone(true);
60                       
61                        return true;
62                    }
63                }
64
65                default:
66                    return false;
67            }
68            return false;
69        }
70};
71
72int main(int argc,char** argv)
73{
74
75    osg::ArgumentParser arguments(&argc, argv);
76    osgViewer::Viewer viewer;
77   
78    typedef std::list< osg::ref_ptr<osg::Image> > Images;
79    Images images;
80
81    std::string hostname;
82    while (arguments.read("--host",hostname))
83    {
84        osg::ref_ptr<osg::Image> image = osgDB::readImageFile(hostname+std::string(".vnc"));
85        if (image.valid()) images.push_back(image.get());
86    }
87   
88    if (images.empty())
89    {
90        return 1;
91    }
92
93    bool xyPlane = false;
94
95    osg::Group* group = new osg::Group;
96
97    osg::Vec3 origin = osg::Vec3(0.0f,0.0f,0.0f);
98    for(Images::iterator itr = images.begin();
99        itr != images.end();
100        ++itr)
101    {
102        osg::Image* image = itr->get();
103        float width = 1.0;
104        float height = float(image->t())/float(image->s());
105        osg::Vec3 widthAxis = osg::Vec3(width,0.0f,0.0f);
106        osg::Vec3 heightAxis = xyPlane ? osg::Vec3(0.0f,height,0.0f) : osg::Vec3(0.0f,0.0f,height);
107        group->addChild(createInteractiveQuad(origin, widthAxis, heightAxis, image));
108       
109        origin += widthAxis*1.1f;
110    }
111   
112    viewer.setSceneData(group);
113
114    viewer.addEventHandler(new osgViewer::StatsHandler);
115   
116    // add a custom escape handler, but disable the standard viewer one to enable the vnc images to handle
117    // the escape without it getting caught by the viewer.
118    viewer.addEventHandler(new EscapeHandler);   
119    viewer.setKeyEventSetsDone(0);
120
121    return viewer.run();
122}
Note: See TracBrowser for help on using the browser.