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

Revision 1706, 6.8 kB (checked in by robert, 11 years ago)

break from osgcluster main loop added for when the master send a killed
messsage to slaves.

  • 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,1.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()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
103    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
104    arguments.getApplicationUsage()->addCommandLineOption("-m","Set viewer to MASTER mode, sending view via packets.");
105    arguments.getApplicationUsage()->addCommandLineOption("-s","Set viewer to SLAVE mode, reciving view via packets.");
106    arguments.getApplicationUsage()->addCommandLineOption("-n <int>","Socket number to transmit packets");
107    arguments.getApplicationUsage()->addCommandLineOption("-f <float>","Field of view of camera");
108    arguments.getApplicationUsage()->addCommandLineOption("-o <float>","Offset angle of camera");
109   
110    // construct the viewer.
111    osgProducer::Viewer viewer(arguments);
112
113    // set up the value with sensible default event handlers.
114    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
115
116    // get details on keyboard and mouse bindings used by the viewer.
117    viewer.getUsage(*arguments.getApplicationUsage());
118
119
120    // read up the osgcluster specific arguments.
121    ViewerMode viewerMode = STAND_ALONE;
122    while (arguments.read("-m")) viewerMode = MASTER;
123    while (arguments.read("-s")) viewerMode = SLAVE;
124   
125    float socketNumber=8100.0f;
126    while (arguments.read("-n",socketNumber)) ;
127
128    float camera_fov=45.0f;
129    while (arguments.read("-f",camera_fov)) ;
130
131    float camera_offset=45.0f;
132    while (arguments.read("-o",camera_offset)) ;
133
134
135    // if user request help write it out to cout.
136    if (arguments.read("-h") || arguments.read("--help"))
137    {
138        arguments.getApplicationUsage()->write(std::cout);
139        return 1;
140    }
141
142    // any option left unread are converted into errors to write out later.
143    arguments.reportRemainingOptionsAsUnrecognized();
144
145    // report any errors if they have occured when parsing the program aguments.
146    if (arguments.errors())
147    {
148        arguments.writeErrorMessages(std::cout);
149        return 1;
150    }
151   
152   
153    // load model.
154    osg::ref_ptr<osg::Node> rootnode = osgDB::readNodeFiles(arguments);
155
156   
157    // set the scene to render
158    viewer.setSceneData(rootnode.get());
159
160    // create the windows and run the threads.
161    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
162
163    // objects for managing the broadcasting and recieving of camera packets.
164    Broadcaster     bc;
165    Receiver        rc;
166
167    while( !viewer.done() )
168    {
169        // wait for all cull and draw threads to complete.
170        viewer.sync();
171
172        // update the scene by traversing it with the the update visitor which will
173        // call all node update callbacks and animations.
174        viewer.update();
175         
176         
177        // special handling for working as a cluster.
178        switch (viewerMode)
179        {
180        case(MASTER):
181            {
182                CameraPacket cp;
183               
184                // take camera zero as the guide.
185                osg::Matrix modelview(viewer.getCameraConfig()->getCamera(0)->getViewMatrix());
186               
187                cp.setPacket(modelview,viewer.getFrameStamp());
188
189                bc.setBuffer(&cp, sizeof( CameraPacket ));
190                bc.sync();
191
192            }
193            break;
194        case(SLAVE):
195            {
196                CameraPacket cp;
197
198                rc.setBuffer(&cp, sizeof( CameraPacket ));
199                rc.sync();
200
201                cp.checkByteOrder();
202
203                osg::Matrix modelview;
204                cp.getModelView(modelview,camera_offset);
205               
206                viewer.setView(modelview.ptr());
207
208                if (cp.getMasterKilled())
209                {
210                    std::cout << "recieved master killed"<<std::endl;
211                    // break out of while (!done) loop since we've now want to shut down.
212                    break;
213                }
214            }
215            break;
216        default:
217            // no need to anything here, just a normal interactive viewer.
218            break;
219        }
220         
221        // fire off the cull and draw traversals of the scene.
222        viewer.frame();
223       
224    }
225
226
227    // if we are master clean up by telling all slaves that we're going down.
228    if (viewerMode==MASTER)
229    {
230        // need to broadcast my death.
231        CameraPacket cp;
232        cp.setPacket(osg::Matrix::identity(),viewer.getFrameStamp());
233        cp.setMasterKilled(true);
234
235        bc.setBuffer(&cp, sizeof( CameraPacket ));
236        bc.sync();
237
238        std::cout << "broadcasting death"<<std::endl;
239
240    }
241
242    return 0;
243}
Note: See TracBrowser for help on using the browser.