Ticket #211: FlexiviewWindow.hpp

File FlexiviewWindow.hpp, 3.6 kB (added by rstanchak, 11 months ago)

support class for the tutorial file

Line 
1 #ifndef FLEXIVIEW_WINDOW_HPP
2 #define FLEXIVIEW_WINDOW_HPP
3
4 #include <OpenSG/OSGMultiDisplayWindow.h>
5 #include <OpenSG/OSGGLUTWindow.h>
6
7 OSG_USING_NAMESPACE;
8
9 class FlexiviewWindow {
10 public:
11         WindowPtr osgWindow;
12         bool _isMultiDisplayWindow;
13
14         FlexiviewWindow():
15                 osgWindow(NullFC),
16                 _isMultiDisplayWindow(false)
17         {
18                 ChangeList::setReadWriteDefault();
19         }
20
21         FlexiviewWindow(int winid):
22                 osgWindow(NullFC),
23                 _isMultiDisplayWindow(false)
24         {
25                 ChangeList::setReadWriteDefault();
26                 this->osgWindow=setupGLUTWindow(winid);
27         }
28        
29         FlexiviewWindow(int winid, int argc, char ** argv):
30                 osgWindow(NullFC),
31                 _isMultiDisplayWindow(false)
32         {
33                 ChangeList::setReadWriteDefault();
34                 init(winid, argc, argv);
35         }
36         int getNumServers(){
37                 if(_isMultiDisplayWindow){
38                         return MultiDisplayWindowPtr::dcast( osgWindow )->getServers().size();
39                 }
40                 return 1;
41         }
42
43         bool init(int winid, int argc, char ** argv)
44         {
45                 // scan arguments and if find x and y then create a multiview window,
46                 // otherwise, create a standard GLUTWindow
47                 for(int a=1; a<argc; ++a){
48                         if(argv[a][0] == '-' &&
49                                 ( argv[a][1] == 'x' || argv[a][1] == 'y' ) )
50                         {
51                                 _isMultiDisplayWindow=true;
52                         }
53                 }
54                 if(_isMultiDisplayWindow){
55                         this->osgWindow = setupMultiDisplayWindow(argc, argv);
56                 }
57                 else{
58                         this->osgWindow = setupGLUTWindow(winid);
59                 }
60                 return this->osgWindow != NullFC;
61         }
62         void redraw(){
63                 if(_isMultiDisplayWindow){
64                         OSG::Thread::getCurrentChangeList()->clearAll();
65                         // clear local navigation window
66                         glClear(GL_COLOR_BUFFER_BIT);
67                         glutSwapBuffers();
68                 }
69         }
70 protected:
71         // Create a plain old GLUT window
72         GLUTWindowPtr setupGLUTWindow(int winid){
73                 GLUTWindowPtr gwin = GLUTWindow::create();
74                 gwin->setId(winid);
75                 return gwin;
76         }
77
78         // Create a Multi Display Window
79         MultiDisplayWindowPtr setupMultiDisplayWindow(int argc, char ** argv){
80                 char * opt;
81                 MultiDisplayWindowPtr mwin = MultiDisplayWindow::create();
82
83                 // all changes must be enclosed in beginEditCP and endEditCP
84                 // otherwise the changes will not be transfered over the network.
85                 beginEditCP(mwin);
86                 {
87
88                         // dummy size for navigator
89                         mwin->setSize(300,300);
90
91                         int lastdashidx=0;
92
93                         // evaluate params
94                         for(int a=1 ; a<argc ; ++a)
95                         {
96                                 if(argv[a][0] == '-')
97                                 {
98                                         switch(argv[a][1])
99                                         {
100                                                 case 'm': mwin->setConnectionType("Multicast");
101                                                                   std::cout << "Connection type set to Multicast" << std::endl;
102                                                                   break;
103                                                 case 'p': mwin->setConnectionType("SockPipeline");
104                                                                   std::cout << "Connection type set to SockPipeline" << std::endl;
105                                                                   break;
106                                                 case 'i': opt = argv[a][2] ? argv[a]+2 : argv[++a];
107                                                                   if(opt != argv[argc])
108                                                                           mwin->setConnectionInterface(opt);
109                                                                   break;
110                                                 case 'a': opt = argv[a][2] ? argv[a]+2 : argv[++a];
111                                                                   if(opt != argv[argc])
112                                                                           mwin->setServiceAddress(opt);
113                                                                   break;
114                                                 case 'x': opt = argv[a][2] ? argv[a]+2 : argv[++a];
115                                                                   if(opt != argv[argc])
116                                                                           mwin->setHServers(atoi(opt));
117                                                                   break;
118                                                 case 'y': opt = argv[a][2] ? argv[a]+2 : argv[++a];
119                                                                   if(opt != argv[argc])
120                                                                           mwin->setVServers(atoi(opt));
121                                                                   break;
122                                         }
123                                         lastdashidx=a;
124                                 }
125                         }
126                         int nservernames=mwin->getHServers()*mwin->getVServers();
127                         assert(nservernames < argc-lastdashidx);
128
129                         // assume server names come at the end of the argument list
130                         std::cout<<"Will try to connect to the following servers:"<<std::endl;
131                         for(int a=argc-nservernames; a<argc; ++a){
132                                 std::cout<<argv[a]<<std::endl;
133                                 mwin->getServers().push_back(argv[a]);
134                         }
135
136                 } // end edit of cluster window
137                 endEditCP(mwin);
138
139                 return mwin;
140         }
141 };
142
143 #endif //FLEXIVIEW_WINDOW_HPP