5 #include "OSGSimpleGeometry.h"
6 #include "OSGGLUTWindow.h"
7 #include "OSGSimpleSceneManager.h"
8 #include "OSGSceneFileHandler.h"
9 #include "OSGGeoOptimization.h"
13 OSG::SimpleSceneManagerRefPtr mgr
;
14 OSG::NodeUnrecPtr scene
;
18 std::deque
<OSG::Time
> timeHistory
;
20 bool showFrameTime
= false;
22 int setupGLUT(int *argc
, char *argv
[]);
23 void doInit(int argc
, char *argv
[]);
25 void doPrintGeoInfo (OSG::Node
*scene
);
26 void doMakeSingleIndexed (OSG::Node
*scene
);
27 void doMakeIndexedTriangles (OSG::Node
*scene
);
28 void doMakeOptimizedIndex (OSG::Node
*scene
);
29 void doMakeOptimizedProperties(OSG::Node
*scene
);
32 printTypes(std::ostream
&os
, OSG::GeoIntegralProperty
*types
);
34 printGeoInfo(std::ostream
&os
, OSG::Geometry
*geo
);
37 int main(int argc
, char *argv
[])
47 // GLUT callback functions
53 startT
= OSG::getSystemTime();
57 OSG::Thread::getCurrentChangeList()->clear();
59 endT
= OSG::getSystemTime();
61 avgT
-= timeHistory
.front();
62 while(timeHistory
.size() > 20)
63 timeHistory
.pop_front();
65 timeHistory
.push_back(endT
- startT
);
66 avgT
+= timeHistory
.back();
68 if(showFrameTime
== true)
70 SLOG
<< "frame time " << (avgT
/ timeHistory
.size())
75 // react to size changes
76 void reshape(int w
, int h
)
82 // react to mouse button presses
83 void mouse(int button
, int state
, int x
, int y
)
86 mgr
->mouseButtonRelease(button
, x
, y
);
88 mgr
->mouseButtonPress(button
, x
, y
);
93 // react to mouse motions with pressed buttons
94 void motion(int x
, int y
)
101 void keyboard(unsigned char k
, int , int )
114 std::cout
<< "printGeoInfo" << std::endl
;
115 doPrintGeoInfo(scene
);
119 std::cout
<< "makeSingleIndexed" << std::endl
;
120 doMakeSingleIndexed(scene
);
121 //doPrintGeoInfo(scene);
125 std::cout
<< "makeIndexedTrianles" << std::endl
;
126 doMakeIndexedTriangles(scene
);
127 //doPrintGeoInfo(scene);
131 std::cout
<< "makeOptimizedIndex" << std::endl
;
132 doMakeOptimizedIndex(scene
);
133 //doPrintGeoInfo(scene);
137 std::cout
<< "makeOptimizedProperties" << std::endl
;
138 doMakeOptimizedProperties(scene
);
139 //doPrintGeoInfo(scene);
143 std::cout
<< "makeSphere(5, 5.f)" << std::endl
;
144 scene
= OSG::makeSphere(5, 5.f
);
148 std::cout
<< "makeTorus(2.f, 4.f, 16, 16)" << std::endl
;
149 scene
= OSG::makeTorus(2.f
, 4.f
, 16, 16);
153 std::cout
<< "makeBox(2.f, 2.f, 2.f, 5, 5, 5)" << std::endl
;
154 scene
= OSG::makeBox(2.f
, 2.f
, 2.f
, 5, 5, 5);
158 std::cout
<< "file '" << fileName
<< "'" << std::endl
;
159 scene
= OSG::SceneFileHandler::the()->read(fileName
.c_str(), NULL
);
163 std::cout
<< "file '" << fileName
<< "' with GraphOps" << std::endl
;
164 scene
= OSG::SceneFileHandler::the()->read(fileName
.c_str());
169 std::cout
<< "mgr->showAll" << std::endl
;
174 showFrameTime
= !showFrameTime
;
175 std::cout
<< "showFrameTime " << showFrameTime
184 // setup the GLUT library which handles the windows for us
185 int setupGLUT(int *argc
, char *argv
[])
187 glutInit(argc
, argv
);
188 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
190 int winid
= glutCreateWindow("OpenSG");
192 glutReshapeFunc(reshape
);
193 glutDisplayFunc(display
);
194 glutIdleFunc(display
);
195 glutMouseFunc(mouse
);
196 glutMotionFunc(motion
);
197 glutKeyboardFunc(keyboard
);
203 doInit(int argc
, char *argv
[])
205 OSG::osgInit(argc
,argv
);
207 int winid
= setupGLUT(&argc
, argv
);
209 OSG::GLUTWindowUnrecPtr gwin
= OSG::GLUTWindow::create();
210 gwin
->setGlutId(winid
);
215 FWARNING(("No file given!\n"));
216 FWARNING(("Supported file formats:\n"));
217 OSG::SceneFileHandler::the()->print();
219 std::cout
<< "makeSphere(5, 5.f)" << std::endl
;
220 scene
= OSG::makeSphere(5, 5.f
);
226 scene
= OSG::SceneFileHandler::the()->read(fileName
.c_str(), NULL
);
229 OSG::commitChanges();
231 mgr
= OSG::SimpleSceneManager::create();
232 mgr
->setWindow(gwin
);
233 mgr
->setRoot (scene
);
237 timeHistory
.push_back(0.0);
241 doPrintGeoInfoFunc(OSG::Node
*node
)
243 OSG::NodeCore
*core
= node
->getCore();
244 OSG::Geometry
*geo
= dynamic_cast<OSG::Geometry
*>(core
);
248 std::cout
<< "== geo info ==" << std::endl
;
249 printGeoInfo(std::cout
, geo
) << std::endl
;
252 return OSG::Action::Continue
;
256 doPrintGeoInfo(OSG::Node
*pScene
)
258 OSG::traverse(pScene
,
259 boost::bind(&doPrintGeoInfoFunc
, _1
));
263 doMakeSingleIndexedFunc(OSG::Node
*node
)
265 OSG::NodeCore
*core
= node
->getCore();
266 OSG::Geometry
*geo
= dynamic_cast<OSG::Geometry
*>(core
);
270 std::cout
<< "== geo make single idx ==" << std::endl
;
271 OSG::makeSingleIndexed(geo
);
274 return OSG::Action::Continue
;
278 doMakeSingleIndexed(OSG::Node
*pScene
)
280 OSG::Time start
= OSG::getSystemTime();
282 OSG::traverse(pScene
,
283 boost::bind(&doMakeSingleIndexedFunc
, _1
));
285 std::cout
<< "time for doMakeSingleIndexed on scene: " << (OSG::getSystemTime() - start
)
290 doMakeIndexedTrianglesFunc(OSG::Node
*node
)
292 OSG::NodeCore
*core
= node
->getCore();
293 OSG::Geometry
*geo
= dynamic_cast<OSG::Geometry
*>(core
);
297 std::cout
<< "== geo make indexed tris ==" << std::endl
;
298 OSG::makeIndexedTriangles(geo
);
301 return OSG::Action::Continue
;
305 doMakeIndexedTriangles(OSG::Node
*pScene
)
307 OSG::Time start
= OSG::getSystemTime();
309 OSG::traverse(pScene
,
310 boost::bind(&doMakeIndexedTrianglesFunc
, _1
));
312 std::cout
<< "time for doMakeIndexedTriangles on scene: " << (OSG::getSystemTime() - start
)
317 doMakeOptimizedIndexFunc(OSG::Node
*node
)
319 OSG::NodeCore
*core
= node
->getCore();
320 OSG::Geometry
*geo
= dynamic_cast<OSG::Geometry
*>(core
);
324 std::cout
<< "== geo make optimized index ==" << std::endl
;
325 OSG::makeOptimizedIndex(geo
);
328 return OSG::Action::Continue
;
332 doMakeOptimizedIndex(OSG::Node
*pScene
)
334 OSG::Time start
= OSG::getSystemTime();
336 OSG::traverse(pScene
,
337 boost::bind(&doMakeOptimizedIndexFunc
, _1
));
339 std::cout
<< "time for doMakeOptimizedIndex on scene: " << (OSG::getSystemTime() - start
)
344 doMakeOptimizedPropertiesFunc(OSG::Node
*node
)
346 OSG::NodeCore
*core
= node
->getCore();
347 OSG::Geometry
*geo
= dynamic_cast<OSG::Geometry
*>(core
);
351 std::cout
<< "== geo make optimized properties ==" << std::endl
;
352 OSG::makeOptimizedProperties(geo
);
355 return OSG::Action::Continue
;
359 doMakeOptimizedProperties(OSG::Node
*pScene
)
361 OSG::Time start
= OSG::getSystemTime();
363 OSG::traverse(pScene
,
364 boost::bind(&doMakeOptimizedPropertiesFunc
, _1
));
366 std::cout
<< "time for doMakeOptimizedProperties on scene: " << (OSG::getSystemTime() - start
)
371 printTypes(std::ostream
&os
, OSG::GeoIntegralProperty
*types
)
373 os
<< "#types " << types
->size();
375 for(OSG::UInt32 i
= 0; i
< types
->size(); ++i
)
378 types
->getValue(val
, i
);
380 os
<< " (" << i
<< ": ";
383 case GL_TRIANGLES
: os
<< "GL_TRIANGLES"; break;
384 case GL_TRIANGLE_STRIP
: os
<< "GL_TRIANGLE_STRIP"; break;
385 case GL_TRIANGLE_FAN
: os
<< "GL_TRIANGLE_FAN"; break;
386 case GL_QUADS
: os
<< "GL_QUADS"; break;
387 case GL_QUAD_STRIP
: os
<< "GL_QUAD_STRIP"; break;
388 case GL_LINES
: os
<< "GL_LINES"; break;
389 case GL_LINE_STRIP
: os
<< "GL_LINE_STRIP"; break;
390 case GL_POINTS
: os
<< "GL_POINTS"; break;
391 case GL_POLYGON
: os
<< "GL_POLYGON"; break;
392 default: os
<< "?? " << val
; break;
401 printGeoInfo(std::ostream
&os
, OSG::Geometry
*geo
)
403 if(geo
->getTypes() != NULL
)
404 printTypes(os
, geo
->getTypes()) << std::endl
;
406 if(geo
->getLengths() != NULL
)
408 os
<< "#lengths " << geo
->getLengths()->size()
412 OSG::Geometry::IndexBag ib
= geo
->getUniqueIndexBag();
414 for(OSG::UInt32 i
= 0; i
< ib
.size(); ++i
)
416 os
<< "ib " << i
<< ": ";
418 for(OSG::UInt32 j
= 0; j
< ib
[i
].second
.size(); ++j
)
420 os
<< ib
[i
].second
[j
] << ", ";
426 for(OSG::UInt16 i
= 0; i
<= OSG::Geometry::LastIndex
; ++i
)
428 OSG::GeoVectorProperty
*prop
= geo
->getProperty(i
);
429 OSG::GeoIntegralProperty
*idx
= geo
->getIndex (i
);
435 << " size " << prop
->size()
436 << " type " << prop
->getType().getName();
440 os
<< " idx @" << idx
441 << " size " << idx
->size()
442 << " type " << idx
->getType().getName();
449 os
<< "prop " << i
<< " NULL idx @" << idx
450 << " size " << idx
->size()
451 << " type " << idx
->getType().getName()
456 os
<< "single indexed? " << (geo
->isSingleIndex() ? "yes" : "no")