root/OpenSceneGraph/trunk/examples/osgcluster/osgcluster.cpp @ 1960

Revision 1960, 7.5 kB (checked in by robert, 12 years ago)

Removed osg::GeoSet? for core osg lib and osgPlugin.

Commented out OpenDX plugin as its still based on GeoSet?.

Added support for loading and converting GeoSet? into the osgconv example.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#ifdef USE_MEM_CHECK
2#include <mcheck.h>
3#endif
4
5#include <osg/Group>
6#include <osg/Notify>
7
8#include <osgDB/Registry>
9#include <osgDB/ReadFile>
10
11#include <osgProducer/Viewer>
12
13#include <osg/Quat>
14
15#if defined (WIN32)
16#include <winsock.h>
17#endif
18
19#include "receiver.h"
20#include "broadcaster.h"
21
22typedef unsigned char * BytePtr;
23template <class T>
24inline void swapBytes(  T &s )
25{
26    if( sizeof( T ) == 1 ) return;
27
28    T d = s;
29    BytePtr sptr = (BytePtr)&s;
30    BytePtr dptr = &(((BytePtr)&d)[sizeof(T)-1]);
31
32    for( unsigned int i = 0; i < sizeof(T); i++ )
33        *(sptr++) = *(dptr--);
34}
35
36class CameraPacket {
37    public:
38   
39        CameraPacket():_masterKilled(false)
40    {
41        _byte_order = 0x12345678;
42    }
43       
44        void setPacket(const osg::Matrix& matrix,const osg::FrameStamp* frameStamp)
45        {
46            _matrix = matrix;
47            if (frameStamp)
48            {
49                _frameStamp    = *frameStamp;
50            }
51        }
52       
53        void getModelView(osg::Matrix& matrix,float angle_offset=0.0f)
54        {
55       
56            matrix = _matrix * osg::Matrix::rotate(angle_offset,0.0f,1.0f,0.0f);
57        }
58       
59    void checkByteOrder( void )
60    {
61        if( _byte_order == 0x78563412 )  // We're backwards
62        {
63            swapBytes( _byte_order );
64        swapBytes( _masterKilled );
65        for( int i = 0; i < 16; i++ )
66            swapBytes( _matrix.ptr()[i] );
67                   
68                // umm.. we should byte swap _frameStamp too...
69        }
70    }
71
72       
73        void setMasterKilled(const bool flag) { _masterKilled = flag; }
74        const bool getMasterKilled() const { return _masterKilled; }
75       
76    unsigned long   _byte_order;
77        bool            _masterKilled;
78        osg::Matrix     _matrix;
79
80        // note don't use a ref_ptr as used elsewhere for FrameStamp
81        // since we don't want to copy the pointer - but the memory.
82        // FrameStamp doesn't have a private destructor to allow
83        // us to do this, even though its a reference counted object.   
84        osg::FrameStamp  _frameStamp;
85       
86};
87
88enum ViewerMode
89{
90    STAND_ALONE,
91    SLAVE,
92    MASTER
93};
94
95int main( int argc, char **argv )
96{
97
98    // use an ArgumentParser object to manage the program arguments.
99    osg::ArgumentParser arguments(&argc,argv);
100   
101    // set up the usage document, in case we need to print out how to use this program.
102    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to approach implementation of clustering. Note, cluster support will soon be encompassed in Producer itself.");
103    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
104    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
105    arguments.getApplicationUsage()->addCommandLineOption("-m","Set viewer to MASTER mode, sending view via packets.");
106    arguments.getApplicationUsage()->addCommandLineOption("-s","Set viewer to SLAVE mode, reciving view via packets.");
107    arguments.getApplicationUsage()->addCommandLineOption("-n <int>","Socket number to transmit packets");
108    arguments.getApplicationUsage()->addCommandLineOption("-f <float>","Field of view of camera");
109    arguments.getApplicationUsage()->addCommandLineOption("-o <float>","Offset angle of camera");
110   
111    // construct the viewer.
112    osgProducer::Viewer viewer(arguments);
113
114    // set up the value with sensible default event handlers.
115    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
116
117    // get details on keyboard and mouse bindings used by the viewer.
118    viewer.getUsage(*arguments.getApplicationUsage());
119
120
121    // read up the osgcluster specific arguments.
122    ViewerMode viewerMode = STAND_ALONE;
123    while (arguments.read("-m")) viewerMode = MASTER;
124    while (arguments.read("-s")) viewerMode = SLAVE;
125   
126    float socketNumber=8100.0f;
127    while (arguments.read("-n",socketNumber)) ;
128
129    float camera_fov=45.0f;
130    while (arguments.read("-f",camera_fov)) ;
131
132    float camera_offset=45.0f;
133    while (arguments.read("-o",camera_offset)) ;
134
135
136    // if user request help write it out to cout.
137    if (arguments.read("-h") || arguments.read("--help"))
138    {
139        arguments.getApplicationUsage()->write(std::cout);
140        return 1;
141    }
142
143    // any option left unread are converted into errors to write out later.
144    arguments.reportRemainingOptionsAsUnrecognized();
145
146    // report any errors if they have occured when parsing the program aguments.
147    if (arguments.errors())
148    {
149        arguments.writeErrorMessages(std::cout);
150        return 1;
151    }
152   
153    if (arguments.argc()<=1)
154    {
155        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
156        return 1;
157    }
158   
159   
160    // load model.
161    osg::ref_ptr<osg::Node> rootnode = osgDB::readNodeFiles(arguments);
162
163   
164    // set the scene to render
165    viewer.setSceneData(rootnode.get());
166
167    // create the windows and run the threads.
168    viewer.realize();
169
170    // objects for managing the broadcasting and recieving of camera packets.
171    Broadcaster     bc;
172    Receiver        rc;
173
174    bc.setPort(static_cast<short int>(socketNumber));
175    rc.setPort(static_cast<short int>(socketNumber));
176
177    bool masterKilled = false;
178
179    while( !viewer.done() && !masterKilled )
180    {
181        // wait for all cull and draw threads to complete.
182        viewer.sync();
183
184        // update the scene by traversing it with the the update visitor which will
185        // call all node update callbacks and animations.
186        viewer.update();
187         
188         
189        // special handling for working as a cluster.
190        switch (viewerMode)
191        {
192        case(MASTER):
193            {
194                CameraPacket cp;
195               
196                // take camera zero as the guide.
197                osg::Matrix modelview(viewer.getCameraConfig()->getCamera(0)->getViewMatrix());
198               
199                cp.setPacket(modelview,viewer.getFrameStamp());
200
201                bc.setBuffer(&cp, sizeof( CameraPacket ));
202            bc.sync();
203
204            }
205            break;
206        case(SLAVE):
207            {
208                CameraPacket cp;
209
210                rc.setBuffer(&cp, sizeof( CameraPacket ));
211
212                rc.sync();
213   
214                cp.checkByteOrder();
215
216                osg::Matrix modelview;
217                cp.getModelView(modelview,camera_offset);
218               
219                viewer.setView(modelview);
220
221                if (cp.getMasterKilled())
222                {
223                    std::cout << "received master killed"<<std::endl;
224                    // break out of while (!done) loop since we've now want to shut down.
225                    masterKilled = true;
226                }
227            }
228            break;
229        default:
230            // no need to anything here, just a normal interactive viewer.
231            break;
232        }
233         
234        // fire off the cull and draw traversals of the scene.
235        if(!masterKilled)
236            viewer.frame();
237       
238    }
239
240    // wait for all cull and draw threads to complete before exit.
241    viewer.sync();
242
243    // if we are master clean up by telling all slaves that we're going down.
244    if (viewerMode==MASTER)
245    {
246        // need to broadcast my death.
247        CameraPacket cp;
248        cp.setPacket(osg::Matrix::identity(),viewer.getFrameStamp());
249        cp.setMasterKilled(true);
250
251        bc.setBuffer(&cp, sizeof( CameraPacket ));
252        bc.sync();
253
254        std::cout << "broadcasting death"<<std::endl;
255
256    }
257
258    return 0;
259}
Note: See TracBrowser for help on using the browser.