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

Revision 1039, 7.3 kB (checked in by cneumann, 11 months ago)

changed: - factory functions return a TransitPtr? that can not be implicitly

converted to C Ptr. Should help with porting.

added: - GlobalRefPtr?, needed for cases where upon return from main

a RefPtr? goes out of scope (it would attempt to access the
FCFactory which is already shutdown at that point).

status: - vrml loader does not compile (needs porting to ref ptr)

  • tutorials compile, run and exit cleanly
  • multithreading and cluster are untested, yet
  • 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     NodeGlobalRefPtr   scene;
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     SortLastWindowGlobalRefPtr mwin = SortLastWindow::create();
77
78     // evaluate params
79     for(int a=1 ; a<argc ; ++a)
80     {
81         if(argv[a][0] == '-')
82         {
83             switch(argv[a][1])
84             {
85                 case 'm': mwin->setConnectionType("Multicast");
86                           cout << "Connection type set to Multicast" << endl;
87                           break;
88                 case 'p': mwin->setConnectionType("SockPipeline");
89                           cout << "Connection type set to SockPipeline" << endl;
90                           break;
91                 case 'i': opt = argv[a][2] ? argv[a]+2 : argv[++a];
92                           if(opt != argv[argc])
93                               mwin->setConnectionInterface(opt);
94                           break;
95                 case 'a': opt = argv[a][2] ? argv[a]+2 : argv[++a];
96                           if(opt != argv[argc])
97                               mwin->setServiceAddress(opt);
98                           break;
99                 case 'f': opt = argv[a][2] ? argv[a]+2 : argv[++a];
100                           if(opt != argv[argc])
101                               scene = SceneFileHandler::the()->read(
102                                   opt,0);
103                           break;
104                 case 'L':
105                     mwin->setComposer(ImageComposerRefPtr(PipelineComposer::create()));
106                     break;
107                 case 'B':
108                     mwin->setComposer(ImageComposerRefPtr(BinarySwapComposer::create()));
109                     break;
110                 case 'P':
111                     mwin->setComposer(ImageComposerRefPtr(ParallelComposer::create()));
112                     break;
113                 default:  std::cout << argv[0] 
114                                     << " -m"
115                                     << " -p"
116                                     << " -i interface"
117                                     << " -f file"
118                                    << endLog;
119                           return 0;
120             }
121         }
122         else
123         {
124             printf("%s\n",argv[a]);
125             mwin->editServers().push_back(argv[a]);
126         }
127     }
128
129     // Set the composer to use
130
131     if(mwin->getComposer() == NullFC)
132     {
133         mwin->setComposer(ImageComposerRefPtr(PipelineComposer::create()));
134     }
135
136     fprintf(stderr, "Using : %s\n", mwin->getComposer()->getType().getCName());
137
138     // window size
139     mwin->setSize(300,300);
140
141     // Create/set the client window that will display the result
142     
143     GLUTWindowGlobalRefPtr clientWindow = GLUTWindow::create();
144    
145     glutReshapeWindow(300,300);
146     clientWindow->setGlutId(winid);
147     clientWindow->init();
148    
149     clientWindow->resize(300,300);
150    
151     // Set the client window that will display the result
152     mwin->setClientWindow(clientWindow);
153    
154     // end edit of cluster window
155
156     // create default scene
157     if(scene == NullFC)
158     {
159         scene = makeNodeFor(GroupRefPtr(Group::create()));
160
161         scene->addChild(NodeRefPtr(makeTorus(.5, 2, 16, 16)));
162         scene->addChild(NodeRefPtr(makeCylinder(1, .3, 8, true, true, true)));
163     }
164    
165     // create the SimpleSceneManager helper
166     mgr = new SimpleSceneManager;
167
168     // tell the manager what to manage
169     mgr->setWindow(mwin );
170     mgr->setRoot  (scene);
171     mgr->setUseTraversalAction(false);
172
173     // show the whole scene
174     mgr->showAll();
175    
176     // initialize window
177     mwin->init();
178    
179     // GLUT main loop
180     glutMainLoop();
181
182     return 0;
183 }
184
185 //
186 // GLUT callback functions
187 //
188
189 // redraw the window
190 void display(void)
191 {
192     // redraw the cluster window
193     mgr->redraw();
194     // clear change list. If you don't clear the changelist,
195     // then the same changes will be transmitted a second time
196     // in the next frame.
197     OSG::Thread::getCurrentChangeList()->clear();
198 }
199
200 // react to size changes
201 void reshape(int w, int h)
202 {
203     glutPostRedisplay();
204 }
205
206 // react to mouse button presses
207 void mouse(int button, int state, int x, int y)
208 {
209     if (state)
210         mgr->mouseButtonRelease(button, x, y);
211     else
212         mgr->mouseButtonPress(button, x, y);
213     glutPostRedisplay();
214 }
215
216 // react to mouse motions with pressed buttons
217 void motion(int x, int y)
218 {
219     mgr->mouseMove(x, y);
220     glutPostRedisplay();
221 }
222
223 // react to keys
224 void keyboard(unsigned char k, int x, int y)
225 {
226     switch(k)
227     {
228         case 27:   
229         {
230             delete mgr;
231        
232             OSG::osgExit();
233             exit(0);
234         }
235         break;
236     }
237 }
238
239 // setup the GLUT library which handles the windows for us
240 int setupGLUT(int *argc, char *argv[])
241 {
242     glutInit(argc, argv);
243     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
244    
245     int winid = glutCreateWindow("OpenSG");
246    
247     glutReshapeFunc(reshape);
248     glutDisplayFunc(display);
249     glutMouseFunc(mouse);
250     glutMotionFunc(motion);
251     glutKeyboardFunc(keyboard);
252
253     return winid;
254 }
Note: See TracBrowser for help on using the browser.