root/OpenSceneGraph/trunk/examples/osgsimulation/osgsimulation.cpp
@
2213
| Revision 2213, 4.3 kB (checked in by robert, 10 years ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield |
| 2 | * |
| 3 | * This application is open source and may be redistributed and/or modified |
| 4 | * freely and without restriction, both in commericial and non commericial applications, |
| 5 | * as long as this copyright notice is maintained. |
| 6 | * |
| 7 | * This application is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | */ |
| 11 | |
| 12 | #include <osgDB/ReadFile> |
| 13 | #include <osgUtil/Optimizer> |
| 14 | #include <osgProducer/Viewer> |
| 15 | |
| 16 | |
| 17 | #include <osgSim/SphereSegment> |
| 18 | |
| 19 | using namespace osgSim; |
| 20 | |
| 21 | osg::Node* createSphereSegment() |
| 22 | { |
| 23 | osgSim::SphereSegment* ss = new osgSim::SphereSegment(osg::Vec3(0.0f,0.0f,0.0f), 1.0f, |
| 24 | osg::Vec3(0.0f,1.0f,0.0f), |
| 25 | osg::DegreesToRadians(360.0f), |
| 26 | osg::DegreesToRadians(45.0f), |
| 27 | 40); |
| 28 | ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); |
| 29 | |
| 30 | return ss; |
| 31 | } |
| 32 | |
| 33 | osg::Node* createModel() |
| 34 | { |
| 35 | return createSphereSegment(); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | int main( int argc, char **argv ) |
| 40 | { |
| 41 | |
| 42 | // use an ArgumentParser object to manage the program arguments. |
| 43 | osg::ArgumentParser arguments(&argc,argv); |
| 44 | |
| 45 | // set up the usage document, in case we need to print out how to use this program. |
| 46 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
| 47 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); |
| 48 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
| 49 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
| 50 | |
| 51 | |
| 52 | // construct the viewer. |
| 53 | osgProducer::Viewer viewer(arguments); |
| 54 | |
| 55 | // set up the value with sensible default event handlers. |
| 56 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
| 57 | |
| 58 | // get details on keyboard and mouse bindings used by the viewer. |
| 59 | viewer.getUsage(*arguments.getApplicationUsage()); |
| 60 | |
| 61 | // if user request help write it out to cout. |
| 62 | if (arguments.read("-h") || arguments.read("--help")) |
| 63 | { |
| 64 | arguments.getApplicationUsage()->write(std::cout); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | // any option left unread are converted into errors to write out later. |
| 69 | arguments.reportRemainingOptionsAsUnrecognized(); |
| 70 | |
| 71 | // report any errors if they have occured when parsing the program aguments. |
| 72 | if (arguments.errors()) |
| 73 | { |
| 74 | arguments.writeErrorMessages(std::cout); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | if (arguments.argc()<=1) |
| 79 | { |
| 80 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | // osg::Timer timer; |
| 85 | // osg::Timer_t start_tick = timer.tick(); |
| 86 | // |
| 87 | // // read the scene from the list of file specified commandline args. |
| 88 | // osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
| 89 | // |
| 90 | // // if no model has been successfully loaded report failure. |
| 91 | // if (!loadedModel) |
| 92 | // { |
| 93 | // std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
| 94 | // return 1; |
| 95 | // } |
| 96 | // |
| 97 | // osg::Timer_t end_tick = timer.tick(); |
| 98 | // |
| 99 | // std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl; |
| 100 | |
| 101 | osg::ref_ptr<osg::Node> loadedModel = createModel(); |
| 102 | |
| 103 | |
| 104 | // optimize the scene graph, remove rendundent nodes and state etc. |
| 105 | osgUtil::Optimizer optimizer; |
| 106 | optimizer.optimize(loadedModel.get()); |
| 107 | |
| 108 | |
| 109 | // set the scene to render |
| 110 | viewer.setSceneData(loadedModel.get()); |
| 111 | |
| 112 | // create the windows and run the threads. |
| 113 | viewer.realize(); |
| 114 | |
| 115 | while( !viewer.done() ) |
| 116 | { |
| 117 | // wait for all cull and draw threads to complete. |
| 118 | viewer.sync(); |
| 119 | |
| 120 | // update the scene by traversing it with the the update visitor which will |
| 121 | // call all node update callbacks and animations. |
| 122 | viewer.update(); |
| 123 | |
| 124 | // fire off the cull and draw traversals of the scene. |
| 125 | viewer.frame(); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | // wait for all cull and draw threads to complete before exit. |
| 130 | viewer.sync(); |
| 131 | |
| 132 | return 0; |
| 133 | } |
Note: See TracBrowser
for help on using the browser.
