| 1 | #include <iostream> |
|---|
| 2 | #include <sstream> |
|---|
| 3 | #include <math.h> |
|---|
| 4 | #include <stdlib.h> |
|---|
| 5 | #include <osg/Notify> |
|---|
| 6 | #include <osg/Group> |
|---|
| 7 | #include <osgDB/ReadFile> |
|---|
| 8 | |
|---|
| 9 | #include <osgDB/FileNameUtils> |
|---|
| 10 | #include <osgDB/Registry> |
|---|
| 11 | |
|---|
| 12 | #include "Normals.h" |
|---|
| 13 | |
|---|
| 14 | class NormalsReader: public osgDB::ReaderWriter |
|---|
| 15 | { |
|---|
| 16 | public: |
|---|
| 17 | NormalsReader() |
|---|
| 18 | { |
|---|
| 19 | supportsExtension("normals","Normals Pseudo loader"); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | virtual const char* className() { return "Normals Pseudo Loader"; } |
|---|
| 23 | |
|---|
| 24 | virtual bool acceptsExtension(const std::string& extension) const |
|---|
| 25 | { |
|---|
| 26 | return osgDB::equalCaseInsensitive(extension,"normals"); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | virtual ReadResult readObject(const std::string& fileName, const Options* opt) const |
|---|
| 30 | { return readNode(fileName,opt); } |
|---|
| 31 | |
|---|
| 32 | virtual ReadResult readNode(const std::string& fileName, const Options* options) const |
|---|
| 33 | { |
|---|
| 34 | std::string ext = osgDB::getFileExtension(fileName); |
|---|
| 35 | if (!acceptsExtension(ext)) |
|---|
| 36 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 37 | |
|---|
| 38 | float scale = 1.0; |
|---|
| 39 | Normals::Mode mode = Normals::VertexNormals; |
|---|
| 40 | |
|---|
| 41 | if (options) |
|---|
| 42 | { |
|---|
| 43 | std::istringstream iss(options->getOptionString()); |
|---|
| 44 | std::string opt; |
|---|
| 45 | while (iss >> opt) |
|---|
| 46 | { |
|---|
| 47 | if( opt == "help" || opt == "HELP" ) |
|---|
| 48 | { |
|---|
| 49 | usage(); |
|---|
| 50 | } |
|---|
| 51 | else |
|---|
| 52 | { |
|---|
| 53 | size_t index = opt.find( "=" ); |
|---|
| 54 | if (index == std::string::npos) { |
|---|
| 55 | usage(); |
|---|
| 56 | } else { |
|---|
| 57 | std::string key = opt.substr(0, index); |
|---|
| 58 | std::string value = opt.substr(index+1); |
|---|
| 59 | if( key == "scale" || key == "SCALE" ) |
|---|
| 60 | { |
|---|
| 61 | scale = atof( value.c_str() ); |
|---|
| 62 | } |
|---|
| 63 | else if( key == "mode" || key == "MODE" ) |
|---|
| 64 | { |
|---|
| 65 | if( value == "VertexNormals" ) |
|---|
| 66 | mode = Normals::VertexNormals; |
|---|
| 67 | else if( value == "SurfaceNormals" ) |
|---|
| 68 | mode = Normals::SurfaceNormals; |
|---|
| 69 | else |
|---|
| 70 | mode = Normals::VertexNormals; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | std::string nodeName = osgDB::getNameLessExtension( fileName ); |
|---|
| 78 | if( !nodeName.empty() ) |
|---|
| 79 | { |
|---|
| 80 | osg::ref_ptr<osg::Node> node = osgDB::readNodeFile( nodeName ); |
|---|
| 81 | if( node.valid() ) |
|---|
| 82 | { |
|---|
| 83 | osg::ref_ptr<osg::Group> group = new osg::Group; |
|---|
| 84 | group->addChild( node.get() ); |
|---|
| 85 | |
|---|
| 86 | const osg::BoundingSphere& bsph = group->getBound(); |
|---|
| 87 | scale = bsph.radius() * 0.05f * scale; |
|---|
| 88 | |
|---|
| 89 | if( mode == Normals::VertexNormals ) |
|---|
| 90 | group->addChild( new VertexNormals( node.get(), scale )); |
|---|
| 91 | else if( mode == Normals::SurfaceNormals ) |
|---|
| 92 | group->addChild( new SurfaceNormals( node.get(), scale )); |
|---|
| 93 | |
|---|
| 94 | return group.get(); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | return 0L; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | private: |
|---|
| 101 | void usage() const { |
|---|
| 102 | osg::notify( osg::INFO ) << |
|---|
| 103 | "Normals Plugin usage: <application> [-O options] <model.ext>.normals\n" |
|---|
| 104 | " options: \"scale=<scale>\" (default = 1.0)\n" |
|---|
| 105 | " \"mode=<VertexNormals|SurfaceNormals>\" (default = VertexNormals)" << std::endl; |
|---|
| 106 | } |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | REGISTER_OSGPLUGIN(normals, NormalsReader) |
|---|