Changeset 10932

Show
Ignore:
Timestamp:
01/08/10 11:52:03 (5 years ago)
Author:
robert
Message:

From Sukender, "ReaderWriter?3DS.cpp fixes:
- Fixed creation of useless intermediate nodes

WriterNodeVisitor?.cpp fixes:
- Fixed naming of textures (path and extension)
"

Note from Robert Osfield, this submission also came with changes to use of ref_ptr<> and removal of delete[]'s, but these were not merged as they didn't actually fix any memory leaks, and in once instance introduced one.

Location:
OpenSceneGraph/trunk/src/osgPlugins/3ds
Files:
2 modified

Legend:

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

    r10914 r10932  
    349349    { 
    350350        if (mesh->faces[i].material>=0) { 
    351             //std::stringstream materialStr; 
    352             //materialStr << mesh->faces[i].material; 
    353             //materialFaceMap[materialStr.str()].push_back(i); 
    354351            materialFaceMap[mesh->faces[i].material].push_back(i); 
    355352        } 
     
    454451 
    455452 
    456     if (node->childs != NULL || pivoted || (!isOsgNodeMatrixIdentity && !noMatrixTransforms)) { 
     453    //if (node->childs != NULL || pivoted || (!isOsgNodeMatrixIdentity && !noMatrixTransforms)) { 
     454    if (node->childs != NULL || (!isOsgNodeMatrixIdentity && !noMatrixTransforms)) { 
    457455        if (isOsgNodeMatrixIdentity || noMatrixTransforms) { 
    458456            group = new osg::Group; 
     
    889887        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_TEXTURE_RGB_TINT     "<<((texture->flags)&LIB3DS_TEXTURE_RGB_TINT)<< std::endl; 
    890888 
    891         bool noTexture = false; 
    892         if (options) 
    893         { 
    894             std::istringstream iss(options->getOptionString()); 
    895             std::string opt; 
    896             while (iss >> opt) 
    897             { 
    898                 if (opt == "noTexture") 
    899                     noTexture = true; 
    900             } 
    901         } 
    902  
    903         osg::ref_ptr<osg::Image> osg_image = NULL; 
    904         if(noTexture) 
    905             osg_image = new osg::Image(); 
    906         else 
    907             osg_image = osgDB::readRefImageFile(fileName.c_str(), options); //Absolute Path 
    908         if (!osg_image) 
     889        osg::ref_ptr<osg::Image> osg_image = osgDB::readRefImageFile(fileName.c_str(), options); //Absolute Path 
     890        if (!osg_image.valid()) 
    909891        { 
    910892            osg::notify(osg::NOTICE) << "Warning: Cannot create texture "<<texture->name<< std::endl; 
  • OpenSceneGraph/trunk/src/osgPlugins/3ds/WriterNodeVisitor.cpp

    r10853 r10932  
    1616#include <osg/io_utils> 
    1717#include <osg/CullFace> 
     18#include <osgDB/WriteFile> 
     19 
    1820#include "WriterNodeVisitor.h" 
    1921#include <assert.h> 
     
    398400 
    399401 
    400 /// Creates a unique 3DS name for a given texture, and copies the image file to disk at an appropriate location if necessary. 
    401 //std::string WriterNodeVisitor::export3DSTexture(const osg::Image * image, const std::string & texName) { 
    402 //    std::string extension = osgDB::getFileExtension(texName); 
    403 //    if (is83(texName)) 
    404 //    { 
    405 //        std::string newName = texName; 
    406 //        if (extension.empty()) 
    407 //            newName += ".rgb"; 
    408 //        if (osgDB::Registry::instance()->writeImage(*image, osgDB::concatPaths(_directory, newName), options).status() ==  
    409 //            osgDB::ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE) 
    410 //            failedApply(); 
    411 //        return texName; 
    412 //    } 
    413 //    if (extension.empty()) 
    414 //    { 
    415 //        extension = "rgb"; 
    416 //    } 
    417 //    std::string newName = getUniqueName(texName, "tex"); 
    418 //    newName += "." + extension; 
    419 //    osgDB::Registry::instance()->writeImage(*image, osgDB::concatPaths(_directory, newName), options); 
    420 //    return newName; 
    421 //} 
    422  
    423402std::string 
    424403getPathRelative(const std::string & srcBad, 
    425404                const std::string & dstBad) 
    426405{ 
     406    if(srcBad.empty()) 
     407        return osgDB::getSimpleFileName(dstBad); 
    427408    const std::string & src = osgDB::convertFileNameToNativeStyle(srcBad); 
    428409    const std::string & dst = osgDB::convertFileNameToNativeStyle(dstBad); 
     
    440421        ++itDst; 
    441422    } 
    442     if (itSrc != src.end() || !is3DSpath(result)) 
     423    if (itSrc != src.end()) 
    443424        result = osgDB::getSimpleFileName(dst); 
    444425    return result; 
     
    477458                    path = oss.str(); 
    478459                } 
    479                 else 
     460                else { 
    480461                    path = getPathRelative(_srcDirectory, mat.image->getFileName()); 
    481                 // if(!is3DSpath(path)) 
    482                     path = osgDB::getSimpleFileName(path); 
     462                } 
     463                if(!is3DSpath(path)) { 
     464                    path = getUniqueName(path, "", true); 
     465                    //path = osgDB::getSimpleFileName(path); 
     466                } 
    483467 
    484468                strcpy(tex.name, path.c_str()); 
     
    486470                osgDB::makeDirectoryForFile(path); 
    487471 
    488                 if (mat.image && mat.image->data()) osgDB::Registry::instance()->writeImage(*(mat.image), path, NULL); 
     472                //if (mat.image->valid()) osgDB::writeImageFile(*(mat.image), path); 
     473                osgDB::writeImageFile(*(mat.image), path); 
    489474                if (mat.texture_transparency) tex.flags |= LIB3DS_TEXTURE_ALPHA_SOURCE; 
    490475                if (mat.texture_no_tile) tex.flags |= LIB3DS_TEXTURE_NO_TILE; 
     
    500485 
    501486 
    502 std::string WriterNodeVisitor::getUniqueName(const std::string& defaultValue, const std::string & _defaultPrefix, bool nameIsPath) { 
     487std::string WriterNodeVisitor::getUniqueName(const std::string& _defaultValue, const std::string & _defaultPrefix, bool nameIsPath) { 
    503488    if (_defaultPrefix.length()>=4) throw "Default prefix is too long";            // Arbitrarily defined to 3 chars. You can modify this, but you may have to change the code so that finding a number is okay, even when changing the default prefix length. 
    504489 
    505490    // Tests if default name is valid and unique 
    506     bool defaultIs83 = is83(defaultValue); 
    507     bool defaultIsValid = nameIsPath ? is3DSpath(defaultValue) : defaultIs83; 
    508     if (defaultIsValid && _nameMap.find(defaultValue) == _nameMap.end()) { 
    509         _nameMap.insert(defaultValue); 
    510         return defaultValue; 
    511     } 
     491    bool defaultIs83 = is83(_defaultValue); 
     492    bool defaultIsValid = nameIsPath ? is3DSpath(_defaultValue) : defaultIs83; 
     493    if (defaultIsValid && _nameMap.find(_defaultValue) == _nameMap.end()) { 
     494        _nameMap.insert(_defaultValue); 
     495        return _defaultValue; 
     496    } 
     497 
     498    // Handling of paths is not well done yet. Defaulting to something very simple. 
     499    // We should actually ensure each component is 8 chars long, and final filename is 8.3, and total is <64 chars. 
     500    std::string defaultValue(nameIsPath ? osgDB::getSimpleFileName(_defaultValue) : _defaultValue); 
     501    std::string ext(nameIsPath ? osgDB::getFileExtensionIncludingDot(_defaultValue).substr(0, std::min<unsigned int>(_defaultValue.size(), 4)) : "");        // 4 chars = dot + 3 chars 
    512502 
    513503    std::string defaultPrefix(_defaultPrefix.empty() ? "_" : _defaultPrefix); 
     
    561551            } 
    562552            _nameMap.insert(res); 
    563             return res; 
     553            return res + ext; 
    564554        } 
    565555    }