changed: gcc8 base update
[opensg.git] / Examples / Simple / clusterserver.cpp
blob6facab333ce5522eeaf57e729628ced277562103
1 // OpenSG Tutorial Example: Cluster Server
2 //
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.
7 //
8 // See the clusterclient.cpp for an example of how to use them.
9 //
10 // Libs: Cluster
12 #include <iostream>
14 #ifdef OSG_BUILD_ACTIVE
15 // GLUT is used for window handling
16 #include <OSGGLUT.h>
17 // General OpenSG configuration, needed everywhere
18 #include <OSGConfig.h>
19 // The Cluster server definition
20 #include <OSGClusterServer.h>
21 // The GLUT-OpenSG connection class
22 #include <OSGGLUTWindow.h>
23 // Render action definition.
24 #include <OSGRenderAction.h>
25 #else
26 // GLUT is used for window handling
27 #include <OpenSG/OSGGLUT.h>
28 // General OpenSG configuration, needed everywhere
29 #include <OpenSG/OSGConfig.h>
30 // The Cluster server defini2tion
31 #include <OpenSG/OSGClusterServer.h>
32 // The GLUT-OpenSG connection class
33 #include <OpenSG/OSGGLUTWindow.h>
34 // Render action definition.
35 #include <OpenSG/OSGRenderAction.h>
36 #endif
38 // local glut window
39 OSG::GLUTWindowRefPtr window;
40 // render action
41 OSG::RenderActionRefPtr ract;
42 // pointer the the cluster server instance
43 OSG::ClusterServer *server;
45 // forward declaration so we can have the interesting stuff upfront
46 void display(void);
47 void update (void);
48 void reshape(int width, int height);
50 // Initialize GLUT & OpenSG and start the cluster server
51 int main(int argc, char **argv)
53 int winid;
54 const char *name = "ClusterServer";
55 const char *connectionType = "StreamSock";
56 bool fullscreen = true;
57 std::string address = "";
59 // initialize Glut
60 glutInit(&argc, argv);
61 glutInitDisplayMode( GLUT_RGB |
62 GLUT_DEPTH |
63 GLUT_DOUBLE);
65 // evaluate params
66 for(int a=1 ; a<argc ; ++a)
68 if(argv[a][0] == '-')
70 switch(argv[a][1])
72 case 'm': connectionType="Multicast";
73 break;
74 case 'p': connectionType="SockPipeline";
75 break;
76 case 'w': fullscreen=false;
77 break;
78 case 'a': address = argv[a][2] ? argv[a]+2 : argv[++a];
79 if(address.empty())
81 SLOG << "address missing" << OSG::endLog;
82 return 0;
84 std::cout << address << OSG::endLog;
85 break;
86 default: std::cout << argv[0]
87 << "-m "
88 << "-p "
89 << "-w "
90 << "-a address "
91 << OSG::endLog;
92 return 0;
95 else
97 name=argv[a];
102 // init OpenSG
103 OSG::osgInit(argc, argv);
105 winid = glutCreateWindow(name);
106 if(fullscreen)
107 glutFullScreen();
108 glutDisplayFunc(display);
109 glutIdleFunc(update);
110 glutReshapeFunc(reshape);
112 glEnable( GL_LIGHTING );
113 glEnable( GL_LIGHT0 );
114 glEnable( GL_NORMALIZE );
115 glutSetCursor(GLUT_CURSOR_NONE);
117 // create the render action
118 ract = OSG::RenderAction::create();
120 // setup the OpenSG Glut window
121 window = OSG::GLUTWindow::create();
122 window->setGlutId(winid);
123 window->init();
125 // create the cluster server
126 server = new OSG::ClusterServer(window,name,connectionType,address);
127 // start the server
128 server->start();
130 // enter glut main loop
131 glutMainLoop();
133 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
135 SLOG << e.what() << OSG::endLog;
137 // clean up global variables
138 delete server;
140 ract = NULL;
141 window = NULL;
143 OSG::osgExit();
145 return 0;
148 /* render loop */
149 void display()
153 // receive scenegraph and do rendering
154 server->render(ract);
155 // clear changelist
156 OSG::Thread::getCurrentChangeList()->clear();
158 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
160 SLOG << e.what() << OSG::endLog;
162 window->clearPorts();
164 // try to restart server
165 server->stop();
166 // start server, wait for client to connect
167 server->start();
171 void update(void)
173 glutPostRedisplay();
176 /* window reshape */
177 void reshape( int width, int height )
179 // set new window size
180 window->resize( width, height );