root/branches/Carsten_PtrWork/Tutorials/28SortLastClusterClient.cpp

Revision 835, 7.3 kB (checked in by vossg, 1 year ago)

ported : cluster windows (basic port still need quite some testing and verification)
added : ptr mfield copy to containers
fixed : tutorial compile problems

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 // OpenSG Tutorial Example: Hello World
2 //
3 // Minimalistic OpenSG cluster client program demonstrating sort-last
4 // clustering (i.e. using multiple machine to draw a single image)
5 //
6 // To test it, run
7 //   ./12ClusterServer -geometry 300x300+200+100 -m -w test1 &
8 //   ./12ClusterServer -geometry 300x300+500+100 -m -w test2 &
9 //   ./28SortLastClusterClient -m -fData/tie.wrl test1 test2
10 //
11 // If you have trouble with multicasting, you can alternatively try
12 //   ./12ClusterServer -geometry 300x300+200+100 -w 127.0.0.1:30000 &
13 //   ./12ClusterServer -geometry 300x300+500+100 -w 127.0.0.1:30001 &
14 //   ./28SortLastClusterClient -fData/tie.wrl 127.0.0.1:30000 127.0.0.1:30001
15 // This will work as long as your loopback interface can handle broadcasts.
16 // If that is not the case you need to use your local IP address instead
17 // of 127.0.0.1.
18 //
19 // The client will open a window that you can use to navigate.
20 //
21 // This will run all three on the same machine, but you can also start the
22 // servers anywhere else, as long as you can reach them via broadcast and
23 // multicast, if using option 1 above.
24 //
25 // Note: This will run two VERY active OpenGL programs on one screen. Not all
26 // OpenGL drivers are happy with that, so if it crashes your X, it's not our
27 // fault! ;)
28
29 // GLUT is used for window handling
30 #include <OpenSG/OSGGLUT.h>
31
32 // General OpenSG configuration, needed everywhere
33 #include <OpenSG/OSGConfig.h>
34
35 // Methods to create simple geometry: boxes, spheres, tori etc.
36 #include <OpenSG/OSGSimpleGeometry.h>
37
38 // The GLUT-OpenSG connection class
39 #include <OpenSG/OSGGLUTWindow.h>
40
41 // A little helper to simplify scene management and interaction
42 #include <OpenSG/OSGSimpleSceneManager.h>
43
44 // The cluster window that handles sort-last (scene-split) clustering
45 #include <OpenSG/OSGSortLastWindow.h>
46 #include <OpenSG/OSGPipelineComposer.h>
47 #include <OpenSG/OSGBinarySwapComposer.h>
48 #include <OpenSG/OSGParallelComposer.h>
49
50 // Scene file handler for loading geometry files
51 #include <OpenSG/OSGSceneFileHandler.h>
52
53 // Activate the OpenSG namespace
54 OSG_USING_NAMESPACE
55
56 using namespace std;
57 // The SimpleSceneManager to manage simple applications
58 SimpleSceneManager *mgr;
59
60 // forward declaration so we can have the interesting stuff upfront
61 int setupGLUT( int *argc, char *argv[] );
62
63 // Initialize GLUT & OpenSG and set up the scene
64 int main(int argc, char **argv)
65 {
66     char     *opt;
67     NodePtr   scene=NullFC;
68
69     // OSG init
70     osgInit(argc,argv);
71
72     // GLUT init
73     int winid = setupGLUT(&argc, argv);
74
75     // the connection between this client and the servers
76     SortLastWindowPtr mwin= SortLastWindow::create();
77
78     // all changes must be enclosed in beginEditCP and endEditCP
79     // otherwise the changes will not be transfered over the network.
80
81     // evaluate params
82     for(int a=1 ; a<argc ; ++a)
83     {
84         if(argv[a][0] == '-')
85         {
86             switch(argv[a][1])
87             {
88                 case 'm': mwin->setConnectionType("Multicast");
89                           cout << "Connection type set to Multicast" << endl;
90                           break;
91                 case 'p': mwin->setConnectionType("SockPipeline");
92                           cout << "Connection type set to SockPipeline" << endl;
93                           break;
94                 case 'i': opt = argv[a][2] ? argv[a]+2 : argv[++a];
95                           if(opt != argv[argc])
96                               mwin->setConnectionInterface(opt);
97                           break;
98                 case 'a': opt = argv[a][2] ? argv[a]+2 : argv[++a];
99                           if(opt != argv[argc])
100                               mwin->setServiceAddress(opt);
101                           break;
102                 case 'f': opt = argv[a][2] ? argv[a]+2 : argv[++a];
103                           if(opt != argv[argc])
104                               scene = SceneFileHandler::the()->read(
105                                   opt,0);
106                           break;
107                 case 'L':
108                     mwin->setComposer(PipelineComposer::create());
109                     break;
110                 case 'B':
111                     mwin->setComposer(BinarySwapComposer::create());
112                     break;
113                 case 'P':
114                     mwin->setComposer(ParallelComposer::create());
115                     break;
116                 default:  std::cout << argv[0] 
117                                     << " -m"
118                                     << " -p"
119                                     << " -i interface"
120                                     << " -f file"
121                                    << endLog;
122                           return 0;
123             }
124         }
125         else
126         {
127             printf("%s\n",argv[a]);
128             mwin->editServers().push_back(argv[a]);
129         }
130     }
131
132     // Set the composer to use
133
134     if(mwin->getComposer() == NullFC)
135     {
136         mwin->setComposer(PipelineComposer::create());
137     }
138
139     fprintf(stderr, "Using : %s\n", mwin->getComposer()->getType().getCName());
140
141     // window size
142     mwin->setSize(300,300);
143
144     // Create/set the client window that will display the result
145     
146     GLUTWindowPtr clientWindow = GLUTWindow::create();
147    
148     glutReshapeWindow(300,300);
149     clientWindow->setGlutId(winid);
150     clientWindow->init();
151    
152     clientWindow->resize(300,300);
153    
154     // Set the client window that will display the result
155     mwin->setClientWindow(clientWindow);
156    
157     // end edit of cluster window
158
159     // create default scene
160     if(scene == NullFC)
161     {
162         scene = makeNodeFor(Group::create());
163
164         scene->addChild(makeTorus(.5, 2, 16, 16));
165         scene->addChild(makeCylinder(1, .3, 8, true, true, true));
166     }
167    
168     // create the SimpleSceneManager helper
169     mgr = new SimpleSceneManager;
170
171     // tell the manager what to manage
172     mgr->setWindow(mwin );
173     mgr->setRoot  (scene);
174     mgr->setUseTraversalAction(false);
175
176     // show the whole scene
177     mgr->showAll();
178    
179     // initialize window
180     mwin->init();
181    
182     // GLUT main loop
183     glutMainLoop();
184
185     return 0;
186 }
187
188 //
189 // GLUT callback functions
190 //
191
192 // redraw the window
193 void display(void)
194 {
195     // redraw the cluster window
196     mgr->redraw();
197     // clear change list. If you don't clear the changelist,
198     // then the same changes will be transmitted a second time
199     // in the next frame.
200     OSG::Thread::getCurrentChangeList()->clear();
201 }
202
203 // react to size changes
204 void reshape(int w, int h)
205 {
206     glutPostRedisplay();
207 }
208
209 // react to mouse button presses
210 void mouse(int button, int state, int x, int y)
211 {
212     if (state)
213         mgr->mouseButtonRelease(button, x, y);
214     else
215         mgr->mouseButtonPress(button, x, y);
216     glutPostRedisplay();
217 }
218
219 // react to mouse motions with pressed buttons
220 void motion(int x, int y)
221 {
222     mgr->mouseMove(x, y);
223     glutPostRedisplay();
224 }
225
226 // react to keys
227 void keyboard(unsigned char k, int x, int y)
228 {
229     switch(k)
230     {
231         case 27:   
232         {
233             OSG::osgExit();
234             exit(0);
235         }
236         break;
237     }
238 }
239
240 // setup the GLUT library which handles the windows for us
241 int setupGLUT(int *argc, char *argv[])
242 {
243     glutInit(argc, argv);
244     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
245    
246     int winid = glutCreateWindow("OpenSG");
247    
248     glutReshapeFunc(reshape);
249     glutDisplayFunc(display);
250     glutMouseFunc(mouse);
251     glutMotionFunc(motion);
252     glutKeyboardFunc(keyboard);
253
254     return winid;
255 }
Note: See TracBrowser for help on using the browser.