| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osgShadow/ShadowedScene> |
|---|
| 14 | #include <osgShadow/ShadowVolume> |
|---|
| 15 | #include <osgShadow/ShadowTexture> |
|---|
| 16 | #include <osgShadow/ShadowMap> |
|---|
| 17 | |
|---|
| 18 | #include <osgDB/ReaderWriter> |
|---|
| 19 | #include <osgDB/FileNameUtils> |
|---|
| 20 | #include <osgDB/Registry> |
|---|
| 21 | #include <osgDB/ReadFile> |
|---|
| 22 | |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | |
|---|
| 25 | #define EXTENSION_NAME "osgShadow" |
|---|
| 26 | |
|---|
| 27 | static bool getFilenameAndParams(const std::string& input, std::string& filename, std::string& params) |
|---|
| 28 | { |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | int noNestedBrackets = 0; |
|---|
| 32 | std::string::size_type pos = input.size(); |
|---|
| 33 | for(; pos>0; ) |
|---|
| 34 | { |
|---|
| 35 | --pos; |
|---|
| 36 | char c = input[pos]; |
|---|
| 37 | if (c==']') ++noNestedBrackets; |
|---|
| 38 | else if (c=='[') --noNestedBrackets; |
|---|
| 39 | else if (c==')') ++noNestedBrackets; |
|---|
| 40 | else if (c=='(') --noNestedBrackets; |
|---|
| 41 | else if (c=='.' && noNestedBrackets==0) break; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | params = input.substr(pos+1, std::string::npos ); |
|---|
| 46 | if( params.empty() ) |
|---|
| 47 | { |
|---|
| 48 | OSG_WARN << "Missing parameters for " EXTENSION_NAME " pseudo-loader" << std::endl; |
|---|
| 49 | return false; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | std::string::size_type params_pos = params.size(); |
|---|
| 54 | for(; params_pos>0; ) |
|---|
| 55 | { |
|---|
| 56 | --params_pos; |
|---|
| 57 | char c = params[params_pos]; |
|---|
| 58 | if (c==']' || c=='[' || c==')' || c=='(') |
|---|
| 59 | { |
|---|
| 60 | params.erase(params_pos,1); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | filename = input.substr(0, pos ); |
|---|
| 66 | |
|---|
| 67 | return true; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | class ReaderWriterOsgShadow : public osgDB::ReaderWriter |
|---|
| 73 | { |
|---|
| 74 | public: |
|---|
| 75 | ReaderWriterOsgShadow() |
|---|
| 76 | { |
|---|
| 77 | supportsExtension("osgShadow","OpenSceneGraph osgShadow extension to .osg ascii format"); |
|---|
| 78 | supportsExtension("shadow","OpenSceneGraph osgShadow extension pseudo loader"); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | virtual const char* className() const { return "osgShadow pseudo-loader"; } |
|---|
| 82 | |
|---|
| 83 | virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const |
|---|
| 84 | { |
|---|
| 85 | std::string ext = osgDB::getLowerCaseFileExtension(fileName); |
|---|
| 86 | if( !acceptsExtension(ext) ) |
|---|
| 87 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | std::string tmpName = osgDB::getNameLessExtension( fileName ); |
|---|
| 91 | |
|---|
| 92 | if (tmpName.empty()) |
|---|
| 93 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 94 | |
|---|
| 95 | std::string subFileName, params; |
|---|
| 96 | if (!getFilenameAndParams(tmpName, subFileName, params)) |
|---|
| 97 | { |
|---|
| 98 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if( subFileName.empty()) |
|---|
| 102 | { |
|---|
| 103 | OSG_WARN << "Missing subfilename for " EXTENSION_NAME " pseudo-loader" << std::endl; |
|---|
| 104 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | OSG_INFO << " params = \"" << params << "\"" << std::endl; |
|---|
| 108 | OSG_INFO << " subFileName = \"" << subFileName << "\"" << std::endl; |
|---|
| 109 | |
|---|
| 110 | osg::ref_ptr<osgShadow::ShadowTechnique> technique; |
|---|
| 111 | if (!params.empty()) |
|---|
| 112 | { |
|---|
| 113 | if (params=="ShadowVolume" || params=="sv") technique = new osgShadow::ShadowVolume; |
|---|
| 114 | else if (params=="ShadowTexture" || params=="st") technique = new osgShadow::ShadowTexture; |
|---|
| 115 | else if (params=="ShadowMap" || params=="sm") technique = new osgShadow::ShadowMap; |
|---|
| 116 | |
|---|
| 117 | else subFileName += std::string(".") + params; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | if (!technique) technique = new osgShadow::ShadowVolume; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | osg::Node *node = osgDB::readNodeFile( subFileName, options ); |
|---|
| 125 | if( !node ) |
|---|
| 126 | { |
|---|
| 127 | |
|---|
| 128 | OSG_WARN << "Subfile \"" << subFileName << "\" could not be loaded" << std::endl; |
|---|
| 129 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | osgShadow::ShadowedScene* shadowedScene = new osgShadow::ShadowedScene; |
|---|
| 133 | shadowedScene->setShadowTechnique(technique.get()); |
|---|
| 134 | shadowedScene->addChild( node ); |
|---|
| 135 | return shadowedScene; |
|---|
| 136 | } |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | REGISTER_OSGPLUGIN(osgShadow, ReaderWriterOsgShadow) |
|---|