Ticket #112: testSwitch.cpp

File testSwitch.cpp, 5.0 kB (added by cneumann, 2 years ago)

simple test of Switch core with RenderTraversalAction

Line 
1
2 #include <OSGGLUT.h>
3 #include <OSGConfig.h>
4 #include <OSGSimpleGeometry.h>
5 #include <OSGPassiveWindow.h>
6 #include <OSGSimpleSceneManager.h>
7 #include <OSGSceneFileHandler.h>
8
9 #include <OSGDrawable.h>
10 #include <OSGSwitch.h>
11
12 OSG_USING_NAMESPACE
13
14 SimpleSceneManager    *mgr(NULL);
15 RenderTraversalAction *tact = NULL;
16
17 PassiveWindowPtr pwin;
18 NodePtr          swNode;
19 SwitchPtr        swCore;
20
21 bool show = true;
22 bool bGLFinish = false;
23
24 // redraw the window
25 void display(void)
26 {
27     mgr->redraw();
28
29     // all done, swap
30     glutSwapBuffers();
31 }
32
33 // react to size changes
34 void reshape(int w, int h)
35 {
36     mgr->resize(w,h);
37     glutPostRedisplay();
38 }
39
40 // react to mouse button presses
41 void mouse(int button, int state, int x, int y)
42 {
43     if (state)
44         mgr->mouseButtonRelease(button, x, y);
45     else
46         mgr->mouseButtonPress(button, x, y);
47
48     glutPostRedisplay();
49 }
50
51 // react to mouse motions with pressed buttons
52 void motion(int x, int y)
53 {
54     mgr->mouseMove(x, y);
55     glutPostRedisplay();
56 }
57
58 // react to keys
59 void keyboard(unsigned char k, int, int)
60 {
61     switch(k)
62     {
63         case 27:
64         {
65             osgExit();
66             exit(0);
67         }
68
69         // Output help about the controls
70         // - If you add an option, please add it here too.
71         case '?':
72         case '/':
73         case 'h':
74         {
75             std::cerr << "\nControls:"
76                       << "v: Toggle drawing of volumes.\n"
77                       << "a: Show all.\n"
78                       << "s: Toggle switch node.\n"
79                       << "g: toggle using gl finish.\n"
80                       << std::endl;
81         }
82         break;
83
84         case 'v':
85         {
86             mgr->getAction()->setVolumeDrawing(
87                                     !mgr->getAction()->getVolumeDrawing());
88             std::cerr << "Volume Drawing: "
89                       << (mgr->getAction()->getVolumeDrawing()?"on":"off")
90                       << std::endl;
91         }
92         break;
93
94         case 'a':
95         {
96             mgr->showAll();
97            
98             std::cerr << "Show all." << std::endl;
99         }
100         break;
101        
102         case 's':
103         {
104             Int32  choice  = swCore->getChoice();
105             UInt32 choices = swNode->getNChildren();
106            
107             if(choice == Switch::ALL)
108             {
109                 choice = Switch::NONE;
110                 std::cerr << "Switch: NONE" << std::endl;
111             }
112             else if(choice == Switch::NONE)
113             {
114                 choice = (choices > 0) ? 0 : Switch::ALL;
115                 std::cerr << "Switch: " << choice << std::endl;
116             }
117             else
118             {
119                 choice += 1;
120                
121                 if(choice == choices)
122                 {
123                     choice = Switch::ALL;
124                     std::cerr << "Switch: ALL" << std::endl;
125                 }
126                 else
127                 {
128                     std::cerr << "Switch: " << choice << std::endl;
129                 }
130             }
131            
132             swCore->setChoice(choice);
133         }
134         break;
135        
136         case 'g':
137             bGLFinish = !bGLFinish;
138             tact->setUseGLFinish(bGLFinish);
139             std::cerr << "Set use gl finish to: " << bGLFinish << std::endl;
140             break;
141     }
142    
143     commitChanges();
144 }
145
146 NodePtr buildScene(void)
147 {
148     swNode = Node::create();
149     swCore = Switch::create();
150     swCore->setChoice(Switch::ALL);
151     swNode->setCore(swCore);
152    
153     NodePtr      t1Node = Node::create();
154     TransformPtr t1Core = Transform::create();
155     t1Core->editMatrix().setTranslate( 10.0, 0.0, 0.0);
156     t1Node->setCore(t1Core);
157    
158     NodePtr      t2Node = Node::create();
159     TransformPtr t2Core = Transform::create();
160     t2Core->editMatrix().setTranslate(-10.0, 0.0, 0.0);
161     t2Node->setCore(t2Core);
162    
163     NodePtr g1Node = makeTorus( 6.0, 10.0, 32, 16);
164     NodePtr g2Node = makeCone (10.0,  4.0, 32, true, true);
165    
166     t1Node->addChild(g1Node);
167     t2Node->addChild(g2Node);
168    
169     swNode->addChild(t1Node);
170     swNode->addChild(t2Node);
171    
172     return swNode;
173 }
174
175
176 int main(int argc, char **argv)
177 {
178     osgInit(argc,argv);
179
180     // GLUT init
181     glutInit(&argc, argv);
182
183     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
184
185     glutInitWindowSize(500, 500);
186     glutCreateWindow("OpenSG");
187
188     glutReshapeFunc(reshape);
189     glutDisplayFunc(display);
190     glutIdleFunc(display);
191     glutMouseFunc(mouse);
192     glutMotionFunc(motion);
193     glutKeyboardFunc(keyboard);
194
195     pwin=PassiveWindow::create();
196     pwin->init();
197
198     // create the scene
199     NodePtr scene;
200
201     scene = buildScene();
202
203     // create the SimpleSceneManager helper
204     mgr = new SimpleSceneManager;
205
206     // create the window and initial camera/viewport
207     mgr->setWindow(pwin );
208     // tell the manager what to manage
209     mgr->setRoot  (scene);
210
211     Thread::getCurrentChangeList()->commitChanges();
212
213     // show the whole scene
214     mgr->showAll();
215
216     mgr->setUseTraversalAction(true);
217
218     tact = RenderTraversalAction::create();
219
220     mgr->setAction(tact);
221
222     // GLUT main loop
223     glutMainLoop();
224
225     return 0;
226 }
227