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

Revision 9858, 9.8 kB (checked in by robert, 5 years ago)

Added handling of ImageStream? Origin to make sure the movie always appears the correct way up.

  • 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            bool flip = image->getOrigin()==osg::Image::TOP_LEFT;
162
163            // start the stream playing.
164            imageStream->play();
165
166            osg::ref_ptr<osg::Geometry> pictureQuad = 0;
167
168            bool useTextureRectangle = true;
169            if (useTextureRectangle)
170            {
171                pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
172                                                   osg::Vec3(image->s(),0.0f,0.0f),
173                                                   osg::Vec3(0.0f,0.0f,image->t()),
174                                                   0.0f, flip ? image->t() : 0.0, image->s(), flip ? 0.0 : image->t());
175
176                pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
177                            new osg::TextureRectangle(image.get()),
178                            osg::StateAttribute::ON);
179            }
180            else
181            {
182                pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
183                                                   osg::Vec3(image->s(),0.0f,0.0f),
184                                                   osg::Vec3(0.0f,0.0f,image->t()),
185                                                   0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f);
186
187                pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0,
188                            new osg::Texture2D(image.get()),
189                            osg::StateAttribute::ON);
190            }
191
192            if (pictureQuad.valid())
193            {
194                osg::ref_ptr<osg::Geode> geode = new osg::Geode;
195                geode->addDrawable(pictureQuad.get());
196                nodeList.push_back(geode.get());
197
198            }
199        }
200        else if (image.valid())
201        {
202            nodeList.push_back(osg::createGeodeForImage(image.get()));
203        }
204    }
205
206    while (arguments.read("--dem",filename))
207    {
208        osg::HeightField* hf = readHeightFieldFile(filename.c_str(), options);
209        if (hf)
210        {
211            osg::Geode* geode = new osg::Geode;
212            geode->addDrawable(new osg::ShapeDrawable(hf));
213            nodeList.push_back(geode);
214        }
215    }
216
217    // note currently doesn't delete the loaded file entries from the command line yet...
218    for(int pos=1;pos<arguments.argc();++pos)
219    {
220        if (!arguments.isOption(pos))
221        {
222            // not an option so assume string is a filename.
223            osg::Node *node = osgDB::readNodeFile( arguments[pos], options);
224
225            if(node)
226            {
227                if (node->getName().empty()) node->setName( arguments[pos] );
228                nodeList.push_back(node);
229            }
230
231        }
232    }
233   
234    if (nodeList.empty())
235    {
236        return NULL;
237    }
238
239    if (nodeList.size()==1)
240    {
241        return nodeList.front().release();
242    }
243    else  // size >1
244    {
245        osg::Group* group = new osg::Group;
246        for(NodeList::iterator itr=nodeList.begin();
247            itr!=nodeList.end();
248            ++itr)
249        {
250            group->addChild((*itr).get());
251        }
252
253        return group;
254    }
255   
256}
257
258osg::ref_ptr<osg::Object> osgDB::readRefObjectFile(const std::string& filename,const ReaderWriter::Options* options)
259{
260    ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
261    if (rr.validObject()) return osg::ref_ptr<osg::Object>(rr.getObject());
262    if (rr.error()) notify(WARN) << rr.message() << std::endl;
263    return NULL;
264}
265
266osg::ref_ptr<osg::Image> osgDB::readRefImageFile(const std::string& filename,const ReaderWriter::Options* options)
267{
268    ReaderWriter::ReadResult rr = Registry::instance()->readImage(filename,options);
269    if (rr.validImage()) return osg::ref_ptr<osg::Image>(rr.getImage());
270    if (rr.error()) notify(WARN) << rr.message() << std::endl;
271    return NULL;
272}
273
274osg::ref_ptr<osg::Shader> osgDB::readRefShaderFile(const std::string& filename,const ReaderWriter::Options* options)
275{
276    ReaderWriter::ReadResult rr = Registry::instance()->readShader(filename,options);
277    if (rr.validShader()) return osg::ref_ptr<osg::Shader>(rr.getShader());
278    if (rr.error()) notify(WARN) << rr.message() << std::endl;
279    return NULL;
280}
281
282osg::ref_ptr<osg::HeightField> osgDB::readRefHeightFieldFile(const std::string& filename,const ReaderWriter::Options* options)
283{
284    ReaderWriter::ReadResult rr = Registry::instance()->readHeightField(filename,options);
285    if (rr.validHeightField()) return osg::ref_ptr<osg::HeightField>(rr.getHeightField());
286    if (rr.error()) notify(WARN) << rr.message() << std::endl;
287    return NULL;
288}
289
290osg::ref_ptr<osg::Node> osgDB::readRefNodeFile(const std::string& filename,const ReaderWriter::Options* options)
291{
292    ReaderWriter::ReadResult rr = Registry::instance()->readNode(filename,options);
293    if (rr.validNode()) return osg::ref_ptr<osg::Node>(rr.getNode());
294    if (rr.error()) notify(WARN) << rr.message() << std::endl;
295    return NULL;
296}
Note: See TracBrowser for help on using the browser.