root/OpenSceneGraph/trunk/src/osgDB/ReadFile.cpp @ 13041

Revision 13041, 9.5 kB (checked in by robert, 2 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 library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osg/Notify>
14#include <osg/Object>
15#include <osg/Image>
16#include <osg/Shader>
17#include <osg/ImageStream>
18#include <osg/Node>
19#include <osg/Group>
20#include <osg/Geode>
21#include <osg/ShapeDrawable>
22#include <osg/Geometry>
23#include <osg/Texture2D>
24#include <osg/TextureRectangle>
25
26#include <osgDB/Registry>
27#include <osgDB/ReadFile>
28
29using namespace osg;
30using namespace osgDB;
31
32Object* osgDB::readObjectFile(const std::string& filename,const Options* options)
33{
34    ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
35    if (rr.validObject()) return rr.takeObject();
36    if (rr.error()) OSG_WARN << rr.message() << std::endl;
37    return NULL;
38}
39
40
41Image* osgDB::readImageFile(const std::string& filename,const Options* options)
42{
43    ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options);
44    if (rr.validImage()) return rr.takeImage();
45    if (rr.error()) OSG_WARN << rr.message() << std::endl;
46    return NULL;
47}
48
49Shader* osgDB::readShaderFile(const std::string& filename,const Options* options)
50{
51    ReaderWriter::ReadResult rr = Registry::instance()->readShader(filename,options);
52    if (rr.validShader()) return rr.takeShader();
53    if (rr.error()) OSG_WARN << rr.message() << std::endl;
54    return NULL;
55}
56
57
58HeightField* osgDB::readHeightFieldFile(const std::string& filename,const Options* options)
59{
60    ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options);
61    if (rr.validHeightField()) return rr.takeHeightField();
62    if (rr.error()) OSG_WARN << rr.message() << std::endl;
63    return NULL;
64}
65
66
67Node* osgDB::readNodeFile(const std::string& filename,const Options* options)
68{
69    ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options);
70    if (rr.validNode()) return rr.takeNode();
71    if (rr.error()) OSG_WARN << rr.message() << std::endl;
72    if (rr.notEnoughMemory()) OSG_INFO << "Not enought memory to load file "<<filename << std::endl;
73    return NULL;
74}
75
76Node* osgDB::readNodeFiles(std::vector<std::string>& fileList,const Options* options)
77{
78    typedef std::vector<osg::Node*> NodeList;
79    NodeList nodeList;
80
81    for(std::vector<std::string>::iterator itr=fileList.begin();
82        itr!=fileList.end();
83        ++itr)
84    {
85        osg::Node *node = osgDB::readNodeFile( *itr , Registry::instance()->getOptions() );
86
87        if( node != (osg::Node *)0L )
88        {
89            if (node->getName().empty()) node->setName( *itr );
90            nodeList.push_back(node);
91        }
92
93    }
94
95    if (nodeList.empty())
96    {
97        return NULL;
98    }
99
100    if (nodeList.size()==1)
101    {
102        return nodeList.front();
103    }
104    else  // size >1
105    {
106        osg::Group* group = new osg::Group;
107        for(NodeList::iterator itr=nodeList.begin();
108            itr!=nodeList.end();
109            ++itr)
110        {
111            group->addChild(*itr);
112        }
113
114        return group;
115    }
116
117}
118
119Node* osgDB::readNodeFiles(osg::ArgumentParser& arguments,const Options* options)
120{
121
122    typedef std::vector< osg::ref_ptr<osg::Node> > NodeList;
123    NodeList nodeList;
124
125    std::string filename;
126
127    while (arguments.read("--file-cache",filename))
128    {
129        osgDB::Registry::instance()->setFileCache(new osgDB::FileCache(filename));
130    }
131
132    while (arguments.read("--image",filename))
133    {
134        osg::ref_ptr<osg::Image> image = readImageFile(filename.c_str(), options);
135        if (image.valid())
136        {
137            osg::Geode* geode = osg::createGeodeForImage(image.get());
138
139            if (image->isImageTranslucent())
140            {
141                OSG_INFO<<"Image "<<image->getFileName()<<" is translucent; setting up blending."<<std::endl;
142                geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
143                geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
144            }
145
146            nodeList.push_back(geode);
147        }
148    }
149
150    while (arguments.read("--movie",filename))
151    {
152        osg::ref_ptr<osg::Image> image = readImageFile(filename.c_str(), options);
153        osg::ref_ptr<osg::ImageStream> imageStream = dynamic_cast<osg::ImageStream*>(image.get());
154        if (imageStream.valid())
155        {
156            bool flip = image->getOrigin()==osg::Image::TOP_LEFT;
157
158            // start the stream playing.
159            imageStream->play();
160
161            osg::ref_ptr<osg::Geometry> pictureQuad = 0;
162
163            bool useTextureRectangle = true;
164            if (useTextureRectangle)
165            {
166                pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
167                                                   osg::Vec3(image->s(),0.0f,0.0f),
168                                                   osg::Vec3(0.0f,0.0f,image->t()),
169                                                   0.0f, flip ? image->t() : 0.0, image->s(), flip ? 0.0 : image->t());
170
171                pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
172                            new osg::TextureRectangle(image.get()),
173                            osg::StateAttribute::ON);
174            }
175            else
176            {
177                pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
178                                                   osg::Vec3(image->s(),0.0f,0.0f),
179                                                   osg::Vec3(0.0f,0.0f,image->t()),
180                                                   0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f);
181
182                pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
183                            new osg::Texture2D(image.get()),
184                            osg::StateAttribute::ON);
185            }
186
187            if (pictureQuad.valid())
188            {
189                osg::ref_ptr<osg::Geode> geode = new osg::Geode;
190                geode->addDrawable(pictureQuad.get());
191                nodeList.push_back(geode.get());
192
193            }
194        }
195        else if (image.valid())
196        {
197            nodeList.push_back(osg::createGeodeForImage(image.get()));
198        }
199    }
200
201    while (arguments.read("--dem",filename))
202    {
203        osg::HeightField* hf = readHeightFieldFile(filename.c_str(), options);
204        if (hf)
205        {
206            osg::Geode* geode = new osg::Geode;
207            geode->addDrawable(new osg::ShapeDrawable(hf));
208            nodeList.push_back(geode);
209        }
210    }
211
212    // note currently doesn't delete the loaded file entries from the command line yet...
213    for(int pos=1;pos<arguments.argc();++pos)
214    {
215        if (!arguments.isOption(pos))
216        {
217            // not an option so assume string is a filename.
218            osg::Node *node = osgDB::readNodeFile( arguments[pos], options);
219
220            if(node)
221            {
222                if (node->getName().empty()) node->setName( arguments[pos] );
223                nodeList.push_back(node);
224            }
225
226        }
227    }
228
229    if (nodeList.empty())
230    {
231        return NULL;
232    }
233
234    if (nodeList.size()==1)
235    {
236        return nodeList.front().release();
237    }
238    else  // size >1
239    {
240        osg::Group* group = new osg::Group;
241        for(NodeList::iterator itr=nodeList.begin();
242            itr!=nodeList.end();
243            ++itr)
244        {
245            group->addChild((*itr).get());
246        }
247
248        return group;
249    }
250
251}
252
253osg::ref_ptr<osg::Object> osgDB::readRefObjectFile(const std::string& filename,const Options* options)
254{
255    ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
256    if (rr.validObject()) return osg::ref_ptr<osg::Object>(rr.getObject());
257    if (rr.error()) OSG_WARN << rr.message() << std::endl;
258    return NULL;
259}
260
261osg::ref_ptr<osg::Image> osgDB::readRefImageFile(const std::string& filename,const Options* options)
262{
263    ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options);
264    if (rr.validImage()) return osg::ref_ptr<osg::Image>(rr.getImage());
265    if (rr.error()) OSG_WARN << rr.message() << std::endl;
266    return NULL;
267}
268
269osg::ref_ptr<osg::Shader> osgDB::readRefShaderFile(const std::string& filename,const Options* options)
270{
271    ReaderWriter::ReadResult rr = Registry::instance()->readShader(filename,options);
272    if (rr.validShader()) return osg::ref_ptr<osg::Shader>(rr.getShader());
273    if (rr.error()) OSG_WARN << rr.message() << std::endl;
274    return NULL;
275}
276
277osg::ref_ptr<osg::HeightField> osgDB::readRefHeightFieldFile(const std::string& filename,const Options* options)
278{
279    ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options);
280    if (rr.validHeightField()) return osg::ref_ptr<osg::HeightField>(rr.getHeightField());
281    if (rr.error()) OSG_WARN << rr.message() << std::endl;
282    return NULL;
283}
284
285osg::ref_ptr<osg::Node> osgDB::readRefNodeFile(const std::string& filename,const Options* options)
286{
287    ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options);
288    if (rr.validNode()) return osg::ref_ptr<osg::Node>(rr.getNode());
289    if (rr.error()) OSG_WARN << rr.message() << std::endl;
290    return NULL;
291}
Note: See TracBrowser for help on using the browser.