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

Revision 9221, 3.7 kB (checked in by robert, 5 years ago)

Updated wrappers

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   
35               
36    osg::ref_ptr<osgViewer::InteractiveImageHandler> callback = new osgViewer::InteractiveImageHandler(image);
37
38    pictureQuad->setEventCallback(callback.get());
39    pictureQuad->setCullCallback(callback.get());
40
41    osg::Geode* geode = new osg::Geode;
42    geode->addDrawable(pictureQuad);
43   
44    return geode;
45}
46
47class EscapeHandler : public osgGA::GUIEventHandler
48{
49    public:
50   
51        EscapeHandler() {}
52
53        bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
54        {
55            if (ea.getHandled()) return false;
56
57            switch(ea.getEventType())
58            {
59                case(osgGA::GUIEventAdapter::KEYUP):
60                {
61                    if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Escape)
62                    {
63                        osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
64                        if (view) view->getViewerBase()->setDone(true);
65                       
66                        return true;
67                    }
68                }
69
70                default:
71                    return false;
72            }
73            return false;
74        }
75};
76
77int main(int argc,char** argv)
78{
79
80    osg::ArgumentParser arguments(&argc, argv);
81    osgViewer::Viewer viewer;
82   
83    typedef std::list< osg::ref_ptr<osg::Image> > Images;
84    Images images;
85
86    std::string hostname;
87    while (arguments.read("--host",hostname))
88    {
89        osg::ref_ptr<osg::Image> image = osgDB::readImageFile(hostname+std::string(".vnc"));
90        if (image.valid()) images.push_back(image.get());
91    }
92   
93    if (images.empty())
94    {
95        return 1;
96    }
97
98    bool xyPlane = false;
99
100    osg::Group* group = new osg::Group;
101
102    osg::Vec3 origin = osg::Vec3(0.0f,0.0f,0.0f);
103    for(Images::iterator itr = images.begin();
104        itr != images.end();
105        ++itr)
106    {
107        osg::Image* image = itr->get();
108        float width = 1.0;
109        float height = float(image->t())/float(image->s());
110        osg::Vec3 widthAxis = osg::Vec3(width,0.0f,0.0f);
111        osg::Vec3 heightAxis = xyPlane ? osg::Vec3(0.0f,height,0.0f) : osg::Vec3(0.0f,0.0f,height);
112        group->addChild(createInteractiveQuad(origin, widthAxis, heightAxis, image));
113       
114        origin += widthAxis*1.1f;
115    }
116   
117    viewer.setSceneData(group);
118
119    viewer.addEventHandler(new osgViewer::StatsHandler);
120   
121    // add a custom escape handler, but disable the standard viewer one to enable the vnc images to handle
122    // the escape without it getting caught by the viewer.
123    viewer.addEventHandler(new EscapeHandler);   
124    viewer.setKeyEventSetsDone(0);
125
126    return viewer.run();
127}
Note: See TracBrowser for help on using the browser.