2001-09-20 05:08:56 +08:00
|
|
|
#include "osg/Image"
|
|
|
|
#include "osg/Group"
|
|
|
|
|
|
|
|
#include "osgDB/FileNameUtils"
|
|
|
|
#include "osgDB/Registry"
|
|
|
|
#include "osgDB/Input"
|
|
|
|
#include "osgDB/Output"
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
using namespace osgDB;
|
|
|
|
|
|
|
|
class OSGReaderWriter : public ReaderWriter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual const char* className() { return "OSG Reader/Writer"; }
|
|
|
|
|
|
|
|
virtual bool acceptsExtension(const std::string& extension)
|
|
|
|
{
|
|
|
|
return equalCaseInsensitive(extension,"osg");
|
|
|
|
}
|
|
|
|
|
2001-10-30 22:20:37 +08:00
|
|
|
virtual ReadResult readObject(const std::string& fileName, const Options* opt) { return readNode(fileName,opt); }
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-05-26 17:27:16 +08:00
|
|
|
virtual ReadResult readNode(const std::string& fileName, const Options* opt)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
std::string ext = getFileExtension(fileName);
|
2001-10-30 22:20:37 +08:00
|
|
|
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-05-26 17:27:16 +08:00
|
|
|
std::ifstream fin(fileName.c_str());
|
2001-09-20 05:08:56 +08:00
|
|
|
if (fin)
|
|
|
|
{
|
2003-05-26 17:27:16 +08:00
|
|
|
return readNode(fin, opt);
|
|
|
|
}
|
|
|
|
return 0L;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ReadResult readNode(std::istream& fin, const Options*)
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-05-26 17:27:16 +08:00
|
|
|
Input fr;
|
|
|
|
fr.attach(&fin);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-05-26 17:27:16 +08:00
|
|
|
typedef std::vector<osg::Node*> NodeList;
|
|
|
|
NodeList nodeList;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-05-26 17:27:16 +08:00
|
|
|
// load all nodes in file, placing them in a group.
|
|
|
|
while(!fr.eof())
|
|
|
|
{
|
|
|
|
Node *node = fr.readNode();
|
|
|
|
if (node) nodeList.push_back(node);
|
|
|
|
else fr.advanceOverCurrentFieldOrBlock();
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-05-26 17:27:16 +08:00
|
|
|
if (nodeList.empty())
|
|
|
|
{
|
|
|
|
return ReadResult("No data loaded");
|
|
|
|
}
|
|
|
|
else if (nodeList.size()==1)
|
|
|
|
{
|
|
|
|
return nodeList.front();
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-05-26 17:27:16 +08:00
|
|
|
Group* group = new Group;
|
|
|
|
group->setName("import group");
|
|
|
|
for(NodeList::iterator itr=nodeList.begin();
|
|
|
|
itr!=nodeList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
group->addChild(*itr);
|
|
|
|
}
|
|
|
|
return group;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2003-05-26 17:27:16 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2001-10-30 22:20:37 +08:00
|
|
|
virtual WriteResult writeObject(const Object& obj,const std::string& fileName, const osgDB::ReaderWriter::Options*)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
Output fout;
|
|
|
|
fout.open(fileName.c_str());
|
|
|
|
if (fout)
|
|
|
|
{
|
|
|
|
fout.writeObject(obj);
|
|
|
|
fout.close();
|
2001-10-30 22:20:37 +08:00
|
|
|
return WriteResult::FILE_SAVED;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2001-10-30 22:20:37 +08:00
|
|
|
return WriteResult("Unable to open file for output");
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2003-07-23 19:45:37 +08:00
|
|
|
virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options*)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-02-19 04:46:47 +08:00
|
|
|
std::string ext = getFileExtension(fileName);
|
|
|
|
if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
|
|
|
|
|
2003-05-26 17:27:16 +08:00
|
|
|
Output fout(fileName.c_str());
|
2001-09-20 05:08:56 +08:00
|
|
|
if (fout)
|
|
|
|
{
|
|
|
|
fout.writeObject(node);
|
|
|
|
fout.close();
|
2001-10-30 22:20:37 +08:00
|
|
|
return WriteResult::FILE_SAVED;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2001-10-30 22:20:37 +08:00
|
|
|
return WriteResult("Unable to open file for output");
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// now register with Registry to instantiate the above
|
|
|
|
// reader/writer.
|
|
|
|
RegisterReaderWriterProxy<OSGReaderWriter> g_OSGReaderWriterProxy;
|