root/branches/Dirk_CPtr/Tutorials/13ClusterClient.cpp

Revision 619, 6.3 kB (checked in by dirk, 2 years ago)

Added support for local libs for individual Tuts
Added Cluster tuts (thanks to Patrick hartling!)

  • 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
4 //
5 // To test it, run
6 //   ./12ClusterServer -geometry 300x300+200+100 -m -w test1 &
7 //   ./12ClusterServer -geometry 300x300+500+100 -m -w test2 &
8 //   ./13ClusterClient -m -fData/tie.wrl test1 test2
9 //
10 // If you have trouble with multicasting, you can alternatively try
11 //   ./12ClusterServer -geometry 300x300+200+100 -w 127.0.0.1:30000 &
12 //   ./12ClusterServer -geometry 300x300+500+100 -w 127.0.0.1:30001 &
13 //   ./13ClusterClient -m -fData/tie.wrl 127.0.0.1:30000 127.0.0.1:30001
14 //
15 // The client will open an emoty window that you can use to navigate. The
16 // display is shown in the server windows.
17 //
18 // This will run all three on the same machine, but you can also start the
19 // servers anywhere else, as long as you can reach them via multicast.
20 //
21 // Note: This will run two VERY active OpenGL programs on one screen. Not all
22 // OpenGL drivers are happy with that, so if it crashes your X, it's not our
23 // fault! ;)
24 //
25 // Libs: Cluster
26
27 // GLUT is used for window handling
28 #include <OpenSG/OSGGLUT.h>
29
30 // General OpenSG configuration, needed everywhere
31 #include <OpenSG/OSGConfig.h>
32
33 // Methods to create simple geometry: boxes, spheres, tori etc.
34 #include <OpenSG/OSGSimpleGeometry.h>
35
36 // The GLUT-OpenSG connection class
37 #include <OpenSG/OSGGLUTWindow.h>
38
39 // A little helper to simplify scene management and interaction
40 #include <OpenSG/OSGSimpleSceneManager.h>
41
42 // A little helper to simplify scene management and interaction
43 #include <OpenSG/OSGMultiDisplayWindow.h>
44
45 // Scene file handler for loading geometry files
46 #include <OpenSG/OSGSceneFileHandler.h>
47
48 // Activate the OpenSG namespace
49 OSG_USING_NAMESPACE
50
51 using namespace std;
52 // The SimpleSceneManager to manage simple applications
53 SimpleSceneManager *mgr;
54
55 // forward declaration so we can have the interesting stuff upfront
56 int setupGLUT( int *argc, char *argv[] );
57
58 // Initialize GLUT & OpenSG and set up the scene
59 int main(int argc, char **argv)
60 {
61     char     *opt;
62     NodePtr   scene=NullFC;
63
64     // OSG init
65     osgInit(argc,argv);
66
67     // GLUT init
68     int winid = setupGLUT(&argc, argv);
69
70     // the connection between this client and the servers
71     MultiDisplayWindowPtr mwin= MultiDisplayWindow::create();
72
73     // evaluate params
74     for(int a=1 ; a<argc ; ++a)
75     {
76         if(argv[a][0] == '-')
77         {
78             switch(argv[a][1])
79             {
80                 case 'm': mwin->setConnectionType("Multicast");
81 cout << "Connection type set to Multicast" << endl;
82                           break;
83                 case 'p': mwin->setConnectionType("SockPipeline");
84 cout << "Connection type set to SockPipeline" << endl;
85                           break;
86                 case 'i': opt = argv[a][2] ? argv[a]+2 : argv[++a];
87                           if(opt != argv[argc])
88                               mwin->setConnectionInterface(opt);
89                           break;
90                 case 'a': opt = argv[a][2] ? argv[a]+2 : argv[++a];
91                           if(opt != argv[argc])
92                               mwin->setServiceAddress(opt);
93                           break;
94                 case 'f': opt = argv[a][2] ? argv[a]+2 : argv[++a];
95                           if(opt != argv[argc])
96                               scene = SceneFileHandler::the()->read(
97                                   opt,0);
98                           break;
99                 case 'x': opt = argv[a][2] ? argv[a]+2 : argv[++a];
100                           if(opt != argv[argc])
101                               mwin->setHServers(atoi(opt));
102                           break;
103                 case 'y': opt = argv[a][2] ? argv[a]+2 : argv[++a];
104                           if(opt != argv[argc])
105                               mwin->setVServers(atoi(opt));
106                           break;
107                 default:  std::cout << argv[0] 
108                                     << " -m"
109                                     << " -p"
110                                     << " -i interface"
111                                     << " -f file"
112                                     << " -x horizontal server cnt"
113                                     << " -y vertical server cnt"
114                                     << endLog;
115                           return 0;
116             }
117         }
118         else
119         {
120             printf("%s\n",argv[a]);
121             mwin->editServers().push_back(argv[a]);
122         }
123     }
124
125     // dummy size for navigator
126     mwin->setSize(300,300);
127
128     // create default scene
129     if(scene == NullFC)
130        scene = makeTorus(.5, 2, 16, 16);
131
132     commitChanges();
133
134     // create the SimpleSceneManager helper
135     mgr = new SimpleSceneManager;
136
137     // tell the manager what to manage
138     mgr->setWindow(mwin );
139     mgr->setRoot  (scene);
140
141     // show the whole scene
142     mgr->showAll();
143    
144     // initialize window
145     mwin->init();
146    
147     // GLUT main loop
148     glutMainLoop();
149
150     return 0;
151 }
152
153 //
154 // GLUT callback functions
155 //
156
157 // redraw the window
158 void display(void)
159 {
160     // redraw the cluster window
161     mgr->redraw();
162     // clear change list. If you don't clear the changelist,
163     // then the same changes will be transmitted a second time
164     // in the next frame.
165     OSG::Thread::getCurrentChangeList()->clear();
166     // clear local navigation window
167     glClear(GL_COLOR_BUFFER_BIT);
168     glutSwapBuffers();
169 }
170
171 // react to size changes
172 void reshape(int w, int h)
173 {
174     glutPostRedisplay();
175 }
176
177 // react to mouse button presses
178 void mouse(int button, int state, int x, int y)
179 {
180     if (state)
181         mgr->mouseButtonRelease(button, x, y);
182     else
183         mgr->mouseButtonPress(button, x, y);
184     glutPostRedisplay();
185 }
186
187 // react to mouse motions with pressed buttons
188 void motion(int x, int y)
189 {
190     mgr->mouseMove(x, y);
191     glutPostRedisplay();
192 }
193
194 // react to keys
195 void keyboard(unsigned char k, int x, int y)
196 {
197     switch(k)
198     {
199         case 27:   
200         {
201             OSG::osgExit();
202             exit(0);
203         }
204         break;
205     }
206 }
207
208 // setup the GLUT library which handles the windows for us
209 int setupGLUT(int *argc, char *argv[])
210 {
211     glutInit(argc, argv);
212     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
213    
214     int winid = glutCreateWindow("OpenSG");
215    
216     glutReshapeFunc(reshape);
217     glutDisplayFunc(display);
218     glutMouseFunc(mouse);
219     glutMotionFunc(motion);
220     glutKeyboardFunc(keyboard);
221
222     return winid;
223 }
Note: See TracBrowser for help on using the browser.