root/OpenSceneGraph/trunk/src/osgPlugins/rot/ReaderWriterROT.cpp @ 13041

Revision 13041, 5.4 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-2006 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/rot/ReaderWriterROT.cpp
14 * author:    Mike Weiblen http://mew.cx/ 2005-06-06
15 * copyright:    (C) 2005 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 "rot"
31
32static bool getFilenameAndParams(const std::string& input, std::string& filename, std::string& params)
33{
34        // find the start of the params list, accounting for nesting of [] and () brackets,
35        // note, we are working backwards.
36        int noNestedBrackets = 0;
37        std::string::size_type pos = input.size();
38        for(; pos>0; )
39        {
40            --pos;
41            char c = input[pos];
42            if (c==']') ++noNestedBrackets;
43            else if (c=='[') --noNestedBrackets;
44            else if (c==')') ++noNestedBrackets;
45            else if (c=='(') --noNestedBrackets;
46            else if (c=='.' && noNestedBrackets==0) break;
47        }
48
49        // get the next "extension", which actually contains the pseudo-loader parameters
50        params = input.substr(pos+1, std::string::npos );
51        if( params.empty() )
52        {
53            OSG_WARN << "Missing parameters for " EXTENSION_NAME " pseudo-loader" << std::endl;
54            return false;
55        }
56
57        // clear the params sting of any brackets.
58        std::string::size_type params_pos = params.size();
59        for(; params_pos>0; )
60        {
61            --params_pos;
62            char c = params[params_pos];
63            if (c==']' || c=='[' || c==')' || c=='(')
64            {
65                params.erase(params_pos,1);
66            }
67        }
68
69        // strip the "params extension", which must leave a sub-filename.
70        filename = input.substr(0, pos );
71
72        return true;
73}
74
75///////////////////////////////////////////////////////////////////////////
76
77/**
78 * An OSG reader plugin for the ".rot" pseudo-loader, which inserts a
79 * rotation transform above the loaded geometry.
80 * This pseudo-loader makes it simple to change the orientation of a saved
81 * model by specifying a correcting rotation as part of the filename.
82 *
83 * Usage: <modelfile.ext>.<rx>,<ry>,<rz>.globe
84 * where:
85 *    <modelfile.ext> = an model filename.
86 *    <rx> = rotation around X axis [degrees]
87 *    <ry> = rotation around Y axis [degrees]
88 *    <rz> = rotation around Z axis [degrees]
89 *
90 * example: osgviewer cow.osg.30,60,-90.rot
91 */
92
93class ReaderWriterROT : public osgDB::ReaderWriter
94{
95public:
96    ReaderWriterROT()
97    {
98        supportsExtension(EXTENSION_NAME,"Rotation pseudo loader");
99    }
100
101    virtual const char* className() const { return "rotation pseudo-loader"; }
102
103    virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
104    {
105        std::string ext = osgDB::getLowerCaseFileExtension(fileName);
106        if( !acceptsExtension(ext) )
107            return ReadResult::FILE_NOT_HANDLED;
108
109        OSG_INFO << "ReaderWriterROT( \"" << fileName << "\" )" << std::endl;
110
111        // strip the pseudo-loader extension
112        std::string tmpName = osgDB::getNameLessExtension( fileName );
113
114        if (tmpName.empty())
115            return ReadResult::FILE_NOT_HANDLED;
116
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 rx, ry, rz;
134        int count = sscanf( params.c_str(), "%f,%f,%f", &rx, &ry, &rz );
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::rotate(
153            osg::DegreesToRadians( rx ), osg::Vec3( 1, 0, 0 ),
154            osg::DegreesToRadians( ry ), osg::Vec3( 0, 1, 0 ),
155            osg::DegreesToRadians( rz ), osg::Vec3( 0, 0, 1 ) ));
156        xform->addChild( node );
157        return xform;
158    }
159};
160
161
162// Add ourself to the Registry to instantiate the reader/writer.
163REGISTER_OSGPLUGIN(rot, ReaderWriterROT)
164
165/*EOF*/
Note: See TracBrowser for help on using the browser.