1 // OpenSG Tutorial Example: Cluster Server
3 // This is a full functional OpenSG cluster server. In OpenSG
4 // the terms server and client are used similar to X11. The
5 // application is the client. Instances that are used for
6 // rendering are called server.
8 // See the ClusterClient.cpp for an example of how to use them.
19 // General OpenSG configuration, needed everywhere
20 #include "OSGConfig.h"
21 // The Cluster server definition
22 #include "OSGClusterServer.h"
23 // The GLUT-OpenSG connection class
24 #include "OSGXWindow.h"
25 // Render action definition.
26 #include "OSGRenderAction.h"
27 #include "OSGGeoFunctions.h"
28 #include "OSGFrameHandler.h"
31 OSG::XWindowRefPtr window
= NULL
;
33 OSG::RenderActionRefPtr ract
= NULL
;
34 // pointer the the cluster server instance
35 OSG::ClusterServer
*server
= NULL
;
36 bool exitOnError
= false;
52 // forward declaration so we can have the interesting stuff upfront
54 void reshape(int width
, int height
);
56 int wait_for_map_notify(Display
*, XEvent
*event
, char *arg
)
58 return( (event
->type
== MapNotify
) &&
59 (event
->xmap
.window
== reinterpret_cast< ::Window
>(arg
) ) );
62 #ifdef OSG_DEBUG_OLD_C_CASTS
63 #ifdef ScreenOfDisplay
64 #undef ScreenOfDisplay
69 #define ScreenOfDisplay(dpy, scr)(&(_XPrivDisplay(dpy))->screens[scr])
70 #define DefaultScreen(dpy) ((_XPrivDisplay(dpy))->default_screen)
73 // Initialize GLUT & OpenSG and start the cluster server
74 int main(int argc
,char **argv
)
76 const char *name
= "ClusterServer";
77 const char *connectionType
= "StreamSock";
78 bool fullscreen
= true;
79 std::string address
= "";
80 bool doStereo
= false;
81 std::string serviceGroup
= "224.245.211.234";
86 OSG::UInt32 width
= 512;
87 OSG::UInt32 height
= 512;
91 FILE *pOut
= fopen("/tmp/cluster_server.log", "w");
95 char **pVal
= environ
;
99 fprintf(pOut
, "%s\n", *pVal
);
108 for(int a
= 1 ; a
< argc
; ++a
)
110 if(argv
[a
][0] == '-')
115 address
= argv
[a
][2] ? argv
[a
]+2 : argv
[++a
];
117 if(address
== argv
[argc
])
119 SLOG
<< "address missing" << OSG::endLog
;
123 std::cout
<< address
<< OSG::endLog
;
138 if(argv
[a
][2] != '\0')
140 rc
= sscanf(argv
[a
] + 2,
149 rc
= sscanf(argv
[++a
],
167 if(argv
[a
][2] != '\0')
168 serviceGroup
=argv
[a
]+2;
170 serviceGroup
=argv
[++a
];
174 connectionType
="Multicast";
199 std::cout
<< "-m use multicast" << std::endl
;
200 std::cout
<< "-s enable stereo" << std::endl
;
201 std::cout
<< "-w no fullscreen" << std::endl
;
202 std::cout
<< "-e exit after closed connection"
204 std::cout
<< "-a Address Server network address"
206 std::cout
<< "-m Address wait for requests on "
207 << "multicast group" << std::endl
;
208 std::cout
<< "-p port wait for requests on port"
221 OSG::osgInit (argc
, argv
);
222 OSG::ClusterServer::init(argc
, argv
);
224 OSG::setVBOUsageOnPropertyProtos(true);
226 int snglBuf
[] = {GLX_RGBA
,
232 dblBuf
[0] = GLX_RGBA
;
233 dblBuf
[1] = GLX_DEPTH_SIZE
;
235 dblBuf
[3] = GLX_DOUBLEBUFFER
;
236 dblBuf
[4] = (doStereo
== true) ? GLX_STEREO
: None
;
240 OSG::DisplayP dpy
= XOpenDisplay(NULL
);
244 std::cerr
<< "Error: Could not open display!" << std::endl
;
249 if(!glXQueryExtension( dpy
, &dummy
, &dummy
))
251 std::cerr
<< "Error: X server has no OpenGL GLX extension"
255 XVisualInfo
*vi
= glXChooseVisual(dpy
, DefaultScreen(dpy
), dblBuf
);
259 vi
= glXChooseVisual(dpy
, DefaultScreen(dpy
), snglBuf
);
263 std::cerr
<< "no RGB visual with depth buffer" << std::endl
;
267 if(vi
->c_class
!= TrueColor
)
269 std::cerr
<< "TrueColor visual required for this program"
273 Colormap cmap
= XCreateColormap(dpy
,
274 RootWindow(dpy
, vi
->screen
),
277 XSetWindowAttributes swa
;
280 swa
.border_pixel
= 0;
293 // Create a Window and connect it to the main display dpy
294 OSG::X11Window hwin
= XCreateWindow(dpy
,
295 RootWindow(dpy
, vi
->screen
),
306 XSetStandardProperties(dpy
, hwin
, "testWindowX", "testWindowX",
307 None
, argv
, argc
, NULL
);
309 if(fullscreen
== true || noDecor
== true)
311 Atom noDecorAtom
= XInternAtom(dpy
,
315 if(noDecorAtom
== None
)
318 "Could not intern X atom for _MOTIF_WM_HINTS.\n");
332 oHints
.decorations
= 0;
340 reinterpret_cast<unsigned char *>(&oHints
), 4);
344 XStoreName(dpy
, hwin
, name
);
346 // create the render action
347 ract
= OSG::RenderAction::create();
349 // setup the OpenSG Glut window
350 window
= OSG::XWindow::create();
352 window
->setDisplay(dpy
);
353 window
->setWindow (hwin
);
359 XMapWindow(dpy
, hwin
);
363 reinterpret_cast<char *>(hwin
));
365 if(fullscreen
== true)
367 XMoveWindow (dpy
, hwin
, 0, 0);
368 XResizeWindow(dpy
, hwin
,
369 DisplayWidth (dpy
, vi
->screen
),
370 DisplayHeight(dpy
, vi
->screen
));
372 static char data
[1] = {0};
378 blank
= XCreateBitmapFromData(dpy
,
382 cursor
= XCreatePixmapCursor(dpy
,
385 &dummyCol
, &dummyCol
, 0, 0);
387 XFreePixmap(dpy
, blank
);
394 else if(geoSpec
== true)
396 XMoveResizeWindow(dpy
,
406 glEnable(GL_LIGHTING
);
407 glEnable(GL_LIGHT0
);
408 glEnable(GL_NORMALIZE
);
410 // create the cluster server
411 server
= new OSG::ClusterServer(window
,
421 // OSG::FieldContainerFactory::the()->dump();
425 while((ip
= XPending(dpy
)) != 0)
427 XNextEvent(dpy
, &event
);
431 case ConfigureNotify
:
433 reshape(event
.xconfigure
.width
,
434 event
.xconfigure
.height
);
450 catch(OSG_STDEXCEPTION_NAMESPACE::exception
&e
)
452 SLOG
<< e
.what() << OSG::endLog
;
467 // fprintf(stderr, "cache %d\n", iCache);
469 ract
->setFrustumCulling(false);
472 OSG::FrameHandler::the()->frame();
474 // receive scenegraph and do rendering
475 server
->render(ract
);
479 ract
->setFrustumCulling(false);
485 OSG::Thread::getCurrentChangeList()->clear();
488 catch(OSG_STDEXCEPTION_NAMESPACE::exception
&e
)
492 SLOG
<< e
.what() << std::endl
;
494 printf("Exit on error %s",e
.what());
508 SLOG
<< e
.what() << OSG::endLog
;
509 // try to restart server
512 ract
= OSG::RenderAction::create();
514 // start server, wait for client to connect
521 void reshape( int width
, int height
)
523 // set new window size
524 window
->resize( width
, height
);
531 int main(int argc
,char **argv
)
533 std::cerr
<< "Not supported on windows platform!" << std::endl
;