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

Revision 9038, 9.7 kB (checked in by robert, 5 years ago)

Introduce osgDB::FileCache?, and updated osgfilecache and DatabasePager? to use it.

  • 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 ReaderWriter::Options* options)
33{
34    ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
35    if (rr.validObject()) return rr.takeObject();
36    if (rr.error()) notify(WARN) << rr.message() << std::endl;
37    return NULL;
38}
39
40
41Image* osgDB::readImageFile(const std::string& filename,const ReaderWriter::Options* options)
42{
43    ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options);
44    if (rr.validImage()) return rr.takeImage();
45    if (rr.error()) notify(WARN) << rr.message() << std::endl;
46    return NULL;
47}
48
49Shader* osgDB::readShaderFile(const std::string& filename,const ReaderWriter::Options* options)
50{
51    ReaderWriter::ReadResult rr = Registry::instance()->readShader(filename,options);
52    if (rr.validShader()) return rr.takeShader();
53    if (rr.error()) notify(WARN) << rr.message() << std::endl;
54    return NULL;
55}
56
57
58HeightField* osgDB::readHeightFieldFile(const std::string& filename,const ReaderWriter::Options* options)
59{
60    ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options);
61    if (rr.validHeightField()) return rr.takeHeightField();
62    if (rr.error()) notify(WARN) << rr.message() << std::endl;
63    return NULL;
64}
65
66
67Node* osgDB::readNodeFile(const std::string& filename,const ReaderWriter::Options* options)
68{
69    ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options);
70    if (rr.validNode()) return rr.takeNode();
71    if (rr.error()) notify(WARN) << rr.message() << std::endl;
72    return NULL;
73}
74
75Node* osgDB::readNodeFiles(std::vector<std::string>& commandLine,const ReaderWriter::Options* options)
76{
77    typedef std::vector<osg::Node*> NodeList;
78    NodeList nodeList;
79
80    // note currently doesn't delete the loaded file entries from the command line yet...
81
82    for(std::vector<std::string>::iterator itr=commandLine.begin();
83        itr!=commandLine.end();
84        ++itr)
85    {
86        if ((*itr)[0]!='-')
87        {
88            // not an option so assume string is a filename.
89            osg::Node *node = osgDB::readNodeFile( *itr , options );
90
91            if( node != (osg::Node *)0L )
92            {
93                if (node->getName().empty()) node->setName( *itr );
94                nodeList.push_back(node);
95            }
96
97        }
98    }
99   
100    if (nodeList.empty())
101    {
102        return NULL;
103    }
104
105    if (nodeList.size()==1)
106    {
107        return nodeList.front();
108    }
109    else  // size >1
110    {
111        osg::Group* group = new osg::Group;
112        for(NodeList::iterator itr=nodeList.begin();
113            itr!=nodeList.end();
114            ++itr)
115        {
116            group->addChild(*itr);
117        }
118
119        return group;
120    }
121   
122}
123
124Node* osgDB::readNodeFiles(osg::ArgumentParser& arguments,const ReaderWriter::Options* options)
125{
126
127    typedef std::vector< osg::ref_ptr<osg::Node> > NodeList;
128    NodeList nodeList;
129
130    std::string filename;
131
132    while (arguments.read("--file-cache",filename))
133    {
134        osgDB::Registry::instance()->setFileCache(new osgDB::FileCache(filename));
135    }
136   
137    while (arguments.read("--image",filename))
138    {
139        osg::ref_ptr<osg::Image> image = readImageFile(filename.c_str(), options);
140        if (image.valid())
141        {
142            osg::Geode* geode = osg::createGeodeForImage(image.get());
143           
144            if (image->isImageTranslucent())
145            {
146                osg::notify()<<"Image "<<image->getFileName()<<" is translucent; setting up blending."<<std::endl;
147                geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
148                geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
149            }
150
151            nodeList.push_back(geode);
152        }
153    }
154
155    while (arguments.read("--movie",filename))
156    {
157        osg::ref_ptr<osg::Image> image = readImageFile(filename.c_str(), options);
158        osg::ref_ptr<osg::ImageStream> imageStream = dynamic_cast<osg::ImageStream*>(image.get());
159        if (imageStream.valid())
160        {
161            // start the stream playing.
162            imageStream->play();
163
164            osg::ref_ptr<osg::Geometry> pictureQuad = 0;
165
166            bool useTextureRectangle = true;
167            if (useTextureRectangle)
168            {
169                pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
170                                                   osg::Vec3(image->s(),0.0f,0.0f),
171                                                   osg::Vec3(0.0f,0.0f,image->t()),
172                                                   0.0f,image->t(), image->s(),0.0f);
173
174                pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
175                            new osg::TextureRectangle(image.get()),
176                            osg::StateAttribute::ON);
177            }
178            else
179            {
180                pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
181                                                   osg::Vec3(image->s(),0.0f,0.0f),
182                                                   osg::Vec3(0.0f,0.0f,image->t()),
183                                                   0.0f,0.0f, 1.0f,1.0f);
184
185                pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
186                            new osg::Texture2D(image.get()),
187                            osg::StateAttribute::ON);
188            }
189
190            if (pictureQuad.valid())
191            {
192                osg::ref_ptr<osg::Geode> geode = new osg::Geode;
193                geode->addDrawable(pictureQuad.get());
194                nodeList.push_back(geode.get());
195
196            }
197        }
198        else if (image.valid())
199        {
200            nodeList.push_back(osg::createGeodeForImage(image.get()));
201        }
202    }
203
204    while (arguments.read("--dem",filename))
205    {
206        osg::HeightField* hf = readHeightFieldFile(filename.c_str(), options);
207        if (hf)
208        {
209            osg::Geode* geode = new osg::Geode;
210            geode->addDrawable(new osg::ShapeDrawable(hf));
211            nodeList.push_back(geode);
212        }
213    }
214
215    // note currently doesn't delete the loaded file entries from the command line yet...
216    for(int pos=1;pos<arguments.argc();++pos)
217    {
218        if (!arguments.isOption(pos))
219        {
220            // not an option so assume string is a filename.
221            osg::Node *node = osgDB::readNodeFile( arguments[pos], options);
222
223            if(node)
224            {
225                if (node->getName().empty()) node->setName( arguments[pos] );
226                nodeList.push_back(node);
227            }
228
229        }
230    }
231   
232    if (nodeList.empty())
233    {
234        return NULL;
235    }
236
237    if (nodeList.size()==1)
238    {
239        return nodeList.front().release();
240    }
241    else  // size >1
242    {
243        osg::Group* group = new osg::Group;
244        for(NodeList::iterator itr=nodeList.begin();
245            itr!=nodeList.end();
246            ++itr)
247        {
248            group->addChild((*itr).get());
249        }
250
251        return group;
252    }
253   
254}
255
256osg::ref_ptr<osg::Object> osgDB::readRefObjectFile(const std::string& filename,const ReaderWriter::Options* options)
257{
258    ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
259    if (rr.validObject()) return osg::ref_ptr<osg::Object>(rr.getObject());
260    if (rr.error()) notify(WARN) << rr.message() << std::endl;
261    return NULL;
262}
263
264osg::ref_ptr<osg::Image> osgDB::readRefImageFile(const std::string& filename,const ReaderWriter::Options* options)
265{
266    ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options);
267    if (rr.validImage()) return osg::ref_ptr<osg::Image>(rr.getImage());
268    if (rr.error()) notify(WARN) << rr.message() << std::endl;
269    return NULL;
270}
271
272osg::ref_ptr<osg::Shader> osgDB::readRefShaderFile(const std::string& filename,const ReaderWriter::Options* options)
273{
274    ReaderWriter::ReadResult rr = Registry::instance()->readShader(filename,options);
275    if (rr.validShader()) return osg::ref_ptr<osg::Shader>(rr.getShader());
276    if (rr.error()) notify(WARN) << rr.message() << std::endl;
277    return NULL;
278}
279
280osg::ref_ptr<osg::HeightField> osgDB::readRefHeightFieldFile(const std::string& filename,const ReaderWriter::Options* options)
281{
282    ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options);
283    if (rr.validHeightField()) return osg::ref_ptr<osg::HeightField>(rr.getHeightField());
284    if (rr.error()) notify(WARN) << rr.message() << std::endl;
285    return NULL;
286}
287
288osg::ref_ptr<osg::Node> osgDB::readRefNodeFile(const std::string& filename,const ReaderWriter::Options* options)
289{
290    ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options);
291    if (rr.validNode()) return osg::ref_ptr<osg::Node>(rr.getNode());
292    if (rr.error()) notify(WARN) << rr.message() << std::endl;
293    return NULL;
294}
Note: See TracBrowser for help on using the browser.