Show
Ignore:
Timestamp:
04/22/09 17:46:24 (6 years ago)
Author:
robert
Message:

From Neil Hughes, converted across to use istream for reading data from file to enable reading .3ds files over http (use OSG's libcurl plugin).

From Robert Osfield, ammendments of the above to better support reading of files from local directories.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgPlugins/3ds/ReaderWriter3DS.cpp

    r9906 r10076  
    3535#include <map> 
    3636#include <iostream> 
     37#include <sstream> 
    3738 
    3839using namespace std; 
     
    9697 
    9798        virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const; 
     99        virtual ReadResult readNode(std::istream& fin, const Options* options) const; 
    98100 
    99101    protected: 
     102        ReadResult constructFrom3dsFile(Lib3dsFile *f,const std::string& filename, const Options* options) const; 
    100103 
    101104 
     
    441444} 
    442445 
     446osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(std::istream& fin,  const osgDB::ReaderWriter::Options* options) const 
     447{ 
     448    osgDB::ReaderWriter::ReadResult result = ReadResult::FILE_NOT_HANDLED; 
     449 
     450    std::string optFileName = ""; 
     451    if (options) 
     452    { 
     453        optFileName = options->getPluginStringData("STREAM_FILENAME"); 
     454    } 
     455 
     456    Lib3dsFile *f = lib3ds_stream_load((iostream *) &fin); 
     457    if (f) 
     458    { 
     459        result = constructFrom3dsFile(f,optFileName,options); 
     460        lib3ds_file_free(f); 
     461    } 
     462 
     463    return(result); 
     464} 
    443465 
    444466osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const 
    445467{ 
     468    osgDB::ReaderWriter::ReadResult result = ReadResult::FILE_NOT_HANDLED; 
    446469 
    447470    std::string ext = osgDB::getLowerCaseFileExtension(file); 
     
    451474    if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; 
    452475 
    453     Lib3dsFile *f = lib3ds_file_load(fileName.c_str()); 
     476    Lib3dsFile *f = lib3ds_file_load(fileName.c_str(),options); 
     477 
     478    if (f) 
     479    { 
     480        osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; 
     481        local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); 
     482 
     483        result = constructFrom3dsFile(f,file,local_opt.get()); 
     484        lib3ds_file_free(f); 
     485    } 
     486 
     487    return result; 
     488} 
     489 
     490osgDB::ReaderWriter::ReadResult ReaderWriter3DS::constructFrom3dsFile(Lib3dsFile *f,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const 
     491{ 
    454492    if (f==NULL) return ReadResult::FILE_NOT_HANDLED; 
    455493 
     
    462500    ReaderObject reader; 
    463501 
    464     reader._directory = osgDB::getFilePath(fileName); 
     502    reader._directory = options->getDatabasePathList().empty() ? osgDB::getFilePath(fileName) : options->getDatabasePathList().front(); 
    465503 
    466504    osg::Group* group = new osg::Group; 
     
    516554    }     
    517555     
    518     lib3ds_file_free(f); 
    519  
    520556    return group; 
    521557} 
     
    691727    if (texture && *(texture->name)) 
    692728    { 
     729        osg::notify(osg::NOTICE)<<"texture->name="<<texture->name<<", _directory="<<_directory<<std::endl; 
     730 
     731 
    693732        std::string fileName = osgDB::findFileInDirectory(texture->name,_directory,osgDB::CASE_INSENSITIVE); 
    694733        if (fileName.empty())  
     
    697736            fileName = osgDB::findDataFile(texture->name,options, osgDB::CASE_INSENSITIVE); 
    698737        } 
    699          
     738 
    700739        if (fileName.empty()) 
    701740        { 
    702             osg::notify(osg::WARN) << "texture '"<<texture->name<<"' not found"<< std::endl; 
    703             return NULL; 
     741            if (osgDB::containsServerAddress(_directory)) 
     742            { 
     743                // if 3DS file is loaded from http, just attempt to load texture from same location. 
     744                fileName = _directory + "/" + texture->name; 
     745            } else { 
     746                osg::notify(osg::WARN) << "texture '"<<texture->name<<"' not found"<< std::endl; 
     747                return NULL; 
     748            } 
    704749        } 
    705750 
     
    822867    } 
    823868 
    824     if (transparency>0.0f || textureTransparancy || mat->opacity_map.flags!=0) 
     869    if (transparency>0.0f || textureTransparancy) 
    825870    { 
    826871        stateset->setMode(GL_BLEND,osg::StateAttribute::ON);