fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Cluster / Base / testClusterClient.cpp
blobe285290155540134ca63364eee3bac439def5411
1 // #define FRAMEINTERLEAVE
2 #include "OSGGLUT.h"
3 #include "OSGGLU.h"
4 #include "OSGConfig.h"
5 #include <iostream>
6 #include "OSGLog.h"
7 #include "OSGTransform.h"
8 #include "OSGGroup.h"
9 #include "OSGNode.h"
10 #include "OSGClusterWindow.h"
11 #include "OSGSceneFileHandler.h"
12 #include "OSGDirectionalLight.h"
13 #include "OSGSimpleGeometry.h"
14 #include "OSGCamera.h"
15 #include "OSGViewport.h"
16 #include "OSGPerspectiveCamera.h"
17 #include "OSGSolidBackground.h"
18 #include "OSGGradientBackground.h"
19 #include "OSGTrackball.h"
20 #include "OSGVolumeDraw.h"
21 #include "OSGGLUTWindow.h"
22 #include "OSGMultiDisplayWindow.h"
23 #include "OSGBalancedMultiWindow.h"
24 #include "OSGSortFirstWindow.h"
25 #include "OSGChunkMaterial.h"
26 #include "OSGSimpleMaterial.h"
27 #include "OSGPolygonChunk.h"
28 #include "OSGGeoFunctions.h"
29 #include "OSGMaterialGroup.h"
30 #include "OSGSortLastWindow.h"
31 #include "OSGImageComposer.h"
32 #include "OSGProxyGroup.h"
33 #ifdef FRAMEINTERLEAVE
34 #include "OSGFrameInterleaveWindow.h"
35 #endif
36 #include "OSGShearedStereoCameraDecorator.h"
37 #include "OSGNameAttachment.h"
38 #include "OSGColorBufferViewport.h"
40 #include "OSGPipelineComposer.h"
41 #include "OSGBinarySwapComposer.h"
42 #include "OSGRenderAction.h"
44 int winid;
45 OSG::Trackball tball;
46 int mouseb = 0;
47 int lastx=0, lasty=0;
48 int winwidth=300, winheight=300;
49 int winx=-1, winy=-1;
50 OSG::NodeRecPtr root;
51 OSG::TransformRecPtr cam_trans;
52 //OSG::PerspectiveCameraRecPtr cam;
53 OSG::ClusterWindowRecPtr clusterWindow;
54 OSG::RenderActionRefPtr ract;
55 OSG::GLUTWindowRecPtr clientWindow;
56 OSG::SortFirstWindowRecPtr sortfirst;
57 OSG::SortLastWindowRecPtr sortlast;
58 #ifdef FRAMEINTERLEAVE
59 OSG::FrameInterleaveWindowRecPtr frameinterleave;
60 #endif
61 OSG::MultiDisplayWindowRecPtr multidisplay;
62 OSG::BalancedMultiWindowRecPtr balancedmultidisplay;
63 bool animate=false;
64 int animLoops=-1;
65 int frameCount=0;
66 int animLength=30;
67 bool multiport=false;
68 float ca=-1,cb=-1,cc=-1;
69 OSG::Int32 stereoMode=0;
70 float eyedistance=1,zeroparallax=10;
71 int serverx=-1,servery=-1;
72 std::vector<OSG::Quaternion> animOri;
73 std::vector<OSG::Vec3f > animPos;
74 std::string animName="animation.txt";
75 OSG::Real32 animTime=0;
76 std::string serviceInterface;
77 bool serviceInterfaceValid = false;
78 std::string serviceAddress;
79 bool serviceAddressValid = false;
80 OSG::UInt32 interleave=0;
81 OSG::Real32 _dsFactor = 1.0; // scale down factor.
82 bool _enablecc = true; // enable color correction.
83 OSG::PolygonChunkRecPtr polygonChunk;
84 bool prepared=false;
85 bool showInfo=false;
86 OSG::Time frame_time=0;
87 OSG::UInt32 sum_positions=0;
88 OSG::UInt32 sum_geometries=0;
89 OSG::UInt32 sum_triangles=0;
90 bool info = false;
91 std::string connectionDestination="";
92 std::string connectionInterface="";
93 OSG::SolidBackgroundRecPtr bkgnd;
94 OSG::Int32 subtilesize=-1;
95 bool pipelinedBufferRead = false;
97 void cleanup(void)
99 root = NULL;
100 cam_trans = NULL;
101 // cam = NULL;
102 clusterWindow = NULL;
103 clientWindow = NULL;
104 sortfirst = NULL;
105 sortlast = NULL;
106 #ifdef FRAMEINTERLEAVE
107 frameinterleave = NULL;
108 #endif
109 multidisplay = NULL;
110 balancedmultidisplay = NULL;
111 polygonChunk = NULL;
112 bkgnd = NULL;
114 OSG::osgExit();
117 /*! Simple show text function
119 void displayInfo(int x, int y)
121 int len, i;
122 #ifdef WIN32
123 #ifdef OSG_WIN32_CL
124 void *font = (void *) 2;
125 #else
126 void *font = 2;
127 #endif
128 #else
129 #ifdef OSG_DEBUG_OLD_C_CASTS
130 void *font = glutBitmap9By15;
131 #else
132 void *font = GLUT_BITMAP_9_BY_15;
133 #endif
134 #endif
136 char text[1024];
137 sprintf(text,
138 "FPS: %12.1f\n"
139 "Positions: %12u\n"
140 "Triangles: %12u\n"
141 "Geometries: %12u",
142 1.0/frame_time,
143 sum_positions,
144 sum_triangles,
145 sum_geometries);
147 glPushAttrib(GL_ALL_ATTRIB_BITS);
148 glDisable(GL_LIGHTING);
149 glEnable(GL_COLOR_MATERIAL);
150 glPushMatrix();
151 glLoadIdentity();
152 glMatrixMode(GL_PROJECTION);
153 glPushMatrix();
154 glLoadIdentity();
155 gluOrtho2D(0,clientWindow->getWidth(),0,clientWindow->getHeight());
156 glDisable(GL_DEPTH_TEST);
157 glEnable(GL_BLEND);
158 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
160 int x1=x-5;
161 int x2=x1+24*9+10;
162 int y1=y+14;
163 int y2=y1-4*20;
164 glBegin(GL_QUADS);
165 glColor4f(.1f, .1f, .7f, .5f);
166 glVertex2i(x1,y1);
167 glVertex2i(x1,y2);
168 glVertex2i(x2,y2);
169 glVertex2i(x2,y1);
170 glEnd();
171 glBegin(GL_LINE_LOOP);
172 glColor3f(1.0, 1.0, 0.0);
173 glVertex2i(x1,y1);
174 glVertex2i(x1,y2);
175 glVertex2i(x2,y2);
176 glVertex2i(x2,y1);
177 glEnd();
179 glColor3f(1.0, 1.0, 0.0);
180 glRasterPos2f(x, y);
181 len = int(strlen(text));
182 for (i = 0; i < len; i++) {
183 if(text[i] == '\n')
185 y-=20;
186 glRasterPos2f(x, y);
188 else
189 glutBitmapCharacter(font, text[i]);
191 glPopMatrix();
192 glMatrixMode(GL_MODELVIEW);
193 glPopMatrix();
194 glPopAttrib();
197 void prepareSceneGraph(OSG::Node * const node)
199 if(!prepared)
201 polygonChunk = OSG::PolygonChunk::create();
202 prepared = true;
205 OSG::NodeCore *core =node->getCore();
206 if(core != NULL)
208 OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(core);
209 if(geo != NULL)
211 OSG::Material *mat = geo->getMaterial();
212 if(mat != NULL)
214 OSG::ChunkMaterial *cmat =
215 dynamic_cast<OSG::ChunkMaterial *>(mat);
216 if(cmat->find(OSG::PolygonChunk::getClassType()) == NULL)
218 cmat->addChunk(polygonChunk);
221 // get num positions
222 OSG::GeoVectorProperty *positionsPtr=geo->getPositions();
223 if(positionsPtr != NULL)
224 sum_positions += positionsPtr->size32();
225 // get num triangles
226 OSG::UInt32 triangle=0;
227 OSG::UInt32 line=0;
228 OSG::UInt32 point=0;
229 OSG::UInt32 patches=0;
230 calcPrimitiveCount(geo,triangle,line,point, patches);
231 sum_triangles += triangle;
232 // sum of geometry nodes
233 ++sum_geometries;
235 else
237 OSG::MaterialGroup *matGrp =
238 dynamic_cast<OSG::MaterialGroup *>(core);
239 if(matGrp != NULL)
241 OSG::Material *mat = matGrp->getMaterial();
242 if(mat != NULL)
244 OSG::ChunkMaterial *cmat =
245 dynamic_cast<OSG::ChunkMaterial *>(mat);
246 if(cmat->find(OSG::PolygonChunk::getClassType()) == NULL)
248 cmat->addChunk(polygonChunk);
252 else
254 OSG::ProxyGroup *proxy = dynamic_cast<OSG::ProxyGroup *>(core);
255 if(proxy != NULL)
257 sum_triangles += proxy->getTriangles();
258 sum_positions += proxy->getPositions();
259 sum_geometries += proxy->getGeometries();
264 for(OSG::MFUnrecChildNodePtr::const_iterator nI=node->getMFChildren()->begin();
265 nI != node->getMFChildren()->end();
266 ++nI)
268 prepareSceneGraph(*nI);
272 void loadAnim()
274 OSG::Real32 ax,ay,az,r,x,y,z;
275 FILE *file=fopen(animName.c_str(),"r");
277 animOri.clear();
278 animPos.clear();
279 if(!file)
280 return;
281 while(fscanf(file,"%f %f %f %f,%f %f %f",&ax,&ay,&az,&r,&x,&y,&z)==7)
283 animOri.push_back(OSG::Quaternion(OSG::Vec3f(ax,ay,az),r));
284 animPos.push_back(OSG::Vec3f(x,y,z));
286 fclose(file);
289 void display(void)
291 OSG::Time t;
293 // std::cout << glutGet(GLUT_WINDOW_WIDTH) << std::endl;
295 t=-OSG::getSystemTime();
297 if(animate && animPos.size()>1)
299 if(animLength>0)
300 animTime = frameCount * (animPos.size())/float(animLength);
302 OSG::UInt32 i=OSG::UInt32(animTime);
303 OSG::Real32 a=animTime-i;
305 OSG::Vec3f v;
306 OSG::Quaternion q;
307 if(i+1 < animPos.size())
309 v = animPos[i] + (animPos[i+1] - animPos[i]) * a;
310 q = OSG::Quaternion::slerp(animOri[i],animOri[i+1],a);
312 else
314 v = animPos[i];
315 q = animOri[i];
317 cam_trans->editMatrix().setTranslate(v[0],v[1],v[2]);
318 cam_trans->editMatrix().setRotate(q);
320 else
322 cam_trans->editSFMatrix()->setValue( tball.getFullTrackballMatrix() );
327 OSG::Thread::getCurrentChangeList()->commitChanges();
328 #if 0
329 clusterWindow->activate();
330 clusterWindow->frameInit();
331 clusterWindow->renderAllViewports(ract);
332 #endif
333 clusterWindow->renderNoFinish(ract);
334 #if 0
335 if(showInfo)
337 displayInfo(10,90);
339 char text[1024];
340 sprintf(text,"FPS: %12.1f",1.0/frame_time);
341 showText(10,70,text);
342 sprintf(text,"Positions: %12d",sum_positions);
343 showText(10,50,text);
344 sprintf(text,"Triangles: %12d",sum_triangles);
345 showText(10,30,text);
346 sprintf(text,"Geometries: %12d",sum_geometries);
347 showText(10,10,text);
350 #endif
351 #if 0
352 clusterWindow->swap();
353 clusterWindow->frameExit();
354 #endif
355 clusterWindow->frameFinish();
356 // clear changelist from prototypes
357 OSG::Thread::getCurrentChangeList()->clear();
359 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
361 std::cout << e.what() << std::endl;
362 cleanup();
363 exit(0);
366 t+=OSG::getSystemTime();
367 frame_time = t;
369 if(animate && animPos.size()>1)
371 OSG::Vec3f v;
373 printf("Frame %8.3f %8.5f %8.3f\n",
374 animTime,
375 t,1/t);
377 frameCount++;
378 if(frameCount == animLength)
380 animTime = 0;
381 frameCount = 0;
383 if(animLoops > 0)
385 animLoops--;
386 if(!animLoops)
388 cleanup();
389 exit(0);
397 void reshape( int width, int height )
399 printf("reshape %d %d\n",width,height);
400 glViewport(0, 0, width, height);
402 clientWindow->resize( width, height );
404 glutPostRedisplay();
407 void motion(int x, int y)
409 OSG::Real32 w = clientWindow->getWidth(), h = clientWindow->getHeight();
412 OSG::Real32 a = -2. * ( lastx / w - .5 ),
413 b = -2. * ( .5 - lasty / h ),
414 c = -2. * ( x / w - .5 ),
415 d = -2. * ( .5 - y / h );
417 if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) )
419 tball.updateRotation( a, b, c, d );
421 else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) )
423 tball.updatePosition( a, b, c, d );
425 else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) )
427 tball.updatePositionNeg( a, b, c, d );
429 lastx = x;
430 lasty = y;
431 glutPostRedisplay();
434 void mouse(int button, int state, int x, int y)
436 if ( state == 0 )
438 switch ( button )
440 case GLUT_LEFT_BUTTON: break;
441 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(true);
442 break;
443 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(true);
444 break;
446 mouseb |= 1 << button;
448 else if ( state == 1 )
450 switch ( button )
452 case GLUT_LEFT_BUTTON: break;
453 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(false);
454 break;
455 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(false);
456 break;
458 mouseb &= ~(1 << button);
460 lastx = x;
461 lasty = y;
462 glutPostRedisplay();
465 void setHEyeWallParameter(OSG::Real32 dsFactor, bool enablecc)
467 static char str[1024];
469 OSG::NameUnrecPtr parameters = dynamic_cast<OSG::Name *>(clusterWindow->findAttachment(OSG::Name::getClassType()));
471 if(parameters == NULL)
473 parameters = OSG::Name::create();
474 clusterWindow->addAttachment(parameters);
477 sprintf(str, "downScale=%f colorCorrection=%d", dsFactor, enablecc);
478 parameters->editFieldPtr()->getValue().assign(str);
481 void key(unsigned char key, int /*x*/, int /*y*/)
483 switch ( key )
485 case 'l':
487 loadAnim();
488 break;
490 case 'c':
492 FILE *file=fopen(animName.c_str(),"w");
493 fclose(file);
494 animOri.clear();
495 animPos.clear();
496 frameCount = 0;
497 animTime = 0;
498 break;
500 case 's':
502 FILE *file=fopen(animName.c_str(),"a");
503 OSG::Matrix m=cam_trans->getMatrix();
504 OSG::Quaternion q(m);
505 OSG::Real32 ax,ay,az,r;
506 animPos.push_back(OSG::Vec3f(m[3][0],
507 m[3][1],
508 m[3][2]));
509 animOri.push_back(q);
510 q.getValueAsAxisRad(ax,ay,az,r);
511 fprintf(file,"%f %f %f %f,%f %f %f\n",ax,ay,az,r,
512 m[3][0],
513 m[3][1],
514 m[3][2]);
515 fclose(file);
516 frameCount = 0;
517 animTime = 0;
518 break;
520 case 'S':
522 FILE *file=fopen((animName+".wrl").c_str(),"w");
523 std::vector<OSG::Quaternion>::iterator qit;
525 fprintf(file,"DEF OriInter OrientationInterpolator {\n\tkey [");
526 for(size_t i = 0; i < animOri.size(); ++i)
528 fprintf(file, "%f", i / OSG::Real32(animOri.size() - 1) );
529 if(i < animOri.size() - 1)
530 fprintf(file,", ");
532 fprintf(file,"]\n\tkeyValue [");
533 for(qit = animOri.begin(); qit != animOri.end(); ++qit)
535 OSG::Real32 ax,ay,az,r;
536 (*qit).getValueAsAxisRad(ax,ay,az,r);
538 fprintf(file, "%f %f %f %f", ax, ay, az, r );
539 if(qit < animOri.end() - 1)
540 fprintf(file,", ");
542 fprintf(file,"]\n}\n\n");
544 std::vector<OSG::Vec3f>::iterator vit;
546 fprintf(file,"DEF PosInter PositionInterpolator {\n\tkey [");
547 for(size_t i = 0; i < animPos.size(); ++i)
549 fprintf(file, "%f", i / OSG::Real32(animPos.size() - 1) );
550 if(i < animPos.size() - 1)
551 fprintf(file,", ");
553 fprintf(file,"]\n\tkeyValue [");
554 for(vit = animPos.begin(); vit != animPos.end(); ++vit)
556 OSG::Vec3f v = *vit;
558 fprintf(file, "%f %f %f, ", v[0], v[1], v[2] );
560 fprintf(file,"]\n}\n\n");
562 fclose(file);
563 break;
565 case 'j':
566 if(sortfirst!=NULL)
568 sortfirst->setCompression("JPEG");
570 break;
571 case 'r':
572 if(sortfirst!=NULL)
574 sortfirst->setCompression("RLE");
576 break;
577 case 'n':
578 if(sortfirst!=NULL)
580 sortfirst->editCompression().erase();
582 break;
583 case 'i':
584 showInfo = !showInfo;
585 break;
586 case 'w':
587 if(polygonChunk->getFrontMode() == GL_FILL)
588 polygonChunk->setFrontMode(GL_LINE);
589 else
590 polygonChunk->setFrontMode(GL_FILL);
592 if(polygonChunk->getBackMode() == GL_FILL)
593 polygonChunk->setBackMode(GL_LINE);
594 else
595 polygonChunk->setBackMode(GL_FILL);
596 break;
597 case 'a':
598 if(animate)
600 glutIdleFunc(NULL);
601 animate=false;
603 else
605 glutIdleFunc(display);
606 animate=true;
608 frameCount = 0;
609 animTime = 0;
610 break;
611 case 'd':
612 // remove tree
613 while(root->getNChildren())
615 root->subChild(0u);
617 break;
618 case '+':
619 _dsFactor += 0.01f;
620 if(_dsFactor > 1.0f)
621 _dsFactor = 1.0f;
622 setHEyeWallParameter(_dsFactor, _enablecc);
623 break;
624 case '-':
625 _dsFactor -= 0.01f;
626 if(_dsFactor <= 0.0f)
627 _dsFactor = 0.01f;
628 setHEyeWallParameter(_dsFactor, _enablecc);
629 break;
630 case 'f':
631 if(_enablecc)
632 _enablecc = false;
633 else
634 _enablecc = true;
635 setHEyeWallParameter(_dsFactor, _enablecc);
636 break;
637 case 'B':
638 if(bkgnd->getColor()[0] == 0.0)
639 bkgnd->setColor( OSG::Color3f(1,1,1) );
640 else
641 bkgnd->setColor( OSG::Color3f(0,0,0) );
642 break;
643 case 27: // should kill the clients here
644 // exit
645 cleanup();
646 OSG::osgExit();
647 exit(0);
650 glutPostRedisplay();
653 void init(std::vector<std::string> &filenames)
655 size_t i;
656 OSG::DirectionalLightUnrecPtr dl;
657 OSG::Real32 x,y,z;
658 OSG::BoxVolume volume;
659 OSG::Vec3f min,max;
660 OSG::Vec3f size;
662 glEnable( GL_DEPTH_TEST );
663 glEnable( GL_LIGHTING );
664 glEnable( GL_LIGHT0 );
665 // GLint twoSide = 1;
666 // glLightModeliv(GL_LIGHT_MODEL_TWO_SIDE,&twoSide);
667 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
669 // create the graph
671 // beacon for camera and light
672 OSG::NodeUnrecPtr b1n = OSG::Node::create();
673 OSG::GroupUnrecPtr b1 = OSG::Group::create();
674 b1n->setCore( b1 );
676 // transformation
677 OSG::NodeUnrecPtr t1n = OSG::Node::create();
678 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
679 t1n->setCore( t1 );
680 t1n->addChild( b1n );
682 cam_trans = t1;
684 // light
686 OSG::NodeUnrecPtr dlight = OSG::Node::create();
687 dl = OSG::DirectionalLight::create();
689 dlight->setCore( dl );
691 dl->setAmbient( .3f, .3f, .3f, 1 );
692 dl->setDiffuse( 1, 1, 1, 1 );
693 dl->setDirection(0,0,1);
694 dl->setBeacon( b1n);
696 // root
697 root = OSG::Node::create();
699 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
701 root->setCore( gr1 );
702 root->addChild( t1n );
703 root->addChild( dlight );
705 // Load the file
706 OSG::NodeUnrecPtr scene = OSG::Node::create();
707 scene->setCore(OSG::Group::create());
709 OSG::NodeUnrecPtr file;
710 for(i=0;i<filenames.size();i++)
712 file = OSG::SceneFileHandler::the()->read(filenames[i].c_str(),0);
713 if(file != NULL)
714 scene->addChild(file);
715 else
716 std::cerr << "Couldn't load file, ignoring " << filenames[i] << std::endl;
718 if ( filenames.size()==0 )
720 file = OSG::makeTorus( .5, 2, 16, 16 );
721 scene->addChild(file);
722 // scene->addChild(makeBox(.6,.6,.6,5,5,5));
725 prepareSceneGraph(scene);
727 OSG::Thread::getCurrentChangeList()->commitChanges();
729 scene->invalidateVolume();
730 scene->updateVolume();
731 volume=scene->getVolume();
732 volume.getBounds(min,max);
733 size = max-min;
735 if(ca>0)
737 if(cb==-1)
738 cb=ca;
739 if(cc==-1)
740 cc=cb;
742 OSG::NodeUnrecPtr node;
743 OSG::NodeUnrecPtr geoNode;
744 OSG::TransformUnrecPtr trans;
745 for(x=-ca/2.0 ; x<ca/2.0 ; x++)
746 for(y=-cb/2.0 ; y<cb/2.0 ; y++)
747 for(z=-cc/2.0 ; z<cc/2.0 ; z++)
749 trans=OSG::Transform::create();
750 node=OSG::Node::create();
752 node->setCore(trans);
753 trans->editMatrix().setTranslate(
754 x*size[0]*1.1,
755 y*size[1]*1.1,
756 z*size[2]*1.1);
757 geoNode = OSG::cloneTree(scene);
758 geoNode->editSFVolume()->getValue() = scene->getSFVolume()->getValue();
759 geoNode->editVolume(false).setValid(true);
760 node->addChild( geoNode );
761 dlight->addChild(node);
764 else
766 dlight->addChild(scene);
769 if(ca>0)
771 sum_geometries*=OSG::UInt32(ca*cb*cc);
772 sum_triangles *=OSG::UInt32(ca*cb*cc);
773 sum_positions *=OSG::UInt32(ca*cb*cc);
776 OSG::Thread::getCurrentChangeList()->commitChanges();
778 // dlight->invalidateVolume();
779 printf("update Volume\n");
780 dlight->updateVolume();
781 printf("update Volume OK\n");
783 // should check first. ok for now.
784 const OSG::BoxVolume &vol = dlight->getVolume();
786 OSG::Pnt3f center;
788 vol.getBounds(min, max);
789 vol.getCenter(center);
791 size = max - min;
793 std::cout << "Volume: from " << min << " to " << max << std::endl;
794 std::cout << "Center: " << center << std::endl;
796 // Camera
798 OSG::PerspectiveCameraUnrecPtr cam = OSG::PerspectiveCamera::create();
799 cam->setBeacon( b1n );
800 cam->setFov( OSG::osgDegree2Rad( 60 ) );
801 cam->setNear( 10 );
802 cam->setFar( 50000 );
804 // Solid Background
805 bkgnd = OSG::SolidBackground::create();
806 bkgnd->setColor( OSG::Color3f(0,0,0) );
807 // bkgnd->setColor( OSG::Color3f(.1,.1,.6) );
808 // bkgnd->setColor( OSG::Color3f(1,1,1) );
810 // Viewport
811 OSG::ViewportUnrecPtr vp1;
812 OSG::ViewportUnrecPtr vp2;
813 if(stereoMode == 0)
815 vp1 = OSG::Viewport::create();
816 vp1->setCamera ( cam );
817 vp1->setBackground( bkgnd );
818 vp1->setRoot ( root );
819 vp1->setSize ( 0,0, 1,1 );
821 if(multiport)
823 vp2 = OSG::Viewport::create();
824 vp2->setCamera ( cam );
825 vp2->setBackground( bkgnd );
826 vp2->setRoot ( root );
827 vp2->setSize ( .1f, .55f, .7f,.95f );
830 else if(stereoMode == 1)
832 OSG::ShearedStereoCameraDecoratorUnrecPtr deco;
833 // left
834 deco=OSG::ShearedStereoCameraDecorator::create();
835 deco->setLeftEye(true);
836 deco->setEyeSeparation(eyedistance);
837 deco->setDecoratee(cam);
838 deco->setZeroParallaxDistance(zeroparallax);
839 vp1 = OSG::Viewport::create();
840 vp1->setCamera ( deco );
841 vp1->setBackground( bkgnd );
842 vp1->setRoot ( root );
843 vp1->setSize ( 0,0, .5,1 );
844 // right
845 deco=OSG::ShearedStereoCameraDecorator::create();
846 deco->setLeftEye(false);
847 deco->setEyeSeparation(eyedistance);
848 deco->setDecoratee(cam);
849 deco->setZeroParallaxDistance(zeroparallax);
850 vp2 = OSG::Viewport::create();
851 vp2->setCamera ( deco );
852 vp2->setBackground( bkgnd );
853 vp2->setRoot ( root );
854 vp2->setSize ( .5,0,1,1 );
856 else if(stereoMode == 2)
858 OSG::ShearedStereoCameraDecoratorUnrecPtr deco;
859 // left
860 deco=OSG::ShearedStereoCameraDecorator::create();
861 deco->setLeftEye(true);
862 deco->setEyeSeparation(eyedistance);
863 deco->setDecoratee(cam);
864 deco->setZeroParallaxDistance(zeroparallax);
866 OSG::ColorBufferViewportUnrecPtr cvp1 =
867 OSG::ColorBufferViewport::create();
868 cvp1->setCamera ( deco );
869 cvp1->setBackground( bkgnd );
870 cvp1->setRoot ( root );
871 cvp1->setSize ( 0,0, 1,1 );
872 cvp1->setRed(GL_FALSE);
873 cvp1->setGreen(GL_TRUE);
874 cvp1->setBlue(GL_TRUE);
875 cvp1->setAlpha(GL_TRUE);
876 vp1 = cvp1;
878 // right
879 deco=OSG::ShearedStereoCameraDecorator::create();
880 deco->setLeftEye(false);
881 deco->setEyeSeparation(eyedistance);
882 deco->setDecoratee(cam);
883 deco->setZeroParallaxDistance(zeroparallax);
885 OSG::ColorBufferViewportUnrecPtr cvp2 =
886 OSG::ColorBufferViewport::create();
887 cvp2->setCamera ( deco );
888 cvp2->setBackground( bkgnd );
889 cvp2->setRoot ( root );
890 cvp2->setSize ( 0,0,1,1 );
891 cvp2->setRed(GL_TRUE);
892 cvp2->setGreen(GL_FALSE);
893 cvp2->setBlue(GL_FALSE);
894 cvp2->setAlpha(GL_FALSE);
895 vp2 = cvp2;
898 GLint glvp[4];
899 glGetIntegerv( GL_VIEWPORT, glvp );
901 if(serverx>0 && servery>0)
902 clusterWindow->setSize( serverx, servery );
903 else
904 clusterWindow->setSize( glvp[2], glvp[3] );
905 clusterWindow->addPort( vp1 );
907 if(multiport || stereoMode > 0)
908 clusterWindow->addPort( vp2 );
910 if(serviceInterfaceValid == true)
912 clusterWindow->setServiceInterface(serviceInterface);
914 fprintf(stderr, "tcclient use if %s\n", serviceInterface.c_str());
917 if(serviceAddressValid == true)
919 clusterWindow->setServiceAddress(serviceAddress);
921 fprintf(stderr, "tcclient use ba %s\n", serviceAddress.c_str());
924 // tball
926 OSG::Vec3f pos(min[0] + ((max[0] - min[0]) * 0.5),
927 min[1] + ((max[1] - min[1]) * 0.5),
928 max[2] + ( max[2] - min[2] ) * 1.5 );
930 float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;
932 tball.setMode( OSG::Trackball::OSGObject );
933 tball.setStartPosition( pos, true );
934 tball.setSum( true );
935 tball.setTranslationMode( OSG::Trackball::OSGFree );
936 tball.setTranslationScale(scale);
937 tball.setRotationCenter(center);
938 tball.setTranslationGen(OSG::Trackball::OSGAbsoluteTranslation);
940 // run...
941 std::cout << size.length() << std::endl;
942 cam->setFar (size.length() * 100.0);
943 cam->setNear(size.length() * 100.0 / 100000.0);
946 int doMain(int argc,char **argv)
948 int i;
949 char *opt;
950 std::vector<std::string> filenames;
951 std::vector<std::string> servers;
952 std::string connectionType = "StreamSock";
953 std::string connectionParameters;
954 int rows=1;
955 int cols=-1;
956 char type='M';
957 bool clientRendering=true;
958 bool compose=false;
960 std::string composerType="";
961 std::string autostart;
963 for(i=1;i<argc;i++)
965 if(strlen(argv[i])>1 && argv[i][0]=='-')
967 switch(argv[i][1])
969 case 'o':
970 opt = argv[i][2] ? argv[i]+2 : argv[++i];
971 connectionParameters = opt;
972 printf("connectionParameters: '%s'\n", connectionParameters.c_str());
973 break;
974 case 'A':
975 opt = argv[i][2] ? argv[i]+2 : argv[++i];
976 autostart = opt;
977 break;
978 case 'D':
979 opt = argv[i][2] ? argv[i]+2 : argv[++i];
980 if(sscanf(opt,"%f,%f,%f",&ca,&cb,&cc)!=3)
982 std::cout << "Copy opton -D x,y,z" << std::endl;
983 return 1;
985 break;
986 case 'b':
987 opt = argv[i][2] ? argv[i]+2 : argv[++i];
988 serviceInterface.assign(opt);
989 serviceInterfaceValid = true;
990 break;
991 case 'B':
992 opt = argv[i][2] ? argv[i]+2 : argv[++i];
993 serviceAddress.assign(opt);
994 serviceAddressValid = true;
995 break;
996 case 'f':
997 opt = argv[i][2] ? argv[i]+2 : argv[++i];
998 filenames.push_back(opt);
999 printf("<%s>\n",opt);
1000 break;
1001 case 'm':
1002 connectionType="Multicast";
1003 break;
1004 case 'r':
1005 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1006 if(sscanf(opt,"%d,%d",&rows,&cols) != 2)
1007 sscanf(opt,"%d",&rows);
1008 break;
1009 case 't':
1010 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1011 subtilesize=atoi(opt);
1012 break;
1013 #ifdef FRAMEINTERLEAVE
1014 case 'i':
1015 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1016 interleave=atoi(opt);
1017 break;
1018 #endif
1019 case 'C':
1020 compose=true;
1021 break;
1022 case 'F':
1023 type='F';
1024 break;
1025 case 'X':
1026 type='X';
1027 break;
1028 case 'P':
1029 type='P';
1030 break;
1031 case 'L':
1033 type='L';
1034 int lpos=2;
1035 while(argv[i][lpos])
1037 if(argv[i][lpos] == 'B')
1038 composerType = "BinarySwapComposer";
1039 if(argv[i][lpos] == 'P')
1040 composerType = "PipelineComposer";
1041 if(argv[i][lpos] == 'S')
1042 composerType = "SepiaComposer";
1043 if(argv[i][lpos] == 'p')
1044 pipelinedBufferRead = true;
1045 ++lpos;
1047 break;
1049 case 'M':
1050 type='M';
1051 break;
1052 case 'I':
1053 type='I';
1054 break;
1055 case 's':
1056 stereoMode=1;
1057 break;
1058 case 'c':
1059 stereoMode=2;
1060 break;
1061 case 'S':
1062 info=true;
1063 break;
1064 case 'e':
1065 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1066 sscanf(opt,"%f",&eyedistance);
1067 break;
1068 case 'z':
1069 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1070 sscanf(opt,"%f",&zeroparallax);
1071 break;
1072 case 'd':
1073 clientRendering=false;
1074 break;
1075 case 'v':
1076 multiport=true;
1077 break;
1078 case 'x':
1079 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1080 sscanf(opt,"%d",&serverx);
1081 break;
1082 case 'y':
1083 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1084 sscanf(opt,"%d",&servery);
1085 break;
1086 case 'a':
1087 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1088 animName=opt;
1089 loadAnim();
1090 animate=true;
1091 break;
1092 case 'l':
1093 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1094 if(sscanf(opt,"%d,%d",&animLoops,&animLength) != 2)
1096 animLength = 30;
1097 if(sscanf(opt,"%d",&animLoops) != 1)
1099 animLoops = -1;
1102 break;
1103 case 'g':
1104 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1105 if(sscanf(opt,"%d,%d,%d,%d",
1106 &winwidth,&winheight,&winx,&winy) != 4)
1107 sscanf(opt,"%d,%d",&winwidth,&winheight);
1108 break;
1109 case 'G':
1110 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1111 connectionDestination = opt;
1112 break;
1113 case 'i':
1114 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1115 connectionInterface = opt;
1116 break;
1117 default:
1118 std::cout << argv[0]
1119 << "-ffile -m -rrows[,cols] -C -M"
1120 << std::endl;
1121 std::cout << "-m use multicast" << std::endl
1122 << "-G multicast group" << std::endl
1123 << "-i interface" << std::endl
1124 << "-b service interface" << std::endl
1125 << "-M multi display" << std::endl
1126 #ifdef FRAMEINTERLEAVE
1127 << "-I frame interleave" << std::endl
1128 #endif
1129 << "-r number of display rows" << std::endl
1130 << "-C compose" << std::endl
1131 << "-F sort-first" << std::endl
1132 << "-L sort-last" << std::endl
1133 << "-h this msg" << std::endl
1134 << "-s stereo" << std::endl
1135 << "-c red/cyan stereo" << std::endl
1136 << "-e eye distance" << std::endl
1137 << "-z zero parallax" << std::endl
1138 << "-d disable client rendering"<<std::endl
1139 << "-v use two viewports" << std::endl
1140 << "-x server x resolution" << std::endl
1141 << "-y server y resolution" << std::endl
1142 << "-t subtile size for img composition" << std::endl
1143 << "-D x,y,z duplicate geometry" << std::endl
1144 << "-A Autostart command" << std::endl
1145 << "-o connection parameter string e.g. \"TTL=8\"" << std::endl;
1146 return 0;
1149 else
1151 servers.push_back(argv[i]);
1155 OSG::ChangeList::setReadWriteDefault(true);
1156 OSG::osgInit(argc, argv);
1157 glutInit(&argc, argv);
1158 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
1159 if(winx >=0 && winy >=0)
1160 glutInitWindowPosition(winx,winy);
1161 glutInitWindowSize(winwidth,winheight);
1162 winid = glutCreateWindow("OpenSG Cluster Client");
1163 glutKeyboardFunc(key);
1164 glutReshapeFunc(reshape);
1165 glutDisplayFunc(display);
1166 if(animate)
1167 glutIdleFunc(display);
1168 glutMouseFunc(mouse);
1169 glutMotionFunc(motion);
1170 ract = OSG::RenderAction::create();
1171 #ifdef OSG_OLD_RENDER_ACTION
1172 ract->setSortTrans(true);
1173 ract->setZWriteTrans(true);
1174 ract->setLocalLights(true);
1175 ract->setCorrectTwoSidedLighting(true);
1176 #endif
1178 // clear changelist from prototypes
1179 OSG::Thread::getCurrentChangeList()->clear();
1181 // create cluster window
1182 switch(type)
1184 case 'M':
1185 multidisplay=OSG::MultiDisplayWindow::create();
1186 clusterWindow=multidisplay;
1187 break;
1188 case 'X':
1189 balancedmultidisplay=OSG::BalancedMultiWindow::create();
1190 clusterWindow=balancedmultidisplay;
1191 break;
1192 case 'F':
1193 sortfirst=OSG::SortFirstWindow::create();
1194 if(compose)
1195 sortfirst->setCompose(true);
1196 else
1197 sortfirst->setCompose(false);
1198 clusterWindow=sortfirst;
1199 break;
1200 case 'L':
1201 sortlast=OSG::SortLastWindow::create();
1202 if(!composerType.empty())
1204 OSG::FieldContainerUnrecPtr fcPtr =
1205 OSG::FieldContainerFactory::the()->
1206 createContainer(composerType.c_str());
1207 OSG::ImageComposer *icPtr =
1208 dynamic_cast<OSG::ImageComposer *>(fcPtr.get());
1210 if(icPtr != NULL)
1212 if(dynamic_cast<OSG::PipelineComposer *>(icPtr) != NULL)
1214 if(subtilesize>0)
1215 dynamic_cast<OSG::PipelineComposer *>(icPtr)->setTileSize(subtilesize);
1216 dynamic_cast<OSG::PipelineComposer *>(icPtr)->setPipelined(pipelinedBufferRead);
1218 if(dynamic_cast<OSG::BinarySwapComposer *>(icPtr) != NULL)
1220 if(subtilesize>0)
1221 dynamic_cast<OSG::BinarySwapComposer *>(icPtr)->setTileSize(subtilesize);
1223 icPtr->setStatistics(info);
1224 // icPtr->setShort(false);
1225 sortlast->setComposer(icPtr);
1228 clusterWindow=sortlast;
1229 break;
1230 #ifdef FRAMEINTERLEAVE
1231 case 'I':
1232 frameinterleave=OSG::FrameInterleaveWindow::create();
1233 clusterWindow=frameinterleave;
1234 if(compose)
1235 frameinterleave->setCompose(true);
1236 else
1237 frameinterleave->setCompose(false);
1238 break;
1239 #endif
1240 case 'P':
1241 sortfirst=OSG::SortFirstWindow::create();
1242 sortfirst->setCompose(false);
1243 clusterWindow=sortfirst;
1244 break;
1247 if(!autostart.empty())
1248 clusterWindow->editMFAutostart()->push_back(autostart);
1250 for(i=0 ; i<int(servers.size()) ; ++i)
1251 clusterWindow->editMFServers()->push_back(servers[i]);
1252 if(cols < 0)
1253 cols = clusterWindow->getMFServers()->size32() / rows;
1254 switch(type)
1256 case 'M':
1257 multidisplay->setHServers(cols);
1258 multidisplay->setVServers(rows);
1259 break;
1260 case 'X':
1261 balancedmultidisplay->setHServers(cols);
1262 balancedmultidisplay->setVServers(rows);
1263 // balancedmultidisplay->setShowBalancing(true);
1264 // balancedmultidisplay->setShowBalancing(info);
1265 break;
1267 #ifdef FRAMEINTERLEAVE
1268 clusterWindow->setInterleave(interleave);
1269 #endif
1271 // create client window
1272 clientWindow=OSG::GLUTWindow::create();
1273 // glutReshapeWindow(800,600);
1274 glutReshapeWindow(winwidth,winheight);
1275 clientWindow->setGlutId(winid);
1276 clientWindow->init();
1278 // init scene graph
1279 init(filenames);
1281 // init client
1282 clusterWindow->setConnectionType(connectionType);
1283 // needs to be called before init()!
1284 clusterWindow->setConnectionParams(connectionParameters);
1285 if(clientRendering)
1287 clusterWindow->setClientWindow(clientWindow);
1289 clusterWindow->setConnectionDestination(connectionDestination);
1290 clusterWindow->setConnectionInterface(connectionInterface);
1291 clusterWindow->init();
1292 if(serverx > 0)
1293 clusterWindow->resize(serverx,servery);
1294 else
1295 clusterWindow->resize(winwidth,winheight);
1296 clientWindow->resize(winwidth,winheight);
1298 // OSG::FieldContainerFactory::the()->dump();
1300 OSG::commitChanges();
1302 glutMainLoop();
1304 return 0;
1307 int main(int argc,char **argv)
1309 doMain(argc, argv);
1311 glutMainLoop();
1313 return 0;