fixed: compile issue
[opensg.git] / Examples / Tutorial / 12traversal.cpp
blobcead678515e309d47428db7b421ed38c1e7d76ea
1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
3 #include <OSGGLUT.h>
4 #include <OSGConfig.h>
5 #include <OSGSimpleGeometry.h>
6 #include <OSGGLUTWindow.h>
7 #include <OSGSimpleSceneManager.h>
9 #include <OSGSceneFileHandler.h>
10 #include <OSGNameAttachment.h>
11 #else
12 #include <OpenSG/OSGGLUT.h>
13 #include <OpenSG/OSGConfig.h>
14 #include <OpenSG/OSGSimpleGeometry.h>
15 #include <OpenSG/OSGGLUTWindow.h>
16 #include <OpenSG/OSGSimpleSceneManager.h>
18 #include <OpenSG/OSGSceneFileHandler.h>
19 #include <OpenSG/OSGNameAttachment.h>
20 #endif
22 OSG::SimpleSceneManagerRefPtr mgr;
23 OSG::NodeRecPtr scene;
25 int setupGLUT(int *argc, char *argv[]);
27 //This is the function that will be called when a node
28 //is entered during traversal.
29 OSG::Action::ResultE enter(OSG::Node * const node)
31 if (getName(node))
33 std::cout << getName(node) << std::endl;
35 else
37 std::cout << "No name was set!" << std::endl;
40 return OSG::Action::Continue;
43 //This function will test if the core is of type
44 //geometry and if it is, it will print the node's
45 //name
46 OSG::Action::ResultE isGeometry(OSG::Node * const node)
48 // this tests if the core is derived from geometry
49 if (node->getCore()->getType().isDerivedFrom(OSG::Geometry::getClassType()))
51 if (getName(node))
53 std::cout << "Found a geometry core stored in " << getName(node)
54 << std::endl;
56 else
58 std::cout << "Found a geometry core but node has no name"
59 << std::endl;
63 return OSG::Action::Continue;
67 OSG::NodeTransitPtr createScenegraph(void)
69 // the scene must be created here
70 OSG::NodeRecPtr n =
71 OSG::SceneFileHandler::the()->read("Data/torus_sphere_cone.wrl");
73 //we check the result
74 if(n == NULL)
76 std::cout << "Loading the specified file was not possible!"
77 << std::endl;
78 return OSG::NodeTransitPtr();
81 return OSG::NodeTransitPtr(n);
84 int main(int argc, char **argv)
86 OSG::osgInit(argc,argv);
89 int winid = setupGLUT(&argc, argv);
90 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
91 gwin->setGlutId(winid);
92 gwin->init();
94 scene = createScenegraph();
96 mgr = OSG::SimpleSceneManager::create();
97 mgr->setWindow(gwin );
98 mgr->setRoot (scene);
99 mgr->showAll();
101 OSG::commitChanges();
104 glutMainLoop();
106 return 0;
109 void reshape(int w, int h)
111 mgr->resize(w, h);
112 glutPostRedisplay();
115 void display(void)
117 mgr->redraw();
120 void mouse(int button, int state, int x, int y)
122 if (state)
123 mgr->mouseButtonRelease(button, x, y);
124 else
125 mgr->mouseButtonPress(button, x, y);
127 glutPostRedisplay();
130 void motion(int x, int y)
132 mgr->mouseMove(x, y);
133 glutPostRedisplay();
136 void keyboard(unsigned char k, int x, int y){
137 switch(k)
139 case 27:
141 // clean up global variables
142 scene = NULL;
143 mgr = NULL;
145 OSG::osgExit();
146 exit(1);
148 break;
150 // this will print the names of all nodes
151 // in the whole graph
152 case 'p':
154 std::cout << std::endl << std::endl;
155 std::cout << "Printing all node names";
156 std::cout << "---------------------------------------";
157 std::cout << std::endl << std::endl;
159 // now we invoke the traversal
160 traverse(scene, enter);
162 break;
164 // this will only print the names of nodes
165 // which have a geometry core
166 case 'g':
168 std::cout << std::endl << std::endl;
169 std::cout << "Printing all geometry nodes";
170 std::cout << "---------------------------------------";
171 std::cout << std::endl << std::endl;
173 // traverse the graph
174 traverse(scene, isGeometry);
176 break;
180 int setupGLUT(int *argc, char *argv[])
182 glutInit(argc, argv);
183 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
185 int winid = glutCreateWindow("OpenSG First Application");
187 glutDisplayFunc(display);
188 glutMouseFunc(mouse);
189 glutMotionFunc(motion);
190 glutReshapeFunc(reshape);
191 glutIdleFunc(display);
192 glutKeyboardFunc(keyboard);
194 return winid;