root/OpenSceneGraph/trunk/src/osgPlugins/trans/ReaderWriterTRANS.cpp @ 13041

Revision 13041, 5.2 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-2004 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/* file:        src/osgPlugins/trans/ReaderWriterTRANS.cpp
14 * author:      Mike Weiblen http://mew.cx/ 2004-07-15
15 * copyright:   (C) 2004 Michael Weiblen
16 * license:     OpenSceneGraph Public License (OSGPL)
17*/
18
19#include <osg/Notify>
20#include <osg/Matrix>
21#include <osg/MatrixTransform>
22
23#include <osgDB/ReaderWriter>
24#include <osgDB/FileNameUtils>
25#include <osgDB/Registry>
26#include <osgDB/ReadFile>
27
28#include <stdio.h>
29
30#define EXTENSION_NAME "trans"
31
32
33static bool getFilenameAndParams(const std::string& input, std::string& filename, std::string& params)
34{
35    // find the start of the params list, accounting for nesting of [] and () brackets,
36    // note, we are working backwards.
37    int noNestedBrackets = 0;
38    std::string::size_type pos = input.size();
39    for(; pos>0; )
40    {
41        --pos;
42        char c = input[pos];
43        if (c==']') ++noNestedBrackets;
44        else if (c=='[') --noNestedBrackets;
45        else if (c==')') ++noNestedBrackets;
46        else if (c=='(') --noNestedBrackets;
47        else if (c=='.' && noNestedBrackets==0) break;
48    }
49
50    // get the next "extension", which actually contains the pseudo-loader parameters
51    params = input.substr(pos+1, std::string::npos );
52    if( params.empty() )
53    {
54        OSG_WARN << "Missing parameters for " EXTENSION_NAME " pseudo-loader" << std::endl;
55        return false;
56    }
57
58    // clear the params sting of any brackets.
59    std::string::size_type params_pos = params.size();
60    for(; params_pos>0; )
61    {
62        --params_pos;
63        char c = params[params_pos];
64        if (c==']' || c=='[' || c==')' || c=='(')
65        {
66            params.erase(params_pos,1);
67        }
68    }
69
70    // strip the "params extension", which must leave a sub-filename.
71    filename = input.substr(0, pos );
72
73    return true;
74}
75
76///////////////////////////////////////////////////////////////////////////
77
78/**
79 * An OSG reader plugin for the ".trans" pseudo-loader, which inserts a
80 * translation transform above the loaded geometry.
81 * This pseudo-loader make it simple to change the origin of a saved model
82 * by specifying a correcting translation as part of the filename.
83 *
84 * Usage: <modelfile.ext>.<tx>,<ty>,<tz>.globe
85 * where:
86 *      <modelfile.ext> = an model filename.
87 *      <tx> = translation along the X axis.
88 *      <ty> = translation along the Y axis.
89 *      <tz> = translation along the Z axis.
90 *
91 * example: osgviewer cow.osg.25,0,0.trans cessna.osg
92 */
93
94class ReaderWriterTRANS : public osgDB::ReaderWriter
95{
96public:
97    ReaderWriterTRANS()
98    {
99        supportsExtension(EXTENSION_NAME,"Translation Psuedo loader.");
100    }
101
102    virtual const char* className() const { return "translation pseudo-loader"; }
103
104    virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
105    {
106        std::string ext = osgDB::getLowerCaseFileExtension(fileName);
107        if( !acceptsExtension(ext) )
108            return ReadResult::FILE_NOT_HANDLED;
109
110        OSG_INFO << "ReaderWriterTRANS( \"" << fileName << "\" )" << std::endl;
111
112        // strip the pseudo-loader extension
113        std::string tmpName = osgDB::getNameLessExtension( fileName );
114
115        if (tmpName.empty())
116            return ReadResult::FILE_NOT_HANDLED;
117
118        std::string subFileName, params;
119        if (!getFilenameAndParams(tmpName, subFileName, params))
120        {
121            return ReadResult::FILE_NOT_HANDLED;
122        }
123
124        if( subFileName.empty())
125        {
126            OSG_WARN << "Missing subfilename for " EXTENSION_NAME " pseudo-loader" << std::endl;
127            return ReadResult::FILE_NOT_HANDLED;
128        }
129
130        OSG_INFO << " params = \"" << params << "\"" << std::endl;
131        OSG_INFO << " subFileName = \"" << subFileName << "\"" << std::endl;
132
133        float tx, ty, tz;
134        int count = sscanf( params.c_str(), "%f,%f,%f", &tx, &ty, &tz );
135        if( count != 3 )
136        {
137            OSG_WARN << "Bad parameters for " EXTENSION_NAME " pseudo-loader: \"" << params << "\"" << std::endl;
138            return ReadResult::FILE_NOT_HANDLED;
139        }
140
141        // recursively load the subfile.
142        osg::Node *node = osgDB::readNodeFile( subFileName, options );
143        if( !node )
144        {
145            // propagate the read failure upwards
146            OSG_WARN << "Subfile \"" << subFileName << "\" could not be loaded" << std::endl;
147            return ReadResult::FILE_NOT_HANDLED;
148        }
149
150        osg::MatrixTransform *xform = new osg::MatrixTransform;
151        xform->setDataVariance( osg::Object::STATIC );
152        xform->setMatrix( osg::Matrix::translate( tx, ty, tz ) );
153        xform->addChild( node );
154        return xform;
155    }
156};
157
158
159// Add ourself to the Registry to instantiate the reader/writer.
160REGISTER_OSGPLUGIN(trans, ReaderWriterTRANS)
161
162/*EOF*/
Note: See TracBrowser for help on using the browser.