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

Revision 1944, 7.3 kB (checked in by robert, 11 years ago)

Updates to the handling of vertex attributes.

  • 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(socketNumber);
175    rc.setPort(socketNumber);
176
177    while( !viewer.done() )
178    {
179        // wait for all cull and draw threads to complete.
180        viewer.sync();
181
182        // update the scene by traversing it with the the update visitor which will
183        // call all node update callbacks and animations.
184        viewer.update();
185         
186         
187        // special handling for working as a cluster.
188        switch (viewerMode)
189        {
190        case(MASTER):
191            {
192                CameraPacket cp;
193               
194                // take camera zero as the guide.
195                osg::Matrix modelview(viewer.getCameraConfig()->getCamera(0)->getViewMatrix());
196               
197                cp.setPacket(modelview,viewer.getFrameStamp());
198
199                bc.setBuffer(&cp, sizeof( CameraPacket ));
200                bc.sync();
201
202            }
203            break;
204        case(SLAVE):
205            {
206                CameraPacket cp;
207
208                rc.setBuffer(&cp, sizeof( CameraPacket ));
209                rc.sync();
210
211                cp.checkByteOrder();
212
213                osg::Matrix modelview;
214                cp.getModelView(modelview,camera_offset);
215               
216                viewer.setView(modelview);
217
218                if (cp.getMasterKilled())
219                {
220                    std::cout << "recieved master killed"<<std::endl;
221                    // break out of while (!done) loop since we've now want to shut down.
222                    break;
223                }
224            }
225            break;
226        default:
227            // no need to anything here, just a normal interactive viewer.
228            break;
229        }
230         
231        // fire off the cull and draw traversals of the scene.
232        viewer.frame();
233       
234    }
235   
236    // wait for all cull and draw threads to complete before exit.
237    viewer.sync();
238
239    // if we are master clean up by telling all slaves that we're going down.
240    if (viewerMode==MASTER)
241    {
242        // need to broadcast my death.
243        CameraPacket cp;
244        cp.setPacket(osg::Matrix::identity(),viewer.getFrameStamp());
245        cp.setMasterKilled(true);
246
247        bc.setBuffer(&cp, sizeof( CameraPacket ));
248        bc.sync();
249
250        std::cout << "broadcasting death"<<std::endl;
251
252    }
253
254    return 0;
255}
Note: See TracBrowser for help on using the browser.