root/OpenSceneGraph/trunk/src/osgPlugins/view/ReaderWriterOsgViewer.cpp @ 13041

Revision 13041, 4.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified
4 * freely and without restriction, both in commercial and non commercial
5 * applications, 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
13#include <sstream>
14
15#include <osgViewer/View>
16
17#include <osgDB/FileNameUtils>
18#include <osgDB/FileUtils>
19#include <osgDB/Registry>
20#include <osgDB/Input>
21#include <osgDB/Output>
22
23class ReaderWriterOsgViewer : public osgDB::ReaderWriter
24{
25public:
26    ReaderWriterOsgViewer()
27    {
28        supportsExtension("osgviewer","OpenSceneGraph viewer configuration format");
29        supportsExtension("view","OpenSceneGraph viewer configuration format");
30        supportsOption("precision","Set the floating point precision of output");
31        supportsOption("OutputTextureFiles","Output texture image to file");
32    }
33
34    virtual const char* className() const { return "osgViewer configuration loader"; }
35
36    void setPrecision(osgDB::Output& fout, const osgDB::ReaderWriter::Options* options) const
37    {
38        if (options)
39        {
40            std::istringstream iss(options->getOptionString());
41            std::string opt;
42            while (iss >> opt)
43            {
44                if(opt=="PRECISION" || opt=="precision")
45                {
46                    int prec;
47                    iss >> prec;
48                    fout.precision(prec);
49                }
50                if (opt=="OutputTextureFiles")
51                {
52                    fout.setOutputTextureFiles(true);
53                }
54            }
55        }
56    }
57
58
59    virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const
60    {
61        std::string ext = osgDB::getLowerCaseFileExtension(file);
62        if( !acceptsExtension(ext) )
63            return ReadResult::FILE_NOT_HANDLED;
64
65        std::string fileName = osgDB::findDataFile( file, options );
66        if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
67
68        // code for setting up the database path so that internally referenced file are searched for on relative paths.
69        osgDB::ifstream fin(fileName.c_str());
70        if (fin)
71        {
72            return readObject(fin, options);
73        }
74        return 0L;
75
76    }
77
78    virtual ReadResult readObject(std::istream& fin, const osgDB::ReaderWriter::Options* options) const
79    {
80        osgDB::Input fr;
81        fr.attach(&fin);
82        fr.setOptions(options);
83
84        typedef std::vector< osg::ref_ptr<osgViewer::View> > ViewList;
85        ViewList viewList;
86
87        // load all nodes in file, placing them in a group.
88        while(!fr.eof())
89        {
90            osg::ref_ptr<osg::Object> object = fr.readObject();
91            osgViewer::View* view = dynamic_cast<osgViewer::View*>(object.get());
92            if (view)
93            {
94                viewList.push_back(view);
95            }
96            else fr.advanceOverCurrentFieldOrBlock();
97        }
98
99        if  (viewList.empty())
100        {
101            return ReadResult("No data loaded");
102        }
103        else if (viewList.size()==1)
104        {
105            return viewList.front().get();
106        }
107        else
108        {
109            OSG_NOTICE<<"Found multiple view's, just taking first"<<std::endl;
110            return viewList.front().get();
111        }
112
113    }
114
115    virtual WriteResult writeObject(const osg::Object& obj,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
116    {
117        std::string ext = osgDB::getLowerCaseFileExtension(fileName);
118        if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
119
120        osgDB::Output fout(fileName.c_str());
121        fout.setOptions(options);
122        if (fout)
123        {
124            setPrecision(fout,options);
125
126            fout.writeObject(obj);
127            fout.close();
128            return WriteResult::FILE_SAVED;
129        }
130        return WriteResult("Unable to open file for output");
131    }
132
133
134    virtual WriteResult writeObject(const osg::Object& obj,std::ostream& fout, const osgDB::ReaderWriter::Options* options) const
135    {
136        osgDB::Output foutput;
137        foutput.setOptions(options);
138
139        std::ios &fios = foutput;
140        fios.rdbuf(fout.rdbuf());
141
142        if (fout)
143        {
144            setPrecision(foutput,options);
145
146            foutput.writeObject(obj);
147            return WriteResult::FILE_SAVED;
148        }
149        return WriteResult("Unable to write to output stream");
150    }
151
152};
153
154
155// Add ourself to the Registry to instantiate the reader/writer.
156REGISTER_OSGPLUGIN(osgViewer, ReaderWriterOsgViewer)
Note: See TracBrowser for help on using the browser.