fixed: compile issue
[opensg.git] / Examples / Simple / switch.cpp
blobae57a719aaa3c11742c2ffabada2ca3588fa5ba9
1 // OpenSG Tutorial Example: Switch
2 //
3 // This example shows how to use the Switch NodeCore to selectively display one
4 // of a number of children of a node.
5 //
6 // You can use the keys 1-0 to display a single node, 'a' to select all nodes
7 // and 'n' to select none.
8 //
10 #ifdef OSG_BUILD_ACTIVE
11 // Headers
12 #include <OSGGLUT.h>
13 #include <OSGConfig.h>
14 #include <OSGSimpleGeometry.h>
15 #include <OSGGLUTWindow.h>
16 #include <OSGSimpleSceneManager.h>
17 #include <OSGBaseFunctions.h>
18 #include <OSGTransform.h>
20 // new headers:
22 // the Switch node core
23 #include <OSGSwitch.h>
24 #else
25 // Headers
26 #include <OpenSG/OSGGLUT.h>
27 #include <OpenSG/OSGConfig.h>
28 #include <OpenSG/OSGSimpleGeometry.h>
29 #include <OpenSG/OSGGLUTWindow.h>
30 #include <OpenSG/OSGSimpleSceneManager.h>
31 #include <OpenSG/OSGBaseFunctions.h>
32 #include <OpenSG/OSGTransform.h>
34 // new headers:
36 // the Switch node core
37 #include <OpenSG/OSGSwitch.h>
38 #endif
41 // the Switch Core
42 OSG::SwitchRefPtr sw;
44 // number of copies to create
45 const OSG::UInt16 ncopies = 10;
47 // Transforms to move the nodes
48 OSG::TransformRefPtr trans[ncopies];
51 // The SimpleSceneManager to manage simple applications
52 OSG::SimpleSceneManagerRefPtr mgr;
54 // forward declaration so we can have the interesting stuff upfront
55 int setupGLUT( int *argc, char *argv[] );
57 // redraw the window
58 void display( void )
60 // create the matrix
61 OSG::Matrix m;
62 OSG::Real32 t = glutGet(GLUT_ELAPSED_TIME );
64 // set the transforms' matrices
65 for(OSG::UInt16 i=0; i<ncopies; ++i)
67 m.setTransform(
68 OSG::Vec3f(OSG::osgSin(t / 1000.f + i * 4 * ncopies / OSG::Pi),
69 OSG::osgCos(t / 1000.f + i * 6 * ncopies / OSG::Pi),
70 OSG::osgSin(t / 1000.f + i * 7 * ncopies / OSG::Pi)),
71 OSG::Quaternion(OSG::Vec3f (1,1,0),
72 t / 1000.f + i * 4 * ncopies / OSG::Pi));
74 trans[i]->setMatrix(m);
77 OSG::commitChanges();
79 mgr->redraw();
82 void update(void)
84 glutPostRedisplay();
87 // Initialize GLUT & OpenSG and set up the scene
88 int main(int argc, char **argv)
90 // OSG init
91 OSG::osgInit(argc,argv);
93 // GLUT init
94 int winid = setupGLUT(&argc, argv);
96 // open a new scope, because the pointers below should go out of scope
97 // before entering glutMainLoop.
98 // Otherwise OpenSG will complain about objects being alive after shutdown.
101 // the connection between GLUT and OpenSG
102 OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
103 gwin->setGlutId(winid);
104 gwin->init();
106 // create the scene
108 // this time, create just the core of the geometry
109 OSG::GeometryRefPtr torus = OSG::makeTorusGeo( .5, 2, 8, 12 );
111 // create the scene
112 // the scene has a single group with ncopies transformations below,
113 // each of these carries a Node that shares the geometry
116 The Switch NodeCore very similar to the Group, but it has the additional
117 capability to only show one or none of its children.
119 This is controlled by the choice Field, and is used below in the keys
120 function.
123 // create the root Switch node
124 OSG::NodeRefPtr scene = OSG::Node::create();
126 sw = OSG::Switch::create();
127 sw->setChoice(OSG::Switch::ALL);
129 scene->setCore(sw);
131 // create the copied geometry nodes and their transformations
132 for(OSG::UInt16 i = 0; i<ncopies; ++i)
134 // create the nodes for the shared Geometry core
135 OSG::NodeRefPtr geonode = OSG::Node::create();
137 // assign the Core to the Node
138 geonode->setCore(torus);
140 // add a transformation for every Geometry
141 OSG::NodeRefPtr transnode = OSG::Node::create();
143 trans[i] = OSG::Transform::create();
145 transnode->setCore (trans[i]);
146 transnode->addChild(geonode );
148 scene->addChild(transnode);
151 OSG::commitChanges();
153 // create the SimpleSceneManager helper
154 mgr = OSG::SimpleSceneManager::create();
156 // tell the manager what to manage
157 mgr->setWindow(gwin );
158 mgr->setRoot (scene);
160 // show the whole scene
161 mgr->showAll();
164 // GLUT main loop
165 glutMainLoop();
167 return 0;
171 // GLUT callback functions
174 // react to size changes
175 void reshape(int w, int h)
177 mgr->resize(w, h);
178 glutPostRedisplay();
181 // react to mouse button presses
182 void mouse(int button, int state, int x, int y)
184 if (state)
185 mgr->mouseButtonRelease(button, x, y);
186 else
187 mgr->mouseButtonPress(button, x, y);
189 glutPostRedisplay();
192 // react to mouse motions with pressed buttons
193 void motion(int x, int y)
195 mgr->mouseMove(x, y);
196 glutPostRedisplay();
199 // react to keys
200 void keyboard(unsigned char k, int , int )
202 switch(k)
204 case 27:
206 // clean up global variables
207 for(OSG::UInt32 i = 0; i < ncopies; ++i)
208 trans[i] = NULL;
210 sw = NULL;
211 mgr = NULL;
213 OSG::osgExit();
214 exit(1);
216 break;
218 case '0': case '1': case '2': case '3': case '4':
219 case '5': case '6': case '7': case '8': case '9':
221 sw->setChoice(k - '0');
222 break;
224 break;
226 case 'a':
228 sw->setChoice(OSG::Switch::ALL);
230 break;
232 case 'n':
234 sw->setChoice(OSG::Switch::NONE);
236 break;
240 // setup the GLUT library which handles the windows for us
241 int setupGLUT(int *argc, char *argv[])
243 glutInit(argc, argv);
244 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
246 int winid = glutCreateWindow("OpenSG");
248 glutReshapeFunc(reshape);
249 glutDisplayFunc(display);
250 glutMouseFunc(mouse);
251 glutMotionFunc(motion);
252 glutKeyboardFunc(keyboard);
254 // call the redraw function whenever there's nothing else to do
255 glutIdleFunc(update);
257 return winid;