| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | #include <osgDB/WriteFile> |
|---|
| 14 | #include <osgUtil/Optimizer> |
|---|
| 15 | #include <osgUtil/Simplifier> |
|---|
| 16 | #include <osgUtil/TriStripVisitor> |
|---|
| 17 | #include <osgUtil/Optimizer> |
|---|
| 18 | #include <osgProducer/Viewer> |
|---|
| 19 | |
|---|
| 20 | int main( int argc, char **argv ) |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 28 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); |
|---|
| 29 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 30 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | float sampleRatio = 0.5f; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | osgProducer::Viewer viewer(arguments); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | while (arguments.read("-s",sampleRatio)) {} |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 49 | { |
|---|
| 50 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 51 | return 1; |
|---|
| 52 | } |
|---|
| 53 | std::string outputFileName; |
|---|
| 54 | while (arguments.read("-o",outputFileName)) {} |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | if (arguments.errors()) |
|---|
| 58 | { |
|---|
| 59 | arguments.writeErrorMessages(std::cout); |
|---|
| 60 | return 1; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | if (arguments.argc()<=1) |
|---|
| 64 | { |
|---|
| 65 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 66 | return 1; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | if (!loadedModel) |
|---|
| 76 | { |
|---|
| 77 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 78 | return 1; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | if (arguments.errors()) |
|---|
| 86 | { |
|---|
| 87 | arguments.writeErrorMessages(std::cout); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | osg::Timer_t end_load_tick = osg::Timer::instance()->tick(); |
|---|
| 91 | |
|---|
| 92 | std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_load_tick)<<std::endl; |
|---|
| 93 | |
|---|
| 94 | osgUtil::Simplifier simplifier(sampleRatio); |
|---|
| 95 | loadedModel->accept(simplifier); |
|---|
| 96 | |
|---|
| 97 | osg::Timer_t end_simplifier_tick = osg::Timer::instance()->tick(); |
|---|
| 98 | |
|---|
| 99 | std::cout << "Time to simplify = "<<osg::Timer::instance()->delta_s(end_load_tick, end_simplifier_tick)<<std::endl; |
|---|
| 100 | |
|---|
| 101 | osgUtil::TriStripVisitor tsv; |
|---|
| 102 | tsv.setMinStripSize(3); |
|---|
| 103 | loadedModel->accept(tsv); |
|---|
| 104 | tsv.stripify(); |
|---|
| 105 | |
|---|
| 106 | osg::Timer_t end_tristrip_tick = osg::Timer::instance()->tick(); |
|---|
| 107 | |
|---|
| 108 | std::cout << "Time to tri strip = "<<osg::Timer::instance()->delta_s(end_simplifier_tick, end_tristrip_tick)<<std::endl; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | osgUtil::Optimizer optimzer; |
|---|
| 112 | optimzer.optimize(loadedModel.get()); |
|---|
| 113 | |
|---|
| 114 | if (!outputFileName.empty()) |
|---|
| 115 | { |
|---|
| 116 | std::cout << "Writing out scene graph as '" << outputFileName << "'"<<std::endl; |
|---|
| 117 | osgDB::writeNodeFile(*loadedModel,outputFileName); |
|---|
| 118 | return 0; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | viewer.setSceneData(loadedModel.get()); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | viewer.realize(); |
|---|
| 126 | |
|---|
| 127 | while( !viewer.done() ) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | viewer.sync(); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | viewer.update(); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | viewer.frame(); |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | viewer.sync(); |
|---|
| 143 | |
|---|
| 144 | return 0; |
|---|
| 145 | } |
|---|