changed: deal with dxt1 compressed dds volumes
[opensg.git] / Examples / Tutorial / 12traversal2.cpp
blob29d7b2502d287ab9622cf9c44a6a90b9ad73d2eb
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)
42 mCount = 0;
45 //method that will be called when entering
46 //a new node
47 OSG::Action::ResultE enter(OSG::Node * const)
49 mCount++;
50 return OSG::Action::Continue;
53 OSG::UInt16 getCount(void)
55 return mCount;
58 void reset(void)
60 mCount = 0;
63 private:
64 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;
109 OSG::NodeTransitPtr createScenegraph(const char* filename)
111 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
113 // define the Graph Op Sequence here
114 OSG::GraphOpSeqRefPtr graphOperator = OSG::GraphOpSeq::create();
115 OSG::GraphOpRefPtr go;
117 //first we verify the geometry
118 go = OSG::VerifyGeoGraphOp::create();
119 graphOperator->addGraphOp(go);
120 //merge identical field containers
121 go = OSG::SharePtrGraphOp::create();
122 graphOperator->addGraphOp(go);
123 //verify again
124 go = OSG::VerifyGeoGraphOp::create();
125 graphOperator->addGraphOp(go);
127 std::cout << "Loading " << filename << " now" << std::endl;
129 OSG::NodeRecPtr n =
130 OSG::SceneFileHandler::the()->read(filename, graphOperator);
132 //we check the result
133 if(n == NULL)
135 std::cout << "Loading the specified file was not possible!"
136 << std::endl;
137 return OSG::NodeTransitPtr();
140 return OSG::NodeTransitPtr(n);
143 int main(int argc, char **argv)
145 OSG::osgInit(argc,argv);
148 int winid = setupGLUT(&argc, argv);
149 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
150 gwin->setGlutId(winid);
151 gwin->init();
153 if (argc > 1)
154 scene = createScenegraph(argv[1]);
155 else
156 scene = createScenegraph("Data/brick_quads.wrl");
158 mgr = OSG::SimpleSceneManager::create();
159 mgr->setWindow(gwin );
160 mgr->setRoot (scene);
161 mgr->showAll();
163 OSG::commitChanges();
166 glutMainLoop();
168 return 0;
171 void reshape(int w, int h)
173 mgr->resize(w, h);
174 glutPostRedisplay();
177 void display(void)
179 mgr->redraw();
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 void motion(int x, int y)
194 mgr->mouseMove(x, y);
195 glutPostRedisplay();
198 void keyboard(unsigned char k, int x, int y)
200 switch(k)
202 case 27:
204 // clean up global variables
205 scene = NULL;
206 mgr = NULL;
208 OSG::osgExit();
209 exit(1);
211 break;
213 // this will print the names of all nodes
214 // in the whole graph
215 case 'p':
217 std::cout << std::endl << std::endl;
218 std::cout << "Printing all node names";
219 std::cout << "---------------------------------------";
220 std::cout << std::endl << std::endl;
222 // now we invoke the traversal
223 traverse(scene, enter);
225 break;
227 // this will only print the names of nodes
228 // which have a geometry core
229 case 'g':
231 std::cout << std::endl << std::endl;
232 std::cout << "Printing all geometry nodes";
233 std::cout << "---------------------------------------";
234 std::cout << std::endl << std::endl;
236 // traverse the graph
237 traverse(scene, isGeometry);
239 break;
241 case 's':
243 std::cout << "Splitting Graph now...";
245 counter c;
247 traverse(scene, boost::bind(&counter::enter, &c, _1));
249 std::cout << "Number of nodes before splitting: " << c.getCount()
250 << std::endl;
252 OSG::SplitGraphOpRefPtr spo = OSG::SplitGraphOp::create();
253 spo->setMaxPolygons(50);
254 spo->traverse(scene);
256 std::cout << "done" << std::endl;
258 c.reset();
260 traverse(scene, boost::bind(&counter::enter, &c, _1));
262 std::cout << "Number of nodes after splitting: " << c.getCount()
263 << std::endl;
265 break;
269 int setupGLUT(int *argc, char *argv[])
271 glutInit(argc, argv);
272 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
274 int winid = glutCreateWindow("OpenSG First Application");
276 glutDisplayFunc(display);
277 glutMouseFunc(mouse);
278 glutMotionFunc(motion);
279 glutReshapeFunc(reshape);
280 glutIdleFunc(display);
281 glutKeyboardFunc(keyboard);
283 return winid;