1 // OpenSG Tutorial Example: Switch
3 // This example shows how to use the Switch NodeCore to selectively display one
4 // of a number of children of a node.
6 // You can use the keys 1-0 to display a single node, 'a' to select all nodes
7 // and 'n' to select none.
10 #ifdef OSG_BUILD_ACTIVE
13 #include <OSGConfig.h>
14 #include <OSGSimpleGeometry.h>
15 #include <OSGGLUTWindow.h>
16 #include <OSGSimpleSceneManager.h>
17 #include <OSGBaseFunctions.h>
18 #include <OSGTransform.h>
22 // the Switch node core
23 #include <OSGSwitch.h>
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>
36 // the Switch node core
37 #include <OpenSG/OSGSwitch.h>
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
[] );
62 OSG::Real32 t
= glutGet(GLUT_ELAPSED_TIME
);
64 // set the transforms' matrices
65 for(OSG::UInt16 i
=0; i
<ncopies
; ++i
)
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
);
87 // Initialize GLUT & OpenSG and set up the scene
88 int main(int argc
, char **argv
)
91 OSG::osgInit(argc
,argv
);
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
);
108 // this time, create just the core of the geometry
109 OSG::GeometryRefPtr torus
= OSG::makeTorusGeo( .5, 2, 8, 12 );
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
123 // create the root Switch node
124 OSG::NodeRefPtr scene
= OSG::Node::create();
126 sw
= OSG::Switch::create();
127 sw
->setChoice(OSG::Switch::ALL
);
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
171 // GLUT callback functions
174 // react to size changes
175 void reshape(int w
, int h
)
181 // react to mouse button presses
182 void mouse(int button
, int state
, int x
, int y
)
185 mgr
->mouseButtonRelease(button
, x
, y
);
187 mgr
->mouseButtonPress(button
, x
, y
);
192 // react to mouse motions with pressed buttons
193 void motion(int x
, int y
)
195 mgr
->mouseMove(x
, y
);
200 void keyboard(unsigned char k
, int , int )
206 // clean up global variables
207 for(OSG::UInt32 i
= 0; i
< ncopies
; ++i
)
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');
228 sw
->setChoice(OSG::Switch::ALL
);
234 sw
->setChoice(OSG::Switch::NONE
);
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
);