root/branches/Carsten_PtrWork2/Tutorials/13ClusterClientShader.cpp

Revision 1039, 11.0 kB (checked in by cneumann, 11 months ago)

changed: - factory functions return a TransitPtr? that can not be implicitly

converted to C Ptr. Should help with porting.

added: - GlobalRefPtr?, needed for cases where upon return from main

a RefPtr? goes out of scope (it would attempt to access the
FCFactory which is already shutdown at that point).

status: - vrml loader does not compile (needs porting to ref ptr)

  • tutorials compile, run and exit cleanly
  • multithreading and cluster are untested, yet
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 #include <OpenSG/OSGImage.h>
49 #include <OpenSG/OSGChunkMaterial.h>
50 #include <OpenSG/OSGMaterialChunk.h>
51 #include <OpenSG/OSGTextureObjChunk.h>
52 #include <OpenSG/OSGTextureEnvChunk.h>
53 #include <OpenSG/OSGSHLChunk.h>
54
55 // Activate the OpenSG namespace
56 OSG_USING_NAMESPACE
57
58 using namespace std;
59 // The SimpleSceneManager to manage simple applications
60 SimpleSceneManager *mgr;
61
62 static SHLChunkGlobalRefPtr _shl;
63 static Int32                _animation = 1;
64
65 // forward declaration so we can have the interesting stuff upfront
66 int setupGLUT( int *argc, char *argv[] );
67
68
69 NodeTransitPtr createScene(void)
70 {
71     NodeRefPtr _scene;
72
73     // Create the shader material
74     ChunkMaterialRefPtr cmat = ChunkMaterial::create();
75
76     // Read the image for the normal texture
77     ImageRefPtr earth_map_img = Image::create();
78     if(!earth_map_img->read("Earth.jpg"))
79     {
80         fprintf(stderr, "Couldn't read texture 'Earth.jpg'\n");
81         return NodeTransitPtr();
82     }
83     TextureObjChunkRefPtr tex_earth     = TextureObjChunk::create();
84     TextureEnvChunkRefPtr tex_earth_env = TextureEnvChunk::create();
85
86     tex_earth->setImage(earth_map_img);
87     tex_earth->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
88     tex_earth->setMagFilter(GL_LINEAR);
89     tex_earth->setWrapS(GL_REPEAT);
90     tex_earth->setWrapT(GL_REPEAT);
91
92     tex_earth_env->setEnvMode(GL_MODULATE);
93
94     // Read the image for the normal texture
95     ImageRefPtr earth_night_map_img = Image::create();
96     if(!earth_night_map_img->read("EarthNight.jpg"))
97     {
98         fprintf(stderr, "Couldn't read texture 'EarthNight.jpg'\n");
99         return NodeTransitPtr();
100     }
101
102     TextureObjChunkRefPtr tex_earth_night     = TextureObjChunk::create();
103     TextureEnvChunkRefPtr tex_earth_night_env = TextureEnvChunk::create();
104
105     tex_earth_night->setImage(earth_night_map_img);
106     tex_earth_night->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
107     tex_earth_night->setMagFilter(GL_LINEAR);
108     tex_earth_night->setWrapS(GL_REPEAT);
109     tex_earth_night->setWrapT(GL_REPEAT);
110
111     tex_earth_night_env->setEnvMode(GL_MODULATE);
112    
113     // Read the image for the normal texture
114     ImageRefPtr earth_clouds_map_img = Image::create();
115     if(!earth_clouds_map_img->read("EarthClouds.jpg"))
116     {
117         fprintf(stderr, "Couldn't read texture 'EarthClouds.jpg'\n");
118         return NodeTransitPtr();
119     }
120
121     TextureObjChunkRefPtr tex_earth_clouds     = TextureObjChunk::create();
122     TextureEnvChunkRefPtr tex_earth_clouds_env = TextureEnvChunk::create();
123
124     tex_earth_clouds->setImage(earth_clouds_map_img);
125     tex_earth_clouds->setMinFilter(GL_LINEAR_MIPMAP_LINEAR);
126     tex_earth_clouds->setMagFilter(GL_LINEAR);
127     tex_earth_clouds->setWrapS(GL_REPEAT);
128     tex_earth_clouds->setWrapT(GL_REPEAT);
129
130     tex_earth_clouds_env->setEnvMode(GL_MODULATE);
131
132
133     _shl = SHLChunk::create();
134
135     if(!_shl->readVertexProgram("Earth.vp"))
136         fprintf(stderr, "Couldn't read vertex program 'Earth.vp'\n");
137     if(!_shl->readFragmentProgram("Earth.fp"))
138         fprintf(stderr, "Couldn't read fragment program 'Earth.fp'\n");
139
140     _shl->setUniformParameter("EarthDay", 0);
141     _shl->setUniformParameter("EarthNight", 1);
142     _shl->setUniformParameter("EarthCloudGloss", 2);
143     _shl->setUniformParameter("season", 0.0f);
144     _shl->setUniformParameter("cos_time_0_2PI", -0.406652f);
145     _shl->setUniformParameter("sin_time_0_2PI", -0.913583f);
146     _shl->setUniformParameter("foo", -0.913583f);
147
148    
149     cmat->addChunk(_shl);
150     cmat->addChunk(tex_earth);
151     cmat->addChunk(tex_earth_env);
152     cmat->addChunk(tex_earth_night);
153     cmat->addChunk(tex_earth_night_env);
154     cmat->addChunk(tex_earth_clouds);
155     cmat->addChunk(tex_earth_clouds_env);
156
157
158     // create root node
159     _scene = Node::create();
160
161     GeometryRefPtr geo = makeLatLongSphereGeo (100, 100, 1.0);
162
163     geo->setMaterial(cmat);
164
165
166     NodeRefPtr torus = Node::create();
167    
168     torus->setCore(geo);
169
170
171     // add torus to scene
172     GroupRefPtr group = Group::create();
173
174     _scene->setCore(group);
175     _scene->addChild(torus);
176
177     return NodeTransitPtr(_scene);
178 }
179
180 // Initialize GLUT & OpenSG and set up the scene
181 int main(int argc, char **argv)
182 {
183     char             *opt;
184     NodeGlobalRefPtr  scene;
185
186     // OSG init
187     osgInit(argc,argv);
188
189     // GLUT init
190     int winid = setupGLUT(&argc, argv);
191
192     // the connection between this client and the servers
193     MultiDisplayWindowGlobalRefPtr mwin = MultiDisplayWindow::create();
194
195     // evaluate params
196     for(int a=1 ; a<argc ; ++a)
197     {
198         if(argv[a][0] == '-')
199         {
200             switch(argv[a][1])
201             {
202                 case 'm': mwin->setConnectionType("Multicast");
203 cout << "Connection type set to Multicast" << endl;
204                           break;
205                 case 'p': mwin->setConnectionType("SockPipeline");
206 cout << "Connection type set to SockPipeline" << endl;
207                           break;
208                 case 'i': opt = argv[a][2] ? argv[a]+2 : argv[++a];
209                           if(opt != argv[argc])
210                               mwin->setConnectionInterface(opt);
211                           break;
212                 case 'a': opt = argv[a][2] ? argv[a]+2 : argv[++a];
213                           if(opt != argv[argc])
214                               mwin->setServiceAddress(opt);
215                           break;
216                 case 'f': opt = argv[a][2] ? argv[a]+2 : argv[++a];
217                           if(opt != argv[argc])
218                               scene = SceneFileHandler::the()->read(
219                                   opt,0);
220                           break;
221                 case 'x': opt = argv[a][2] ? argv[a]+2 : argv[++a];
222                           if(opt != argv[argc])
223                               mwin->setHServers(atoi(opt));
224                           break;
225                 case 'y': opt = argv[a][2] ? argv[a]+2 : argv[++a];
226                           if(opt != argv[argc])
227                               mwin->setVServers(atoi(opt));
228                           break;
229                 default:  std::cout << argv[0] 
230                                     << " -m"
231                                     << " -p"
232                                     << " -i interface"
233                                     << " -f file"
234                                     << " -x horizontal server cnt"
235                                     << " -y vertical server cnt"
236                                     << endLog;
237                           return 0;
238             }
239         }
240         else
241         {
242             printf("%s\n",argv[a]);
243             mwin->editServers().push_back(argv[a]);
244         }
245     }
246
247     // dummy size for navigator
248     mwin->setSize(300,300);
249
250     // create default scene
251     if(scene == NullFC)
252         scene = createScene();
253
254     if(scene == NullFC)
255         scene = makeTorus(.5, 2, 16, 16);
256
257     commitChanges();
258
259     // create the SimpleSceneManager helper
260     mgr = new SimpleSceneManager;
261
262     // tell the manager what to manage
263     mgr->setWindow(mwin );
264     mgr->setRoot  (scene);
265
266     // show the whole scene
267     mgr->showAll();
268    
269     // initialize window
270     mwin->init();
271    
272     // GLUT main loop
273     glutMainLoop();
274
275     return 0;
276 }
277
278 //
279 // GLUT callback functions
280 //
281
282 // redraw the window
283 void display(void)
284 {
285     static Real32 speed = 10000.0f;
286     static Real32 t = glutGet(GLUT_ELAPSED_TIME);
287     static Real32 t2 = 0.0;
288    
289     Real32 td = glutGet(GLUT_ELAPSED_TIME) - t;
290
291     if(td > speed)
292         t = glutGet(GLUT_ELAPSED_TIME);
293
294     if(_animation)
295     {
296         t2 = (2 * OSG::Pi / speed) * td;
297
298         _shl->setUniformParameter("cos_time_0_2PI", osgCos(t2));
299         _shl->setUniformParameter("sin_time_0_2PI", osgSin(t2));
300     }
301
302     Thread::getCurrentChangeList()->commitChanges();
303
304     // redraw the cluster window
305     mgr->redraw();
306     // clear change list. If you don't clear the changelist,
307     // then the same changes will be transmitted a second time
308     // in the next frame.
309     OSG::Thread::getCurrentChangeList()->clear();
310     // clear local navigation window
311     glClear(GL_COLOR_BUFFER_BIT);
312     glutSwapBuffers();
313 }
314
315 // react to size changes
316 void reshape(int w, int h)
317 {
318     glutPostRedisplay();
319 }
320
321 // react to mouse button presses
322 void mouse(int button, int state, int x, int y)
323 {
324     if (state)
325         mgr->mouseButtonRelease(button, x, y);
326     else
327         mgr->mouseButtonPress(button, x, y);
328     glutPostRedisplay();
329 }
330
331 // react to mouse motions with pressed buttons
332 void motion(int x, int y)
333 {
334     mgr->mouseMove(x, y);
335     glutPostRedisplay();
336 }
337
338 // react to keys
339 void keyboard(unsigned char k, int x, int y)
340 {
341     static Real32 season = 0.0f;
342
343     switch(k)
344     {
345         case 27:   
346         {
347             delete mgr;
348        
349             OSG::osgExit();
350             exit(0);
351         }
352         break;
353         case 's':
354             if(season < 0.435)
355                 season += 0.01;
356
357             _shl->setUniformParameter("season", season);
358
359         break;
360         case 'S':
361             if(season > -0.435)
362                 season -= 0.01;
363
364             _shl->setUniformParameter("season", season);
365         break;
366         case 'a':
367             _animation = 1 - _animation;
368         break;
369     }
370 }
371
372 // setup the GLUT library which handles the windows for us
373 int setupGLUT(int *argc, char *argv[])
374 {
375     glutInit(argc, argv);
376     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
377    
378     int winid = glutCreateWindow("OpenSG");
379    
380     glutReshapeFunc(reshape);
381     glutDisplayFunc(display);
382     glutMouseFunc(mouse);
383     glutMotionFunc(motion);
384     glutKeyboardFunc(keyboard);
385
386     glutIdleFunc(display);
387
388     return winid;
389 }
Note: See TracBrowser for help on using the browser.