root/OpenSceneGraph/trunk/src/osgPlugins/shadow/ReaderWriterOsgShadow.cpp @ 13041

Revision 13041, 5.0 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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 <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
27static bool getFilenameAndParams(const std::string& input, std::string& filename, std::string& params)
28{
29        // find the start of the params list, accounting for nesting of [] and () brackets,
30        // note, we are working backwards.
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        // get the next "extension", which actually contains the pseudo-loader parameters
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        // clear the params sting of any brackets.
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        // strip the "params extension", which must leave a sub-filename.
65        filename = input.substr(0, pos );
66
67        return true;
68}
69
70///////////////////////////////////////////////////////////////////////////
71
72class ReaderWriterOsgShadow : public osgDB::ReaderWriter
73{
74public:
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        // strip the pseudo-loader extension
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//            else if (params=="ParallelSplitShadowMap" || params=="pssm") technique = new osgShadow::ParallelSplitShadowMap;
117            else subFileName += std::string(".") + params;
118        }
119
120        // default fallback to using ShadowVolume
121        if (!technique) technique = new osgShadow::ShadowVolume;
122
123        // recursively load the subfile.
124        osg::Node *node = osgDB::readNodeFile( subFileName, options );
125        if( !node )
126        {
127            // propagate the read failure upwards
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// Add ourself to the Registry to instantiate the reader/writer.
141REGISTER_OSGPLUGIN(osgShadow, ReaderWriterOsgShadow)
Note: See TracBrowser for help on using the browser.