fixed: push transparency enforcement to state so the backend is aware of it
[opensg.git] / Examples / Simple / clusterclient.cpp
blobe29fd9d6f0cb9e7d7a919935a560480b3854f27b
1 // OpenSG Tutorial Example: Minimalistic OpenSG cluster client program
2 //
3 // To test it, run
4 // ./clusterserver -geometry 300x300+200+100 -m -w test1 &
5 // ./clusterserver -geometry 300x300+500+100 -m -w test2 &
6 // ./clusterclient -m -fData/tie.wrl test1 test2
7 //
8 // If you have trouble with multicasting, you can alternatively try
9 // ./clusterserver -geometry 300x300+200+100 -w 127.0.0.1:30000 &
10 // ./clusterserver -geometry 300x300+500+100 -w 127.0.0.1:30001 &
11 // ./clusterclient -m -fData/tie.wrl 127.0.0.1:30000 127.0.0.1:30001
12 //
13 // The client will open an empty window that you can use to navigate. The
14 // display is shown in the server windows.
16 // This will run all three on the same machine, but you can also start the
17 // servers anywhere else, as long as you can reach them via multicast.
19 // Note: This will run two VERY active OpenGL programs on one screen. Not all
20 // OpenGL drivers are happy with that, so if it crashes your X, it's not our
21 // fault! ;)
23 // Libs: Cluster
25 #ifdef OSG_BUILD_ACTIVE
26 // GLUT is used for window handling
27 #include <OSGGLUT.h>
29 // General OpenSG configuration, needed everywhere
30 #include <OSGConfig.h>
32 // Methods to create simple geometry: boxes, spheres, tori etc.
33 #include <OSGSimpleGeometry.h>
35 // The GLUT-OpenSG connection class
36 #include <OSGGLUTWindow.h>
38 // A little helper to simplify scene management and interaction
39 #include <OSGSimpleSceneManager.h>
41 // A little helper to simplify scene management and interaction
42 #include <OSGMultiDisplayWindow.h>
44 // Scene file handler for loading geometry files
45 #include <OSGSceneFileHandler.h>
46 #else
47 // GLUT is used for window handling
48 #include <OpenSG/OSGGLUT.h>
50 // General OpenSG configuration, needed everywhere
51 #include <OpenSG/OSGConfig.h>
53 // Methods to create simple geometry: boxes, spheres, tori etc.
54 #include <OpenSG/OSGSimpleGeometry.h>
56 // The GLUT-OpenSG connection class
57 #include <OpenSG/OSGGLUTWindow.h>
59 // A little helper to simplify scene management and interaction
60 #include <OpenSG/OSGSimpleSceneManager.h>
62 // A little helper to simplify scene management and interaction
63 #include <OpenSG/OSGMultiDisplayWindow.h>
65 // Scene file handler for loading geometry files
66 #include <OpenSG/OSGSceneFileHandler.h>
67 #endif
69 // The SimpleSceneManager to manage simple applications
70 OSG::SimpleSceneManagerRefPtr mgr;
72 // forward declaration so we can have the interesting stuff upfront
73 int setupGLUT( int *argc, char *argv[] );
75 // Initialize GLUT & OpenSG and set up the scene
76 int main(int argc, char **argv)
78 char *opt;
80 // OSG init
81 OSG::osgInit(argc,argv);
83 // GLUT init
84 /*int winid =*/ setupGLUT(&argc, argv);
86 // open a new scope, because the pointers below should go out of scope
87 // before entering glutMainLoop.
88 // Otherwise OpenSG will complain about objects being alive after shutdown.
90 // the connection between this client and the servers
91 OSG::MultiDisplayWindowRefPtr mwin = OSG::MultiDisplayWindow::create();
92 OSG::NodeRefPtr scene;
94 // evaluate params
95 for(int a=1 ; a<argc ; ++a)
97 if(argv[a][0] == '-')
99 switch(argv[a][1])
101 case 'm': mwin->setConnectionType("Multicast");
102 std::cout << "Connection type set to Multicast"
103 << std::endl;
104 break;
105 case 'p': mwin->setConnectionType("SockPipeline");
106 std::cout << "Connection type set to SockPipeline"
107 << std::endl;
108 break;
109 case 'i': opt = argv[a][2] ? argv[a]+2 : argv[++a];
110 if(opt != argv[argc])
111 mwin->setConnectionInterface(opt);
112 break;
113 case 'a': opt = argv[a][2] ? argv[a]+2 : argv[++a];
114 if(opt != argv[argc])
115 mwin->setServiceAddress(opt);
116 break;
117 case 'f': opt = argv[a][2] ? argv[a]+2 : argv[++a];
118 if(opt != argv[argc])
119 scene = OSG::SceneFileHandler::the()->read(opt,
121 break;
122 case 'x': opt = argv[a][2] ? argv[a]+2 : argv[++a];
123 if(opt != argv[argc])
124 mwin->setHServers(atoi(opt));
125 break;
126 case 'y': opt = argv[a][2] ? argv[a]+2 : argv[++a];
127 if(opt != argv[argc])
128 mwin->setVServers(atoi(opt));
129 break;
130 default: std::cout << argv[0]
131 << " -m"
132 << " -p"
133 << " -i interface"
134 << " -f file"
135 << " -x horizontal server cnt"
136 << " -y vertical server cnt"
137 << std::endl;
138 return 0;
141 else
143 printf("%s\n",argv[a]);
144 mwin->editMFServers()->push_back(argv[a]);
148 // dummy size for navigator
149 mwin->setSize(300,300);
151 // create default scene
152 if(scene == NULL)
153 scene = OSG::makeTorus(.5, 2, 16, 16);
155 OSG::commitChanges();
157 // create the SimpleSceneManager helper
158 mgr = OSG::SimpleSceneManager::create();
160 // tell the manager what to manage
161 mgr->setWindow(mwin );
162 mgr->setRoot (scene);
164 // show the whole scene
165 mgr->showAll();
167 // initialize window
168 mwin->init();
171 // GLUT main loop
172 glutMainLoop();
174 return 0;
178 // GLUT callback functions
181 // redraw the window
182 void display(void)
184 // redraw the cluster window
185 mgr->redraw();
186 // clear change list. If you don't clear the changelist,
187 // then the same changes will be transmitted a second time
188 // in the next frame.
189 OSG::Thread::getCurrentChangeList()->clear();
190 // clear local navigation window
191 glClear(GL_COLOR_BUFFER_BIT);
192 glutSwapBuffers();
195 // react to size changes
196 void reshape(int w, int h)
198 glutPostRedisplay();
201 // react to mouse button presses
202 void mouse(int button, int state, int x, int y)
204 if (state)
205 mgr->mouseButtonRelease(button, x, y);
206 else
207 mgr->mouseButtonPress(button, x, y);
208 glutPostRedisplay();
211 // react to mouse motions with pressed buttons
212 void motion(int x, int y)
214 mgr->mouseMove(x, y);
215 glutPostRedisplay();
218 // react to keys
219 void keyboard(unsigned char k, int x, int y)
221 switch(k)
223 case 27:
225 // clean up global variables
226 mgr = NULL;
228 OSG::osgExit();
229 exit(0);
231 break;
235 // setup the GLUT library which handles the windows for us
236 int setupGLUT(int *argc, char *argv[])
238 glutInit(argc, argv);
239 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
241 int winid = glutCreateWindow("OpenSG");
243 glutReshapeFunc(reshape);
244 glutDisplayFunc(display);
245 glutMouseFunc(mouse);
246 glutMotionFunc(motion);
247 glutKeyboardFunc(keyboard);
249 return winid;