fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / BackgroundLoader / testBackgroundLoaderClusterClient.cpp
blob222196307121d1b4ef2fbfc286e7e310757464e6
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 #include "OSGBackgroundLoader.h"
45 #include "OSGModelRequest.h"
47 int winid;
48 OSG::Trackball tball;
49 int mouseb = 0;
50 int lastx=0, lasty=0;
51 int winwidth=300, winheight=300;
52 int winx=-1, winy=-1;
53 OSG::NodeRecPtr root;
54 OSG::TransformRecPtr cam_trans;
55 //OSG::PerspectiveCameraRecPtr cam;
56 OSG::ClusterWindowRecPtr clusterWindow;
57 OSG::RenderActionRefPtr ract;
58 OSG::GLUTWindowRecPtr clientWindow;
59 OSG::SortFirstWindowRecPtr sortfirst;
60 OSG::SortLastWindowRecPtr sortlast;
61 #ifdef FRAMEINTERLEAVE
62 OSG::FrameInterleaveWindowRecPtr frameinterleave;
63 #endif
64 OSG::MultiDisplayWindowRecPtr multidisplay;
65 OSG::BalancedMultiWindowRecPtr balancedmultidisplay;
66 bool animate=false;
67 int animLoops=-1;
68 int frameCount=0;
69 int animLength=30;
70 bool multiport=false;
71 float ca=-1,cb=-1,cc=-1;
72 OSG::Int32 stereoMode=0;
73 float eyedistance=1,zeroparallax=10;
74 int serverx=-1,servery=-1;
75 std::vector<OSG::Quaternion> animOri;
76 std::vector<OSG::Vec3f > animPos;
77 std::string animName="animation.txt";
78 OSG::Real32 animTime=0;
79 std::string serviceInterface;
80 bool serviceInterfaceValid = false;
81 std::string serviceAddress;
82 bool serviceAddressValid = false;
83 OSG::UInt32 interleave=0;
84 OSG::Real32 _dsFactor = 1.0; // scale down factor.
85 bool _enablecc = true; // enable color correction.
86 OSG::PolygonChunkRecPtr polygonChunk;
87 bool prepared=false;
88 bool showInfo=false;
89 OSG::Time frame_time=0;
90 OSG::UInt32 sum_positions=0;
91 OSG::UInt32 sum_geometries=0;
92 OSG::UInt32 sum_triangles=0;
93 bool info = false;
94 std::string connectionDestination="";
95 std::string connectionInterface="";
96 OSG::SolidBackgroundRecPtr bkgnd;
97 OSG::Int32 subtilesize=-1;
98 bool pipelinedBufferRead = false;
100 void cleanup(void)
102 root = NULL;
103 cam_trans = NULL;
104 // cam = NULL;
105 clusterWindow = NULL;
106 clientWindow = NULL;
107 sortfirst = NULL;
108 sortlast = NULL;
109 #ifdef FRAMEINTERLEAVE
110 frameinterleave = NULL;
111 #endif
112 multidisplay = NULL;
113 balancedmultidisplay = NULL;
114 polygonChunk = NULL;
115 bkgnd = NULL;
117 OSG::osgExit();
120 /*! Simple show text function
122 void displayInfo(int x, int y)
124 int len, i;
125 #ifdef WIN32
126 #ifdef OSG_WIN32_CL
127 void *font = (void *) 2;
128 #else
129 void *font = 2;
130 #endif
131 #else
132 #ifdef OSG_DEBUG_OLD_C_CASTS
133 void *font = glutBitmap9By15;
134 #else
135 void *font = GLUT_BITMAP_9_BY_15;
136 #endif
137 #endif
139 char text[1024];
140 sprintf(text,
141 "FPS: %12.1f\n"
142 "Positions: %12u\n"
143 "Triangles: %12u\n"
144 "Geometries: %12u",
145 1.0/frame_time,
146 sum_positions,
147 sum_triangles,
148 sum_geometries);
150 glPushAttrib(GL_ALL_ATTRIB_BITS);
151 glDisable(GL_LIGHTING);
152 glEnable(GL_COLOR_MATERIAL);
153 glPushMatrix();
154 glLoadIdentity();
155 glMatrixMode(GL_PROJECTION);
156 glPushMatrix();
157 glLoadIdentity();
158 gluOrtho2D(0,clientWindow->getWidth(),0,clientWindow->getHeight());
159 glDisable(GL_DEPTH_TEST);
160 glEnable(GL_BLEND);
161 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
163 int x1=x-5;
164 int x2=x1+24*9+10;
165 int y1=y+14;
166 int y2=y1-4*20;
167 glBegin(GL_QUADS);
168 glColor4f(.1f, .1f, .7f, .5f);
169 glVertex2i(x1,y1);
170 glVertex2i(x1,y2);
171 glVertex2i(x2,y2);
172 glVertex2i(x2,y1);
173 glEnd();
174 glBegin(GL_LINE_LOOP);
175 glColor3f(1.0, 1.0, 0.0);
176 glVertex2i(x1,y1);
177 glVertex2i(x1,y2);
178 glVertex2i(x2,y2);
179 glVertex2i(x2,y1);
180 glEnd();
182 glColor3f(1.0, 1.0, 0.0);
183 glRasterPos2f(x, y);
184 len = int(strlen(text));
185 for (i = 0; i < len; i++) {
186 if(text[i] == '\n')
188 y-=20;
189 glRasterPos2f(x, y);
191 else
192 glutBitmapCharacter(font, text[i]);
194 glPopMatrix();
195 glMatrixMode(GL_MODELVIEW);
196 glPopMatrix();
197 glPopAttrib();
200 void prepareSceneGraph(OSG::Node * const node)
202 if(!prepared)
204 polygonChunk = OSG::PolygonChunk::create();
205 prepared = true;
208 OSG::NodeCore *core =node->getCore();
209 if(core != NULL)
211 OSG::Geometry *geo = dynamic_cast<OSG::Geometry *>(core);
212 if(geo != NULL)
214 OSG::Material *mat = geo->getMaterial();
215 if(mat != NULL)
217 OSG::ChunkMaterial *cmat =
218 dynamic_cast<OSG::ChunkMaterial *>(mat);
219 if(cmat->find(OSG::PolygonChunk::getClassType()) == NULL)
221 cmat->addChunk(polygonChunk);
224 // get num positions
225 OSG::GeoVectorProperty *positionsPtr=geo->getPositions();
226 if(positionsPtr != NULL)
227 sum_positions += positionsPtr->size32();
228 // get num triangles
229 OSG::UInt32 triangle=0;
230 OSG::UInt32 line=0;
231 OSG::UInt32 point=0;
232 OSG::UInt32 patches=0;
233 calcPrimitiveCount(geo,triangle,line,point, patches);
234 sum_triangles += triangle;
235 // sum of geometry nodes
236 ++sum_geometries;
238 else
240 OSG::MaterialGroup *matGrp =
241 dynamic_cast<OSG::MaterialGroup *>(core);
242 if(matGrp != NULL)
244 OSG::Material *mat = matGrp->getMaterial();
245 if(mat != NULL)
247 OSG::ChunkMaterial *cmat =
248 dynamic_cast<OSG::ChunkMaterial *>(mat);
249 if(cmat->find(OSG::PolygonChunk::getClassType()) == NULL)
251 cmat->addChunk(polygonChunk);
255 else
257 OSG::ProxyGroup *proxy = dynamic_cast<OSG::ProxyGroup *>(core);
258 if(proxy != NULL)
260 sum_triangles += proxy->getTriangles();
261 sum_positions += proxy->getPositions();
262 sum_geometries += proxy->getGeometries();
267 for(OSG::MFUnrecChildNodePtr::const_iterator nI=node->getMFChildren()->begin();
268 nI != node->getMFChildren()->end();
269 ++nI)
271 prepareSceneGraph(*nI);
275 void loadAnim()
277 OSG::Real32 ax,ay,az,r,x,y,z;
278 FILE *file=fopen(animName.c_str(),"r");
280 animOri.clear();
281 animPos.clear();
282 if(!file)
283 return;
284 while(fscanf(file,"%f %f %f %f,%f %f %f",&ax,&ay,&az,&r,&x,&y,&z)==7)
286 animOri.push_back(OSG::Quaternion(OSG::Vec3f(ax,ay,az),r));
287 animPos.push_back(OSG::Vec3f(x,y,z));
289 fclose(file);
292 void display(void)
294 OSG::Time t;
296 // std::cout << glutGet(GLUT_WINDOW_WIDTH) << std::endl;
298 t=-OSG::getSystemTime();
300 if(animate && animPos.size()>1)
302 if(animLength>0)
303 animTime = frameCount * (animPos.size())/float(animLength);
305 OSG::UInt32 i=OSG::UInt32(animTime);
306 OSG::Real32 a=animTime-i;
308 OSG::Vec3f v;
309 OSG::Quaternion q;
310 if(i+1 < animPos.size())
312 v = animPos[i] + (animPos[i+1] - animPos[i]) * a;
313 q = OSG::Quaternion::slerp(animOri[i],animOri[i+1],a);
315 else
317 v = animPos[i];
318 q = animOri[i];
320 cam_trans->editMatrix().setTranslate(v[0],v[1],v[2]);
321 cam_trans->editMatrix().setRotate(q);
323 else
325 cam_trans->editSFMatrix()->setValue( tball.getFullTrackballMatrix() );
330 OSG::BackgroundLoader::the()->sync();
333 OSG::Thread::getCurrentChangeList()->commitChanges();
334 #if 0
335 clusterWindow->activate();
336 clusterWindow->frameInit();
337 clusterWindow->renderAllViewports(ract);
338 #endif
339 clusterWindow->renderNoFinish(ract);
340 #if 0
341 if(showInfo)
343 displayInfo(10,90);
345 char text[1024];
346 sprintf(text,"FPS: %12.1f",1.0/frame_time);
347 showText(10,70,text);
348 sprintf(text,"Positions: %12d",sum_positions);
349 showText(10,50,text);
350 sprintf(text,"Triangles: %12d",sum_triangles);
351 showText(10,30,text);
352 sprintf(text,"Geometries: %12d",sum_geometries);
353 showText(10,10,text);
356 #endif
357 #if 0
358 clusterWindow->swap();
359 clusterWindow->frameExit();
360 #endif
361 clusterWindow->frameFinish();
362 // clear changelist from prototypes
363 OSG::Thread::getCurrentChangeList()->clear();
365 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
367 std::cout << e.what() << std::endl;
368 cleanup();
369 exit(0);
372 t+=OSG::getSystemTime();
373 frame_time = t;
375 if(animate && animPos.size()>1)
377 OSG::Vec3f v;
379 printf("Frame %8.3f %8.5f %8.3f\n",
380 animTime,
381 t,1/t);
383 frameCount++;
384 if(frameCount == animLength)
386 animTime = 0;
387 frameCount = 0;
389 if(animLoops > 0)
391 animLoops--;
392 if(!animLoops)
394 cleanup();
395 exit(0);
403 void reshape( int width, int height )
405 printf("reshape %d %d\n",width,height);
406 glViewport(0, 0, width, height);
408 clientWindow->resize( width, height );
410 glutPostRedisplay();
413 void motion(int x, int y)
415 OSG::Real32 w = clientWindow->getWidth(), h = clientWindow->getHeight();
418 OSG::Real32 a = -2. * ( lastx / w - .5 ),
419 b = -2. * ( .5 - lasty / h ),
420 c = -2. * ( x / w - .5 ),
421 d = -2. * ( .5 - y / h );
423 if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) )
425 tball.updateRotation( a, b, c, d );
427 else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) )
429 tball.updatePosition( a, b, c, d );
431 else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) )
433 tball.updatePositionNeg( a, b, c, d );
435 lastx = x;
436 lasty = y;
437 glutPostRedisplay();
440 void mouse(int button, int state, int x, int y)
442 if ( state == 0 )
444 switch ( button )
446 case GLUT_LEFT_BUTTON: break;
447 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(true);
448 break;
449 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(true);
450 break;
452 mouseb |= 1 << button;
454 else if ( state == 1 )
456 switch ( button )
458 case GLUT_LEFT_BUTTON: break;
459 case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(false);
460 break;
461 case GLUT_RIGHT_BUTTON: tball.setAutoPositionNeg(false);
462 break;
464 mouseb &= ~(1 << button);
466 lastx = x;
467 lasty = y;
468 glutPostRedisplay();
471 void setHEyeWallParameter(OSG::Real32 dsFactor, bool enablecc)
473 static char str[1024];
475 OSG::NameUnrecPtr parameters = dynamic_cast<OSG::Name *>(clusterWindow->findAttachment(OSG::Name::getClassType()));
477 if(parameters == NULL)
479 parameters = OSG::Name::create();
480 clusterWindow->addAttachment(parameters);
483 sprintf(str, "downScale=%f colorCorrection=%d", dsFactor, enablecc);
484 parameters->editFieldPtr()->getValue().assign(str);
487 void key(unsigned char key, int /*x*/, int /*y*/)
489 switch ( key )
491 case 'l':
493 loadAnim();
494 break;
496 case 'c':
498 FILE *file=fopen(animName.c_str(),"w");
499 fclose(file);
500 animOri.clear();
501 animPos.clear();
502 frameCount = 0;
503 animTime = 0;
504 break;
506 case 's':
508 FILE *file=fopen(animName.c_str(),"a");
509 OSG::Matrix m=cam_trans->getMatrix();
510 OSG::Quaternion q(m);
511 OSG::Real32 ax,ay,az,r;
512 animPos.push_back(OSG::Vec3f(m[3][0],
513 m[3][1],
514 m[3][2]));
515 animOri.push_back(q);
516 q.getValueAsAxisRad(ax,ay,az,r);
517 fprintf(file,"%f %f %f %f,%f %f %f\n",ax,ay,az,r,
518 m[3][0],
519 m[3][1],
520 m[3][2]);
521 fclose(file);
522 frameCount = 0;
523 animTime = 0;
524 break;
526 case 'S':
528 FILE *file=fopen((animName+".wrl").c_str(),"w");
529 std::vector<OSG::Quaternion>::iterator qit;
531 fprintf(file,"DEF OriInter OrientationInterpolator {\n\tkey [");
532 for(size_t i = 0; i < animOri.size(); ++i)
534 fprintf(file, "%f", i / OSG::Real32(animOri.size() - 1) );
535 if(i < animOri.size() - 1)
536 fprintf(file,", ");
538 fprintf(file,"]\n\tkeyValue [");
539 for(qit = animOri.begin(); qit != animOri.end(); ++qit)
541 OSG::Real32 ax,ay,az,r;
542 (*qit).getValueAsAxisRad(ax,ay,az,r);
544 fprintf(file, "%f %f %f %f", ax, ay, az, r );
545 if(qit < animOri.end() - 1)
546 fprintf(file,", ");
548 fprintf(file,"]\n}\n\n");
550 std::vector<OSG::Vec3f>::iterator vit;
552 fprintf(file,"DEF PosInter PositionInterpolator {\n\tkey [");
553 for(size_t i = 0; i < animPos.size(); ++i)
555 fprintf(file, "%f", i / OSG::Real32(animPos.size() - 1) );
556 if(i < animPos.size() - 1)
557 fprintf(file,", ");
559 fprintf(file,"]\n\tkeyValue [");
560 for(vit = animPos.begin(); vit != animPos.end(); ++vit)
562 OSG::Vec3f v = *vit;
564 fprintf(file, "%f %f %f, ", v[0], v[1], v[2] );
566 fprintf(file,"]\n}\n\n");
568 fclose(file);
569 break;
571 case 'j':
572 if(sortfirst!=NULL)
574 sortfirst->setCompression("JPEG");
576 break;
577 case 'r':
578 if(sortfirst!=NULL)
580 sortfirst->setCompression("RLE");
582 break;
583 case 'n':
584 if(sortfirst!=NULL)
586 sortfirst->editCompression().erase();
588 break;
589 case 'i':
590 showInfo = !showInfo;
591 break;
592 case 'w':
593 if(polygonChunk->getFrontMode() == GL_FILL)
594 polygonChunk->setFrontMode(GL_LINE);
595 else
596 polygonChunk->setFrontMode(GL_FILL);
598 if(polygonChunk->getBackMode() == GL_FILL)
599 polygonChunk->setBackMode(GL_LINE);
600 else
601 polygonChunk->setBackMode(GL_FILL);
602 break;
603 case 'a':
604 if(animate)
606 glutIdleFunc(NULL);
607 animate=false;
609 else
611 glutIdleFunc(display);
612 animate=true;
614 frameCount = 0;
615 animTime = 0;
616 break;
617 case 'd':
618 // remove tree
619 while(root->getNChildren())
621 root->subChild(0u);
623 break;
624 case '+':
625 _dsFactor += 0.01f;
626 if(_dsFactor > 1.0f)
627 _dsFactor = 1.0f;
628 setHEyeWallParameter(_dsFactor, _enablecc);
629 break;
630 case '-':
631 _dsFactor -= 0.01f;
632 if(_dsFactor <= 0.0f)
633 _dsFactor = 0.01f;
634 setHEyeWallParameter(_dsFactor, _enablecc);
635 break;
636 case 'f':
637 if(_enablecc)
638 _enablecc = false;
639 else
640 _enablecc = true;
641 setHEyeWallParameter(_dsFactor, _enablecc);
642 break;
643 case 'B':
644 if(bkgnd->getColor()[0] == 0.0)
645 bkgnd->setColor( OSG::Color3f(1,1,1) );
646 else
647 bkgnd->setColor( OSG::Color3f(0,0,0) );
648 break;
649 case 27: // should kill the clients here
650 // exit
651 cleanup();
652 OSG::osgExit();
653 exit(0);
656 glutPostRedisplay();
659 void init(std::vector<std::string> &filenames)
661 size_t i;
662 OSG::DirectionalLightUnrecPtr dl;
663 OSG::Real32 x,y,z;
664 OSG::BoxVolume volume;
665 OSG::Vec3f min,max;
666 OSG::Vec3f size;
668 glEnable( GL_DEPTH_TEST );
669 glEnable( GL_LIGHTING );
670 glEnable( GL_LIGHT0 );
671 // GLint twoSide = 1;
672 // glLightModeliv(GL_LIGHT_MODEL_TWO_SIDE,&twoSide);
673 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
675 // create the graph
677 // beacon for camera and light
678 OSG::NodeUnrecPtr b1n = OSG::Node::create();
679 OSG::GroupUnrecPtr b1 = OSG::Group::create();
680 b1n->setCore( b1 );
682 // transformation
683 OSG::NodeUnrecPtr t1n = OSG::Node::create();
684 OSG::TransformUnrecPtr t1 = OSG::Transform::create();
685 t1n->setCore( t1 );
686 t1n->addChild( b1n );
688 cam_trans = t1;
690 // light
692 OSG::NodeUnrecPtr dlight = OSG::Node::create();
693 dl = OSG::DirectionalLight::create();
695 dlight->setCore( dl );
697 dl->setAmbient( .3f, .3f, .3f, 1 );
698 dl->setDiffuse( 1, 1, 1, 1 );
699 dl->setDirection(0,0,1);
700 dl->setBeacon( b1n);
702 // root
703 root = OSG::Node::create();
705 OSG::GroupUnrecPtr gr1 = OSG::Group::create();
707 root->setCore( gr1 );
708 root->addChild( t1n );
709 root->addChild( dlight );
711 // Load the file
712 OSG::NodeUnrecPtr scene = OSG::Node::create();
713 scene->setCore(OSG::Group::create());
715 OSG::NodeUnrecPtr file;
716 for(i=0;i<filenames.size();i++)
718 #if 0
719 file = OSG::SceneFileHandler::the()->read(filenames[i].c_str(),0);
720 if(file != NULL)
721 scene->addChild(file);
722 else
723 std::cerr << "Couldn't load file, ignoring " << filenames[i] << std::endl;
724 #endif
725 OSG::ModelRequestPtr req =
726 OSG::ModelRequest::create()->init(scene,
727 filenames[i]);
729 req->setVerifyModel(false );
730 req->setGraphOp ("none");
732 OSG::BackgroundLoader::the()->addRequest(req);
734 // if ( filenames.size()==0 )
736 file = OSG::makeTorus( 5000, 10000, 16, 16 );
737 scene->addChild(file);
738 // scene->addChild(makeBox(.6,.6,.6,5,5,5));
741 prepareSceneGraph(scene);
743 // Start the background loader.
744 OSG::BackgroundLoader::the()->start();
746 OSG::Thread::getCurrentChangeList()->commitChanges();
748 scene->invalidateVolume();
749 scene->updateVolume();
750 volume=scene->getVolume();
751 volume.getBounds(min,max);
752 size = max-min;
754 if(ca>0)
756 if(cb==-1)
757 cb=ca;
758 if(cc==-1)
759 cc=cb;
761 OSG::NodeUnrecPtr node;
762 OSG::NodeUnrecPtr geoNode;
763 OSG::TransformUnrecPtr trans;
764 for(x=-ca/2.0 ; x<ca/2.0 ; x++)
765 for(y=-cb/2.0 ; y<cb/2.0 ; y++)
766 for(z=-cc/2.0 ; z<cc/2.0 ; z++)
768 trans=OSG::Transform::create();
769 node=OSG::Node::create();
771 node->setCore(trans);
772 trans->editMatrix().setTranslate(
773 x*size[0]*1.1,
774 y*size[1]*1.1,
775 z*size[2]*1.1);
776 geoNode = OSG::cloneTree(scene);
777 geoNode->editSFVolume()->getValue() = scene->getSFVolume()->getValue();
778 geoNode->editVolume(false).setValid(true);
779 node->addChild( geoNode );
780 dlight->addChild(node);
783 else
785 dlight->addChild(scene);
788 if(ca>0)
790 sum_geometries*=OSG::UInt32(ca*cb*cc);
791 sum_triangles *=OSG::UInt32(ca*cb*cc);
792 sum_positions *=OSG::UInt32(ca*cb*cc);
795 OSG::Thread::getCurrentChangeList()->commitChanges();
797 // dlight->invalidateVolume();
798 printf("update Volume\n");
799 dlight->updateVolume();
800 printf("update Volume OK\n");
802 // should check first. ok for now.
803 const OSG::BoxVolume &vol = dlight->getVolume();
805 OSG::Pnt3f center;
807 vol.getBounds(min, max);
808 vol.getCenter(center);
810 size = max - min;
812 std::cout << "Volume: from " << min << " to " << max << std::endl;
813 std::cout << "Center: " << center << std::endl;
815 // Camera
817 OSG::PerspectiveCameraUnrecPtr cam = OSG::PerspectiveCamera::create();
818 cam->setBeacon( b1n );
819 cam->setFov( OSG::osgDegree2Rad( 60 ) );
820 cam->setNear( 10 );
821 cam->setFar( 50000 );
823 // Solid Background
824 bkgnd = OSG::SolidBackground::create();
825 bkgnd->setColor( OSG::Color3f(0,0,0) );
826 // bkgnd->setColor( OSG::Color3f(.1,.1,.6) );
827 // bkgnd->setColor( OSG::Color3f(1,1,1) );
829 // Viewport
830 OSG::ViewportUnrecPtr vp1;
831 OSG::ViewportUnrecPtr vp2;
832 if(stereoMode == 0)
834 vp1 = OSG::Viewport::create();
835 vp1->setCamera ( cam );
836 vp1->setBackground( bkgnd );
837 vp1->setRoot ( root );
838 vp1->setSize ( 0,0, 1,1 );
840 if(multiport)
842 vp2 = OSG::Viewport::create();
843 vp2->setCamera ( cam );
844 vp2->setBackground( bkgnd );
845 vp2->setRoot ( root );
846 vp2->setSize ( .1f, .55f, .7f,.95f );
849 else if(stereoMode == 1)
851 OSG::ShearedStereoCameraDecoratorUnrecPtr deco;
852 // left
853 deco=OSG::ShearedStereoCameraDecorator::create();
854 deco->setLeftEye(true);
855 deco->setEyeSeparation(eyedistance);
856 deco->setDecoratee(cam);
857 deco->setZeroParallaxDistance(zeroparallax);
858 vp1 = OSG::Viewport::create();
859 vp1->setCamera ( deco );
860 vp1->setBackground( bkgnd );
861 vp1->setRoot ( root );
862 vp1->setSize ( 0,0, .5,1 );
863 // right
864 deco=OSG::ShearedStereoCameraDecorator::create();
865 deco->setLeftEye(false);
866 deco->setEyeSeparation(eyedistance);
867 deco->setDecoratee(cam);
868 deco->setZeroParallaxDistance(zeroparallax);
869 vp2 = OSG::Viewport::create();
870 vp2->setCamera ( deco );
871 vp2->setBackground( bkgnd );
872 vp2->setRoot ( root );
873 vp2->setSize ( .5,0,1,1 );
875 else if(stereoMode == 2)
877 OSG::ShearedStereoCameraDecoratorUnrecPtr deco;
878 // left
879 deco=OSG::ShearedStereoCameraDecorator::create();
880 deco->setLeftEye(true);
881 deco->setEyeSeparation(eyedistance);
882 deco->setDecoratee(cam);
883 deco->setZeroParallaxDistance(zeroparallax);
885 OSG::ColorBufferViewportUnrecPtr cvp1 =
886 OSG::ColorBufferViewport::create();
887 cvp1->setCamera ( deco );
888 cvp1->setBackground( bkgnd );
889 cvp1->setRoot ( root );
890 cvp1->setSize ( 0,0, 1,1 );
891 cvp1->setRed(GL_FALSE);
892 cvp1->setGreen(GL_TRUE);
893 cvp1->setBlue(GL_TRUE);
894 cvp1->setAlpha(GL_TRUE);
895 vp1 = cvp1;
897 // right
898 deco=OSG::ShearedStereoCameraDecorator::create();
899 deco->setLeftEye(false);
900 deco->setEyeSeparation(eyedistance);
901 deco->setDecoratee(cam);
902 deco->setZeroParallaxDistance(zeroparallax);
904 OSG::ColorBufferViewportUnrecPtr cvp2 =
905 OSG::ColorBufferViewport::create();
906 cvp2->setCamera ( deco );
907 cvp2->setBackground( bkgnd );
908 cvp2->setRoot ( root );
909 cvp2->setSize ( 0,0,1,1 );
910 cvp2->setRed(GL_TRUE);
911 cvp2->setGreen(GL_FALSE);
912 cvp2->setBlue(GL_FALSE);
913 cvp2->setAlpha(GL_FALSE);
914 vp2 = cvp2;
917 GLint glvp[4];
918 glGetIntegerv( GL_VIEWPORT, glvp );
920 if(serverx>0 && servery>0)
921 clusterWindow->setSize( serverx, servery );
922 else
923 clusterWindow->setSize( glvp[2], glvp[3] );
924 clusterWindow->addPort( vp1 );
926 if(multiport || stereoMode > 0)
927 clusterWindow->addPort( vp2 );
929 if(serviceInterfaceValid == true)
931 clusterWindow->setServiceInterface(serviceInterface);
933 fprintf(stderr, "tcclient use if %s\n", serviceInterface.c_str());
936 if(serviceAddressValid == true)
938 clusterWindow->setServiceAddress(serviceAddress);
940 fprintf(stderr, "tcclient use ba %s\n", serviceAddress.c_str());
943 // tball
945 OSG::Vec3f pos(min[0] + ((max[0] - min[0]) * 0.5),
946 min[1] + ((max[1] - min[1]) * 0.5),
947 max[2] + ( max[2] - min[2] ) * 1.5 );
949 float scale = (max[2] - min[2] + max[1] - min[1] + max[0] - min[0]) / 6;
951 tball.setMode( OSG::Trackball::OSGObject );
952 tball.setStartPosition( pos, true );
953 tball.setSum( true );
954 tball.setTranslationMode( OSG::Trackball::OSGFree );
955 tball.setTranslationScale(scale);
956 tball.setRotationCenter(center);
957 tball.setTranslationGen(OSG::Trackball::OSGAbsoluteTranslation);
959 // run...
960 std::cout << size.length() << std::endl;
961 cam->setFar (size.length() * 100.0);
962 cam->setNear(size.length() * 100.0 / 100000.0);
965 int doMain(int argc,char **argv)
967 int i;
968 char *opt;
969 std::vector<std::string> filenames;
970 std::vector<std::string> servers;
971 std::string connectionType = "StreamSock";
972 std::string connectionParameters;
973 int rows=1;
974 int cols=-1;
975 char type='M';
976 bool clientRendering=true;
977 bool compose=false;
979 std::string composerType="";
980 std::string autostart;
982 for(i=1;i<argc;i++)
984 if(strlen(argv[i])>1 && argv[i][0]=='-')
986 switch(argv[i][1])
988 case 'o':
989 opt = argv[i][2] ? argv[i]+2 : argv[++i];
990 connectionParameters = opt;
991 printf("connectionParameters: '%s'\n", connectionParameters.c_str());
992 break;
993 case 'A':
994 opt = argv[i][2] ? argv[i]+2 : argv[++i];
995 autostart = opt;
996 break;
997 case 'D':
998 opt = argv[i][2] ? argv[i]+2 : argv[++i];
999 if(sscanf(opt,"%f,%f,%f",&ca,&cb,&cc)!=3)
1001 std::cout << "Copy opton -D x,y,z" << std::endl;
1002 return 1;
1004 break;
1005 case 'b':
1006 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1007 serviceInterface.assign(opt);
1008 serviceInterfaceValid = true;
1009 break;
1010 case 'B':
1011 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1012 serviceAddress.assign(opt);
1013 serviceAddressValid = true;
1014 break;
1015 case 'f':
1016 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1017 filenames.push_back(opt);
1018 printf("<%s>\n",opt);
1019 break;
1020 case 'm':
1021 connectionType="Multicast";
1022 break;
1023 case 'r':
1024 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1025 if(sscanf(opt,"%d,%d",&rows,&cols) != 2)
1026 sscanf(opt,"%d",&rows);
1027 break;
1028 case 't':
1029 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1030 subtilesize=atoi(opt);
1031 break;
1032 #ifdef FRAMEINTERLEAVE
1033 case 'i':
1034 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1035 interleave=atoi(opt);
1036 break;
1037 #endif
1038 case 'C':
1039 compose=true;
1040 break;
1041 case 'F':
1042 type='F';
1043 break;
1044 case 'X':
1045 type='X';
1046 break;
1047 case 'P':
1048 type='P';
1049 break;
1050 case 'L':
1052 type='L';
1053 int lpos=2;
1054 while(argv[i][lpos])
1056 if(argv[i][lpos] == 'B')
1057 composerType = "BinarySwapComposer";
1058 if(argv[i][lpos] == 'P')
1059 composerType = "PipelineComposer";
1060 if(argv[i][lpos] == 'S')
1061 composerType = "SepiaComposer";
1062 if(argv[i][lpos] == 'p')
1063 pipelinedBufferRead = true;
1064 ++lpos;
1066 break;
1068 case 'M':
1069 type='M';
1070 break;
1071 case 'I':
1072 type='I';
1073 break;
1074 case 's':
1075 stereoMode=1;
1076 break;
1077 case 'c':
1078 stereoMode=2;
1079 break;
1080 case 'S':
1081 info=true;
1082 break;
1083 case 'e':
1084 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1085 sscanf(opt,"%f",&eyedistance);
1086 break;
1087 case 'z':
1088 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1089 sscanf(opt,"%f",&zeroparallax);
1090 break;
1091 case 'd':
1092 clientRendering=false;
1093 break;
1094 case 'v':
1095 multiport=true;
1096 break;
1097 case 'x':
1098 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1099 sscanf(opt,"%d",&serverx);
1100 break;
1101 case 'y':
1102 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1103 sscanf(opt,"%d",&servery);
1104 break;
1105 case 'a':
1106 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1107 animName=opt;
1108 loadAnim();
1109 animate=true;
1110 break;
1111 case 'l':
1112 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1113 if(sscanf(opt,"%d,%d",&animLoops,&animLength) != 2)
1115 animLength = 30;
1116 if(sscanf(opt,"%d",&animLoops) != 1)
1118 animLoops = -1;
1121 break;
1122 case 'g':
1123 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1124 if(sscanf(opt,"%d,%d,%d,%d",
1125 &winwidth,&winheight,&winx,&winy) != 4)
1126 sscanf(opt,"%d,%d",&winwidth,&winheight);
1127 break;
1128 case 'G':
1129 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1130 connectionDestination = opt;
1131 break;
1132 case 'i':
1133 opt = argv[i][2] ? argv[i]+2 : argv[++i];
1134 connectionInterface = opt;
1135 break;
1136 default:
1137 std::cout << argv[0]
1138 << "-ffile -m -rrows[,cols] -C -M"
1139 << std::endl;
1140 std::cout << "-m use multicast" << std::endl
1141 << "-G multicast group" << std::endl
1142 << "-i interface" << std::endl
1143 << "-b service interface" << std::endl
1144 << "-M multi display" << std::endl
1145 #ifdef FRAMEINTERLEAVE
1146 << "-I frame interleave" << std::endl
1147 #endif
1148 << "-r number of display rows" << std::endl
1149 << "-C compose" << std::endl
1150 << "-F sort-first" << std::endl
1151 << "-L sort-last" << std::endl
1152 << "-h this msg" << std::endl
1153 << "-s stereo" << std::endl
1154 << "-c red/cyan stereo" << std::endl
1155 << "-e eye distance" << std::endl
1156 << "-z zero parallax" << std::endl
1157 << "-d disable client rendering"<<std::endl
1158 << "-v use two viewports" << std::endl
1159 << "-x server x resolution" << std::endl
1160 << "-y server y resolution" << std::endl
1161 << "-t subtile size for img composition" << std::endl
1162 << "-D x,y,z duplicate geometry" << std::endl
1163 << "-A Autostart command" << std::endl
1164 << "-o connection parameter string e.g. \"TTL=8\"" << std::endl;
1165 return 0;
1168 else
1170 servers.push_back(argv[i]);
1174 OSG::ChangeList::setReadWriteDefault(true);
1175 OSG::osgInit(argc, argv);
1176 glutInit(&argc, argv);
1177 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
1178 if(winx >=0 && winy >=0)
1179 glutInitWindowPosition(winx,winy);
1180 glutInitWindowSize(winwidth,winheight);
1181 winid = glutCreateWindow("OpenSG Cluster Client");
1182 glutKeyboardFunc(key);
1183 glutReshapeFunc(reshape);
1184 glutDisplayFunc(display);
1185 if(animate)
1186 glutIdleFunc(display);
1187 glutMouseFunc(mouse);
1188 glutMotionFunc(motion);
1189 ract = OSG::RenderAction::create();
1190 #ifdef OSG_OLD_RENDER_ACTION
1191 ract->setSortTrans(true);
1192 ract->setZWriteTrans(true);
1193 ract->setLocalLights(true);
1194 ract->setCorrectTwoSidedLighting(true);
1195 #endif
1197 // clear changelist from prototypes
1198 OSG::Thread::getCurrentChangeList()->clear();
1200 // create cluster window
1201 switch(type)
1203 case 'M':
1204 multidisplay=OSG::MultiDisplayWindow::create();
1205 clusterWindow=multidisplay;
1206 break;
1207 case 'X':
1208 balancedmultidisplay=OSG::BalancedMultiWindow::create();
1209 clusterWindow=balancedmultidisplay;
1210 break;
1211 case 'F':
1212 sortfirst=OSG::SortFirstWindow::create();
1213 if(compose)
1214 sortfirst->setCompose(true);
1215 else
1216 sortfirst->setCompose(false);
1217 clusterWindow=sortfirst;
1218 break;
1219 case 'L':
1220 sortlast=OSG::SortLastWindow::create();
1221 if(!composerType.empty())
1223 OSG::FieldContainerUnrecPtr fcPtr =
1224 OSG::FieldContainerFactory::the()->
1225 createContainer(composerType.c_str());
1226 OSG::ImageComposer *icPtr =
1227 dynamic_cast<OSG::ImageComposer *>(fcPtr.get());
1229 if(icPtr != NULL)
1231 if(dynamic_cast<OSG::PipelineComposer *>(icPtr) != NULL)
1233 if(subtilesize>0)
1234 dynamic_cast<OSG::PipelineComposer *>(icPtr)->setTileSize(subtilesize);
1235 dynamic_cast<OSG::PipelineComposer *>(icPtr)->setPipelined(pipelinedBufferRead);
1237 if(dynamic_cast<OSG::BinarySwapComposer *>(icPtr) != NULL)
1239 if(subtilesize>0)
1240 dynamic_cast<OSG::BinarySwapComposer *>(icPtr)->setTileSize(subtilesize);
1242 icPtr->setStatistics(info);
1243 // icPtr->setShort(false);
1244 sortlast->setComposer(icPtr);
1247 clusterWindow=sortlast;
1248 break;
1249 #ifdef FRAMEINTERLEAVE
1250 case 'I':
1251 frameinterleave=OSG::FrameInterleaveWindow::create();
1252 clusterWindow=frameinterleave;
1253 if(compose)
1254 frameinterleave->setCompose(true);
1255 else
1256 frameinterleave->setCompose(false);
1257 break;
1258 #endif
1259 case 'P':
1260 sortfirst=OSG::SortFirstWindow::create();
1261 sortfirst->setCompose(false);
1262 clusterWindow=sortfirst;
1263 break;
1266 if(!autostart.empty())
1267 clusterWindow->editMFAutostart()->push_back(autostart);
1269 for(i=0 ; i<int(servers.size()) ; ++i)
1270 clusterWindow->editMFServers()->push_back(servers[i]);
1271 if(cols < 0)
1272 cols = clusterWindow->getMFServers()->size32() / rows;
1273 switch(type)
1275 case 'M':
1276 multidisplay->setHServers(cols);
1277 multidisplay->setVServers(rows);
1278 break;
1279 case 'X':
1280 balancedmultidisplay->setHServers(cols);
1281 balancedmultidisplay->setVServers(rows);
1282 // balancedmultidisplay->setShowBalancing(true);
1283 // balancedmultidisplay->setShowBalancing(info);
1284 break;
1286 #ifdef FRAMEINTERLEAVE
1287 clusterWindow->setInterleave(interleave);
1288 #endif
1290 // create client window
1291 clientWindow=OSG::GLUTWindow::create();
1292 // glutReshapeWindow(800,600);
1293 glutReshapeWindow(winwidth,winheight);
1294 clientWindow->setGlutId(winid);
1295 clientWindow->init();
1297 // init scene graph
1298 init(filenames);
1300 // init client
1301 clusterWindow->setConnectionType(connectionType);
1302 // needs to be called before init()!
1303 clusterWindow->setConnectionParams(connectionParameters);
1304 if(clientRendering)
1306 clusterWindow->setClientWindow(clientWindow);
1308 clusterWindow->setConnectionDestination(connectionDestination);
1309 clusterWindow->setConnectionInterface(connectionInterface);
1310 clusterWindow->init();
1311 if(serverx > 0)
1312 clusterWindow->resize(serverx,servery);
1313 else
1314 clusterWindow->resize(winwidth,winheight);
1315 clientWindow->resize(winwidth,winheight);
1317 // OSG::FieldContainerFactory::the()->dump();
1319 OSG::commitChanges();
1321 glutMainLoop();
1323 return 0;
1326 int main(int argc,char **argv)
1328 doMain(argc, argv);
1330 glutMainLoop();
1332 return 0;