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

Revision 9082, 4.5 kB (checked in by robert, 6 years ago)

Added very basic osgvnc example that uses the LibVNCServer client libries for implementing a vnc client
as an osg::Image with the vnc data stream going to it.

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 <SDL.h>
11
12#include <iostream>
13
14extern "C" {
15#include <rfb/rfbclient.h>
16}
17
18struct ButtonMapping { int sdl; int rfb; };
19
20ButtonMapping buttonMapping[]={
21    {1, rfbButton1Mask},
22    {2, rfbButton2Mask},
23    {3, rfbButton3Mask},
24    {0,0}
25};
26
27static rfbBool resize(rfbClient* client) {
28
29    static char first=TRUE;
30   
31    osg::Image* image = (osg::Image*)(rfbClientGetClientData(client, 0));
32   
33    int width=client->width;
34    int height=client->height;
35    int depth=client->format.bitsPerPixel;
36
37    std::cout<<"resize "<<width<<", "<<height<<", "<<depth<<" image = "<<image<<std::endl;
38
39    image->allocateImage(width,height,1,GL_RGBA,GL_UNSIGNED_BYTE);
40   
41    client->frameBuffer= (uint8_t*)(image->data());
42   
43    return TRUE;
44}
45
46static void update(rfbClient* client,int x,int y,int w,int h) {
47
48    osg::Image* image = (osg::Image*)(rfbClientGetClientData(client, 0));
49    image->dirty();
50}
51
52static void kbd_leds(rfbClient* client, int value, int pad) {
53    printf("kbd_leds %d %d\n",value,pad);
54
55    /* note: pad is for future expansion 0=unused */
56    fprintf(stderr,"Led State= 0x%02X\n", value);
57    fflush(stderr);
58}
59
60/* trivial support for textchat */
61static void text_chat(rfbClient* client, int value, char *text) {
62    switch(value) {
63    case rfbTextChatOpen:
64        fprintf(stderr,"TextChat: We should open a textchat window!\n");
65        TextChatOpen(client);
66        break;
67    case rfbTextChatClose:
68        fprintf(stderr,"TextChat: We should close our window!\n");
69        break;
70    case rfbTextChatFinished:
71        fprintf(stderr,"TextChat: We should close our window!\n");
72        break;
73    default:
74        fprintf(stderr,"TextChat: Received \"%s\"\n", text);
75        break;
76    }
77    fflush(stderr);
78}
79
80class RfbThread : public OpenThreads::Thread
81{
82public:
83   
84    RfbThread(rfbClient* client):
85        _client(client),
86        _done(false) {}
87   
88    virtual ~RfbThread()
89    {
90        _done = true;
91        cancel();
92        while(isRunning())
93        {
94            OpenThreads::Thread::YieldCurrentThread();
95        }
96    }
97
98    virtual void run()
99    {
100        do
101        {
102            int i=WaitForMessage(_client,500);
103            if(i<0)
104                return;
105            if(i)
106                if(!HandleRFBServerMessage(_client))
107                return;
108               
109        } while (!_done && !testCancel());
110    }
111   
112    rfbClient*  _client;
113    bool        _done;
114   
115};
116
117int main(int argc,char** argv)
118{
119    int i,buttonMask=0;
120   
121    osg::ref_ptr<osg::Image> image = new osg::Image;
122    // image->setPixelBufferObject(new osg::PixelBufferObject(image.get()));
123
124    osg::notify(osg::NOTICE)<<"image = "<<image.get()<<std::endl;
125
126    /* 16-bit: client=rfbGetClient(5,3,2); */
127    rfbClient* client=rfbGetClient(8,3,4);
128    client->MallocFrameBuffer=resize;
129    client->canHandleNewFBSize = TRUE;
130    client->GotFrameBufferUpdate=update;
131    client->HandleKeyboardLedState=kbd_leds;
132    client->HandleTextChat=text_chat;   
133
134    rfbClientSetClientData(client, 0, image.get());
135   
136    osg::notify(osg::NOTICE)<<"Before rfbInitClient"<<std::endl;
137
138    if(!rfbInitClient(client,&argc,argv))
139        return 1;
140
141    osg::ArgumentParser arguments(&argc, argv);
142    osgViewer::Viewer viewer;
143
144
145    bool xyPlane = false;
146    bool flip = true;
147    float width = image->s();
148    float height = image->t();
149
150    osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
151                                       osg::Vec3(width,0.0f,0.0f),
152                                       xyPlane ? osg::Vec3(0.0f,height,0.0f) : osg::Vec3(0.0f,0.0f,height),
153                                       0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f);
154
155    osg::Texture2D* texture = new osg::Texture2D(image.get());
156    texture->setResizeNonPowerOfTwoHint(false);
157    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
158    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
159    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
160   
161    pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
162                texture,
163                osg::StateAttribute::ON);
164
165    osg::Geode* geode = new osg::Geode;
166    geode->addDrawable(pictureQuad);
167   
168    viewer.setSceneData(geode);
169
170    viewer.addEventHandler(new osgViewer::StatsHandler);
171   
172    RfbThread rfbThread(client);
173    rfbThread.startThread();
174
175    return viewer.run();
176}
Note: See TracBrowser for help on using the browser.