fixed: compile issue
[opensg.git] / Examples / Tutorial / 12traversal2.cpp
blobc8a2bb8805f21684ba6eefd88d00f75d7a5b4637
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>
12 #include <OSGGraphOpSeq.h>
13 #include <OSGSplitGraphOp.h>
14 #include <OSGVerifyGeoGraphOp.h>
15 #include <OSGSharePtrGraphOp.h>
16 #else
17 #include <OpenSG/OSGGLUT.h>
18 #include <OpenSG/OSGConfig.h>
19 #include <OpenSG/OSGSimpleGeometry.h>
20 #include <OpenSG/OSGGLUTWindow.h>
21 #include <OpenSG/OSGSimpleSceneManager.h>
23 #include <OpenSG/OSGSceneFileHandler.h>
24 #include <OpenSG/OSGNameAttachment.h>
26 #include <OpenSG/OSGGraphOpSeq.h>
27 #include <OpenSG/OSGSplitGraphOp.h>
28 #include <OpenSG/OSGVerifyGeoGraphOp.h>
29 #include <OpenSG/OSGSharePtrGraphOp.h>
30 #endif
32 OSG::SimpleSceneManagerRefPtr mgr;
33 OSG::NodeRecPtr scene;
35 // A simple class that counts the number of entered nodes
36 class counter
38 public:
39 //constructor
40 counter(void) : mCount(0)
44 //method that will be called when entering
45 //a new node
46 OSG::Action::ResultE enter(OSG::Node * const)
48 mCount++;
49 return OSG::Action::Continue;
52 OSG::UInt16 getCount(void)
54 return mCount;
57 void reset(void)
59 mCount = 0;
62 private:
63 OSG::UInt16 mCount;
66 int setupGLUT(int *argc, char *argv[]);
68 //This is the function that will be called when a node
69 //is entered during traversal.
70 OSG::Action::ResultE enter(OSG::Node * const node)
72 if (getName(node))
74 std::cout << getName(node) << std::endl;
76 else
78 std::cout << "No name was set!" << std::endl;
81 return OSG::Action::Continue;
84 //This function will test if the core is of type
85 //geometry and if it is, it will print the node's
86 //name
87 OSG::Action::ResultE isGeometry(OSG::Node * const node)
89 // this tests if the core is derived from geometry
90 if (node->getCore()->getType().isDerivedFrom(OSG::Geometry::getClassType()))
92 if (getName(node))
94 std::cout << "Found a geometry core stored in " << getName(node)
95 << std::endl;
97 else
99 std::cout << "Found a geometry core but node has no name"
100 << std::endl;
104 return OSG::Action::Continue;
108 OSG::NodeTransitPtr createScenegraph(const char* filename)
110 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
112 // define the Graph Op Sequence here
113 OSG::GraphOpSeqRefPtr graphOperator = OSG::GraphOpSeq::create();
114 OSG::GraphOpRefPtr go;
116 //first we verify the geometry
117 go = OSG::VerifyGeoGraphOp::create();
118 graphOperator->addGraphOp(go);
119 //merge identical field containers
120 go = OSG::SharePtrGraphOp::create();
121 graphOperator->addGraphOp(go);
122 //verify again
123 go = OSG::VerifyGeoGraphOp::create();
124 graphOperator->addGraphOp(go);
126 std::cout << "Loading " << filename << " now" << std::endl;
128 OSG::NodeRecPtr n =
129 OSG::SceneFileHandler::the()->read(filename, graphOperator);
131 //we check the result
132 if(n == NULL)
134 std::cout << "Loading the specified file was not possible!"
135 << std::endl;
136 return OSG::NodeTransitPtr();
139 return OSG::NodeTransitPtr(n);
142 int main(int argc, char **argv)
144 OSG::osgInit(argc,argv);
147 int winid = setupGLUT(&argc, argv);
148 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
149 gwin->setGlutId(winid);
150 gwin->init();
152 if (argc > 1)
153 scene = createScenegraph(argv[1]);
154 else
155 scene = createScenegraph("Data/brick_quads.wrl");
157 mgr = OSG::SimpleSceneManager::create();
158 mgr->setWindow(gwin );
159 mgr->setRoot (scene);
160 mgr->showAll();
162 OSG::commitChanges();
165 glutMainLoop();
167 return 0;
170 void reshape(int w, int h)
172 mgr->resize(w, h);
173 glutPostRedisplay();
176 void display(void)
178 mgr->redraw();
181 void mouse(int button, int state, int x, int y)
183 if (state)
184 mgr->mouseButtonRelease(button, x, y);
185 else
186 mgr->mouseButtonPress(button, x, y);
188 glutPostRedisplay();
191 void motion(int x, int y)
193 mgr->mouseMove(x, y);
194 glutPostRedisplay();
197 void keyboard(unsigned char k, int x, int y)
199 switch(k)
201 case 27:
203 // clean up global variables
204 scene = NULL;
205 mgr = NULL;
207 OSG::osgExit();
208 exit(1);
210 break;
212 // this will print the names of all nodes
213 // in the whole graph
214 case 'p':
216 std::cout << std::endl << std::endl;
217 std::cout << "Printing all node names";
218 std::cout << "---------------------------------------";
219 std::cout << std::endl << std::endl;
221 // now we invoke the traversal
222 traverse(scene, enter);
224 break;
226 // this will only print the names of nodes
227 // which have a geometry core
228 case 'g':
230 std::cout << std::endl << std::endl;
231 std::cout << "Printing all geometry nodes";
232 std::cout << "---------------------------------------";
233 std::cout << std::endl << std::endl;
235 // traverse the graph
236 traverse(scene, isGeometry);
238 break;
240 case 's':
242 std::cout << "Splitting Graph now...";
244 counter c;
246 traverse(scene, boost::bind(&counter::enter, &c, _1));
248 std::cout << "Number of nodes before splitting: " << c.getCount()
249 << std::endl;
251 OSG::SplitGraphOpRefPtr spo = OSG::SplitGraphOp::create();
252 spo->setMaxPolygons(50);
253 spo->traverse(scene);
255 std::cout << "done" << std::endl;
257 c.reset();
259 traverse(scene, boost::bind(&counter::enter, &c, _1));
261 std::cout << "Number of nodes after splitting: " << c.getCount()
262 << std::endl;
264 break;
268 int setupGLUT(int *argc, char *argv[])
270 glutInit(argc, argv);
271 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
273 int winid = glutCreateWindow("OpenSG First Application");
275 glutDisplayFunc(display);
276 glutMouseFunc(mouse);
277 glutMotionFunc(motion);
278 glutReshapeFunc(reshape);
279 glutIdleFunc(display);
280 glutKeyboardFunc(keyboard);
282 return winid;