/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield * * This application is open source and may be redistributed and/or modified * freely and without restriction, both in commercial and non commercial * applications, as long as this copyright notice is maintained. * * This application is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * */ #include #include #include #include #include #include #include class ReaderWriterOsgViewer : public osgDB::ReaderWriter { public: ReaderWriterOsgViewer() { supportsExtension("osgviewer","OpenSceneGraph viewer configuration format"); supportsExtension("view","OpenSceneGraph viewer configuration format"); supportsOption("precision","Set the floating point precision of output"); supportsOption("OutputTextureFiles","Output texture image to file"); } virtual const char* className() const { return "osgViewer configuration loader"; } void setPrecision(osgDB::Output& fout, const osgDB::ReaderWriter::Options* options) const { if (options) { std::istringstream iss(options->getOptionString()); std::string opt; while (iss >> opt) { if(opt=="PRECISION" || opt=="precision") { int prec; iss >> prec; fout.precision(prec); } if (opt=="OutputTextureFiles") { fout.setOutputTextureFiles(true); } } } } virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const { std::string ext = osgDB::getLowerCaseFileExtension(file); if( !acceptsExtension(ext) ) return ReadResult::FILE_NOT_HANDLED; std::string fileName = osgDB::findDataFile( file, options ); if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; // code for setting up the database path so that internally referenced file are searched for on relative paths. osgDB::ifstream fin(fileName.c_str()); if (fin) { return readObject(fin, options); } return 0L; } virtual ReadResult readObject(std::istream& fin, const osgDB::ReaderWriter::Options* options) const { osgDB::Input fr; fr.attach(&fin); fr.setOptions(options); typedef std::vector< osg::ref_ptr > ViewList; ViewList viewList; // load all nodes in file, placing them in a group. while(!fr.eof()) { osg::ref_ptr object = fr.readObject(); osgViewer::View* view = dynamic_cast(object.get()); if (view) { viewList.push_back(view); } else fr.advanceOverCurrentFieldOrBlock(); } if (viewList.empty()) { return ReadResult("No data loaded"); } else if (viewList.size()==1) { return viewList.front().get(); } else { OSG_NOTICE<<"Found multiple view's, just taking first"<