Added new osg::ApplicationUsage and osg::ArgumentParser to help streamline
application/example developement.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
TOPDIR = ../../..
|
||||
TOPDIR = ../../../
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
//Distributed under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE (LGPL)
|
||||
//as published by the Free Software Foundation.
|
||||
|
||||
#include <osg/ArgumentParser>
|
||||
#include <osg/ApplicationUsage>
|
||||
|
||||
|
||||
#include <osgUtil/Optimizer>
|
||||
|
||||
#include <osgDB/ReadFile>
|
||||
@@ -15,83 +19,85 @@
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
|
||||
// create the commandline args.
|
||||
// use an ArgumentParser object to manage the program arguments.
|
||||
osg::ArgumentParser arguments(&argc,argv);
|
||||
|
||||
// set up the usage document, in case we need to print out how to use this program.
|
||||
osg::ApplicationUsage::instance()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
|
||||
osg::ApplicationUsage::instance()->addCommandLineOption("-h or --help","Display this information");
|
||||
osg::ApplicationUsage::instance()->addCommandLineOption("-p <filename>","Specify camera path file to animate the camera through the loaded scene");
|
||||
|
||||
|
||||
// construct the viewer.
|
||||
osgProducer::Viewer viewer(arguments);
|
||||
|
||||
// set up the value with sensible default event handlers.
|
||||
viewer.setUpViewer();
|
||||
|
||||
// if a pathfile has been specified on command line use it to animate the camera via an AnimationPathManipulator.
|
||||
std::string pathfile;
|
||||
std::string configfile;
|
||||
std::vector<std::string> commandLine;
|
||||
for(int i=1;i<argc;++i) {
|
||||
if( std::string(argv[i]) == "-p" ) {
|
||||
if( (i+1) >= argc ) {
|
||||
std::cout << "path argument required for -p option."<<std::endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
pathfile = std::string(argv[++i]);
|
||||
}
|
||||
else
|
||||
if( std::string(argv[i]) == "-c" ) {
|
||||
if( (i+1) >= argc ) {
|
||||
std::cout << "path argument required for -c option."<<std::endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
configfile = std::string(argv[++i]);
|
||||
}
|
||||
else
|
||||
commandLine.push_back(argv[i]);
|
||||
}
|
||||
|
||||
osg::DisplaySettings::instance()->readCommandLine(commandLine);
|
||||
osgDB::readCommandLine(commandLine);
|
||||
|
||||
osgProducer::Viewer* viewer = 0;
|
||||
if (!configfile.empty()) viewer = new osgProducer::Viewer(configfile);
|
||||
else viewer = new osgProducer::Viewer;
|
||||
|
||||
|
||||
// configure the plugin registry from the commandline arguments, and
|
||||
// eat any parameters that have been matched.
|
||||
osgDB::readCommandLine(commandLine);
|
||||
|
||||
|
||||
// read the scene.
|
||||
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(commandLine);
|
||||
if (!loadedModel) return 1;
|
||||
|
||||
// optimize it, remove rendundent nodes and state etc.
|
||||
osgUtil::Optimizer optimizer;
|
||||
optimizer.optimize(loadedModel.get());
|
||||
|
||||
|
||||
// set up the value with sensible defaults.
|
||||
viewer->setUpViewer();
|
||||
|
||||
if( !pathfile.empty() ) {
|
||||
while (arguments.read("-p",pathfile))
|
||||
{
|
||||
osg::ref_ptr<osgGA::AnimationPathManipulator> apm = new osgGA::AnimationPathManipulator(pathfile);
|
||||
if( apm.valid() && apm->valid() )
|
||||
{
|
||||
unsigned int num = viewer->addCameraManipulator(apm.get());
|
||||
viewer->selectCameraManipulator(num);
|
||||
unsigned int num = viewer.addCameraManipulator(apm.get());
|
||||
viewer.selectCameraManipulator(num);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// if user request help pritn it out to cout.
|
||||
if (arguments.read("-h") || arguments.read("--help"))
|
||||
{
|
||||
osg::ApplicationUsage::instance()->write(cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// read the scene from the list of file specified commandline args.
|
||||
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
||||
|
||||
// any option left unread a converted into errors to write out later.
|
||||
arguments.reportRemainingOptionsAsUnrecognized();
|
||||
|
||||
// report any errors if they have occured when parsing the program aguments.
|
||||
if (arguments.errors())
|
||||
{
|
||||
arguments.writeErrorMessages(cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// if no model has been successfully loaded report failure.
|
||||
if (!loadedModel)
|
||||
{
|
||||
std::cout << arguments.getProgramName() <<": No input files" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// optimize the scene graph, remove rendundent nodes and state etc.
|
||||
osgUtil::Optimizer optimizer;
|
||||
optimizer.optimize(loadedModel.get());
|
||||
|
||||
// set the scene to render
|
||||
viewer->setSceneData(loadedModel.get());
|
||||
viewer.setSceneData(loadedModel.get());
|
||||
|
||||
// create the windows and run the threads.
|
||||
viewer->realize(Producer::CameraGroup::ThreadPerCamera);
|
||||
viewer.realize(Producer::CameraGroup::ThreadPerCamera);
|
||||
|
||||
while( !viewer->done() )
|
||||
while( !viewer.done() )
|
||||
{
|
||||
// wait for all cull and draw threads to complete.
|
||||
viewer->sync();
|
||||
viewer.sync();
|
||||
|
||||
// update the scene by traversing it with the the update visitor which will
|
||||
// call all node update callbacks and animations.
|
||||
viewer->update();
|
||||
viewer.update();
|
||||
|
||||
// fire off the cull and draw traversals of the scene.
|
||||
viewer->frame();
|
||||
viewer.frame();
|
||||
|
||||
}
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user