1 // testDynamicClusterClient2
3 // OpenSG cluster client program where you can dynamicly connect/disconnect
5 // It is similar to testDynamicClusterClient but this time we create from the
6 // current state a changelist and send this to the cluster servers.
7 // This variant is much easier to implement in your own app but could be
8 // problematic if you use more than one aspect in your app.
11 // ./testClusterServer -w pipe0 &
12 // ./testDynamicClusterClient2 pipe0
14 // press 'c' to connect to the servers and 'd' to disconnect.
18 #include "OSGConfig.h"
19 #include "OSGSimpleGeometry.h"
20 #include "OSGGLUTWindow.h"
21 #include "OSGSimpleSceneManager.h"
22 #include "OSGMultiDisplayWindow.h"
23 #include "OSGSceneFileHandler.h"
24 #include "OSGRemoteAspect.h"
25 #include "OSGFieldContainerFactory.h"
30 // The SimpleSceneManager to manage simple applications
31 OSG::SimpleSceneManagerRefPtr _mgr
= NULL
;
32 OSG::GLUTWindowRecPtr _client_win
= NULL
;
33 OSG::MultiDisplayWindowRecPtr _cluster_win
= NULL
;
34 OSG::NodeRecPtr _root
= NULL
;
35 std::vector
<std::string
> _pipenames
;
37 // forward declaration so we can have the interesting stuff upfront
38 int setupGLUT( int *argc
, char *argv
[] );
41 // Initialize GLUT & OpenSG and set up the scene
42 int doMain(int argc
, char **argv
)
44 std::cout
<< "start a cluster server with './testClusterServer -w pipe0'\n"
45 "press 'c' to connect to the servers.\n"
46 "press 'd' to disconnect from the servers.\n"
47 "press 'n' to delete current scene.\n"
48 "press 't' to create a torus.\n"
49 "press 'l' to load scene 'tie.wrl'.\n"
53 OSG::osgInit(argc
,argv
);
56 int winid
= setupGLUT(&argc
, argv
);
58 // the connection between GLUT and OpenSG
60 _client_win
= OSG::GLUTWindow::create();
62 _client_win
->setGlutId(winid
);
64 _client_win
->setSize(300,300);
66 for(OSG::Int32 i
=0;i
<argc
-1;++i
)
69 _pipenames
.push_back(argv
[i
+1]);
72 if(_pipenames
.empty())
73 _pipenames
.push_back("pipe0");
75 _root
= OSG::Node::create();
77 _root
->setCore(OSG::Group::create());
79 // create default scene
80 OSG::NodeUnrecPtr scene
= OSG::makeTorus(.5, 2, 16, 16);
82 _root
->addChild(scene
);
84 // create the SimpleSceneManager helper
85 _mgr
= OSG::SimpleSceneManager::create();
87 // tell the manager what to manage
88 _mgr
->setWindow(_client_win
);
89 _mgr
->setRoot (_root
);
91 // show the whole scene
97 int main(int argc
, char **argv
)
106 static void connectCluster(void)
108 if(_cluster_win
!= NULL
)
111 OSG::Viewport
*clientvp
= _client_win
->getPort(0);
113 // create the viewports for the cluster just a simple one ...
114 OSG::ViewportUnrecPtr vp
= OSG::Viewport::create();
116 vp
->setCamera (_mgr
->getCamera());
117 vp
->setBackground(clientvp
->getBackground());
118 vp
->setRoot (clientvp
->getRoot());
119 vp
->setSize (0,0, 1,1);
121 // the connection between this client and the servers
122 _cluster_win
= OSG::MultiDisplayWindow::create();
124 // all changes must be enclosed in beginEditCP and endEditCP
125 // otherwise the changes will not be transfered over the network.
127 for(OSG::UInt32 i
=0;i
<_pipenames
.size();++i
)
128 _cluster_win
->editMFServers()->push_back(_pipenames
[i
]);
129 // dummy size for navigator
130 _cluster_win
->setSize(300,300);
131 _cluster_win
->addPort(vp
);
133 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
134 OSG::Thread::getCurrentChangeList()->fillFromCurrentState();
135 //Thread::getCurrentChangeList()->dump();
136 // create from the current state a changelist.
139 _cluster_win
->init();
141 // apply changelist to the servers
142 _cluster_win
->render(_mgr
->getRenderAction());
145 OSG::Thread::getCurrentChangeList()->clear();
150 static void disconnectCluster(void)
152 if(_cluster_win
== NULL
)
159 // GLUT callback functions
165 // redraw the client window
169 OSG::commitChanges();
173 if(_cluster_win
!= NULL
)
175 // redraw the server windows
176 _cluster_win
->render(_mgr
->getRenderAction());
180 catch(OSG_STDEXCEPTION_NAMESPACE::exception
&)
182 //printf("error: '%s'\n", e.what());
183 printf("ClusterServer was killed!\n");
188 OSG::commitChanges();
189 OSG::clearChangeList();
192 // react to size changes
193 void reshape(int w
, int h
)
198 // react to mouse button presses
199 void mouse(int button
, int state
, int x
, int y
)
202 _mgr
->mouseButtonRelease(button
, x
, y
);
204 _mgr
->mouseButtonPress(button
, x
, y
);
209 // react to mouse motions with pressed buttons
210 void motion(int x
, int y
)
212 _mgr
->mouseMove(x
, y
);
218 void keyboard(unsigned char k
, int x
, int y
)
234 while(_root
->getNChildren() > 0)
235 _root
->subChild(_root
->getChild(0));
241 OSG::NodeUnrecPtr scene
=
242 OSG::SceneFileHandler::the()->read("tie.wrl", NULL
);
246 _root
->addChild(scene
);
256 OSG::NodeUnrecPtr scene
= OSG::makeTorus(.5, 2, 16, 16);
258 _root
->addChild(scene
);
274 // setup the GLUT library which handles the windows for us
275 int setupGLUT(int *argc
, char *argv
[])
277 glutInit(argc
, argv
);
278 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
280 int winid
= glutCreateWindow("OpenSG");
282 glutReshapeFunc(reshape
);
283 glutDisplayFunc(display
);
284 glutMouseFunc(mouse
);
285 glutMotionFunc(motion
);
286 glutKeyboardFunc(keyboard
);