#include <sstream>
#include <osg/Image>
#include <osg/Group>
#include <osg/Notify>
#include <osg/Geode>
#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/fstream>
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include <iostream>
using namespace osg;
using namespace osgDB;
class FSReaderWriter :public ReaderWriter
{
public:
FSReaderWriter()
{
supportsExtension("fs", "FreeSouth's format");
}
virtual const char* className() const
{
return "FSReaderWriter";
}
virtual ReadResult readNode(const std::string& file, const Options* opt) const
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext))
return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile(file, opt);
if (fileName.empty())
return ReadResult::FILE_NOT_FOUND;
osgDB::ifstream fin(fileName.c_str());
if (fin)
return readNode(fin, opt);
return 0L;
}
virtual ReadResult readNode(std::istream& fin, const Options* opt) const
{
Input fr;
fr.attach(&fin);
osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
osg::Vec3 center;
float radius;
while (!fr.eof())
{
std::cout << "out" << std::endl;
if (fr.matchSequence("%f %f %f %f"))
{
std::cout << "in" << std::endl;
fr.readSequence(center);
fr.readSequence(radius);
std::cout << "Center is :" << center.x() << "" << center.y() << " " << center.z() << " " << std::endl;
std::cout << "Radius is " << radius << std::endl;
gnode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(center, radius)));
}
else
{
std::cout << "fr is NULL" << std::endl;
++fr;
}
}
return gnode;
}
};
REGISTER_OSGPLUGIN(fs, FSReaderWriter)//注册
命名为osgdb_fs.dll,复制到OSG3.4\bin\osgPlugins-3.4目录下。
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
int main()
{
osgDB::Registry::instance()->addFileExtensionAlias("fs", "fs");//将文件扩展名与dll名关联起来
osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(osgDB::readNodeFile("D://data//osg_study//test.fs"));
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
viewer->setSceneData(group.get());
viewer->run();
return 0;
}