fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Tutorial / 12traversal3.cpp
blob272bfdb1ce02c355c465abbcc7d8ba2ab00a66aa
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;
67 int setupGLUT( int *argc, char *argv[] );
69 //This is the function that will be called when a node
70 //is entered during traversal.
71 OSG::Action::ResultE enter(OSG::Node * const node)
73 if (getName(node))
75 std::cout << getName(node) << std::endl;
77 else
79 std::cout << "No name was set!" << std::endl;
82 return OSG::Action::Continue;
85 //This function will test if the core is of type
86 //geometry and if it is, it will print the node's
87 //name
88 OSG::Action::ResultE isGeometry(OSG::Node * const node)
90 // this tests if the core is derived from geometry
91 if (node->getCore()->getType().isDerivedFrom(OSG::Geometry::getClassType()))
93 if (getName(node))
95 std::cout << "Found a geometry core stored in " << getName(node)
96 << std::endl;
98 else
100 std::cout << "Found a geometry core but node has no name"
101 << std::endl;
105 return OSG::Action::Continue;
108 OSG::NodeTransitPtr createScenegraph(const char* filename)
110 OSG::NodeRecPtr n = OSG::SceneFileHandler::the()->read(filename);
112 //we check the result
113 if(n == NULL)
115 std::cout << "Loading the specified file was not possible!"
116 << std::endl;
117 return OSG::NodeTransitPtr();
120 return OSG::NodeTransitPtr(n);
123 int main(int argc, char **argv)
125 OSG::osgInit(argc,argv);
128 int winid = setupGLUT(&argc, argv);
129 OSG::GLUTWindowRecPtr gwin= OSG::GLUTWindow::create();
130 gwin->setGlutId(winid);
131 gwin->init();
133 if (argc > 1)
134 scene = createScenegraph(argv[1]);
135 else
136 scene = createScenegraph("Data/brick_quads.wrl");
138 mgr = OSG::SimpleSceneManager::create();
139 mgr->setWindow(gwin );
140 mgr->setRoot (scene);
141 mgr->showAll();
143 OSG::commitChanges();
146 glutMainLoop();
148 return 0;
151 void reshape(int w, int h)
153 mgr->resize(w, h);
154 glutPostRedisplay();
157 void display(void)
159 mgr->redraw();
162 void mouse(int button, int state, int x, int y)
164 if (state)
166 mgr->mouseButtonRelease(button, x, y);
168 else
170 mgr->mouseButtonPress(button, x, y);
172 OSG::Line ray = mgr->calcViewRay(x, y);
173 OSG::IntersectActionRefPtr iAct = OSG::IntersectAction::create();
174 iAct->setLine(ray);
175 iAct->apply(scene);
177 if(iAct->didHit())
179 OSG::Pnt3f p = iAct->getHitPoint();
180 std::cout << "Hit point : " << p[0] << " " << p[1] << " " << p[2]
181 << std::endl;
182 OSG::NodeRecPtr n = iAct->getHitObject();
183 OSG::NodeRecPtr parent = n->getParent();
185 parent->subChild(n);
188 iAct = NULL;
191 glutPostRedisplay();
194 void motion(int x, int y)
196 mgr->mouseMove(x, y);
197 glutPostRedisplay();
200 void keyboard(unsigned char k, int x, int y)
202 switch(k)
204 case 27:
206 // clean up global variables
207 scene = NULL;
208 mgr = NULL;
210 OSG::osgExit();
211 exit(1);
213 break;
215 // this will print the names of all nodes
216 // in the whole graph
217 case 'p':
219 std::cout << std::endl << std::endl;
220 std::cout << "Printing all node names";
221 std::cout << "---------------------------------------";
222 std::cout << std::endl << std::endl;
224 // now we invoke the traversal
225 traverse(scene, enter);
227 break;
229 // this will only print the names of nodes
230 // which have a geometry core
231 case 'g':
233 std::cout << std::endl << std::endl;
234 std::cout << "Printing all geometry nodes";
235 std::cout << "---------------------------------------";
236 std::cout << std::endl << std::endl;
238 // traverse the graph
239 traverse(scene, isGeometry);
241 break;
243 case 's':
244 std::cout << "Splitting Graph now...";
246 counter c;
248 traverse(scene, boost::bind(&counter::enter, &c, _1));
250 std::cout << "Number of nodes before splitting: " << c.getCount()
251 << std::endl;
253 OSG::SplitGraphOpRefPtr spo = OSG::SplitGraphOp::create();
254 spo->setMaxPolygons(50);
255 spo->traverse(scene);
257 std::cout << "done" << std::endl;
259 c.reset();
261 traverse(scene, boost::bind(&counter::enter, &c, _1));
263 std::cout << "Number of nodes after splitting: " << c.getCount()
264 << std::endl;
265 break;
270 int setupGLUT(int *argc, char *argv[])
272 glutInit(argc, argv);
273 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
275 int winid = glutCreateWindow("OpenSG First Application");
277 glutDisplayFunc(display);
278 glutMouseFunc(mouse);
279 glutMotionFunc(motion);
280 glutReshapeFunc(reshape);
281 glutIdleFunc(display);
282 glutKeyboardFunc(keyboard);
284 return winid;